I am trying to erase one shapefile from the other, similar to ArcGIS Erase tool:
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 sf
or sfc
objects. But, st_difference
does not keep a not overlapping geometry. Instead, it keeps buffer minus one
:
Instead, I want to keep part encircled in blue:
How to correctly Erase features using sf
objects? I tried tools st_difference
, 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)[[1]],]
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???
u
is meant to be here. Each feature is the combination of the source feature and the feature you buffered. That is complicating the difference situation.plot(st_geometry(st_difference(buff, st_union(u))))
seems to extract the bit of the buffer aboveu
but I'm not sure the way you constructu
makes sense in the first place...u
should just spatially merge my first geometryone
and it's neighborsnbrs.buff
and create single file. So, I just simply want to eraseu
from thebuffer
. Thanks,plot(st_geometry(st_difference(buff, st_union(u))))
makes correctly eraseu
frombuff
. Do you think I should rerun union toolu2 <- st_union(u)
to have correct output? Or how would you simplify the union operation?rbind
:uu = rbind(nbrs.buff, one)
.one
vertices completely overlaps with vertices innbrs.buff
, as Ione
was tonbrs.buff
geometry!!! when I createu = st_union(rbind(nbrs.buff, one))
(as suggested by @Spacedman) and then runst_difference()
it creates correct output! I'll testst_dissolve
andst_combine
to see which one is more appropriate to run.