微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 R 中将调用的 OSM 数据从 SF 转换为 Shapefile 的最佳方法

如何解决在 R 中将调用的 OSM 数据从 SF 转换为 Shapefile 的最佳方法

完全不熟悉 R,请原谅 -

我正在尝试使用 R 创建一些历史 OSM 数据,这些数据在 R 脚本中存储为 sf(简单功能)。我想将这些数据导出为 QGIS 可读的 shapefile(或 GEOJSON)。我这样做的原因是,在我看来,通过 Rs osmdata提取特定历史数据集是在其编辑历史记录中的给定时间段内创建特定 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. 

此外,我猜测更重要的是,当我添加 function 以从 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 新手。

解决方法

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

关于其他问题:

{sf} 的世界中工作时,我建议通过 osmdata_sf() 调用下载您的数据;除非绝对必要,否则避免混用 sfsp 世界,这对您的理智有益。

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

使用循环路径时,只需将 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))

Sevilla bike paths

# 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。