# NOT RUN {
# This code compares centroids of municipalities against esp_get_capimun
# It also download tiles, make sure you are online
library(sf)
# Get shape
area <- esp_get_munic_siane(munic = "Valladolid", epsg = 3857)
# Area in km2
print(paste0(round(as.double(sf::st_area(area)) / 1000000, 2), " km2"))
# Extract centroid
centroid <- sf::st_centroid(area)
centroid$type <- "Centroid"
# Compare with capimun
capimun <- esp_get_capimun(munic = "Valladolid", epsg = 3857)
capimun$type <- "Capimun"
# Get a tile to check
tile <- esp_getTiles(area, zoommin = 2)
# Join both point geometries
points <- rbind(
centroid[, "type"],
capimun[, "type"]
)
# Check on plot
library(ggplot2)
ggplot(points) +
layer_spatraster(tile) +
geom_sf(data = area, fill = NA, color = "blue") +
geom_sf(data = points, aes(fill = type), size = 5, shape = 21) +
scale_fill_manual(values = c("green", "red")) +
theme_void() +
labs(title = "Centroid vs. capimun")
# }
Run the code above in your browser using DataLab