ropensci / osmplotr

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

sf geometry class warning #47

Closed brry closed 5 years ago

brry commented 5 years ago

Expanding on https://github.com/ropensci/osmplotr/issues/46, here is a suggestion to change a cryptic error to a useful warning. Essence of the previous issue: if sf is not loaded, the subsetting method [.sf is not available and [.data.frame is used. This causes a cryptic error in add_osm_objects -> get_obj_type -> tolower -> strsplit

Here's a MWE. Make sure sf is not loaded when runnning it:

library(osmplotr)
bbox <- get_bbox(c(12.5, 53.1, 12.6, 53.2))
dat_F <- extract_osm_objects(key="landuse", bbox=bbox, quiet=TRUE, value="forest")
map <- osm_basemap(bbox=bbox, bg='gray95')
map <- add_osm_objects(map, dat_F[1:10,], col='green')

Error in strsplit(class(obj$geometry)[i], "sfc_")[[1]] : 
  subscript out of bounds

I suggest adding a warning / error in get_obj_type in line 19

obj <- dat_F[1:10,]

if(!inherits(obj$geometry, "sfc")) warning("object class is sf, but the geometry",
  " column class is '", toString(class(obj$geometry)), "' instead of 'sfc'.\n",
  "This can occur e.g. after subsetting sf objects without the sf package loaded.")
mpadge commented 5 years ago

That's a grand idea, care to do a pull request?