My use case is related to calculating the area of a polygon in a shapefile that is covered by another polygon.
Specifically, I want to do the following:
- Create circles that have a 100m radius, from a data.frame of long/lat points
- Overlay these circles onto a shapefile
- Calculate the area of each of these polygons that are covered by these circles.
- Ideally return back a dataframe where each row contains the area covered by all circles for each polygon.
Here I create some simulated data points.
mean_lat <- 46.07998
sd_lat <- 0.1609196
mean_long <- 8.931849
sd_long <- 0.1024659
dat_sim <- data.frame(lat = rnorm(500, mean = mean_lat, sd = sd_lat),
long = rnorm(500, mean = mean_long, sd = sd_long))
Then we get the shapefiles
library(raster)
library(tidyverse)
library(sf)
# regular sp format
dat_ticino_sp <- getData(name = "GADM",
country = "CHE",
level = 3) %>%
subset(.@data$NAME_1 == "Ticino")
# convert to sf format
dat_ticino_sf <- getData(name = "GADM",
country = "CHE",
level = 3) %>%
# convert to simple features
sf::st_as_sf() %>%
# Filter down to Ticino
dplyr::filter(NAME_1 == "Ticino")
So what I want to do is :
create circles with a 100m radius in the
dat_sim, to overlay onto the shapefiledat_ticino_sfordat_ticino_spfiles.For each polygon in the shapefile, calculate how much of that polygon is covered by circles.
Return a data frame where each row (polygon) contains the area covered by the circles.
Plot this.
Bonus points if this can be done in Simple Features.
