2

I would like to get the greens polygons that intersects (or overlapsed) the red outlined polygon (see image below).

I tried sf::st_intersection() but the polygon in yellow (yes that just one polygon) was not taken in account.

In QGIS, the clip (cut) tool did exactly what I wanted. Is there a tool similar in R to cut a layer using another layer ?

Tried to use sf::st_overlaps() but the returned matrix was biggest than my feature table. sf::st_crops() does not fit either as I want to cut in a specific shape.

map issue

  • 2
    The yellow polygon doesn't "fit into" the red outline. It intersects or overlaps it. "Fit into" to me means "is fully inside". These are very complex looking polygons and its possible some validity constraint isn't valid and we can't tell that without your data.... – Spacedman Apr 4 at 19:41
  • Thanks for your comments. I'm well aware that the yellow polygon does not fit into I want either selected it (by an intersection) or get the part inside the red outlined polygon (by cutting it). Actually sf::st_intersects() miss the yellow polygon (and so does QGIS geoprocessing tool). I'm not a native english speaker so I might have misused the word "fit". Actually, I started by an intersection by habit, and because I didn't thought there will be big polygons like this one, but cutting is more what I need for this use case. So my question is still valid. – Nicolas Roelandt Apr 5 at 7:25
  • I edited my question to remove the misused word. – Nicolas Roelandt Apr 5 at 7:27
  • I'm surprised that sf::st_intersection() is not working: geocompr.robinlovelace.net/figures/venn-clip-1.png Do you need to clip the green subsection within the red outline, or both the yellow and the green together? Would performing a st_union() before clipping work for you? – philiporlando Apr 5 at 8:11
  • @spacedSparking, I'm surprised too. I checked the validity of the polygons and all are valids. Actually the green and the yellow are in the same layer, I just highlighted the problematic polygon. – Nicolas Roelandt Apr 5 at 8:53
2

sf::st_intersection() will work with the last version of sf (0.7-3). I don't know why it was not working in the previous version.

  • I had {sf} 0.7.2; upgraded to {sf} 0.7.3 and the issue with sf::st_intersect() was resolved. Not sign in the changelog that the algorithm was changed, maybe the new grob operator ? ¯_(ツ)_/¯ Thanks @rcarto – Nicolas Roelandt Apr 5 at 14:05
  • Final result is visible here. so No need for a clip function. – Nicolas Roelandt Apr 5 at 14:10
0

There is now a sf::st_crop function available but, you may need the development github version of sf. You can download the development version using the devtools/remotes package(s).

install.packages("devtools")
remotes::install_github("r-spatial/sf")

For specific irregular geometries, the raster::intersect function can clip data, and retain attributes, using the rgeos package. I believe that the package can handle sf class objects but, if not, you can coerce to sp using as(x, "Spatial").

  • Thanks for your comments. As I said, sf::st_crop() does not respond to my need as it takes a bounding box (so 4 coordinates). I want to cut with another (irregular) shape from another layer. I'll look into raster::intersect – Nicolas Roelandt Apr 5 at 7:32

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.