ropensci / osmplotr

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

add_osm_groups() doesn't seem to capture 'waterway' #25

Closed beemyfriend closed 6 years ago

beemyfriend commented 6 years ago

I'm trying to isolate the Schuylkill River from the rest of my map. The following code works with add_osm_objects():

trial <- get_bbox(c(-75.1, 39.9, -75.2, 40.21))

schuylkill <- cbind(c(-75.16, -75, -75, -75.16),
                    c(39.9, 39.9, 40, 40))

dat_H <- extract_osm_objects(key = 'highway', bbox = trial)
dat_W <- extract_osm_objects(key = 'waterway',  bbox = trial, return_type = 'polygon')

osm_basemap(trial, bg = 'gray80') %>%
  add_osm_objects(dat_H) %>%
  add_osm_objects(dat_W,
                  col = 'blue') %>%
  add_axes() %>%
  print_osm_map()

working

However, when I try to isolate the river by using add_osm_groups(), the waterway is completely ignored. It neither turns blue (representing that it is part of the group) nor turns yellow (representing that it is not part of the group):


osm_basemap(trial, bg = 'gray80') %>%
  add_osm_objects(dat_H) %>%
  add_osm_groups(dat_W,
                groups = schuylkill,
                cols = 'blue',
                bg = 'yellow',
                boundary = 0,
                make_hull = T) %>%
  add_axes() %>%
  print_osm_map()

notworking

I'm confident that my grouping box is accurate. I am not sure what the issue is. Could it be with the feature type ('waterway') or am I missing something really obvious?

mpadge commented 6 years ago

Nice catch Ben! It should now work like this (with bboxes and stuff adjusted to give a smaller map):

trial <- get_bbox(c(-75.1, 39.9, -75.16, 39.99))
schuylkill <- cbind(c(-75.15, -75.1, -75.1, -75.15),
                    c(39.9, 39.9, 39.94, 39.94))
osm_basemap(trial, bg = 'gray80') %>%
    add_osm_objects(dat_H, col = "skyblue") %>%
    add_osm_groups(dat_W", groups = schuylkill,
        cols = 'blue', bg = 'yellow', make_hull = T) %>%
    add_axes() %>%
    print_osm_map()

map

The polygons don't quite match up for reasons described in the vignette. As suggested there, it's best in such cases to first plot the object using the desired bg colour, and then overlay with add_osm_groups(). Otherwise this should now do what you want.