Similar to this question I get an error from 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)
The error is
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
I ran RStudio as administrator, I installed gzip, but it still does not work. Any ideas? I am on windows.
(The example is from here)
Edit:
My path looks like C:/07 Rprogress/00 Erste Testprogramme/140-muenchen.osm.gz
but I hope this is not the problem
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
I follow the instructions from here and set
set PATH=%PATH%;C:\Program Files\7-Zip\
echo %PATH%
Edit 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: ...
The following code should work for reading into R the muenchen.osm.gz
file.
# 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")
I'm coding now with the sf
approach. I'm quite sure that it's possible to use osmdata
to read in R the same .osm
file but I can't make it work for the moment.
# 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")
The printing of the object is quite messy here in SO but it's more clear if you copy and paste the code in 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))
Created on 2020-05-26 by the reprex package (v0.3.0)
You should also check the osmdata vignettes if you want to build your own queries for downloading data from Open Street Map.
If you want to check a few references on sf
I would suggest reading the vignettes (see the Articles) of the package and Chapters 1 to 7 of Geocomputation with R. If you want to check how to use osmdata
to create your own queries to Open Street Map I suggest reading the vignettes. There are a few differences between the two approaches but you can read them in the vignettes.
AFAIK the suggested way (see here, here and here) for reading .osm
data into R using sf
is 1) transform the .osm
data into .gpkg
format (you can use sf::gdal_utils
) and 2) read the .gpkg file. If you can share the .osm file I can check it from my laptop.
The error that you show in the OP is mentioned here and if you can share the .osm
file I can test reading it.