ropensci / osmplotr

Data visualisation using OpenStreetMap objects
https://docs.ropensci.org/osmplotr
132 stars 21 forks source link

if(nrow(NULL)==0) error in extract_osm_objects #50

Closed brry closed 5 years ago

brry commented 5 years ago

If no data is returned for a given key in a bbox, a warning is intended. In some cases there may be an error instead:

library(osmplotr)
bbox <- get_bbox(c(12.481225, 53.169504, 12.681225, 53.269504))
dat_W <- extract_osm_objects(key="waterway", bbox=bbox)
# if(nrow(obj) == 0) warning('No valid data returned')
# Error: argument is of length zero

This happens because obj is NULL, which it is because extract_osm_objects uses obj$osm_multipolygons instead of obj$lines (or obj$polygons) if 'waterway' %in% q_keys and for this particular bbox there are no multipols in OSM.

The solution to avoid the error is to use NROW instead of nrow:

nrow(NULL) ; NROW(NULL)
# returning NULL and 0, respectively

Before I submit a pull request:

I'm currently thinking "yes" for both, but am relatively new to the package and may be overlooking things. Also, did I PR the other two things (#48, #49) the way you'd want them?

mpadge commented 5 years ago

That all sounds great. Thanks for the other 2 PRs. As for your questions:

  • should the default for waterway be obj$lines instead of obj$osm_multipolygons? (Or obj$polygons, which als has a few entries for this bbox?)

My inclination is to have waterways as polygons, because I believe this is more common than waterways as lines. The main waterway-as-line case is for river mid-lines, but there are lots of other kinds of waterways that are almost all polygonal. That said, this is mostly just subjective opinion, so I'm open to other options.

  • should the warning include No valid data returned. (Maybe try a different 'return_type').

Yep, that would help a lot. Please PR! - thanks in advance