forked from Robinlovelace/Creating-maps-in-R
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosmplotr.R
More file actions
121 lines (113 loc) · 4.38 KB
/
Copy pathosmplotr.R
File metadata and controls
121 lines (113 loc) · 4.38 KB
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
# Aim: test osmplotr's capabilities
# devtools::install_github("mpadge/osmplotr")
library(osmplotr)
# withr::with_libpaths("D:/R/Rlibs",
# devtools::install_github("hrbrmstr/overpass"))
library(overpass)
library(tmap)
b = bb("Hackney, London", ext = 2)
b = as.vector(b)
class(b)
h = extract_osm_objects(key = "highway", bbox = b)
summary(h)
# download the cycleways/cyclable routes
hc = extract_osm_objects(key = "highway", value = "cycleway", bbox = b)
hb = extract_osm_objects(key = "highway", value = "yes", bbox = b)
# hlcn = extract_osm_objects(bbox = b, extra_pairs = c("network", "ncn")) # failed
?opq
overpass::opq()
q = '[out:xml];
(node["network"="lcn"](51.523240,-0.069362,51.563240,-0.029362);
way["network"="lcn"](51.523240,-0.069362,51.563240,-0.029362);
relation["network"="lcn"](51.523240,-0.069362,51.563240,-0.029362););
(._;>;);
out;'
lnd = overpass_query(q)
hlcn9 = extract_osm_objects(key = "highway", bbox = b, extra_pairs = c("lcn_ref", "9"))
hlcna = extract_osm_objects(key = "highway", bbox = b, extra_pairs = c("Cycle Route", "London Cycle Network Route 9"))
plot(h)
plot(hb, add = T, col = "red", lwd = 1)
plot(hc, add = T, col = "red", lwd = 2)
plot(hlcn, add = T, col = "green", lwd = 3)
plot(lnd, add = T, col = "green", lwd = 3)
library(geojsonio)
geojson_write(hc, file = "data/hackney-cycleway.geojson")
geojson_write(lnd, file = "data/lcn-cycleway.geojson")
# # Example from GitHub
# bbox = c(-0.15,51.5,-0.1,51.52)
# structures = c ('highway', 'highway', 'building', 'building', 'building',
# 'amenity', 'grass', 'park', 'natural', 'tree')
# structs = osm_structures (structures=structures, col_scheme='dark')
# structs$value [1] = '!primary'
# structs$value [2] = 'primary'
# structs$suffix [2] = 'HP'
# structs$value [3] = '!residential'
# structs$value [4] = 'residential'
# structs$value [5] = 'commercial'
# structs$suffix [3] = 'BNR'
# structs$suffix [4] = 'BR'
# structs$suffix [5] = 'BC'
#
# # Then download the corresponding data, noting that extract_osm_objects returns two components: warn containing any warnings generated during download, and obj containing the appropriate spatial object.
#
# london = list ()
# for (i in 1:(nrow (structs) - 1))
# {
# dat = extract_osm_objects (key=structs$key [i], value=structs$value [i],
# bbox=bbox)
# if (!is.null (dat$warn))
# warning (dat$warn)
# fname = paste0 ('dat_', structs$suffix [i])
# assign (fname, dat)
# london [[i]] = get (fname)
# names (london)[i] = fname
# rm (list=c(fname))
# }
#
# # And finally the additional data for specific buildings and highways (ignoring potential warnings this time).
#
# extra_pairs = c ('name', 'Royal.Festival.Hall')
# london$dat_RFH = extract_osm_objects (key='building', extra_pairs=extra_pairs,
# bbox=bbox)
# extra_pairs = list (c ('addr:street', 'Stamford.St'),
# c ('addr:housenumber', '150'))
# london$dat_ST = extract_osm_objects (key='building', extra_pairs=extra_pairs,
# bbox=bbox)
# highways = c ('Kingsway', 'Holborn', 'Farringdon.St', 'Strand',
# 'Fleet.St', 'Aldwych')
# london$highways1 = highways2polygon (highways=highways, bbox=bbox)
# highways = c ('Queen.s.Walk', 'Blackfriars', 'Waterloo', 'The.Cut')
# london$highways2 = highways2polygon (highways=highways, bbox=bbox)
# highways = c ('Regent.St', 'Oxford.St', 'Shaftesbury')
# london$highways3 = highways2polygon (highways=highways, bbox=bbox)
#
# plotting
# library(maptools)
# indx = which (!london$dat_BR$id %in% london$dat_BNR$id)
# dat_B = spRbind (london$dat_BR [indx,], london$dat_BNR)
# indx = which (!london$dat_H$id %in% london$dat_HP$id)
# dat_H = spRbind (london$dat_H [indx,], london$dat_HP)
# dat_T = london$dat_T
#
# # Specify the bounding box for the desired region
#
# bbox = c(-0.15,51.5,-0.1,51.52)
#
# # Download the desired data---in this case, all building perimeters.
#
# dat_B = extract_osm_objects (key="building", bbox=bbox)
#
# # Initiate an osm_basemap with desired background (bg) colour
#
# plot_osm_basemap (xylims=get_xylims (bbox), bg="gray20", file="map1.png")
#
# graphics.off ()
#
# Add desired plotting objects in the desired colour.
#
# add_osm_objects (dat_B, col="gray40")
#
# # this works
# plot_osm_basemap (xylims=get_xylims (bbox), bg="gray20")
# add_osm_objects (dat_B, col="gray40")
#