首页 > 解决方案 > 在 R 中将调用的 OSM 数据从 SF 转换为 Shapefile 的最佳方法

问题描述

对 R 完全陌生,请原谅我-

我正在尝试使用 R 创建一些历史 OSM 数据,这些数据存储为sfR 脚本中的(简单功能)。我想将这些数据导出为 QGIS 可读的 shapefile(或 GEOJSON)。我这样做的原因是,在我看来,通过 Rsosmdata包提取特定的历史数据集是在其编辑历史中的给定时期创建 OSM 历史数据的特定段数据的最有效方法(如果有人有,请告诉我对于不同年份的国家级批量数据,这是一种更快的方法。)

我当前的代码使用示例数据集,如下所示:

library(osmdata)

library(sf)

q1 <- opq('Sevilla') %>%
  add_osm_feature(key = 'highway', value = 'cycleway')
cway_sev <- osmdata_sp(q1)
sp::plot(cway_sev$osm_lines)

我收到两种不同类型的错误:

当我添加一个特定的日期时间(像这样:)时q1 <- opq('Sevilla',datetime = "2015-01-01T12:00:00Z") %>%,我收到了这个错误:

Error in check_for_error(doc) : General overpass server error; returned:
The data included in this document is from www.openstreetmap.org. The data is made available under ODbL. runtime error: Query timed out in "query" at line 4 after 45 seconds. 

此外,我猜更重要的是,当我添加从 SF 转换为 SHP的函数st_write(cway_sev, "sev_t_1.shp")时 ( )

我收到此错误:

Error in UseMethod("st_write") : 
  no applicable method for 'st_write' applied to an object of class "c('list', 'osmdata', 'osmdata_sp')"

有什么建议吗?再次,在这里完成 R 新手。

标签: rgisopenstreetmapsfrgdal

解决方案


我无法帮助您解决历史性的超时问题datetime;据我所知,可能存在服务器端问题(我得到相同的错误,并且您对参数的构造似乎遵循文档)。

关于其他问题:

在世界工作时,{sf}我建议通过osmdata_sf()电话下载您的数据;除非绝对必要,否则避免混合sf和世界对你的理智有好处。sp

返回的对象不仅包含线,还包含点、多边形和(在您的情况下为空)多类型对象。

使用循环路径时,只需将osm_lines对象选择为新变量;它将包含具有线串类型几何形状的塞维利亚自行车道。

目视检查后,您现在可以将其保存为 ESRI Shapefile;请注意,这是一种古老的文件格式,基于 Ashton Tate dBase IV(为 Pete 准备的 DOS 程序 :),因此允许数据列名称仅包含有限数量的字符,因此会发出警告。

library(osmdata)
library(dplyr)
library(sf)

sevilla <- opq('Sevilla') %>%
  add_osm_feature(key = 'highway', value = 'cycleway') %>% 
  osmdata_sf()

names(sevilla) # note the points, polygons, multilines and multipolygons
# [1] "bbox"              "overpass_call"     "meta"              "osm_points"        "osm_lines"        
# [6] "osm_polygons"      "osm_multilines"    "osm_multipolygons"

# just a single geometry type
sevilla_lines <- sevilla$osm_lines

# visual check of lines geometry only / does this look right?
plot(st_geometry(sevilla_lines))

塞维利亚自行车道

# this will work, with the lines only
st_write(sevilla_lines, "sevilla.shp")
# Writing layer `sevilla' to data source `sevilla.shp' using driver `ESRI Shapefile'
# Writing 555 features with 34 fields and geometry type Line String.
# Warning message:
# In abbreviate_shapefile_names(obj) :
#  Field names abbreviated for ESRI Shapefile driver

推荐阅读