ropensci / osmplotr

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

Displaying ocean #26

Closed richardbeare closed 6 years ago

richardbeare commented 6 years ago

Hi, I'm wondering about the best way to display ocean. I'm aware of the coastline tag for key "natural". However this returns a mix of polygons and lines and it is not obvious how to fill the appropriate category.

Thanks

mpadge commented 6 years ago

Thanks a load for the inspiration here @richardbeare! This hefty commit implements new ~lines2poly~ osm_line2poly function. The example is directly inspired by your question. Check it:

bb <- osmdata::getbb ("melbourne, australia")
coast <- opq (bb) %>%
    add_osm_feature (key = "natural", value = "coastline") %>%
    osmdata_sf ()
coast <- osm_line2poly (coast$osm_lines, bbox = bb)
map <- osm_basemap (bbox = bb) %>%
    add_osm_objects (coast [[1]], col = "lightsteelblue") %>%
    print_osm_map ()

ocean

Give it a spin and let me know what you think!

issues

  1. ~line2poly()~ osm_line2poly() always returns two polygons. For coastline, one will be land, the other water, but there are likely other use cases where distinction won't be so clear, so no attempts are made to distinguish them.
  2. There are probably cases where lines loop outside a given bbox before coming back in and then finally extending beyond again. I think these cases will likely work smoothly, but am not sure.

TODO

  1. ~Return a single sf data.frame with the two polygons instead of a list.~ This ain't easy to do because it needs to have a default [,] -> [,drop=FALSE] operator to work properly. Standard solution would be to re-define/overload the [[.sf operator](https://github.com/r-spatial/sf/blob/master/R/sf.R), but that's silly and dangerous so it's probably better to leave it as a list.
  2. ~Describe this really useful functionality in vignette prior to closing this issue.~
richardbeare commented 6 years ago

Hi, This is close, but didn't work for my case. I'm working on modifications now.

mpadge commented 6 years ago

Okay, let me know and we'll see what else can be added...