Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
889 views
in Technique[技术] by (71.8m points)

gis - Best way to convert called OSM data from SF to Shapefile within R

Completely new to R, forgive me -

I'm trying to use R to create some historic OSM data thats stored as a sf (simple feature) within the R script. I'd like to export this data once called as a shapefile (or GEOJSON) readable by QGIS. The reason I am doing this is that it seems to me that extracting specific historic datasets via Rs osmdata package is the most efficient way of creating data of specific segments of OSM historic data for a given period in its editing history (Let me know if anyone have a faster way of doing this for country-sized batches of data for different years.)

My current code, using a sample dataset, looks like this:

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)

I'm getting two different types of errors:

When I add a specific datetime (like this: q1 <- opq('Sevilla',datetime = "2015-01-01T12:00:00Z") %>%), I'm getting this error:

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. 

Additionally, and I'm guessing more improtantly, when I add the function to convert from an SF to a SHP (st_write(cway_sev, "sev_t_1.shp"))

I get this error:

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

Any advice? Again, complete R newbie here.

question from:https://stackoverflow.com/questions/65846657/best-way-to-convert-called-osm-data-from-sf-to-shapefile-within-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I can not help you with the timeout on a historic datetime; there may be a server side issue for all that I know (I get the same error, and your construction of the argument seems to follow documentation).

Regarding the other issues:

When working in the world of {sf} I suggest to download your data via osmdata_sf() call; it will be good for your sanity if you avoid mixing sf and sp worlds unless absolutely necessary.

The returned object will contain not only lines, but also points, polygons and (in your case empty) multi-type objects.

When working with cycle paths just select the osm_lines object to a new variable; it will contain Sevilla bike paths with geometry of type linestring.

After checking it visually you can now save it as a ESRI Shapefile; note that this is an ancient file format, based on Ashton Tate dBase IV (a DOS program for Pete's sake :) and as such allows data column names of only limited number of characters, hence the warning.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...