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

Feture Requet: st_oriented_envelope #10

Open
harryprince opened this issue Mar 11, 2022 · 5 comments
Open

Feture Requet: st_oriented_envelope #10

harryprince opened this issue Mar 11, 2022 · 5 comments

Comments

@harryprince
Copy link

harryprince commented Mar 11, 2022

sfx::st_extent() is great work to get a bbox of polygon, does some function can support ST_OrientedEnvelope?

@harryprince
Copy link
Author

@harryprince
Copy link
Author

And I found the similar function in Python shapely like this:

p <- 'POLYGON((-21.992187499999996 48.11282619955873,-23.398437499999993 22.763371521603847,-4.062499999999996 23.248770192923608,-3.3593749999999956 40.31920408353505,18.261718750000004 40.453095700341784,18.085937500000004 21.787408345916234,36.71875 22.43880858441266,69.765625 47.87756363384051,-21.992187499999996 48.11282619955873))' 

reticulate::py_run_string("import shapely")
p %>% 
  reticulate::py$shapely$wkt$loads()$minimum_rotated_rectangle$wkt
"POLYGON ((-23.40066709551779 21.89377798109121, 69.69839181920925 21.65507652483939, 69.765625 47.87756363384051, -23.33343391472703 48.11626509009232, -23.40066709551779 21.89377798109121))"

@seasmith
Copy link
Owner

It might be worth looking into the s2 package (which sf uses by default). There are a couple bounding functions available:
https://github.com/r-spatial/s2/blob/main/R/s2-bounds.R

I have not experimented with the s2 package to see its capabilities nor the original S2Geometry:

https://s2geometry.io/devguide/basic_types.html

@harryprince
Copy link
Author

harryprince commented Mar 14, 2022

Thanks a lot! I found s2::s2_bounds_rect return a data.frame object. I am curious about how I can convert it to sf polygon object?
Currently, I implement like this:

do_shotgroups4 <- function(x){
  bb <- p %>%
        sf::st_as_sfc() %>%
        s2::s2_bounds_rect() %>% 
     # select(xmin=lng_lo,ymin=lat_lo,xman=lng_hi,ymax=lat_hi) %>%
     unlist()
   
   matrix( c(bb['lng_lo'],bb['lat_lo'],
             bb['lng_lo'],bb['lat_hi'],
             bb['lng_hi'],bb['lat_hi'],
             bb['lng_hi'],bb['lat_lo'],
             bb['lng_lo'],bb['lat_lo']),
                    ncol = 2, byrow = T) %>%
     list() %>%
     sf::st_polygon()
 }

And I got some other solutions for this issue: dwoll/shotGroups#9

@harryprince
Copy link
Author

harryprince commented Mar 14, 2022

And I found s2::s2_bound2_rect only return a bbox like st_bbox instead of st_oriented_envelop.

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

2 participants