This worked pretty well with
rgeos::gDifference(spdf1, spdf2), which needs as input geometry of SpatialPolygonDataFrame.
Now, I want to switch from SPDF files to
sfc objects. But,
st_difference does not keep a not overlapping geometry. Instead, it keeps
buffer minus one :
How to correctly Erase features using
sf objects? I tried tools
st_sym_difference or using features
st_geometry(sf) for operation, but I get to have the same, for me unwanted results. Also, I found a suggestion of st_erase function:
st_erase = function(x, y) st_difference(x, st_union(st_combine(y)))
But using it on my data
erased <- st_erase(buff, u) generates error:
`Error in CPL_geos_op2(op, st_geometry(x), st_geometry(y)) Evaluation error: TopologyException: Input geom 1 is invalid: Self-intersection at or near point`
Here is my example:
# Load data shp = system.file("shape/nc.shp", package="sf") my.sf <- st_read(shp, quiet = TRUE) # Convert crs to projected system to make buffer my.sf.web<- st_transform(my.sf, 3857) # Subset the data to create two independent shps i = 10 # Split datasets in two files one = my.sf.web[i, ] left = my.sf.web[-i,] # Create buffer buff = st_buffer(one, 35000 ) # distance # CHeck which polygons overlaps with my buffer out.overlap = st_overlaps(buff, left) # Subset the polygons that overlaps with the buffer nbrs.buff <- left[st_overlaps(buff,left)[],] u <- st_union(st_geometry(nbrs.buff), st_geometry(one), by_feature = FALSE) #u <- st_union(st_combine(st_geometry(nbrs.buff)), # st_combine(st_geometry(one))) int.buff.one = st_difference(buff, u) # NOT WORKING HERE???