0

I try to clip shape "a" with shape "b". The result should be a polygon with the attributes and borders of "a" but cliped on the extent of "b". Basically a simple clip as we know it from ArcMap or QField. Now I want to use the sf package in R. But all the suggestions I find tell me to use````st_intersection```. But this is not actually a clip because it will result in a layer containing also the borders of "b".

a<-st_read("a.shp")
be<-st_read("b.shp"
Clip1<- st_intersection(a,b)

What I want

What I want

what st_intersection does

what st_intersect does

3
  • What are the black dots in your diagram? Are the green, blue, and grey areas separate features? – Spacedman Oct 30 '20 at 17:42
  • There is no st_intersect function in sf. There's st_intersects, which tests for intersection, and there's st_intersection, which seems to do exactly what you want. – Spacedman Oct 30 '20 at 17:50
  • Yes they are separate features. My problem is, that a intersection preserves the feature boundaries of the intersect feature as you can see in the second image. I solved this problem now by using st_union on the intersect feature before the intersection. But it is odd that there is no command to do this directly. – sfetan Nov 3 '20 at 8:21
2

This all works for me:

> library(sf)
> plot(input$geom, col=input$colour)
> plot(clip$geom, col=NA, border="red", add=TRUE)

enter image description here

> output = st_intersection(input, clip)
> plot(output$geom, col=output$colour)

enter image description here

The output object has the attributes from the underlying input object:

> output 
Simple feature collection with 3 features and 1 field
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: 350715.5 ymin: 556199.3 xmax: 378850.3 ymax: 579962.8
CRS:            27700
  colour                       geometry
1  green POLYGON ((365346.3 579873.5...
2   grey POLYGON ((377844 569519.7, ...
3   blue POLYGON ((354933 574695.8, ...
> 
3
  • Yes for your case this works. But if your "clip" layer consists of multiple features overlaying the "input" layer, these feature are as well integrated into the "output" layer. I found a solution to use st_union for the "clip" layer before the intersection. – sfetan Nov 3 '20 at 8:17
  • 1
    This case is exactly the example of "what I want" from your question. You show the clip feature as a single feature. If you'd shown it as two semicircles then I'd have suggested unioning the clipping features. – Spacedman Nov 3 '20 at 11:03
  • 1
    You're right I replaced the images. – sfetan Nov 3 '20 at 14:42

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.