-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgeocode_OSM.R
More file actions
291 lines (236 loc) · 10.9 KB
/
geocode_OSM.R
File metadata and controls
291 lines (236 loc) · 10.9 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
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
#' Geocodes a location using OpenStreetMap Nominatim
#'
#' Geocodes a location (based on a search query) to coordinates and a bounding box. Similar to geocode from the ggmap package. It uses OpenStreetMap Nominatim. For processing large amount of queries, please read the usage policy (\url{https://operations.osmfoundation.org/policies/nominatim/}).
#'
#' @param q a character (vector) that specifies a search query. For instance \code{"India"} or \code{"CBS Weg 11, Heerlen, Netherlands"}.
#' @param projection projection in which the coordinates and bounding box are returned. See \code{\link[sf:st_crs]{st_crs}} for details. By default latitude longitude coordinates (EPSG 4326).
#' @param return.first.only Only return the first result
#' @param keep.unfound Keep list items / data.frame rows with \code{NA}s for unfound search terms. By default \code{FALSE}
#' @param details provide output details, other than the point coordinates and bounding box
#' @param as.data.frame Return the output as a \code{data.frame}. If \code{FALSE}, a list is returned with at least two items: \code{"coords"}, a vector containing the coordinates, and \code{"bbox"}, the corresponding bounding box. By default false, unless \code{q} contains multiple queries. If \code{as.sf = TRUE} (see below), \code{as.data.frame} will set to \code{TRUE}.
#' @param as.sf Return the output as \code{\link[sf:sf]{sf}} object. If \code{TRUE}, \code{return.first.only} will be set to \code{TRUE}. Two geometry columns are added: \code{bbox} and \code{point}. The argument \code{geometry} determines which of them is set to the default geometry.
#' @param geometry When \code{as.sf}, this argument determines which column (\code{bbox} or \code{point}) is set as geometry column. Note that the geometry can be changed afterwards with \code{\link[sf:st_geometry]{st_set_geometry}}.
#' @param server OpenStreetMap Nominatim server name. Could also be a local OSM Nominatim server.
#' @return If \code{as.sf} then a \code{\link[sf:sf]{sf}} object is returned. Else, if \code{as.data.frame}, then a \code{data.frame} is returned, else a list.
#' @export
#' @importFrom XML xmlChildren xmlRoot xmlAttrs xmlTreeParse xmlValue
#' @importFrom stars st_as_stars
#' @example ./examples/geocode_OSM.R
#' @seealso \code{\link{rev_geocode_OSM}}, \code{\link{bb}}
geocode_OSM <- function(q, projection=NULL, return.first.only=TRUE, keep.unfound = FALSE, details=FALSE, as.data.frame=NA, as.sf=FALSE, geometry=c("point", "bbox"), server="https://nominatim.openstreetmap.org") {
n <- length(q)
q2 <- gsub(" ", "+", enc2utf8(q), fixed = TRUE)
addr <- paste0(server, "/search?q=", q2, "&format=xml&polygon=0&addressdetails=0")
geometry <- match.arg(geometry)
project <- !missing(projection)
if (is.na(as.data.frame)) as.data.frame <- (n>1)
if (as.sf) {
as.data.frame <- TRUE
return.first.only <- TRUE
}
sn_names <- c("place_id", "osm_type", "osm_id", "place_rank", "display_name", "class", "type", "importance", "icon")
output2 <- lapply(1:n, function(k) {
tmpfile <- tempfile()
suppressWarnings(download.file(addr[k], destfile = tmpfile, mode= "wb", quiet = TRUE))
doc <- xmlTreeParse(tmpfile, encoding="UTF-8")
unlink(tmpfile)
res <- xmlChildren(xmlRoot(doc))
if (length(res)==0) {
message(paste("No results found for \"", q[k], "\".", sep="")) #if (n==1)
if (keep.unfound) {
if (as.data.frame) {
res = if (project) {
list(query = q[k], x = as.numeric(as.numeric(NA)), y = as.numeric(NA), y_min = as.numeric(NA),
y_max = as.numeric(NA), x_min = as.numeric(NA), x_max = as.numeric(NA))
} else {
list(query = q[k], lat = as.numeric(NA), lon = as.numeric(NA), lat_min = as.numeric(NA),
lat_max = as.numeric(NA), lon_min = as.numeric(NA), lon_max = as.numeric(NA))
}
} else {
res = list(queue = q[k], coords = c(x=NA, y = NA), bbox = sf::st_bbox())
}
if (details) res[sn_names] = as.character(NA)
if (as.sf) res$bbox = sf::st_sfc(sf::st_polygon())
if (as.data.frame) res <- as.data.frame(res, stringsAsFactors=FALSE)
return(list(res))
} else return(NULL)
}
idx <- if (return.first.only) 1 else 1:length(res)
output <- lapply(idx, function(i) {
search_result <- xmlAttrs(res[[i]])
search_result_id <- search_result[sn_names]
names(search_result_id) <- sn_names # in case of missings
Encoding(search_result_id) <- "UTF-8"
search_result_loc <- as.numeric(search_result[c("lat", "lon")])
names(search_result_loc) <- c("lat", "lon")
search_result_bb <- as.numeric(unlist(strsplit(search_result["boundingbox"], ",")))
if (!project) {
names(search_result_bb) <- c("lat_min", "lat_max", "lon_min", "lon_max")
b <- bb(xlim=search_result_bb[3:4], ylim=search_result_bb[1:2])
coords <- search_result_loc[c("lon", "lat")]
names(coords) <- c("x", "y")
} else {
b <- bb(xlim=search_result_bb[3:4], ylim=search_result_bb[1:2], current.projection = .crs_longlat, projection=projection)
search_result_bb <- b[c(2,4,1,3)]
names(search_result_bb) <- c("y_min", "y_max", "x_min", "x_max")
p <- sf::st_sf(sf::st_sfc(sf::st_point(search_result_loc[2:1]), crs = .crs_longlat))
p <- sf::st_transform(p, crs=projection)
coords <- as.vector(sf::st_coordinates(p))
names(coords) <- c("x", "y")
search_result_loc <- as.list(coords)
names(search_result_loc) <- c("x", "y")
}
if (as.sf) {
bbpoly <- bb_poly(b)
}
res <- if (as.data.frame) {
c(list(query=q[k]),
search_result_loc,
search_result_bb)
} else {
c(list(query=q[k],
coords=coords,
bbox=b))
}
if (as.sf) {
res <- c(res, list(bbox=bbpoly))
}
if (details) res <- c(res, search_result_id)
if (as.data.frame) res <- as.data.frame(res, stringsAsFactors=FALSE)
res
})
})
output3 <- do.call(c, output2)
if (is.null(output3)) return(NULL)
if (as.data.frame) {
df <- do.call(rbind, output3)
if (as.sf) {
names(df)[names(df) == "geometry"] <- "bbox"
if (!project) {
df$x <- df$lon
df$y <- df$lat
res <- suppressWarnings(sf::st_as_sf(df, coords = c("x","y"), crs=.crs_longlat, na.fail = FALSE))
} else {
df$x2 <- df$x
df$y2 <- df$y
res <- suppressWarnings(sf::st_as_sf(df, coords = c("x2","y2"), crs=.crs_longlat, na.fail = FALSE))
}
names(res)[names(res) == "geometry"] <- "point"
if (geometry == "point") res <- sf::st_set_geometry(res, "point")
sf::st_set_crs(res, .crs_longlat)
} else {
df
}
} else {
if (length(output3)==1) {
output3[[1]]
} else output3
}
}
#' Reverse geocodes a location using OpenStreetMap Nominatim
#'
#' Reverse geocodes a location (based on spatial coordinates) to an address. It uses OpenStreetMap Nominatim. For processing large amount of queries, please read the usage policy (\url{https://operations.osmfoundation.org/policies/nominatim/}).
#'
#' @param x x coordinate(s), or a spatial points object (\code{\link[sf:sf]{sf}} or \code{\link[sp:SpatialPoints]{SpatialPoints}})
#' @param y y coordinate(s)
#' @param zoom zoom level
#' @param projection projection in which the coordinates \code{x} and \code{y} are provided.
#' @param as.data.frame return as data.frame (\code{TRUE}) or list (\code{FALSE}). By default a list, unless multiple coordinates are provided.
#' @param server OpenStreetMap Nominatim server name. Could also be a local OSM Nominatim server.
#' @param params Additional parameters to pass to server. (must start with &), ex: "&accept-language=en" to return english rather than local language results.
#' @export
#' @importFrom XML xmlChildren xmlRoot xmlAttrs xmlTreeParse xmlValue
#' @return A data frame or a list with all attributes that are contained in the search result
#' @example ./examples/rev_geocode_OSM.R
#' @seealso \code{\link{geocode_OSM}}
rev_geocode_OSM <- function(x, y=NULL, zoom=NULL, projection=4326, as.data.frame=NA, server="https://nominatim.openstreetmap.org",params=NULL) {
project <- !missing(projection)
if (inherits(x, "Spatial")) x <- as(x, "sf")
if (inherits(x, "sf")) {
if (!all(sf::st_geometry_type(x) == "POINT")) stop("sf object should only contain POINT geometries")
x <- sf::st_transform(x, crs = .crs_longlat)
n <- nrow(x)
co <- sf::st_coordinates(x)
lon <- x <- co[,1]
lat <- y <- co[,2]
} else {
n <- 1
if (length(x) > 1 || length(y) > 1) {
n <- max(length(x), length(y))
x <- rep(x, length.out=n)
y <- rep(y, length.out=n)
}
if (!project) {
lon <- x
lat <- y
} else {
projection <- sf::st_crs(projection)
coords <- data.frame(x=x, y=y)
single_point <- sf::st_as_sf(x = coords, coords = c("x", "y"), crs = projection)
coords <- sf::st_transform(single_point, crs = .crs_longlat)
lon <- sf::st_coordinates(coords)[,1]
lat <- sf::st_coordinates(coords)[,2]
}
}
if (is.na(as.data.frame)) as.data.frame <- (n>1)
if (missing(zoom)) {
qzoom <- ""
strzoom <- ""
} else {
qzoom <- paste0("&zoom=", zoom)
strzoom <- paste0(", zoom = ", zoom)
}
addr <- paste0(server, "/reverse?format=xml&lat=", lat, "&lon=", lon, qzoom, "&addressdetails=1",params)
dfs <- lapply(1:n, function(i) {
# download query
tmpfile <- tempfile()
suppressWarnings(download.file(addr[i], destfile = tmpfile, mode= "wb", quiet = TRUE))
doc <- xmlTreeParse(tmpfile, encoding="UTF-8")
unlink(tmpfile)
# read xml document
res <- xmlChildren(xmlRoot(doc))
# get name
result_name <- xmlValue(res[[1]])
Encoding(result_name) <- "UTF-8"
# get osm id, location, bbox
search_result <- xmlAttrs(res[[1]])
search_result_id <- search_result[c("place_id", "osm_type", "osm_id", "ref")]
names(search_result_id) <- c("place_id", "osm_type", "osm_id", "ref") # in case of missings
Encoding(search_result_id) <- "UTF-8"
search_result_ll <- as.numeric(search_result[c("lat", "lon")])
names(search_result_ll) <- c("lat", "lon")
search_result_bb <- as.numeric(unlist(strsplit(search_result["boundingbox"], ",")))
names(search_result_bb) <- c("lat_min", "lat_max", "lon_min", "lon_max")
# get address
addr_result <- xmlChildren(res[[2]])
dfnames <- names(addr_result)
dfvalues <- lapply(1:length(addr_result), function(j) {
v <- xmlValue(addr_result[[j]])
Encoding(v) <- "UTF-8"
v
})
names(dfvalues) <- dfnames
c(list(x=x[i],
y=y[i],
name=result_name),
search_result_id,
search_result_ll,
search_result_bb,
dfvalues)
})
# cast to data.frame
if (as.data.frame) {
addrnames <- sort(unique(unlist(lapply(dfs, function(df) {
names(df)[14:length(df)]
}))))
addrlist <- lapply(addrnames, function(a) NA)
names(addrlist) <- addrnames
do.call(rbind, c(lapply(dfs, function(df) {
sel <- 14:length(df)
addrlist[names(df)[sel]] <- df[sel]
as.data.frame(c(df[1:13], addrlist), stringsAsFactors=FALSE)
}), list(stringsAsFactors=FALSE)))
} else {
dfs
}
}