3

I am trying to identify, from a set of overlapping buffers, the smallest of their intersections. In other words (easier to understand if you look at the picture below), identify amonst all the intersections which are not containing another, smaller, one (the yellow one, below). I was very excited to find the st_contains() function in R sf a while ago, but it doesn't work as I thought. Here is a working example.


> library(sf)
#creating a set of three points
> dfpt<-cbind.data.frame(X=c(328900,328400,331200),Y=c(403000,403400,402300),ID=1:3)
> sfpt<-st_as_sf(dfpt,coords=c('X','Y'))
# creating buffers of 2000m
> sfb<-st_buffer(sfpt,dist=2000)

# intersecting the buffers
> sfint<-st_intersection(sfb,sfb)

# I am interested in two buffers that are embedded
> sfsel<-subset(sfint,(ID==3&ID.1==1)|(ID==3&ID.1==2)) # whether it's ID or ID.1 might change

#check what it looks like
> plot(st_geometry(sfb),col=NA,border='black')
> plot(st_geometry(sfsel[1,]),col='blue',add=TRUE)
> plot(st_geometry(sfsel[2,]),col='yellow',add=TRUE)

three buffers and two embedded polygons

# checking which of the three buffers contain which of the two polygons
> st_contains(sfb,sfsel)
Sparse geometry binary predicate list of length 3, where the predicate was `contains'
 1: 2
 2: (empty)
 3: (empty)

(the expected answer should be 1:2, 2:1,2,3:1,2, which I got in other, similar cases)

# checking if the two polygons contain each other (the first should contain the second!)
> st_contains(sfsel,sfsel)
Sparse geometry binary predicate list of length 2, where the predicate was `contains'
 1: 1
 2: 2

(the expected answer should be 1:1,2, 2:2)

What happens there? I think it might be the approximation of the polygons arcs that lead R to believe the border of the two polygons (blue and yellow above) are not perfectly aligned. I tried to change the scale (by increasing all distances by 1000) and it did improve the function in some cases, but not many. What can I do?

(For info)

> R.version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          6.1                         
year           2019                        
month          07                          
day            05                          
svn rev        76782                       
language       R                           
version.string R version 3.6.1 (2019-07-05)
nickname       Action of the Toes 

1 Answer 1

1

set a precision, e.g.

> st_contains(st_set_precision(sfsel, 1))
Sparse geometry binary predicate list of length 2, where the predicate was `contains'
 1: 1, 2
 2: 2
1
  • Thanks! precision of 1 did not work in some cases, so I tried precision of 0.1. But then I get an error message: Error in CPL_geos_binop(st_geometry(x), st_geometry(y), op, par, pattern, : Evaluation error: TopologyException: side location conflict at 328590 401410. I am aware this is a non-reproducible example, btw, as I was trying on my full dataset after trying the example (which worked). I can try to make one if it helps. Oct 26, 2019 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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