-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathosmdata-methods.R
241 lines (201 loc) · 6.92 KB
/
osmdata-methods.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
#' @export
print.osmdata <- function (x, ...) {
msg <- NULL
# print meta-data
if (!all (vapply (x, is.null, FUN.VALUE = logical (1)))) {
msg <- "Object of class 'osmdata' with:\n"
}
msg <- c (msg, c (rep (" ", 17), "$bbox : ", x$bbox, "\n"))
objs <- c ("overpass_call", "meta")
prnts <- c (
"The call submitted to the overpass API",
"metadata including timestamp and version numbers"
)
for (i in seq (objs)) {
if (!is.null (x [objs [i]])) {
nm <- c (rep (" ", 21 - nchar (objs [i])), "$", objs [i])
msg <- c (msg, nm, " : ", prnts [i], "\n")
}
}
# print geometry data
sf <- any (grep ("sf", lapply (x, class)))
if (sf) {
msg <- msg_sf (msg, x)
} else {
msg <- msg_non_sf (msg, x)
}
message (msg)
invisible (x)
}
msg_sf <- function (msg, x) {
indx <- which (grepl ("osm", names (x)))
for (i in names (x) [indx]) {
xi <- x [[i]]
nm <- c (rep (" ", 21 - nchar (i)), "$", i)
if (is.null (xi)) {
msg <- c (msg, nm, " : NULL\n")
} else if (grepl ("line", i)) { # sf "lines" -> "linestrings"
msg <- c (
msg, nm,
" : 'sf' Simple Features Collection with ",
nrow (xi), " ", strsplit (i, "osm_") [[1]] [2],
"trings\n"
)
} else {
msg <- c (
msg, nm, " : 'sf' Simple Features Collection with ",
nrow (xi), " ",
strsplit (i, "osm_") [[1]] [2], "\n"
)
}
}
return (msg)
}
msg_non_sf <- function (msg, x) {
indx <- which (grepl ("osm", names (x)))
for (i in names (x) [indx]) {
xi <- x [[i]]
nm <- c (rep (" ", 21 - nchar (i)), "$", i)
if (is.null (xi)) {
msg <- c (msg, nm, " : NULL", "\n")
} else {
type <- strsplit (i, "osm_") [[1]] [2]
types <- c (
"points", "lines", "polygons",
"multlines", "multipolygons"
)
sp_types <- c (
"Points", "Lines", "Polygons",
"Lines", "Polygons"
)
types <- sp_types [match (type, types)]
msg <- c (
msg, nm, " : 'sp' Spatial", types, "DataFrame with ",
nrow (xi), " ", strsplit (i, "osm_") [[1]] [2],
"\n"
)
}
}
return (msg)
}
#' @export
c.osmdata <- function (...) {
x <- list (...)
cl_null <- vapply (x, function (i) {
is.null (i$osm_points) &
is.null (i$osm_lines) &
is.null (i$osm_polygons) &
is.null (i$osm_multilines) &
is.null (i$osm_multipolygons)
},
FUN.VALUE = logical (1)
)
x <- x [which (!cl_null)]
if (length (x) < 1) {
stop ("osmdata object is entirely NULL")
}
cl_sf <- vapply (x, function (i) {
any (grep ("sf", lapply (i, class)))
},
FUN.VALUE = logical (1)
)
if (!(all (cl_sf) | all (!cl_sf))) {
stop ("All objects must be either osmdata_sf or osmdata_sp")
}
sf <- all (cl_sf)
res <- osmdata ()
res$bbox <- x [[1]]$bbox
res$overpass_call <- x [[1]]$overpass_call
res$meta <- x [[1]]$meta
if (sf) {
res <- c_sf (res, x)
} else {
# TODO: implement sp version
stop (
"'c' method currently implemented only for osmdata_sf. ",
"You could use\n'osmdata_sf()', and convert with ",
"'as(x,'Spatial')' from package 'sf'."
)
}
return (res)
}
#' @export
c.osmdata_sc <- function (...) {
x <- list (...)
nms <- unique (unlist (lapply (x, names)))
res <- lapply (nms, function (n) {
unique (do.call (rbind, lapply (x, function (i) i [[n]])))
})
names (res) <- nms
class (res) <- c ("SC", "sc", "osmdata_sc")
return (res)
}
c_sf <- function (res, x) {
osm_names <- names (x [[1]]) [which (grepl ("osm_", names (x [[1]])))]
core_names <- c ("osm_id", "name", "geometry")
for (i in osm_names) {
xi <- lapply (x, function (j) j [[i]])
nrows <- lapply (xi, function (j) ifelse (is.null (j), 0, nrow (j)))
indx <- which (unlist (nrows) > 0)
xi <- xi [indx]
xi [vapply (xi, is.null, logical (1))] <- NULL
if (length (xi) > 0) {
ids <- cnames <- NULL
for (j in xi) {
ids <- c (ids, rownames (j))
cnames <- c (cnames, colnames (j))
}
ids <- sort (unique (ids))
cnames <- cnames [!cnames %in% core_names]
cnames <- sort (unique (c ("osm_id", "name", cnames, "geometry")))
resi <- xi [[1]]
# then expand resi to final number of columns keeping sf
# integrity
cnames_new <- cnames [which (!cnames %in% names (resi))]
for (j in cnames_new) {
resi [j] <- rep (NA, nrow (resi))
}
# and re-order columns again
indx1 <- which (names (resi) %in% core_names)
indx2 <- which (!seq (ncol (resi)) %in% indx1)
indx <- c (
which (names (resi) == "osm_id"),
which (names (resi) == "name"),
indx2 [order (names (resi) [indx2])],
which (names (resi) == "geometry")
)
att <- attributes (resi)
resi <- resi [, indx, drop = FALSE]
nms <- names (resi)
attributes (resi) <- att # restored sf_column and agr attributes
names (resi) <- nms
# Then we're finally ready to pack in the remaining bits
xi [[1]] <- NULL
for (j in xi) {
rindx <- which (!rownames (j) %in% rownames (resi))
# cindx <- which (names (j) %in% names (resi))
resj <- j [rindx, , drop = FALSE] # nolint
# then expand resj as for resi above
cnames_new <- cnames [which (!cnames %in% names (resj))]
for (k in cnames_new) {
resj [k] <- rep (NA, nrow (resj))
}
indx1 <- which (names (resj) %in% core_names)
indx2 <- which (!seq (ncol (resj)) %in% indx1)
indx <- c (
which (names (resj) == "osm_id"),
which (names (resj) == "name"),
indx2 [order (names (resj) [indx2])],
which (names (resj) == "geometry")
)
resj <- resj [, indx]
resi <- rbind (resi, resj)
} # end for j in x
res [[i]] <- resi
attr (res [[i]], "sf_column") <- attr (resi, "sf_column")
attr (res [[i]], "agr") <- attr (resi, "agr")
} # end if length (xi) > 0
} # end for i in osm_names
class (res) <- c (class (res), "osmdata_sf")
return (res)
}