Importing geographical data from OpenStreetMap¶
The following makes use of the sf package. You may need to install it from
CRAN using the code
install.packages("sf")
if you want to run this on your computer. (The
package is already installed on the notebook container, however.)
library(sf)
Linking to GEOS 3.9.0, GDAL 3.2.2, PROJ 7.2.1
The file used here was extracted by hand from http://www.openstreetmap.org. It is available from here.
st_layers("stpauls.osm")
Driver: OSM
Available layers:
layer_name geometry_type features fields
1 points Point NA 10
2 lines Line String NA 9
3 multilinestrings Multi Line String NA 4
4 multipolygons Multi Polygon NA 25
5 other_relations Geometry Collection NA 4
stpauls_lines <- st_read("stpauls.osm",layer="lines")
Reading layer `lines' from data source `/home/elff/webdevel/sphinx/elff.eu/book/data-management-r/08-spatial-geographical-data/stpauls.osm' using driver `OSM'
Simple feature collection with 130 features and 9 fieldsgeometry type: LINESTRING
dimension: XY
bbox: xmin: -0.1004085 ymin: 51.51318 xmax: -0.0970669 ymax: 51.51434geographic CRS: WGS 84
stpauls_polygons <- st_read("stpauls.osm",layer="multipolygons")
Reading layer `multipolygons' from data source `/home/elff/webdevel/sphinx/elff.eu/book/data-management-r/08-spatial-geographical-data/stpauls.osm' using driver `OSM'
Simple feature collection with 11 features and 25 fieldsgeometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -0.1048984 ymin: 51.50898 xmax: -0.0842498 ymax: 51.51706geographic CRS: WGS 84
- R file: importing-openstreetmap.R
- Rmarkdown file: importing-openstreetmap.Rmd
- Jupyter notebook file: importing-openstreetmap.ipynb
- Interactive version of the Jupyter notebook (shuts down after 60s):
- Interactive version of the Jupyter notebook (sign in required):