r - 如何使用osmar提取osm数据

标签 r windows osmar

类似于this question我从 get_osm

收到错误
library(maptools)
library(osmar)
url <- "http://osmar.r-forge.r-project.org/"
file <- "muenchen.osm.gz"
# download.file(sprintf("%s%s", url, file), file)
# gzip is linux only, on windows I unzipped this manually with 7zip!
# unzip("gzip -d muenchen.osm.gz") 

src <- osmsource_osmosis(file = "140-muenchen.osm")
muc_bbox <- center_bbox(11.575278, 48.137222, 3000, 3000)
muc <- get_osm(x = muc_bbox, source = src)

错误是

Fehler in file(con, "r") : kann Verbindung nicht öffnen Zusätzlich: Warnmeldung: In file(con, "r") : kann Datei 'C:\Users\sqc\AppData\Local\Temp\RtmpQtndOZ\file3bc03a7b5127' nicht öffnen: No such file or directory

我以管理员身份运行 RStudio,安装了 gzip,但它仍然不起作用。有任何想法吗?我在 Windows 上。

(示例来自here)


编辑:

我的路径看起来像C:/07 Rprogress/00 Erste Testprogramme/140-muenchen.osm.gz,但我希望这不是问题

file.exists("muenchen.osm.gz")
[1] TRUE
> shell("7z e muenchen.osm.gz")
Der Befehl "7z" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
Warnmeldung:
In shell("7z e muenchen.osm.gz") :
  '7z e muenchen.osm.gz' Ausführung mit Fehlerkode 1 fehlgeschlagen
> shell.exec("7z e muenchen.osm.gz")
Fehler in shell.exec("7z e muenchen.osm.gz") : 
  '7z e 140-muenchen.osm.gz' nicht gefunden
> system("7z e muenchen.osm.gz")
[1] 127

我按照 here 的说明进行操作并设置

set PATH=%PATH%;C:\Program Files\7-Zip\
echo %PATH%

编辑 2:

st_layers("myfile.osm", do_count = TRUE)
Driver: OSM 
Available layers:
        layer_name       geometry_type features fields
1           points               Point       61     10
2            lines         Line String        0      9
3 multilinestrings   Multi Line String        0      4
4    multipolygons       Multi Polygon        0     25
5  other_relations Geometry Collection        0      4
Warnmeldungen:
1: In CPL_get_layers(dsn, options, do_count) :
  GDAL Error 1: Non increasing node id. Use OSM_USE_CUSTOM_INDEXING=NO
2: ...

最佳答案

以下代码应该适用于将 muenchen.osm.gz 文件读入 R。

# packages
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
options(width = 120)

# download muenchen.osm.gz file
download.file(
  url = "http://osmar.r-forge.r-project.org/muenchen.osm.gz", 
  destfile = "muenchen.osm.gz", 
  mode = "wb"
)
# Unzip using 7zip. You can also do this manually. 
shell("7z e muenchen.osm.gz")

我现在正在使用 sf 方法进行编码。我非常确定可以使用 osmdata 在 R 中读取相同的 .osm 文件,但我暂时无法使其工作。

# 1. List all the layers
st_layers("muenchen.osm", do_count = TRUE)
#> Driver: OSM 
#> Available layers:
#>         layer_name       geometry_type features fields
#> 1           points               Point    37583     10
#> 2            lines         Line String    56325      9
#> 3 multilinestrings   Multi Line String       96      4
#> 4    multipolygons       Multi Polygon    32328     25
#> 5  other_relations Geometry Collection      337      4

# 2. Read the lines layer (or any other layer)
muenchen_lines <- read_sf("muenchen.osm", layer = "lines")

在 SO 中,对象的打印相当困惑,但如果在 R 中复制并粘贴代码,就会更清晰。

