Hi, I've recently started learning R. I'm working on a script where I'm trying to overlay shapefiles of over 2000 conservation units on top of a shapefile of animal species. However, when I attempt this intersection (this overlay), the intersection results in 0 and I want to generate a shapefile at the end with this overlay made for each unit with the species of that respective area. I'm not sure what else to do. Below is a screenshot and the script.
library (terra)
library (sf)
Read the shapefiles
conservation_units <- vect("C:/Users/Users/Desktop//gustavo/platirrino mapa/shape file UC/ucs.shp")
Create a new field with the names of the conservation units
conservation_units$name_uc <- as.factor(conservation_units$name_uc)
Separate the conservation units by name
separated_units <- split(conservation_units, conservation_units$name_uc)
Suppose 'biomes' is a list of shapefiles of the biomes
biomes <- list(
cerrado = st_read("C:/Users/Users/Desktop/gustavo/platirrino mapa/recorte das espécies por bioma/platirrino no cerrado/primatas do cerrado.shp"),
mata_atlantica = st_read("C:/Users/Users/Desktop//gustavo/platirrino mapa/recorte das espécies por bioma/platirrino na mata atlântica/platirrino mata atlântica.shp"),
pampa = st_read("C:/Users/Users/Desktop/gustavo/platirrino mapa/recorte das espécies por bioma/platirrino no pampa/platirrino no pampa.shp"),
amazonia = st_read("C:/Users/Users/Desktop/gustavo/platirrino mapa/recorte das espécies por bioma/platirrino na amazônia/platirrino amazônia.shp"),
pantanal = st_read("C:/Users/Users/Desktop/gustavo/platirrino mapa/recorte das espécies por bioma/platirrino no pantanal/platirrino pantanal.shp"),
caatinga = st_read("C:/Users/Users/Desktop/gustavo/platirrino mapa/recorte das espécies por bioma/platirrino caatinga/teste novo.shp")
)
Initialize a list to store the results
intersections <- list()
For each conservation unit...
for(i in seq_along(separated_units)) {
... and for each biome...
for(j in seq_along(biomes)) {
... calculate the intersection of the conservation unit with the biome
intersection <- intersect(separated_units[[i]], biomes[[j]])
Add the intersection to the list of results
intersections[[paste0("uc_", i, "biome", names(biomes)[j])]] <- intersection
}
}
For each intersection...# ... plot the intersection
for(i in seq_along(intersections)) {
plot(intersections[[i]], main = names(intersections)[i])
}
Test to see if this *** is working
For each conservation unit...
for(i in seq_along(separated_units)) {
... and for each biome...
for(j in seq_along(biomes)) {
... calculate the intersection of the conservation unit with the biome
intersection <- intersect(separated_units[[i]], biomes[[j]])
Verify if the intersection is empty
if(nrow(intersection) > 0) {
If it's not empty, add the intersection to the list of results
intersections[[paste0("uc_", i, "biome", names(biomes)[j])]] <- intersection
}
}
}