0
library(sf)
library(geobr)
library(tidyverse)
I’m having trouble using the st_intersection()
package sf
for certain data loaded with the package geobr
.
For example, if I try to make an intersection between the sf of a state and an sf of Conservation Units of the country, I can’t:
ucs <- read_conservation_units()
paraiba <- read_state(code_state = "PB")
If I try to make the intersection, the following message appears:
st_intersection(ucs, paraiba)
Error in s2_geography_from_wkb(x, oriented = oriented, check = check) :
Evaluation error: Found 5 features with invalid spherical geometry.
[402] Loop 0 is not valid: Edge 3 is degenerate (duplicate vertex)
[820] Loop 0 is not valid: Edge 3 is degenerate (duplicate vertex)
[1394] Loop 0 is not valid: Edge 3 is degenerate (duplicate vertex)
[1429] Loop 0 is not valid: Edge 3 is degenerate (duplicate vertex)
[1555] Loop 0 is not valid: Edge 3 is degenerate (duplicate vertex).
On the other hand, I use the same logic to make the intersection between the biomes, the function st_intersection()
works:
biomas <- read_biomes()
biomas_paraiba <- st_intersection(biomas, paraiba)
Checking the biomas_paraiba
:
biomas_paraiba
Simple feature collection with 3 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -38.7656 ymin: -8.302955 xmax: -34.79288 ymax: -6.026569
Geodetic CRS: SIRGAS 2000
name_biome code_biome year code_state abbrev_state name_state code_region name_region geom
2 Caatinga 2 2019 25 PB Paraíba 2 Nordeste MULTIPOLYGON (((-35.22321 -...
4 Mata Atlântica 4 2019 25 PB Paraíba 2 Nordeste MULTIPOLYGON (((-35.23148 -...
7 Sistema Costeiro NA 2019 25 PB Paraíba 2 Nordeste MULTIPOLYGON (((-35.00158 -...
And the Plot:
ggplot()+
geom_sf(data = biomas_paraiba, aes(fill = name_biome))
In short: because I can make the intersection with the biomes, but I can’t with the conservation units?
See the error message, one of the shapefiles (probably the one from Ucs) has invalid geometry, which is quite common. Try using
sf::st_make_valid
before the intersection.– Carlos Eduardo Lagosta
It worked perfectly with the
sf::st_make_valid
. Thanks @Carloseduardolagosta– itamar