Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issues with st_crop on s2_data_countries() #239

Closed
MatthieuStigler opened this issue May 25, 2023 · 3 comments
Closed

issues with st_crop on s2_data_countries() #239

MatthieuStigler opened this issue May 25, 2023 · 3 comments

Comments

@MatthieuStigler
Copy link

When using st_crop() on st_as_sf(s2_data_countries()), I get some strange results. I faced the same problem with rnaturalearth (see issue ropensci/rnaturalearth#78), and assumed the problem was that rnaturalearth contained invalid geometries (which s2::s2_data_countries doesn't seem to contain), but it seems even with valid geometries, there is an issue?

Note I am posting here and might need to post instead on sf?

Thanks!

library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.3, PROJ 8.2.0; sf_use_s2() is TRUE
library(s2)

co <- st_as_sf(s2_data_countries())

pol <- st_sfc(st_polygon(list(cbind(c(-120, -120, 30, 30, -120),
                                      c(30, 55, 55, 30, 30)))))

plot(co)
plot(pol, add=TRUE, border ="blue")
co |> 
  st_crop(st_bbox(pol)) |>
  plot(border="red", lwd=2, add=TRUE)

Created on 2023-05-25 with reprex v2.0.2

@paleolimbot
Copy link
Collaborator

I'm pretty sure what's happening is that the box is getting interpreted to have a geodesic edge. This is almost certainly not what anybody doing st_crop() is asking for...sf could tessellate the box edges before sending to s2 just use GEOS?

@edzer
Copy link
Member

edzer commented Sep 20, 2023

Yes, something along these lines should work:

library(sf)                                                                           
#> Linking to GEOS 3.10.2, GDAL 3.4.3, PROJ 8.2.0; sf_use_s2() is TRUE                
library(s2)                                                                           
                                                                                      
co <- st_as_sf(s2_data_countries())                                                   
                                                                                      
pol <- st_sfc(st_polygon(list(cbind(c(-120, -120, 30, 30, -120),                      
                                      c(30, 55, 55, 30, 30)))))                       
                                                                                      
pol1 <- st_segmentize(pol, 1)                                                         
plot(co)                                                                              
plot(pol, add=TRUE, border ="blue")                                                   
st_crs(pol1) = st_crs(co)                                                             
co |>                                                                                 
  st_intersection(pol1) |>                                                            
  plot(border="red", lwd=2, add=TRUE)   

x

@edzer edzer closed this as completed Sep 20, 2023
@edzer
Copy link
Member

edzer commented Sep 20, 2023

(It works because pol has an NA crs when passed to st_segmentize)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants