Overlaying shapefiles or raster over interactive maps
Asked Answered
L

2

6

I'm using R, and I want to overlay some raster data (e.g. a temperature map from a model) over an interactive map which allows panning and zooming. Ideally, I'd like to overlay over Google Maps or OpenStreetMaps. The input data can be in shapefiles, KML, raster data or whatever comes in handy.

  • I know I can easily do this non-interactively using either googleVis, ggmap or RgoogleMaps. But I do not want to use tiles, I want interaction! Zooming, panning etc., directly from the browser.

  • googleVis, as far as I know, unfortunately only allows to show interactively points or addresses, not areas.

  • This question is very similar but I definitely want to try to do this using R. I can create the KML or geoJSON from R, but how do I overlay it from R directly?

  • OpenStreetMaps is also fine, however I've not found any reference on how to overlay data over it from R, despite the fact that OSM seems to have a pretty straightforward API.

Livy answered 2/12, 2015 at 10:39 Comment(0)
F
3

The leaflet package may be of interest for you. You can easily add a raster object. From the documentation

Two-dimensional RasterLayer objects (from the raster package) can be turned into images and added to Leaflet maps using the addRasterImage function.

And here is an example also from the documentation:

library(leaflet)
library(raster)

r <- raster("nc/oisst-sst.nc")
pal <- colorNumeric(c("#0C2C84", "#41B6C4", "#FFFFCC"), values(r),
  na.color = "transparent")

leaflet() %>% addTiles() %>%
  addRasterImage(r, colors = pal, opacity = 0.8) %>%
  addLegend(pal = pal, values = values(r),
    title = "Surface temp")
Flying answered 2/12, 2015 at 11:50 Comment(1)
That is absolutely excellent! Thanks. I really have no idea how I managed to miss this wonderful package.Livy
M
6

The mapview package has been developed for this particular purpose. It also comes with a variety of background map layers. For a short introduction to what mapview is capable of, feel free to browse the package vignette. Here, for example, is some code that displays locations of selected breweries in Franconian Switzerland overlaid by a sample Landsat 8 scene (band 10). Check out ?breweries91 and ?poppendorf to retrieve information about the data used below and ?mapview to grow familiar with the numerous costumization options.

## require package
# install.packages("mapview")
library(mapview)

## visualize breweries and add landsat 8 band 10
mapview(breweries91) + 
  poppendorf[[10]]

mapview_viewer

Momentum answered 16/12, 2015 at 10:1 Comment(1)
This is brilliant! Unfortunately I already implemented this using leaflet, but I really like this package. The quality, quantity and availability of R packages will never cease to impress me. Well done with the package!Livy
F
3

The leaflet package may be of interest for you. You can easily add a raster object. From the documentation

Two-dimensional RasterLayer objects (from the raster package) can be turned into images and added to Leaflet maps using the addRasterImage function.

And here is an example also from the documentation:

library(leaflet)
library(raster)

r <- raster("nc/oisst-sst.nc")
pal <- colorNumeric(c("#0C2C84", "#41B6C4", "#FFFFCC"), values(r),
  na.color = "transparent")

leaflet() %>% addTiles() %>%
  addRasterImage(r, colors = pal, opacity = 0.8) %>%
  addLegend(pal = pal, values = values(r),
    title = "Surface temp")
Flying answered 2/12, 2015 at 11:50 Comment(1)
That is absolutely excellent! Thanks. I really have no idea how I managed to miss this wonderful package.Livy

© 2022 - 2024 — McMap. All rights reserved.