Skip to content

Instantly share code, notes, and snippets.

@pepijn-devries
Created October 28, 2016 14:49
Show Gist options
  • Save pepijn-devries/fd29506ad0771c08423d30b4c1aaf40b to your computer and use it in GitHub Desktop.
Save pepijn-devries/fd29506ad0771c08423d30b4c1aaf40b to your computer and use it in GitHub Desktop.
## This package is used to retrieve data from openstreetmap.org:
require(osmar)
## This pacakge is used to process the spatial data:
require(rgeos)
## This package is used to convert the data from openstreetmap into something workable:
require(maptools)
## function to retrieve a relation from openstreetmap and convert it
## into a SpatialPolygons object:
OSM_relation_to_SP <- function(rel) {
## get the data from openstreetmap.org:
sp_dat <- get_osm(relation(rel), source = osmsource_api(), full = T)
## convert the data to a SpatialLines object:
sp_dat <- as_sp(sp_dat, "lines")
## Assuming that the lines form an outline of something,
## join the connected lines (this was making me pull my
## hair out before including this step, without this
## step the SpatialPolygons object will be messed up):
sp_dat <- gLineMerge(sp_dat)
## These two steps convert the SpatialLines object
## into a SpatialPolygons object:
sp_dat <- SpatialLines2PolySet(sp_dat)
sp_dat <- PolySet2SpatialPolygons(sp_dat)
}
## Create a SpatialPolygons object for Lake IJssel:
lake_ijssel <- OSM_relation_to_SP(945096)
## plot it:
plot(lake_ijssel, col = "lightblue", axes = T)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment