[R-sig-Geo] projection of ggmap:get_map() output

Agustin Lobo alobolistas at gmail.com
Thu May 8 10:17:04 CEST 2014


It turns out that the Mary Poppins bag of Robert actually includes
a function dismo:gmap() that returns the GM subscene as a raster layer
of package raster
with correct CRS information, thus http://rpubs.com/alobo/getmapCRS is
kind of obsolete.

We can just do:

require(utils)
require(rgdal)
require(raster)
require(dismo)

## Vector data
download.file("https://dl.dropboxusercontent.com/u/3180464/NWEur.zip","NWEur.zip",method="curl")
unzip("NWEur.zip",exdir="NWEur")
NWEur <- readOGR(dsn="./NWEur",layer="NWEur",stringsAsFactors=FALSE)
NWEurGM <- spTransform(NWEur,CRS("+init=epsg:3857"))

## Background object
miext <- extent(-2.020264,12.042236,46.354589,55.210287)
migmap <- gmap(x=miext, type = "hybrid", zoom = 6,file="NWEurdismo")
migmap

## 1 Standard plots
plot(migmap)
plot(NWEurGM,add=TRUE,lwd=2,ext=extent(migmap),border="yellow")

## 2. rasterVis:
require(latticeExtra)
require(rasterVis)
#NOTE: the order of loading these packages matters!
colors <- migmap at legend@colortable #extract the color from the
pseudocolor raster
levelplot(migmap, maxpixels = ncell(migmap),
          col.regions = colors, at = 0:255,
          panel = panel.levelplot.raster,
          interpolate=TRUE,
          colorkey = FALSE, margin = FALSE) +
  spplot(NWEurGM, zcol = "NAME", fill = "transparent", col = "yellow",
         xlim = c(extent(rgmaprgbGM)@xmin, extent(rgmaprgbGM)@xmax), ylim =
           c(extent(rgmaprgbGM)@ymin, extent(rgmaprgbGM)@ymax),
         colorkey = FALSE)

Note the need of configuring the color style in levelplot() as pointed
out by Oscar.

Agus


On Wed, May 7, 2014 at 2:07 PM, Oscar Perpiñan <oscar.perpinan at upm.es> wrote:
> Hi,
>
>> As far as I can tell, "levelplot() + layer()"
>> or "spplot() + layer()"
>> do not check correspondence of the proj4string of the operands on both sides of
>> the +. Actually, both commands totally ignore any CRS information.
>> This is why we've got into this mess and must be aware of providing consistent layers.
>
> "layer" and "+.trellis" are functions of the latticeExtra package that
> modify the panel function of a trellis graphic, for example, the ones
> produced by spplot or rasterVis::levelplot. Of course, they don't know
> anything about projections. They only display the graphical result of
> another function.
>
> Note that "+ layer()" is equivalent to "sp.layout" or modifying
> directly the panel function. Some code to explain it better:
>
> library(sp)
> library(lattice)
> library(latticeExtra)
>
> data(meuse)
> coordinates(meuse) <- ~x+y
>
> ## These three methods produce the same result:
>
> ## Standard lattice method: modification of the panel function
> spplot(meuse["ffreq"], panel=function(...){
>     panel.pointsplot(...)
>     sp.text(c(180500, 329900), 'Meuse')
> })
>
> ## sp.layout method
> l1 <- list("sp.text", c(180500, 329900), 'Meuse')
> spplot(meuse["ffreq"], sp.layout=list(l1))
>
> ## latticeExtra method: +.trellis and layer
> spplot(meuse["ffreq"]) + layer(sp.text(c(180500, 329900), 'Meuse'))
>
> These three methods do not use CRS information. Review the first
> example of the spplot help page and you will find that the components
> of the sp.layout do not use CRS information. It is the user's
> responsibility to make correct use of the corresponding functions.
>
> On the other hand, you can use +.trellis without layer to combine two
> trellis objects. For example, levelplot + spplot, or spplot + spplot,
> etc. Therefore, in section 2.3 of your RPubs, when you say that
> "layer() does not work with raster layers, therefore cannot use the
> rgmaprgbGM object.", you can try this:
>
> levelplot(rgmaprgbGM[[3]], margin = FALSE, par.settings = GrTheme) +
> spplot(NWEurGM, zcol = "NAME", fill = "transparent", col = "yellow",
> xlim = c(extent(rgmaprgbGM)@xmin, extent(rgmaprgbGM)@xmax), ylim =
> c(extent(rgmaprgbGM)@ymin, extent(rgmaprgbGM)@ymax),
>     colorkey = FALSE)
>
> Best,
>
> Oscar.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list