www.nr.no rgooglemaps norsk statistikermøte, halden, 11. juni 2013 andré teigland forskningssjef...
TRANSCRIPT
www.nr.no
RgoogleMapsNorsk statistikermøte, Halden, 11. juni 2013
André TeiglandForskningssjefSAMBA
www.nr.no
Elisabeth OrskaugThordis Thorarinsdottir
Norsk Regnesentral
RgoogleMaps
˃ Provide a comfortable R interface to query the Google server for static maps
> Use the map as background image to overlay plots within R.
5. RgoogleMaps
2/20
Plot world map from google maps
˃ library(RgoogleMaps)
> MyMap = GetMap(center=c(0,0), zoom =1, destfile = "World1.png ")
Section 5.1
320
5. RgoogleMaps
Change the center
You can change the center (lat, lon):
˃ MyMap = GetMap(center=c(59.95,10.7), zoom =1, destfile = "World2.png")
Section 5.2
4/20
5. RgoogleMaps
Change the zoom variable
You can zoom the map by changing "zoom":Section 5.3
5/20
MyMap = GetMap(center = c(59.95,10.7), zoom =10, destfile = "World3.png")
MyMap = GetMap(center = c(59.95,10.7), zoom =13, destfile = "World4.png")
5. RgoogleMaps
Change the size
You can change the size (pixels). (The default and maximum size is 640x640 pixels):Section 5.4
6/20
MyMap = GetMap(center=c(59.95,10.7), zoom =10, destfile = "World3Size200.png", size=c(200,200))
MyMap = GetMap(center=c(59.95,10.7), zoom =10, destfile = "World3Size500.png", size=c(500,500))
5. RgoogleMaps
Choose the right maptype
There are different maptypes to choose from:
Section 5.5
maptype = "satellite"
maptype = "roadmap" maptype = "terrain" maptype = "hybrid"
7/20
5. RgoogleMaps
Make a plot in grayscale
You can make a plot in greyscale by adding GRAYSCALE=TRUE:
˃ GetMap(center=c(59.95,10.7), zoom =10, destfile = "OsloTerrainGRAYSCALE.png", maptype = "terrain", GRAYSCALE=TRUE)
Section 5.6
8/20
5. RgoogleMaps
Make a map including several points
#The function qbbox() basically computes a bounding box for the given lat, lon points (with a few additional options -> see ?qbbox)
#We want to include the points (lat, lon) = (59.95, 10.7), (59.7, 10.3) and (59.3, 11.4).
> bb = qbbox(c(59.95, 59.7, 59.3, 62),c(10.7,10.3, 11.4,11.4))
˃ print(bb)
#$latR
#[1] 59.2865 62.0135
#$lonR
#[1] 10.2945 11.4055
> MyMap = GetMap.bbox(bb$lonR, bb$latR,destfile = "OsloBig.png", maptype = "roadmap")
Section 5.7
9/20
5. RgoogleMaps
Assignment
˃ Make a map that includes both Oslo (lat, lon) = (59.95, 10.7) and Kristiansand (lat, lon) = (58.13, 8.02).
Section 5.8
10/20
5. RgoogleMaps
Suggestion
> bb = qbbox(c(59.95, 58.13),c(10.7,8.02))
˃ MyMap = GetMap.bbox(bb$lonR, bb$latR,destfile = "OsloKristiansand.png", maptype = "roadmap")
11/20
5. RgoogleMaps
Adding points and lines
#Make plot on map:
> bb = qbbox(c(59.95, 58.13),c(10.7,8.02))
˃ MyMap = GetMap.bbox(bb$lonR, bb$latR,destfile = "OsloKristiansand.png", maptype = "roadmap")
#Define the markers:
> mymarkers = cbind.data.frame(lat = c(59.95, 58.13), lon = c(10.7,8.02))
#plot
˃ png("OsloKristiansandLine.png",640,640)
#adding points
> tmp = PlotOnStaticMap(MyMap,lat = mymarkers[,"lat"], lon = mymarkers[,"lon"], cex=2.5,pch=20,col=c("cyan", "brown1"), add=F)
#adding line
> tmp = PlotOnStaticMap(MyMap,lat = mymarkers[,"lat"], lon = mymarkers[,"lon"], col=c("blueviolet"), add=T, FUN = lines, lwd = 4)
> dev.off()
Section 5.9
12/20
5. RgoogleMaps
Example of plotting points
> png("cosomapPoints.png")
> data(cosomap)
> bb = qbbox(lon=cosomap$POINTS$lon-360,lat=cosomap$POINTS$lat)
> MyMap = GetMap.bbox(bb$lonR, bb$latR,destfile = "Coso.png", maptype= "satellite",zoom=11)
> tmp = PlotOnStaticMap(MyMap,lon=cosomap$POINTS$lon-360,lat=cosomap$POINTS$lat, pch=20,cex = .5,col= "red", verbose=0)
> dev.off()
Section 5.10
13/20
5. RgoogleMaps
Add a square in a map
Plot one square in a map over Norway.
˃ MyMap = GetMap.bbox(c(4.51,31.26), c(57.91,71.24), destfile= "Norway.png" , GRAYSCALE=T, size=c(410,410), zoom=4)
˃ pdf("MapNorwayOneSquare.pdf")
˃ indNor = 600 #choose one grid out of 777.
˃ polys = data.frame(PID=rep(1,4),SID=rep(1,4),POS=1:4, X=c(coord.lon[indNor,1],coord.lon[indNor,4],coord.lon[indNor,3],coord.lon[indNor,2]), Y = c(coord.lat[indNor,1], coord.lat[indNor,4], coord.lat[indNor,3], coord.lat[indNor,2]))
˃ color1 = rainbow(20,alpha=0.8) #transparent color; the map is shown under the square.
˃ nr.color = 17
˃ PlotPolysOnStaticMap(MyMap, polys, lwd=.5, col=color1[nr.color], add = F)
˃ graphics.off()
Section 5.11
14/20
5. RgoogleMaps
Add border of counties
Map over Norway adding border of counties.
> PlotPolysOnStaticMap(MyMap, shp[ind[1]:ind2[1],], col=0, lwd=.5, add = F,verbose=.5)
> for(i in 2:length(ind)){
> PlotPolysOnStaticMap(MyMap, shp[ind[i]:ind2[i],], col=0, lwd=.5, add = T,verbose=.5)
> }
Section 5.12
15/20
5. RgoogleMaps
Add squares and border of countiesPlot squares that covers Norway and add border of counties.
˃ PlotPolysOnStaticMap(MyMap, shp[ind[1]:ind2[1],], col=0, lwd=.5, add = F,verbose=.5)
˃ for(i in 2:length(ind)){
˃ PlotPolysOnStaticMap(MyMap, shp[ind[i]:ind2[i],], col=0, lwd=.5, add = T,verbose=.5)
˃ }
˃ for(indNor in 1:nrow(index.norway)){
˃ polys=data.frame(PID=rep(1,4),SID=rep(1,4),POS=1:4, X=c(coord.lon[indNor,1],coord.lon[indNor,4],coord.lon[indNor,3],coord.lon[indNor,2]), Y = c(coord.lat[indNor,1],coord.lat[indNor,4],coord.lat[indNor,3],coord.lat[indNor,2]))
˃ color1 = rainbow(20,alpha=0.2)
˃ nr.color = 10
˃ PlotPolysOnStaticMap(MyMap, polys, lwd=.5, col=color1[nr.color], add = T)
˃ }
Section 5.13
16/20
5. RgoogleMaps
Add squares and border of counties
maptype = hybridSection 5.14
17/20
5. RgoogleMaps
Assignment
> Add grids covering only Finnmark (the county longest North) on the map "Norway.png".
Section 5.15
18/20
5. RgoogleMaps
Suggestion
˃ pdf("MapFinnmark.pdf")
˃ …
˃ for(indNor in c(633,639:nrow(index.norway))){
˃ …
˃ }
˃ graphics.off()
19/20
5. RgoogleMaps
www.nr.no
André TeiglandForskningssjefSAMBA
www.nr.no
Short break