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

wdpa_clean.R #26

Closed fguilhaumon closed 4 years ago

fguilhaumon commented 4 years ago

Hello,

working with wdpa_clean.R. Regarding the following step:

## return empty dataset if no valid non-empty geometries remain

I'm wondering if line 311:

if (all(sf::st_is_empty(x))) {

should be:

if (any(sf::st_is_empty(x))) {

so that the error exists if any geometry is empty. As for now it seems that all geometries need to be empty. Am I missing something ?

Cheers.

jeffreyhanson commented 4 years ago

Hi,

Thanks for checking out the wdpar package. No, I think line 311 is correct (maybe the comment above it is a bit confusing). It's perfectly valid for sf objects to have empty geometries (indeed functions like st_buffer should still work), so an empty geometry is not an necceasrily an error. The reason why this code (line 311) checks for empty geometries is that some of the cleaning code later in the function assumes that there is at least one non-empty (per sf::st_is_empty) and valid geometry (per sf::st_is_valid) in the x object. The subsequent code can handle a mix of empty and non-empty geometries (because the code constantly checks and removes for any empty geometries, e.g. lines 323, 336, 390). So, this code (line 311) is used to detect when all of the geometries in the sf object are empty, so that the function can exit early and return an sf object that just contains an empty geometry (instead of throwing an error when the subsequent code encounters an sf object that just contains an empty geometry). I suppose it might make more sense to have such a check multiple times throughout the function, but I haven't yet encountered a version of the WDPA where this is needed.

Does that make sense? If not, I can try and explain this more clearly?

fguilhaumon commented 4 years ago

Thanks for the follow up. Yes totally makes sense. The problem was on the side of my understanding of the scope of the functions.