I am trying to figure out the best way to calculate the maximum lat and long range (i.e. max "height" and "length") of a polygon.
Starting with an sf object of polygons, a possible approach would be:
- st_cast("POINT")
- extract lat and long for all points
- find the points with maximum lat, max long, min lat and min long per polygon
- calculate distances
However, I have to do this for hundreds of polygons and was wondering if there is a more elegant/easy way of doing it. I just cannot find anything on this (which seems strange)...
Thanks.
EDIT
Sure, I did not include an example because I think it is a very general question. But this could be of use:
library(sf)
library(dplyr)
demo(nc, ask = FALSE, verbose = FALSE)
Then what I need is to calculate the "length" (i.e. distance between max lon and min lon) and "height" (distance between max lat and min lat) for each of the polygons.
I think bbox()
could help indeed. I think I could use a for loop or apply
to run bbox()
over each of the polygons. But then I have no idea how to convert the bbox()
object into a data frame, to work from there.
Any suggestions?
st_bbox
to get an approximate value of max/min of lat/long. Can you share some sort of reproducible example? – agilasfheaders::sf_boxes( nc )
will give you the bounding box of each polygon if that helps? – SymbolixAU