-
Notifications
You must be signed in to change notification settings - Fork 1
/
B3_osm_mainapi.R
380 lines (340 loc) · 10.8 KB
/
B3_osm_mainapi.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#' ---
#' title: "B3 Die OSM main api"
#' author: "Jan-Philipp Kolb"
#' date: "23 Oktober 2018"
#' output:
#' beamer_presentation:
#' colortheme: beaver
#' fonttheme: structurebold
#' highlight: espresso
#' incremental: yes
#' keep_tex: yes
#' theme: CambridgeUS
#' slidy_presentation:
#' keep_md: yes
#' ---
#'
## ----setup_mainosmapi, include=FALSE-------------------------------------
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
Ex <- T
#'
#'
#' ## [OSM Ausschnitte herunterladen](http://www.openstreetmap.org/export)
#'
#' <www.openstreetmap.org/export>
#'
#' ![osm export](figure/openstreetmap_export-1024x505.png)
#'
#'
#' ## Das R-Paket `XML` - Gaston Sanchez
#'
## ------------------------------------------------------------------------
library("XML")
#'
#' ### Gaston Sanchez - Dataflow
#'
#' ![](figure/GastonSanchez2.png)
#'
#' Seine Arbeit sieht man [**hier**](http://gastonsanchez.com/).
#'
#'
#' ## [Das Arbeiten mit XML Daten](https://github.com/gastonstat/tutorial-R-web-data/blob/master/04-parsing-xml/04-parsing-xml.pdf)
#'
#' ![Gaston Sanchez - Webdaten bekommen](figure/GastonSanchez3.PNG)
#'
#'
#' ## Funktionen im XML Paket
#'
## ----echo=F,eval=T-------------------------------------------------------
names <- c("xmlName()","name of the node","xmlSize()","number of subnodes","xmlAttrs()","named character vector of all attributes","xmlGetAttr()","value of a single attribute","xmlValue()","contents of a leaf node","xmlParent()","name of parent node","xmlAncestors()","name of ancestor nodes","getSibling()","siblings to the right or to the left","xmlNamespace()","the namespace (if there’s one)")
tab <- data.frame(t(array(names,dim=c(2,length(names)/2))))
colnames(tab) <- c("Function","Description")
kable(tab)
#'
#'
#' ## [Einzelne Objekte finden](http://www.openstreetmap.org/export)
#'
#' <www.openstreetmap.org/export>
#'
#' ![osm export](figure/admgrBer.PNG)
#'
#'
#' ## Beispiel: administrative Grenzen Berlin
#'
#' [Administrative Grenzen für Deutschland](http://wiki.openstreetmap.org/wiki/DE:Grenze#Bundesl.C3.A4ndergrenze_-_admin_level.3D4)
#'
## ----echo=T--------------------------------------------------------------
url <- "https://api.openstreetmap.org/api/0.6/relation/62422"
#'
## ----echo=F,eval=F-------------------------------------------------------
## library(xml2)
## BE <- xml2::read_xml.raw(url)
#'
#'
## ----eval=F--------------------------------------------------------------
## BE <- xmlParse(url)
#'
## ------------------------------------------------------------------------
BE <- xmlParse("../data/62422.xml")
#'
#'
#' ![Administrative Grenzen Berlin](figure/ExampleAdmBE.PNG)
#'
#' ## Das XML analysieren
#'
#' - [Tobi Bosede - Working with XML Data in R](http://www.informit.com/articles/article.aspx?p=2215520)
#'
## ----eval=Ex-------------------------------------------------------------
xmltop = xmlRoot(BE)
class(xmltop)
xmlSize(xmltop)
xmlSize(xmltop[[1]])
#'
#'
#' ## Nutzung von Xpath
#'
#' > [Xpath](https://de.wikipedia.org/wiki/XPath), the XML Path Language, is a query language for selecting nodes from an XML document.
#'
## ----eval=Ex-------------------------------------------------------------
xpathApply(BE,"//tag[@k = 'population']")
#'
#'
#' ## Quelle für die Bevölkerungsgröße
#'
## ------------------------------------------------------------------------
xpathApply(BE,"//tag[@k = 'source:population']")
#'
#' -[**Statistik Berlin Brandenburg**](https://www.statistik-berlin-brandenburg.de/datenbank/inhalt-datenbank.asp)
#'
#' ## Etwas überraschend:
#'
## ------------------------------------------------------------------------
xpathApply(BE,"//tag[@k = 'name:ta']")
#'
#' ![](figure/OSMBerta.png)
#'
#' ## Geographische Region
#'
## ------------------------------------------------------------------------
region <- xpathApply(BE,
"//tag[@k = 'geographical_region']")
# regular expressions
region[[1]]
#'
#' ```
#' <tag k="geographical_region"
#' v="Barnim;Berliner Urstromtal;
#' Teltow;Nauener Platte"/>
#' ```
#'
#' ## Landkreis
#'
#' ![Barnim](figure/Barnim.png)
#'
#'
#' ## Weiteres Beispiel
#'
## ----eval=F--------------------------------------------------------------
## url2<-"http://api.openstreetmap.org/api/0.6/node/25113879"
## obj2<-xmlParse(url2)
## obj_amenity<-xpathApply(obj2,"//tag[@k = 'amenity']")[[1]]
## obj_amenity
#'
## ----echo=F--------------------------------------------------------------
obj2<-xmlParse("../data/25113879.xml")
obj_amenity<-xpathApply(obj2,"//tag[@k = 'amenity']")[[1]]
obj_amenity
#'
#'
#'
#' ## Wikipedia Artikel
#'
## ------------------------------------------------------------------------
xpathApply(obj2,"//tag[@k = 'wikipedia']")[[1]]
#'
## ----eval=F--------------------------------------------------------------
## xpathApply(obj2,"//tag[@k = 'wheelchair']")[[1]]
#'
## ----eval=F--------------------------------------------------------------
## xpathApply(obj2,"//tag[@k = 'name']")[[1]]
#'
#'
#' ## Das C und das A
#'
## ----eval=F--------------------------------------------------------------
## url3<-"http://api.openstreetmap.org/api/0.6/node/303550876"
## obj3 <- xmlParse(url3)
## xpathApply(obj3,"//tag[@k = 'opening_hours']")[[1]]
#'
## ----echo=F--------------------------------------------------------------
obj3 <- xmlParse("../data/303550876.xml")
xpathApply(obj3,"//tag[@k = 'opening_hours']")[[1]]
#'
#'
#' ## Hin und weg
#'
## ----eval=F--------------------------------------------------------------
## url4<-"http://api.openstreetmap.org/api/0.6/node/25439439"
## obj4 <- xmlParse(url4)
## xpathApply(obj4,"//tag[@k = 'railway:station_category']")[[1]]
#'
## ----echo=F--------------------------------------------------------------
obj4 <- xmlParse("../data/25439439.xml")
xpathApply(obj4,"//tag[@k = 'railway:station_category']")[[1]]
#'
#'
#'
#' - [**Wikipedia Artikel Bahnhofskategorien**](https://de.wikipedia.org/wiki/Bahnhofskategorie)
#'
#' ![](figure/Bahnhofskategorien.PNG)
#'
#' ## Exkurs: Bahnhofskategorien
#'
#' - [**rvest: Easily Harvest (Scrape) Web Pages**](https://cran.r-project.org/web/packages/rvest/index.html)
#'
## ------------------------------------------------------------------------
library(rvest)
bhfkat<-read_html(
"https://de.wikipedia.org/wiki/Bahnhofskategorie")
df_html_bhfkat<-html_table(
html_nodes(bhfkat, "table")[[2]],fill = TRUE)
#'
#' ## Bahnhofskategorien Übersicht
#'
## ----echo=F--------------------------------------------------------------
library(knitr)
kable(df_html_bhfkat)
#'
#' ## Nur fliegen ist schöner
#'
## ----eval=F--------------------------------------------------------------
## url5<-"http://api.openstreetmap.org/api/0.6/way/162149882"
## obj5<-xmlParse(url5)
## xpathApply(obj5,"//tag[@k = 'name']")[[1]]
#'
## ----echo=F--------------------------------------------------------------
obj5<-xmlParse("../data/162149882.xml")
xpathApply(obj5,"//tag[@k = 'name']")[[1]]
#'
#'
## ------------------------------------------------------------------------
xpathApply(obj5,"//tag[@k = 'website']")[[1]]
#'
#'
## ------------------------------------------------------------------------
xpathApply(obj5,"//tag[@k = 'iata']")[[1]]
#'
#' ## Das Paket `osmar` benutzen
#'
## ------------------------------------------------------------------------
library("osmar")
node_ <- xmlParse("../data/162149882.xml")
node_osmar <- as_osmar(node_)
node_osmar
#'
#' ## [Drei Typen von Vektorobjekten](https://www.earthdatascience.org/courses/earth-analytics/spatial-data-r/intro-vector-data-r/)
#'
#' <!--
#' https://www.earthdatascience.org/courses/earth-analytics/spatial-data-r/intro-vector-data-r/
#' -->
#'
#' ![](figure/points-lines-polygons-vector-data-types.png)
#'
#' ## Die Ausdehnung
#'
#' ![](figure/spatial-extent.png)
#'
#'
#' ## [Import mit dem Paket `sf`](https://cran.r-project.org/web/packages/sf/vignettes/sf2.html)
#'
#'
## ----eval=T--------------------------------------------------------------
library(sf)
#'
#' - Mit dem Befehl `st_layers` kann man sehen, welche Layer verfügbar sind:
#'
## ------------------------------------------------------------------------
st_layers("../data/Amsterdam_highway_primary.osm")
#'
#' ## Import von Layer `lines`
#'
## ------------------------------------------------------------------------
dat <- st_read("../data/Amsterdam_highway_primary.osm",
"lines")
#'
#' ## Den Layer plotten
#'
## ------------------------------------------------------------------------
plot(dat$geometry)
#'
#'
#' ## Import von Layer `points`
#'
## ------------------------------------------------------------------------
datp <- st_read("../data/Amsterdam_highway_primary.osm",
"points")
#'
#' ## Den Layer `points` plotten
#'
## ------------------------------------------------------------------------
plot(dat$geometry,pch=20,col=rgb(0,0,1,.1))
#'
#'
#' ## Mit einem anderen Paket plotten
#'
## ------------------------------------------------------------------------
library(tmap)
qtm(dat$geometry)
#'
#' ##
#'
## ------------------------------------------------------------------------
st_layers("../data/ams_centraal.osm")
#'
## ------------------------------------------------------------------------
datm <- st_read("../data/ams_centraal.osm","multipolygons")
sp::plot(datm$geometry)
#'
#'
#' ## Mehr Beispiele, wie man mit XML Daten umgeht:
#'
#' - Deborah Nolan - [**Extracting data from XML**](http://www.stat.berkeley.edu/~statcur/Workshop2/Presentations/XML.pdf)
#' - Duncan Temple Lang - [**A Short Introduction to the XML package for R**](http://www.omegahat.net/RSXML/shortIntro.pdf)
#'
#'
#' ### Noch mehr Informationen
#'
#' - [**Web Daten manipulieren**](http://www.di.fc.ul.pt/~jpn/r/web/index.html#parsing-xml)
#'
#' - [**Tutorial zu xquery**](http://www.w3schools.com/xml/xquery_intro.asp)
#'
#' - [**R und das Web (für Anfänger), Teil II: XML und R**](http://giventhedata.blogspot.de/2012/06/r-and-web-for-beginners-part-ii-xml-in.html)
#'
#' - Gaston Sanchez - [**String Manipulation**](http://gastonsanchez.com/Handling_and_Processing_Strings_in_R.pdf)
#'
#' - [**Nutzung, Vor- und Nachteile OSM**](https://www.e-education.psu.edu/geog585/node/738)
#'
#' - [**Forschungsprojekte im Zusammenhang mit OpenStreetMap**](http://wiki.openstreetmap.org/wiki/Research)
#'
#' ## Referenzen
#'
## ------------------------------------------------------------------------
citation("XML")
#'
#' ## Das neuere Paket
#'
## ------------------------------------------------------------------------
citation("xml2")
#'
#'
#' <!--
#' https://cran.r-project.org/web/packages/XML2R/XML2R.pdf
#' -->
#'
#'
## ----eval=FALSE, include=FALSE-------------------------------------------
## library(sf)
## dat <- st_read("../data/162149882.xml")
## dat <- st_read("../data/Amsterdam_highway_primary.osm")
#'