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

Agustin Lobo alobolistas at gmail.com
Tue Apr 29 09:42:35 CEST 2014


This is great information, thanks.
It is clear that without an infrastructure such as the proj4 used by
spTransform() the overlays will never be
without error.

Agus

On Mon, Apr 28, 2014 at 4:57 PM, Edzer Pebesma
<edzer.pebesma at uni-muenster.de> wrote:
> Nice, thank you!
>
> Markus Loecher, author of RgoogleMaps, explained that ggmap uses
>
> https://github.com/dkahle/ggmap/blob/master/R/LonLat2XY.R
>
> to go from long/lat to XY (pixels in google map tiles), which was taken
> from the RgoogleMaps one:
>
> https://github.com/markusloecher/RgoogleMaps/blob/master/R/LatLon2XY.R
>
> which points to code by someone called Neil Young in a google groups:
>
> https://groups.google.com/forum/?hl=en#!topic/Google-Maps-API/0hA6wp6VaW8
>
> I would expect that your cylindrical, equidistant projection (linear in
> long/lat) must collide more with the Mercator used by google when the
> plotted area becomes larger.
>
> On 04/28/2014 11:58 AM, Oscar Perpiñan wrote:
>> Hello,
>>
>> I couldn't run your code because I am not able to read the file.
>> Anyway, some comments about your last email:
>>
>> - "layer" is a function of the latticeExtra package. This function
>> modifies the panel function of a "trellis" graphic produced with a
>> function of the lattice package. This function, together with
>> "+.trellis" (of the same package) allows for overlaying layers. It is
>> important to note that it does not carry out any projection. It only
>> displays the graphical result of another function (for example,
>> sp.polygons or whatever).
>>
>> - You create the canvas with "levelplot" displaying the ggmap raster,
>> and then overlay the data with layer. This is the same approach that
>> ggmap uses. Instead, I prefer to rely on the main data to be displayed
>> to configure the graphical window instead of using an auxiliary image
>> as the starting point.
>>
>> - I'm not a fan of the ggmap approach. I agree with you that the
>> resulting object should include the projection information and even
>> adhere to the sp classes. However, I think that their results are
>> useful if managed with care.
>>
>> In order to show that this is not an issue strictly related to
>> rasterVis::levelplot, I have written some code with spplot. I think
>> that the concordance between the ggmap raster and the shapefile is
>> aceptable.
>>
>> Best,
>>
>> Oscar.
>>
>> ##########################################################
>> library(sp)
>> library(ggmap)
>> ## latticeExtra must be loaded after ggmap because both ggplot2 and
>> ## latticeExtra define a 'layer' function. We need the definition from
>> ## latticeExtra.
>> library(latticeExtra)
>>
>> ## Shape example
>> library(maptools)
>> owd <- getwd()
>> setwd(system.file("shapes", package = "maptools"))
>> sc90 <- readShapeSpatial("co45_d90")
>> proj4string(sc90) <- CRS("+proj=longlat +datum=NAD27")
>> setwd(owd)
>>
>> ## Get the stamen map
>> bbPoly <- bbox(sc90)
>> gmap <- get_map(c(bbPoly), maptype='toner', source='stamen', crop=FALSE)
>> bbMap <- attr(gmap, 'bb')
>> latCenter <- with(bbMap, ll.lat + ur.lat)/2
>> lonCenter <- with(bbMap, ll.lon + ur.lon)/2
>> height <- with(bbMap, ur.lat - ll.lat)
>> width <- with(bbMap, ur.lon - ll.lon)
>>
>> ## Use latticeExtra::+.trellis and latticeExtra::layer
>> spplot(sc90['AREA'], fill='transparent', col='indianred1') +
>>                   layer(grid.raster(gmap,
>>                                     x=lonCenter, y=latCenter,
>>                                     width=width, height=height,
>>                                     default.units='native'),
>>                         under=TRUE)
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
> --
> Edzer Pebesma
> Institute for Geoinformatics (ifgi), University of Münster
> Heisenbergstraße 2, 48149 Münster, Germany. Phone: +49 251
> 83 33081 http://ifgi.uni-muenster.de GPG key ID 0xAC227795
>
>
> _______________________________________________
> 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