Failure with st_intersection() function

Asked

Viewed 20 times

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))

inserir a descrição da imagem aqui

In short: because I can make the intersection with the biomes, but I can’t with the conservation units?

  • 1

    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.

  • It worked perfectly with the sf::st_make_valid . Thanks @Carloseduardolagosta

1 answer

0


Sharing more information: the error message mentioned occurs when working from version 1.0 of the package sf, as PEBESMA explains in that post.

Apparently, as of version 1.0 there has been a change in the way the sf works with the geometry S2.

Thus, I believe that there are two possibilities beyond that presented by @Carloseduardolagosta if working with version 1.0:

  1. the function "sf_use_sf2(FALSE)" is used before performing certain operations (such as st_intersection(), for example);
  2. install the version before version 1.0 of the package sf

Browser other questions tagged

You are not signed in. Login or sign up in order to post.