[R-sig-Geo] Plotting shapefiles on top of Google map tiles

Robert J. Hijmans r.hijmans at gmail.com
Mon Jan 18 08:50:42 CET 2010


Dear Milton,

You need a Google maps API key for this. If you have a Google account,
you can request one for http://localhost/
at http://code.google.com/apis/maps/signup.html

I have made this more explicit by adding a mykey argument to the function:

gmap <- function(xy, maptype='terrain', mykey, ...) {
    require(RgoogleMaps)
	fun = points
	if (inherits(xy, 'Spatial')) {
		b = bbox(xy)
		if (inherits(xy, 'SpatialPoints')) {
			xy = coordinates(xy)
		} else {
			x = unlist(lapply(xy at polygons, function(i)slot(i, 'Polygons')))
			x = lapply(x, function(x)slot(x, 'coords'))
			xy = matrix(ncol=2, nr=0)
			for (i in 1:length(x)) xy = rbind(xy, x[[i]], c(NA,NA))
			fun = lines
		}
	} else {
		b = rbind(range(xy[,1], na.rm=TRUE), range(xy[,2], na.rm=TRUE))
	}
    gm <- GetMap.bbox(key=mykey, lonR=b[1,], latR=b[2,], maptype=maptype)
    tmp <- PlotOnStaticMap(gm, lon=xy[,1], lat=xy[,2], FUN=fun, verbose=0, ... )
}


Robert

On Sun, Jan 17, 2010 at 11:37 PM, milton ruser <milton.ruser at gmail.com> wrote:
> Dear prof. Robert Hijmans,
>
> I tryed your suggestion (under Vista), but got the following errors:
>
>> gmap(a)
> Loading required package: RgoogleMaps
> Error in paste(googleurl, "center=", center, "&zoom=", zoom, "&size=",  :
>   object 'localhostkey' not found
>> sessionInfo()
> R version 2.9.0 alpha (2009-03-26 r48224)
> i386-pc-mingw32
> locale:
> LC_COLLATE=English_Canada.1252;LC_CTYPE=English_Canada.1252;LC_MONETARY=English_Canada.1252;LC_NUMERIC=C;LC_TIME=English_Canada.1252
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> other attached packages:
> [1] maptools_0.7-20   foreign_0.8-34    sp_0.9-32         RgoogleMaps_1.1.6
> fortunes_1.3-6
> loaded via a namespace (and not attached):
> [1] grid_2.9.0      lattice_0.17-20 tools_2.9.0
>
> Any idea?
>
> bests
>
> milton
>
>
> On Mon, Jan 18, 2010 at 2:06 AM, Robert J. Hijmans <r.hijmans at gmail.com>
> wrote:
>>
>> Patrick,
>>
>> I have had some success with plotting sp objects on top of G maps with
>> this function
>>
>>
>> gmap <- function(xy, maptype='terrain', ...) {
>>    require(RgoogleMaps)
>>        fun = points
>>        if (inherits(xy, 'Spatial')) {
>>                b = bbox(xy)
>>                if (inherits(xy, 'SpatialPoints')) {
>>                        xy = coordinates(xy)
>>                } else {
>>                        x = unlist(lapply(xy at polygons, function(i)slot(i,
>> 'Polygons')))
>>                        x = lapply(x, function(x)slot(x, 'coords'))
>>                        xy = matrix(ncol=2, nr=0)
>>                        for (i in 1:length(x)) xy = rbind(xy, x[[i]],
>> c(NA,NA))
>>                        fun = lines
>>                }
>>        } else {
>>                b = rbind(range(xy[,1], na.rm=TRUE), range(xy[,2],
>> na.rm=TRUE))
>>        }
>>        mykey <-  ### fill in your google maps API key for
>> http://localhost/
>>    gm <- GetMap.bbox(key=localhostkey, lonR=b[1,], latR=b[2,],
>> maptype=maptype)
>>    tmp <- PlotOnStaticMap(gm, lon=xy[,1], lat=xy[,2], FUN=fun, verbose=0,
>> ... )
>> }
>>
>>
>> a = matrix(runif(100)*100-50, ncol=2)
>> gmap(a)
>>
>> library(sp)
>> gmap(SpatialPoints(a), col='red', cex=2, pch='+')
>>
>> library(maptools)
>> data(wrld_simpl)
>> hti = wrld_simpl[wrld_simpl$ISO3=='HTI',]
>> gmap(hti, lwd=2, col='red')
>>
>>
>> Hth,
>> Robert
>>
>>
>> On Sat, Jan 16, 2010 at 5:35 PM, Patrick Crutcher <pcrutcher at gmail.com>
>> wrote:
>> > I have some shapefiles I want to plot over Google Maps tiles. I have
>> > polygons, lines and point data. What's the most efficient way to do
>> > this?
>> > One path might be to use the pkg RgoogleMaps, however, it is still
>> > unclear
>> > to me how to do this. I assume using PlotonStaticMap with some
>> > combination
>> > of reformatting the shapefile data. Thanks.
>> >
>> > P.
>> >
>> >        [[alternative HTML version deleted]]
>> >
>> > _______________________________________________
>> > R-sig-Geo mailing list
>> > R-sig-Geo at stat.math.ethz.ch
>> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> >
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>



More information about the R-sig-Geo mailing list