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

Agustin Lobo alobolistas at gmail.com
Thu Apr 24 09:43:38 CEST 2014


Barry,

On Wed, Apr 23, 2014 at 4:37 PM, Barry Rowlingson
<b.rowlingson at lancaster.ac.uk> wrote:
> On Wed, Apr 23, 2014 at 2:42 PM, Agustin Lobo <alobolistas at gmail.com> wrote:
>
>> I think that the output of get_map commands, i.e.
>>
>> gmap <- get_map(location = rprobextLL at bbox, maptype = "hybrid", source
>> = "google",
>>     crop = FALSE, zoom = 5)
>>
>> is a raster (not in the sense of an object of the raster package) in
>> gepgraphic coordinates
>> (epsg:4326), but actually do not see it specified anywhere. Does
>> anyone know for sure?
>> The alternative would be google PseudoMercator (eps:3857)
>>
>> The bounding boxes generally just get passed through to the web service
> that is providing the tiles - eg for google maps its getting:
>
> http://maps.googleapis.com/maps/api/staticmap?center=29.763284,-95.363271&zoom=10&size=640x640&scale=
> 2&&maptype=terrain&&&&&&&sensor=false
>
>  - I imagine the web services expect EPSG:4326.

What they expect, yes. But what about what we retrieve? The bb
coordinates might be in geographic coordinates (epsg:4326) but
the actual projection of the matrix could be any other. Actually, I
think that no geographic object without CRS information should be
admitted in R.

In order to investigate this further, I've done the following:


gmap <- get_map(location = rprobextLL at bbox, maptype = "hybrid",
source= "google", crop = FALSE, zoom = 5)

(use
> rprobextLL at bbox
        min       max
x -69.88634 -48.78450
y -34.05533 -18.63424

i.e.
mibb <- matrix(c(69.88634, -48.78450, -34.05533, -18.63424),nrow=2)

mgmap <- as.matrix(gmap)
vgmap <- as.vector(mgmap)
vgmaprgb <- col2rgb(vgmap)
gmapr <- matrix(vgmaprgb[1,],ncol=ncol(mgmap),nrow=nrow(mgmap))
gmapg <- matrix(vgmaprgb[2,],ncol=ncol(mgmap),nrow=nrow(mgmap))
gmapb <- matrix(vgmaprgb[3,],ncol=ncol(mgmap),nrow=nrow(mgmap))
rgmaprgb <- brick(raster(gmapr),raster(gmapg),raster(gmapb))
projection(rgmaprgb) <- CRS("+init=epsg:4326")
extent(rgmaprgb) <- unlist(attr(gmap,which="bb"))[c(2,4,1,3)]
rgmaprgb
plotRGB(rgmaprgb)
writeRaster(rgmaprgb,file="rgmaprgb",format="GTiff",overwrite=TRUE,datatype="INT1U")



>
>> Also, I'm kind of confused with the get_map help page:
>> "Value
>> a data.frame with columns latitude, longitude, and fill"
>>
>> while in R we get:
>> > str(gmap)
>>  chr [1:1280, 1:1280] "#24392B" "#2E4832" "#3E583B" "#3E583B"
>> "#55675F" "#5B6C52" "#656B58" ...
>>  - attr(*, "class")= chr [1:2] "ggmap" "raster"
>>  - attr(*, "bb")='data.frame':    1 obs. of  4 variables:
>>   ..$ ll.lat: num -38.2
>>   ..$ ll.lon: num -73.4
>>   ..$ ur.lat: num -13.2
>>   ..$ ur.lon: num -45.3
>>
>>
>  I think its lying - why not report a documentation bug?
>
>  The returned objects are just matrices of colour values with bb attributes
> and class "ggmap" "raster". You can make one yourself easily enough:
>
>  > green = matrix("#00FF00",256,256)
>  > attr(green,"bb")=attr(gmap,"bb")
>  > class(green)=c("ggmap","raster")
>
>  - taking the bbox from a previous get_map call. Looking at the code the
> matrix just comes from readPNG via the png file it dumps in your current
> directory (by default). So... if you can work out the WMS call to get a PNG
> then you can easily make a ggmap object.
>
> Barry
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> 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