Monday, April 23, 2012

Beautiful maps with R and Google Maps

It’s been awhile since I have posted on this blog. I have been caught up in the midst of publishing my thesis works as a book which is now available for at amazon and trying to be adventurous during the harsh but still exciting Norwegian winter.
A friend of mine asked me last week to help him plot locations of boreholes, that he is doing research on, to a decent map. I came up with a suggestion of creating it in R using Google Maps as a base map. Of course this could have been done easily and more aesthetically attractive using the conventional GIS softwares. It is more exciting and reproducible where the R script can be reused easily.


The location of the boreholes was originally recorded using gps in UTM coordinate format. Therefore, I used the spTransform method of rgdal package to convert them into latitudes and longitudes.  The rest of the codes are listed below.

library(rgdal)
library(RgoogleMaps)
boreholes<- read.csv("boreholes.csv", sep=",", header=TRUE)
bh_grd <- SpatialPoints(boreholes[,c("East","North")],
proj4string=CRS("+proj=utm +zone=32 +datum=WGS84"))
bholes<- SpatialPointsDataFrame(bh_grd,boreholes[,-c(2,3)])
bholes<-bholes[,-c(2,3)]
bh_geo<- spTransform(bholes, CRS("+proj=longlat +datum=WGS84"))
North<-coordinates(bh_geo)[,2]
East<-coordinates(bh_geo)[,1]
bb <- qbbox(lat=range(North), lon=range(East))
m <- c(mean(North), mean(North))
zoom <- min(MaxZoom(latrange=bb$latR,lonrange=bb$lonR))
Map <- GetMap.bbox(bb$lonR, bb$latR, zoom=zoom, maptype="mobile",
NEWMAP=F, destfile="map.png", RETURNIMAGE=T);
tmp <- PlotOnStaticMap(Map,lat=North, lon=East,
cex=1.5,pch=20,col="red", add=F, verbose=0);
tmp <- TextOnStaticMap(Map,lat=North, lon=East+.0007, cex=.8, font=4,
col="black", labels= boreholes$Number, add=T, verbose=0);
tmp <- PlotArrowsOnStaticMap(Map, lat0=min(North),lon0=max(East)+.006,
lat1=min(North)+.0006,lon1=max(East)+.006,lwd=6,col="black",add=T);
#Satellite image as a base map
SMap <- GetMap.bbox(bb$lonR, bb$latR, zoom=zoom, maptype="satellite",
NEWMAP=F, destfile="map.png", RETURNIMAGE=TRUE);
tmp <- PlotOnStaticMap(SMap,lat=North, lon=East,
cex=1.5,pch=20,col="red", add=F, verbose=0);
tmp <- TextOnStaticMap(SMap,lat=North, lon=East+.0007, cex=.8, font=4,
col="yellow", labels= boreholes$Number, add=T, verbose=0);
tmp <- PlotArrowsOnStaticMap(SMap, lat0=min(North),lon0=max(East)+.006,
lat1=min(North)+.0006,lon1=max(East)+.006,lwd=6,col="yellow",add=T);
view raw boreholes.R hosted with ❤ by GitHub

No comments:

Post a Comment