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

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


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
>



More information about the R-sig-Geo mailing list