0

Why does st_intersection add duplicate lines to my sf data frame?

 sf_points

Simple feature collection with 156421 features and 9 fields
geometry type:  POINT
dimension:      XY
bbox:           xmin: 596640.7 ymin: 7317066 xmax: 607113.9 ymax: 7326187
epsg (SRID):    32722
proj4string:    +proj=utm +zone=22 +south +datum=WGS84 +units=m +no_defs

sf_polygons

Simple feature collection with 225 features and 4 fields
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: 543943.6 ymin: 7317069 xmax: 607110.9 ymax: 7342239
epsg (SRID):    32722
proj4string:    +proj=utm +zone=22 +south +datum=WGS84 +units=m +no_defs


nrow(sf_points[!duplicated(sf_points), ]) == nrow(sf_points)
> TRUE

st_crs(sf_points) == st_crs(parcelas) # test crs
> TRUE

sf_points_intersection <- st_intersection(sf_points, sf_polygon)

sf_points_intersection
Simple feature collection with 121716 features and 9 fields
geometry type:  POINT
dimension:      XY
bbox:           xmin: 596646.7 ymin: 7317070 xmax: 607109.9 ymax: 7326184
epsg (SRID):    32722
proj4string:    +proj=utm +zone=22 +south +datum=WGS84 +units=m +no_defs


nrow(sf_points_intersection[!duplicated(sf_points_intersection), ]) == nrow(sf_points_intersection)
> FALSE


nrow(sf_points_intersection[duplicated(as.data.frame(sf_points_intersection)), ])
> 37056
1

The sf_polygon layer contains overlaping geometries. I didn't know the data well enough.

st_difference() solves this.

st_intersection(sf_points, st_difference(sf_polygon)) # 
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.