# 3. Print the result
muenchen_lines
#> Simple feature collection with 56325 features and 9 fields
#> geometry type:  LINESTRING
#> dimension:      XY
#> bbox:           xmin: 11.30704 ymin: 48.0155 xmax: 11.82806 ymax: 48.28529
#> geographic CRS: WGS 84
#> # A tibble: 56,325 x 10
#>    osm_id name    highway waterway aerialway barrier man_made z_order other_tags                                geometry
#>    <chr>  <chr>   <chr>   <chr>    <chr>     <chr>   <chr>      <int> <chr>                             <LINESTRING [°]>
#>  1 98     <NA>    tertia~ <NA>     <NA>      <NA>    <NA>           4 "\"junction\"=>\"r~ (11.36604 48.1764, 11.36607 4~
#>  2 99     <NA>    tertia~ <NA>     <NA>      <NA>    <NA>           4 "\"junction\"=>\"r~ (11.36614 48.17414, 11.36617 ~
#>  3 100    <NA>    second~ <NA>     <NA>      <NA>    <NA>           6 "\"junction\"=>\"r~ (11.33037 48.18013, 11.33031 ~
#>  4 101    <NA>    second~ <NA>     <NA>      <NA>    <NA>           6 "\"junction\"=>\"r~ (11.32986 48.18367, 11.32973 ~
#>  5 102    <NA>    primary <NA>     <NA>      <NA>    <NA>           7 "\"junction\"=>\"r~ (11.45455 48.1672, 11.45424 4~
#>  6 680    <NA>    motorw~ <NA>     <NA>      <NA>    <NA>          29 "\"bridge\"=>\"yes~ (11.54136 48.22608, 11.543 48~
#>  7 1939   Gerani~ reside~ <NA>     <NA>      <NA>    <NA>           3  <NA>               (11.48282 48.10904, 11.48291 ~
#>  8 1945   Sonnen~ reside~ <NA>     <NA>      <NA>    <NA>           3  <NA>               (11.48714 48.10564, 11.48732 ~
#>  9 122956 Berber~ reside~ <NA>     <NA>      <NA>    <NA>           3 "\"maxspeed\"=>\"3~ (11.42325 48.15542, 11.42308 ~
#> 10 122957 Otto-K~ reside~ <NA>     <NA>      <NA>    <NA>           3 "\"maxspeed\"=>\"3~ (11.42276 48.15559, 11.42251 ~
#> # ... with 56,315 more rows

# 4. Plot
par(mar = rep(0, 4))
plot(st_geometry(muenchen_lines))

reprex package于2020年5月26日创建(v0.3.0)

您还应该检查 osmdata vignettes如果您想构建自己的查询以从开放街道 map 下载数据。

引用文献

如果您想查看有关 sf 的一些引用资料,我建议您阅读 vignettes (参见文章)的包和Geocomputation with R的第1章至第7章。如果您想了解如何使用 osmdata 创建自己的 Open Street Map 查询,我建议您阅读 vignettes 。这两种方法之间存在一些差异,但您可以在小插图中阅读它们。

据我所知,使用 sf.osm 数据读取到 R 的建议方法(参见 hereherehere )是 1) 转换 .osm 数据转换为 .gpkg 格式(您可以使用 sf::gdal_utils)并 2) 读取 .gpkg 文件。如果您可以共享 .osm 文件,我可以从我的笔记本电脑上查看它。

提到了您在OP中显示的错误here如果您可以共享 .osm 文件,我可以测试阅读它。

关于r - 如何使用osmar提取osm数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62005168/

相关文章:

c++ - 无法运行在 Qt Creator 中创建的程序?

r - 在 R 中使用 Open Street Map 和 get_osm {osmar}

R ggplot2 - 使用 RGui 绘图中没有背景或网格线

r - 如何在不加载内存文件的情况下以像素为单位获取图像宽度和高度

c# - 存储 Windows 密码

windows - PowerShell 复制项方法失败 - 文件名中的括号

r - 如何使用 full_join 将 sf map 数据与 tibble 数据连接起来?

r - 将 UI 插入 R Shiny 模块服务器

r - 根据 gps 坐标列表更改基于开放街道 map 的 igraph 中路径边缘的权重