-
Notifications
You must be signed in to change notification settings - Fork 92
/
osmar.R
149 lines (127 loc) · 5.66 KB
/
osmar.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
##################################################################
## Source code for the book: "Displaying time series, spatial and
## space-time data with R"
##
## Copyright (C) 2013-2012 Oscar Perpiñán Lamigueiro
##
## This program is free software you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; either version 2 of the License,
## or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
## 02111-1307, USA.
####################################################################
##################################################################
## Initial configuration
##################################################################
## Clone or download the repository and set the working directory
## with setwd to the folder where the repository is located.
##################################################################
## OpenStreetMap with Hill Shade layers
##################################################################
##################################################################
## Retrieving data from OpenStreetMap
##################################################################
library('osmar')
api <- osmsource_api()
ymax <- 43.7031
ymin <- 43.6181
xmax <- -8.0224
xmin <- -8.0808
box <- corner_bbox(xmin, ymin, xmax, ymax)
cedeira <- get_osm(box, source=api, full=TRUE)
summary(cedeira$nodes)
idxHighways <- find(cedeira, way(tags(k=='highway')))
highways <- subset(cedeira, way_ids=idxHighways)
idxStreets <- find(highways, way(tags(v=='residential')))
idxPrimary <- find(highways, way(tags(v=='primary')))
idxSecondary <- find(highways, way(tags(v=='secondary')))
idxTertiary <- find(highways, way(tags(v=='tertiary')))
idxOther <- find(highways,
way(tags(v=='unclassified' |
v=='footway' |
v=='steps')))
spFromOSM <- function(source, index, type='lines'){
idx <- find_down(source, index)
obj <- subset(source, ids=idx)
objSP <- as_sp(obj, type)
}
streets <- spFromOSM(cedeira, way(idxStreets))
primary <- spFromOSM(cedeira, way(idxPrimary))
secondary <- spFromOSM(cedeira, way(idxSecondary))
tertiary <- spFromOSM(cedeira, way(idxTertiary))
other <- spFromOSM(cedeira, way(idxOther))
idxPlaces <- find(cedeira, node(tags(k=='name')))
places <- spFromOSM(cedeira, node(idxPlaces), 'points')
nms <- subset(cedeira$nodes$tags, subset=(k=='name'), select=c('id', 'v'))
ord <- match(idxPlaces, nms$id)
nms <- nms[ord,]
places$name <- nms$v[ord]
## Cedeira town will be printed differently
idxCedeira <- which(nms$v=='Cedeira') ##Main town
cedeiraCoords <- coordinates(places[idxCedeira,])
places <- places[-idxCedeira,]
##################################################################
## Hill Shading
##################################################################
library(raster)
cedeiraSP <- as_sp(cedeira, 'points')
projCedeira <- projection(cedeiraSP)
demCedeira <- raster('data/demCedeira')
projection(demCedeira) <- projCedeira
demCedeira[demCedeira <= 0] <- NA
slope <- terrain(demCedeira, 'slope')
aspect <- terrain(demCedeira, 'aspect')
hsCedeira <- hillShade(slope=slope, aspect=aspect,
angle=20, direction=30)
##################################################################
## Overlaying layers of information
##################################################################
pdf(file="figs/cedeiraOsmar.pdf")
library(maptools)
library(latticeExtra)
library(colorspace)
library(rasterVis)
##Auxiliary function to display the roads. A thicker black line in
##the background and a thinner one with an appropiate color.
sp.road <- function(line, lwd=5, blwd=7,
col='indianred1', bcol='black'){
sp.lines(line, lwd=blwd, col=bcol)
sp.lines(line, lwd=lwd, col=col)
}
## The background color of the panel is set to blue to represent the sea
hsTheme <- modifyList(GrTheme(), list(panel.background=list(col='skyblue3')))
## DEM with terrain colors and semitransparency
terrainTheme <- modifyList(rasterTheme(region=terrain_hcl(n=15)),
list(regions=list(alpha=0.6)))
## Hill shade and DEM overlaid
levelplot(hsCedeira, maxpixels=ncell(hsCedeira),
par.settings=hsTheme, margin=FALSE, colorkey=FALSE) +
levelplot(demCedeira, maxpixels=ncell(demCedeira),
par.settings=terrainTheme) +
## Roads and places
layer({
## Street and roads
sp.road(streets, lwd=1, blwd=2, col='white')
sp.road(other, lwd=2, blwd=3, col='white')
sp.road(tertiary, lwd=3, blwd=4, col='palegreen')
sp.road(secondary, lwd=4, blwd=6, col='midnightblue')
sp.road(primary, col='indianred1')
## Places except Cedeira town
sp.points(places, pch=19, col='black', cex=0.4, alpha=0.8)
sp.pointLabel(places, labels=places$name,
fontfamily = 'Palatino',
cex=0.6, col='black')
## Cedeira town
panel.points(cedeiraCoords, pch=18, col='black', cex=1)
panel.text(cedeiraCoords, labels='Cedeira', pos=2, offset=1)
})
dev.off()