【问题标题】:How to plot river banks instead of a single line in R?如何在 R 中绘制河岸而不是单线?
【发布时间】:2021-11-13 22:44:22
【问题描述】:

我一直在尝试使用 sf 和 osm 制作城市地图,但我正在与河流作斗争。我已经设法提取了河流线,所以它看起来像在图像上,即每条河流有一条线。

有没有办法画出河流的边界,这样就可以看出河流有多宽?

我这样提取行:

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

然后这是我的 ggplot2 代码中的相关位:

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

【问题讨论】:

    标签: r geospatial openstreetmap sf


    【解决方案1】:

    您可以同时查询;你只需要记住标签waterway返回一个线几何,而water返回一个多边形(通常是多多边形类型)。您可能需要对 OSM Overpass API 进行两次调用。

    举一个具体的例子,考虑这段代码,它使用了来自英国伦敦的河流——您希望泰晤士河在此地突出;它穿过城市时变得相当宽......

    请注意,从 API 调用返回的多边形和多多边形都需要映射。

    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", ])
    

    【讨论】:

    • 完美,这正是我所需要的!谢谢你:)
    • @MagdaD 很高兴为您服务! :)
    猜你喜欢
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多