Learn R Programming

rosm (version 0.1.3)

osm.raster: Get Open Street Map Tiles As A RasterStack

Description

Get Open Street Map tiles as RasterStack object (requires package raster to be installed).

Usage

osm.raster(x, zoomin = 0, zoom = NULL, type = "osm",
  forcedownload = FALSE, cachedir = NULL, projection = NULL,
  crop = FALSE, filename = NULL, ...)

Arguments

x
A bounding box as generated by sp::bbox() or prettymapr::searchbbox(). Must be in lon/lat (epsg:4326)! Alternatively, pass a Spatial* object to use the bounding box of that
zoomin
The amount by which to adjust the automatically calculated zoom (or manually specified if the zoom parameter is passed). Use +1 to zoom in, or -1 to zoom out.
zoom
Manually specify the zoom level (not recommended; adjust zoomin instead.
type
A map type; one of that returned by osm.types. User defined types are possible by defining tile.url.TYPENAME <- function(xtile, ytile, zoom){} and passing TYPENAME as the type argument.
forcedownload
TRUE if cached tiles should be re-downloaded. Useful if some tiles are corrupted.
cachedir
The directory in which tiles should be cached. Defaults to getwd()/rosm.cache.
projection
A map projection in which to reproject the RasterStack as generated by CRS() or Spatial*@proj4string. If a Spatial* object is passed as the first argument, this argument will be ignored.
crop
TRUE if results should be cropped to the specified bounding box (see x), FALSE otherwise.
filename
A filename to which the raster should be written (see raster::writeRaster()). Use a ".tif" extension to write as a GeoTIFF.
...
Arguments passed on to raster::writeRaster() if filename is specified.

Value

  • A projected RasterStack of the fused tiles.

Examples

Run this code
library(cartography)
library(raster)
library(prettymapr)

ns <- makebbox(47.2, -59.7, 43.3, -66.4)
x <- osm.raster(ns, projection=CRS("+init=epsg:26920"), crop=TRUE)
#plot using plotRGB (from the raster package)
plotRGB(x)

#use a Spatial* object as the first argument to automatically set the bounding
#box and projection
data(nuts2006)
spdf <- nuts0.spdf[nuts0.spdf$id=="DE",]
x <- osm.raster(spdf, type="thunderforestlandscape")
plotRGB(x)

#write to disk by passing a filename argument (use .tif extension to write GeoTIFF)
osm.raster(ns, projection=CRS("+init=epsg:26920"), crop=TRUE, filename="ns.tif")

#can also write Raster* objects using osm.raster
osm.raster(x, filename="germany.tif")

Run the code above in your browser using DataLab