【发布时间】: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