source("zscripts/z_helpFX.R")
6 Equal-area grid for your area of interest
6.1 Data import
Load the required packages.
6.2 Polygons on the area of interest
# Creating the bounding box
<- st_bbox(c(xmin = 30, xmax = 60, ymax = -7, ymin = -35),
bbox crs = st_crs("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")) %>%
st_as_sfc() %>%
st_transform(crs = robin)
# Taking 0.1 from borders to avoid border effect
<- st_bbox(c(xmin = 30.1, xmax = 59.9, ymax = -7.1, ymin = -34.9),
f_bbox crs = st_crs("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")) %>%
st_as_sfc() %>%
st_transform(crs = robin)
# Area
<- 100 # in km2
CellArea <- 2 * sqrt((CellArea*1e6)/((3*sqrt(3)/2))) * sqrt(3)/2 # Diameter in m
h_diameter <- sqrt(CellArea*1e6) # Diameter in m s_diameter
6.3 Planning units for the whole region
# Creating an equal-area grid
<- st_make_grid(f_bbox,
PUs square = F,
cellsize = c(h_diameter, h_diameter),
what = "polygons",
crs = st_crs(f_bbox)) %>%
st_sf()
# Check cell size worked OK.
print(paste0("Range of cellsize are ",
round(as.numeric(range(units::set_units(st_area(PUs), "km^2")))[1])," km2 to ",
round(as.numeric(range(units::set_units(st_area(PUs), "km^2")))[2])," km2"))
6.4 Final equal-area grid
# Get rid of "land" polygons
<- st_centroid(PUs) %>%
logi_PUs st_intersects(world_sfRob) %>%
> 0 # Get logical vector instead of sparse geometry binary
lengths <- PUs[logi_PUs == FALSE, ]
PUs1 plot(st_geometry(PUs1))
6.5 Plot the output
<- ggplot() +
g1 geom_sf(data = PUs1, size = 0.05) +
geom_sf(data = world_sfRob, size = 0.05, fill = "grey20") +
theme_bw()
print(g1)
# ggsave("MYFILE.png", plot = g1, width = 30, height = 30, dpi = 600, limitsize = FALSE)
# st_write(obj = PUs, dsn = "MYDIRECTORY", layer = "MYFILE", driver = "ESRI Shapefile")