Magda D
Magda D

Reputation: 37

How to plot river banks instead of a single line in R?

I have been trying to make a map of a city using sf and osm, but I am struggling with rivers. I have managed to extract the river lines, so it looks like on the image, i.e. there is a single line per river.

Is there a way to plot the river borders, so one can see how wide the river is?

I extract the lines like this:

rivers <- bbx %>%
  opq()%>%
  add_osm_feature(key = "waterway", 
                  value = c("river", "riverbank", "canal", "stream")) %>%
  osmdata_sf() 

And then this is the relevant bit in my ggplot2 code:

  geom_sf(data = rivers$osm_lines,
          col = "red",
          size = .15,
          alpha = 1)+

enter image description here

Upvotes: 1

Views: 532

Answers (1)

Jindra Lacko
Jindra Lacko

Reputation: 8749

You can query both; you just need to keep in mind that the tag waterway returns a line geometry, while water returns a polygon (often of the multipolygon kind). You may need to place two calls to the OSM Overpass API.

For a concrete example consider this piece of code, it uses rivers from London, UK - where you expect the Thames to feature prominently; it gets quite wide as it passes thru the city...

Note that both polygons and multipolygons returned from the API call need to be mapped.

library(dplyr)
library(ggplot2)
library(osmdata)

bbx <- getbb("London")

# first API call - rivers as lines
rivers_as_lines <- opq(bbox = bbx) %>% 
  add_osm_feature(key = "waterway") %>%
  osmdata_sf(quiet = F) 

# second API call - rivers as polygons
rivers_as_polygons <- opq(bbox = bbx) %>% 
  add_osm_feature(key = "water") %>%
  osmdata_sf(quiet = F) 

# a visual overview; note the polygons + multipolygons plotted separately
ggplot() +
  geom_sf(data = rivers_as_polygons$osm_polygons, fill = "steelblue") +
  geom_sf(data = rivers_as_polygons$osm_multipolygons, fill = "steelblue") +
  geom_sf(data = rivers_as_lines$osm_lines, color = "red") +
  coord_sf(xlim = bbx["x", ],
           ylim = bbx["y", ])

waters and waterways of Greater London

Upvotes: 4

Related Questions