Find intersection of two geometries with st_intersects() for sf >1.0.0

Viewed 483

I would like to know how to properly use st_intersect() for sf >1.0.0. I'm reusing a code that I wrote on a sf 0.9.7 version and notice a change. I'm aware of the implementation of s2 for sf, but I'm not sure how to use it in this case. I have a data.frame with many rows including Latitude and Longitude, and I want to select the rows within an area (polygon). The following code (for sf ==0.9.7) would return a warning:

# Creates some points
table<-data.frame(type=c("1","2","3"),Longitude=c(0,2,4),Latitude=c(0,2,5))
df<-st_as_sf(table,coords=c("Longitude","Latitude"),crs=4326)
# Creates a polygon
s <- rbind(c(-180, -90), c(-180, 90), c(180, 90), c(180, -90), c(-180, -90)) %>% list %>% st_polygon %>% st_sfc(crs=4326)
# Evaluates intersection
st_intersects(df,s,sparse = FALSE)

For 0.9.7:

although coordinates are longitude/latitude, st_intersects assumes that they are planar

     [,1]
[1,] TRUE
[2,] TRUE
[3,] TRUE

For sp>1.0.0 In this case the functions returns:

      [,1]
[1,] FALSE
[2,] FALSE
[3,] FALSE

I'm not sure how to adjust the code to use st_intersects() with my dataframe. For example using a sfc object seems to work:

# create square
s <- rbind(c(1, 1), c(10, 1), c(10, 10), c(1, 10), c(1, 1)) %>% list %>% st_polygon %>% st_sfc(crs=4326)
# create random points
p <- runif(50, 0, 11) %>% cbind(runif(50, 0, 11)) %>% st_multipoint %>% st_sfc(crs=4326) %>% st_cast("POINT")

# Intersect
st_intersects(p,s,sparse=FALSE)

       [,1]
 [1,]  TRUE
 [2,] FALSE
 [3,]  TRUE
   ...
[49,] FALSE
[50,] FALSE

0 Answers
Related