【问题标题】:combining osm data (downloaded with osmdata package) in a existing shapefile map ggplot r在现有的 shapefile 映射 ggplot r 中结合 osm 数据(使用 osmdata 包下载)
【发布时间】:2023-03-22 21:11:01
【问题描述】:

我想使用 OSM 数据绘制地图。可以使用osmdata-R-package 轻松下载所需的数据。不幸的是,对于我所在的地区,OSM 中并非所有数据都可用。因此,我下载了一个带有我感兴趣的行政边界的区域 shapefile。我的问题是,我无法将 shapefile 信息与 OSM 信息一起绘制在一个图中。

我的工作流程:

library(raster)
library(rgdal)
library(tidyverse)
library(broom)
library(rgeos)
library(osmdata)
library(sf)



# Download shapefile from Leipzig with "Ortteile" (engl. urban districts)
https://gruenlink.de/1py4
# import Ortsteile Leipzigs
lpz_ot <- readOGR("ot.shp")
names(lpz_ot)
# convert spatial object to a ggplot ready data frame
lpz_ot_df <- tidy(lpz_ot,OT = "id")
# make sure the shapefile attribute table has an id column
lpz_ot$id <- rownames(lpz_ot@data)
# join the attribute table from the spatial object to the new data frame
lpz_ot_df <- lpz_ot_df %>%
  left_join(lpz_ot@data,by = "id")
# check names
names(lpz_ot_df)


# Download interessting OSM Data (e.g. railways and tramlines)
#bounding box Leipzig
lpz_box <- opq(bbox = 'Leipzig')
# Plygon for Leipzig
lpz_poly <- getbb(place_name = c("Leipzig"),format_out = "polygon")
# railways and tramlines in Leipzig (in bounding box)
sv <- lpz_box%>% 
  add_osm_feature(key = "railway", value = c("tram","rail")) %>%
  osmdata_sf() 
# railways and tramlines in Leipzig (within administrative boundaries of Leipzig)
svt <-  trim_osmdata (sv,lpz_poly,exclude =TRUE)

我可以很容易地用 ggplot 绘制形状文件或 osmdata。但我不能在一个情节中同时绘制两者。我在调整中的错误是什么?

我的剧情代码:

ggplot() +
  geom_path(data = lpz_ot_df, aes(x = long, y = lat, group = group,color="black"))+
  geom_sf(data = svt$osm_lines, aes(color = railway),size=1.3) +
  theme_void() +
  guides(color = FALSE)+
  labs(title ="Urban districts in Leipzigs (with railwaynet)")

要对其进行测试,只需注释掉 geom_path()geom_sf() 行。

我认为,这与坐标有关,但我不知道如何分配正确的坐标。

感谢您的帮助!

【问题讨论】:

    标签: r ggplot2 openstreetmap shapefile


    【解决方案1】:

    这确实与坐标系有关。我建议您也使用sfot.shp 读入R,然后更改坐标系。这将替换代码的第一部分(在下载 OSM 数据之前):

    lpz_ot = st_read('ot.shp') %>% st_transform(crs = 4326)
    

    要绘制它,只需将 ggplot 代码中的 geom_path 行替换为:

    geom_sf(data = lpz_ot)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      • 1970-01-01
      • 2016-01-02
      • 2019-01-02
      • 1970-01-01
      相关资源
      最近更新 更多