RgoogleMaps - Norsk Regnesentral

Download Report

Transcript RgoogleMaps - Norsk Regnesentral

www.nr.no
www.nr.no
RgoogleMaps
Norsk statistikermøte, Halden, 11. juni 2013
Elisabeth Orskaug
Thordis
Thorarinsdottir
André Teigland
Forskningssjef
SAMBA
Norsk Regnesentral
5. RgoogleMaps
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.
2/20
5. RgoogleMaps
Plot world map from google maps
Section 5.1
˃
library(RgoogleMaps)
>
MyMap = GetMap(center=c(0,0), zoom
=1, destfile = "World1.png ")
320
5. RgoogleMaps
Change the center
Section 5.2
You can change the center (lat, lon):
˃
MyMap = GetMap(center=c(59.95,10.7),
zoom =1, destfile = "World2.png")
4/20
5. RgoogleMaps
Change the zoom variable
Section 5.3
You can zoom the map by changing "zoom":
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/20
5. RgoogleMaps
Change the size
Section 5.4
You can change the size (pixels). (The default
and maximum size is 640x640 pixels):
MyMap = GetMap(center=c(59.95,10.7),
zoom =10, destfile = "World3Size500.png",
size=c(500,500))
MyMap = GetMap(center=c(59.95,10.7),
zoom =10, destfile =
"World3Size200.png", size=c(200,200))
6/20
5. RgoogleMaps
Choose the right maptype
Section 5.5
maptype = "satellite"
There are different maptypes to choose
from:
maptype = "roadmap"
maptype = "terrain"
maptype = "hybrid"
7/20
5. RgoogleMaps
Make a plot in grayscale
Section 5.6
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)
8/20
5. RgoogleMaps
Make a map including several points
Section 5.7
#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")
9/20
5. RgoogleMaps
Assignment
Section 5.8
˃
Make a map that includes both
Oslo (lat, lon) = (59.95, 10.7) and
Kristiansand (lat, lon) = (58.13,
8.02).
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
Section 5.9
#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()
12/20
5. RgoogleMaps
Example of plotting points
Section 5.10
>
png("cosomapPoints.png")
>
data(cosomap)
>
bb = qbbox(lon=cosomap$POINTS$lon360,lat=cosomap$POINTS$lat)
>
MyMap = GetMap.bbox(bb$lonR, bb$latR,destfile =
"Coso.png", maptype= "satellite",zoom=11)
>
tmp = PlotOnStaticMap(MyMap,lon=cosomap$POINTS$lon360,lat=cosomap$POINTS$lat, pch=20,cex = .5,col= "red",
verbose=0)
>
dev.off()
13/20
5. RgoogleMaps
Add a square in a map
Section 5.11
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[indN
or,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()
14/20
5. RgoogleMaps
Add border of counties
Section 5.12
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)
>
}
15/20
5. RgoogleMaps
Add squares and border of counties
Section 5.13
Plot 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[i
ndNor,2]))
˃
color1 = rainbow(20,alpha=0.2)
˃
nr.color = 10
˃
PlotPolysOnStaticMap(MyMap, polys, lwd=.5, col=color1[nr.color],
add = T)
˃
}
16/20
5. RgoogleMaps
Add squares and border of counties
Section 5.14
maptype = hybrid
17/20
5. RgoogleMaps
Assignment
Section 5.15
>
Add grids covering only Finnmark
(the county longest North) on the
map "Norway.png".
18/20
5. RgoogleMaps
Suggestion
˃
pdf("MapFinnmark.pdf")
˃
…
˃
for(indNor in c(633,639:nrow(index.norway))){
…
˃
˃
}
˃
graphics.off()
19/20
www.nr.no
www.nr.no
Short break
André Teigland
Forskningssjef
SAMBA