ipeaGIT / geobr

Easy access to official spatial data sets of Brazil in R and Python
https://ipeagit.github.io/geobr/
800 stars 119 forks source link

problem saving urban areas #135

Closed pedro-andrade-inpe closed 4 years ago

pedro-andrade-inpe commented 4 years ago

I'm trying to save urban areas into shapefiles, but I got an error:

urb <- read_urban_area()
sf::write_sf(urb, "urban.shp")
Failed to create feature 13075 in urban
Error in CPL_write_ogr(obj, dsn, layer, driver, as.character(dataset_options),  : 
  Feature creation failed.
In addition: Warning messages:
1: In abbreviate_shapefile_names(obj) :
  Field names abbreviated for ESRI Shapefile driver
2: In CPL_write_ogr(obj, dsn, layer, driver, as.character(dataset_options),  :
  GDAL Error 1: Attempt to write non-polygon (MULTILINESTRING) geometry to POLYGON type shapefile.

I think I've seen this problem before in geobr, but I coundn't find an issue.

rafapereirabr commented 4 years ago

Hi Pedro. Thanks for the heads up. We have to dig into this to find what exactly is causing the problem. It might be something related to st_make_valid , see this sf issue. In the mean time, this will probably solve your problem. I am copying @babisan08 so perhaps she could help us find the origin of the problem.

 p = st_collection_extract(urb, "POLYGON")
 st_write(p, "p.shp")
pedro-andrade-inpe commented 4 years ago

I think it is just a missing call to st_cast("MULTIPOLYGON").

rafapereirabr commented 4 years ago

Yest, using st_cast("MULTIPOLYGON") is probably the best approach to fix this. I have already updated our prep_ script and updated the files in the server. Closing the issue now.

rafapereirabr commented 4 years ago

@pedro-andrade-inpe , this problem is also present in other data sets (see issue #151 ). One way to automate the solution to this would be to run mysf<- sf::st_cast(mysf, "MULTIPOLYGON") for all polygon data sets before saving them in our server.

I wondering though whether this could have any side effects... do you think this is safe?