prioritizr / wdpar

Interface to the World Database on Protected Areas
https://prioritizr.github.io/wdpar
GNU General Public License v3.0
37 stars 5 forks source link

Cleaning Brazil areas issue #13

Closed GatesDupont closed 4 years ago

GatesDupont commented 5 years ago

I'm trying to download and clean the wdpa data for Brazil, and have run into an error.

I start by downloading the data, which works, but I get an error just about halfway through cleaning it.

brazil = wdpa_fetch("BRA") %>%
  wdpa_clean()

Here's the result, note the error on the bottom:

removing areas that are not implemented: v
removing UNESCO reserves: v
removing points with no reported area: v
repairing geometry: v
wrapping dateline: v
repairing geometry: v
projecting areas: v
repairing geometry: v
buffering by zero: v
buffering points: v
repairing geometry: v
snapping geometry to grid: v
repairing geometry: v
formatting attribute data: v
erasing overlaps: ~
[============>--------------] 1012/2062 ( 49% eta:) eta:  2mError in CPL_geos_union(st_geometry(x), by_feature) : 
  Evaluation error: TopologyException: Input geom 0 is invalid: Self-intersection at or near point -4572218.671977994 -3014698.965008833 at -4572218.671977994 -3014698.965008833.

I get a similar issue with Mexico, as well.

Any help with this would be greatly appreciated!

Thanks!

jeffreyhanson commented 5 years ago

Hi,

I'm very sorry for the slow response. I've been on vacation and then attending a conference, so I haven't had a chance to respond to you earlier. It looks like the wdpar is having problems with the geometry of the data. Broadly speaking, R's ability to clean invalid geometry is generally pretty good (and by this I specifically mean the lwgeom package), but it can fail to clean really invalid geometries. Unfortunately, there isn't much I can do about this.

I would recommend: (i) manually saving the data to a shapefile (see sf::st_write), (ii) fixing it ESRI ArcMap using the repair geometry tool, and (iii) then importing the fixed data into R and running the wdpa_clean function again. I'm sorry I don't have a better solution that doesn't involve commercial software.

Cheers,

Jeff

antoinestevens commented 4 years ago

Hello, I ran into the same problem (see also #16 ) and solved it by changing in st_erase_overlaps this line:

 u <- sf::st_union(sf::st_set_precision(sf::st_buffer(o[ovr], 0), precision))

to

u <- sf::st_buffer(sf::st_union(sf::st_set_precision(sf::st_buffer(o[ovr], 0), precision)),dist = 0)
jeffreyhanson commented 4 years ago

Awesome work @antoinestevens! Thank you so much for coming up with a fix! I'll patch that into the dev version of the wdpar package now.

antoinestevens commented 4 years ago

Actually, it essentially fixed #16 but not this issue. Modifying 2 lines below (don't ask me why) with st_make_valid will finally fix all my problems d <- sf::st_difference(sf::st_set_precision(g[i],precision), lwgeom::st_make_valid(sf::st_set_precision(u, precision)))

antoinestevens commented 4 years ago

Thank YOU for your awesome work (inc. your other super useful packages)

jeffreyhanson commented 4 years ago

Ok brilliant - I'll patch that fix in as well - thanks again!

jeffreyhanson commented 4 years ago

Thank you - I really appreciate it.

jeffreyhanson commented 4 years ago

I forgot to tag this issue in the latest commit, so I'll close this manually now.