I have a Bbox of Germany with coordinates (lon,lat):
g_bbox<-c(xmin=5.98865807458, ymin=47.3024876979, xmax=15.0169958839, ymax=54.983104153)
I would like to divide the bbox in (x) amount of bboxes with a diagonal of 50 km each.
The length of diagonal can vary a little (+/-10km).
The Resulting BBoxes should not overlap.
The amount of BBoxes (x) should be the maximum number of BBoxes that fit into the g_bbox
The result I would like to have is in best case a data frame with xmin,ymin,xmax,ymax columns.
How would I go about it?
I would suggest the following solution.
Load packages
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
Load data
g_bbox <- st_bbox(c(xmin = 5.98865807458, ymin = 47.3024876979, xmax = 15.0169958839, ymax = 54.983104153), crs = 4326)
Convert bbox to polygon with projected coords
g_poly <- st_as_sfc(g_bbox) %>%
st_transform(32632)
Plot
mapview::mapview(g_poly)
Create the grid
g_grid <- st_make_grid(g_poly, cellsize = 50000 / sqrt(2))
Check one diagonal
sqrt(
(st_bbox(g_grid[[1]])[3] - st_bbox(g_grid[[1]])[1]) ^ 2 # x axis
+
(st_bbox(g_grid[[1]])[4] - st_bbox(g_grid[[1]])[2]) ^ 2 # y axis
)
#> xmax
#> 50000
Plot
mapview::mapview(g_grid)
Estimate all bboxes
head(do.call("rbind", lapply(g_grid %>% st_transform(4326), st_bbox)))
#> xmin ymin xmax ymax
#> [1,] 5.970445 47.30249 6.455849 47.63163
#> [2,] 6.440454 47.31382 6.923301 47.64117
#> [3,] 6.910729 47.32325 7.390967 47.64877
#> [4,] 7.381223 47.33077 7.858798 47.65445
#> [5,] 7.851885 47.33638 8.326746 47.65819
#> [6,] 8.322667 47.34009 8.794764 47.66001
Created on 2021-06-30 by the reprex package (v2.0.0)