-
Notifications
You must be signed in to change notification settings - Fork 0
/
reconcileGeographies.R
373 lines (327 loc) · 13 KB
/
reconcileGeographies.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
#' Reconcile geographies (features) from two spatial polygon objects.
#'
#' @param polyA The first spatial polygon object.
#' @param polyB The second spatial polygon object.
#' @param idA The name of the id column in the first object. If not provided, the first column is assume to be the ID.
#' @param idB The name of the id column in the second object. If not provided, the first column is assume to be the ID.
#' @param project_crs The EPSG coordinate system. The unit must be the metre.
#' @param dist_buffer The distance for the buffer. Default is 5 metres.
#' @param min_inters_area The minimum intersection area for to two geometries to be intersecting. Default is 1 m2.
#' @return A data.frame relating the geographies in the two spatial objects (one-to-one, one-to-many) or many-to-many.
#' @examples
#' data(polygons, package = "recogeo")
#' res <- reconcileGeographies(poly_a, poly_b)
reconcileGeographies <- function(polyA, polyB,
idA = NULL, idB = NULL,
project_crs = NULL,
dist_buffer = 5,
min_inters_area = 1) {
if (class(polyA)[1] != 'sf'){
if (class(polyA)[1] == 'SpatialPolygonsDataFrame') {
polyA <- sf::st_as_sf(polyA)
} else {
stop("polyA is not a spatial object of a supported class")
}
}
if (class(polyB)[1] != 'sf'){
if (class(polyB)[1] == 'SpatialPolygonsDataFrame') {
polyB <- sf::st_as_sf(polyB)
} else {
stop("polyB is not a spatial object of a supported class")
}
}
if(is.null(idA)) {
idA <- colnames(polyA)[1]
}
if(is.null(idB)) {
idB <- colnames(polyB)[1]
}
if (is.null(project_crs)) {
project_crs <- sf::st_crs(polyB)
}
if(any(duplicated(as.character(polyA[[idA]]))) {
stop("Duplicated ids in idA")
}
if(any(duplicated(as.character(polyB[[idB]]))) {
stop("Duplicated ids in idB")
}
polyA[['.unigeokey']] <- as.character(polyA[[idA]])
polyB[['.unigeokey']] <- as.character(polyB[[idB]])
polyA <- sf::st_transform(polyA, crs=project_crs)
polyB <- sf::st_transform(polyB, crs=project_crs)
if (!grepl("units\\=m ", as.character(sf::st_crs(polyA))[2])) {
stop("CRS units must meters. Indicate CRS with `project_crs`.")
}
# 1: A Equals B
res1a <-
sf::st_contains(polyA %>%
sf::st_buffer(dist_buffer),
polyB,
sparse = FALSE)
res1b <-
sf::st_within(polyA,
polyB %>%
sf::st_buffer(dist_buffer),
sparse = FALSE)
res1 <- res1a == TRUE & res1b == TRUE
rownames(res1) <- paste0("s", polyA$`.unigeokey`)
colnames(res1) <- paste0("s", polyB$`.unigeokey`)
require(reshape2)
these_combinations1 <- melt(res1)
these_combinations1 <- these_combinations1[these_combinations1$value,]
these_combinations1$value <- NULL
colnames(these_combinations1) <- c("unigeokey_A", "unigeokey_B")
these_combinations1$unigeokey_A <-
gsub("^s", "", these_combinations1$unigeokey_A)
these_combinations1$unigeokey_B <-
gsub("^s", "", these_combinations1$unigeokey_B)
these_combinations1$type <- 'same'
# 2: A Contains B
polyA <-
polyA %>%
dplyr::filter(!`.unigeokey` %in%
these_combinations1$unigeokey_A)
polyB <-
polyB %>%
dplyr::filter(!`.unigeokey` %in%
these_combinations1$unigeokey_B)
res2 <-
sf::st_contains(polyA %>%
sf::st_buffer(dist_buffer),
polyB,
sparse = FALSE)
rownames(res2) <- paste0("s", polyA$`.unigeokey`)
colnames(res2) <- paste0("s", polyB$`.unigeokey`)
these_combinations2 <- reshape2::melt(res2)
these_combinations2 <- these_combinations2[these_combinations2$value,]
these_combinations2$value <- NULL
colnames(these_combinations2) <- c("unigeokey_A", "unigeokey_B")
these_combinations2$unigeokey_A <-
gsub("^s", "", these_combinations2$unigeokey_A)
these_combinations2$unigeokey_B <-
gsub("^s", "", these_combinations2$unigeokey_B)
these_combinations2$type <- 'AcontainsB'
# 3: B Contains A
res3 <-
sf::st_within(polyA,
polyB %>%
st_buffer(5),
sparse = FALSE)
rownames(res3) <- paste0("s", polyA$`.unigeokey`)
colnames(res3) <- paste0("s", polyB$`.unigeokey`)
require(reshape2)
these_combinations3 <- reshape2::melt(res3)
these_combinations3 <- these_combinations3[these_combinations3$value,]
these_combinations3$value <- NULL
colnames(these_combinations3) <- c("unigeokey_A", "unigeokey_B")
these_combinations3$unigeokey_A <-
gsub("^s", "", these_combinations3$unigeokey_A)
these_combinations3$unigeokey_B <-
gsub("^s", "", these_combinations3$unigeokey_B)
these_combinations3$type <- 'BcontainsA'
# Combines
all_combinations <-
rbind(these_combinations1, these_combinations2)
all_combinations <-
rbind(all_combinations, these_combinations3)
# 4: A intersects B
res4 <-
sf::st_intersects(polyA,
polyB,
sparse = FALSE)
these_combinations4 <- data.frame()
if (!is.null(res4)) {
rownames(res4) <-
paste0("s", polyA$`.unigeokey`)
colnames(res4) <-
paste0("s", polyB$`.unigeokey`)
these_combinations4 <- reshape2::melt(res4)
these_combinations4 <- these_combinations4[these_combinations4$value,]
these_combinations4$value <- NULL
colnames(these_combinations4) <- c("unigeokey_A", "unigeokey_B")
these_combinations4$unigeokey_A <-
gsub("^s", "", these_combinations4$unigeokey_A)
these_combinations4$unigeokey_B <-
gsub("^s", "", these_combinations4$unigeokey_B)
these_combinations4$type <- 'AintersectsB'
is_intersecting <-
mapply(testIntersectionArea,
these_combinations4$unigeokey_A,
these_combinations4$unigeokey_B,
MoreArgs =
list(polyA, polyB, min_inters_area))
these_combinations4 <-
these_combinations4[is_intersecting,]
these_combinations4 <-
these_combinations4[!paste0(these_combinations4$unigeokey_A,
"|",
these_combinations4$unigeokey_B) %in%
paste0(all_combinations$unigeokey_A,
"|",
all_combinations$unigeokey_B),]
}
all_combinations <-
rbind(all_combinations, these_combinations4)
final_check_a <-
polyA$.unigeokey[!polyA$.unigeokey %in% all_combinations$unigeokey_A]
if(length(final_check_a)>0) {
warning(paste0("Warning: Geographies from the first spatial object not related to ",
"any geography in the second spatial object: ",
paste(final_check_a, collapse = ", "), "."))
}
final_check_b <-
polyB$.unigeokey[!polyB$.unigeokey %in% all_combinations$unigeokey_B]
if(length(final_check_b)>0) {
warning(paste0("Warning: Geographies from the second spatial object not related to ",
"any geography in the first spatial object: ",
paste(final_check_b, collapse = ", "), "."))
}
return(all_combinations)
}
#' Test that the intersection area of two geometries is larger than a minimum
#'
#' @param unigeokey_A The unique ID of the geometry in the first spatial object.
#' @param unigeokey_B The unique ID of the geometry in the second spatial object.
#' @param polyA The first spatial object.
#' @param polyB The second spatial object.
#' @param min_inters_area The minimum intersecting area.
#' @return A logical vector.
#' @examples
#' mapply(testIntersectionArea, unigeokeys_A, unigeokeys_B, MoreArgs = list(polyA, polyB, min_inters_area))
testIntersectionArea <- function(unigeokey_A,
unigeokey_B,
polyA,
polyB,
min_inters_area) {
intersection_area <-
st_area(
st_intersection(st_geometry(polyA[polyA$.unigeokey == unigeokey_A,]),
st_geometry(polyB[polyB$.unigeokey == unigeokey_B,]))
)
return(as.numeric(intersection_area) > min_inters_area)
}
#' Reconcile data based on results from reconcileGeographies().
#' @param res Results from reconcileGeographies().
#' @param dataA The first data object. Either a spatial object with a data.frame or a data.frame.
#' @param dataB The second data object. Either a spatial object with a data.frame or a data.frame.
#' @param idA The name of the id column in the first object. If not provided, the first column is assume to be the ID.
#' @param idB The name of the id column in the second object. If not provided, the first column is assume to be the ID.
#' @param varA An ordered character vector with the names of the variables to concile from the first spatial object.
#' @param varB An ordered character vector with the names of the variables to concile from the second spatial object.
#' @param return_spatial Whether to return a spatial object (set to "A" or "B") or a simple data.frame (set to FALSE). "A" or "B" determines which geometries to return, either from the first or the seconf spatial object.
#' @return A Simple Feature with a data.frame or only a data.frame.
#' @example
#'
reconcileData <- function(res, dataA, dataB,
idA = NULL, idB = NULL,
varA, varB,
return_spatial = "A") {
if(is.null(idA)) {
idA <- colnames(dataA)[1]
}
if(is.null(idB)) {
idB <- colnames(dataB)[1]
}
reconciliation <-
getUniqueReconciliationKey(res)
dataA[['.unigeokey']] <- as.character(dataA[[idA]])
dataB[['.unigeokey']] <- as.character(dataB[[idB]])
if (return_spatial != FALSE) {
return_spatial <- toupper(return_spatial)
}
if (return_spatial == FALSE) {
if (class(dataA)[1] == 'sf') {
st_geometry(dataA) <- NULL
}
if (class(dataB)[1] == 'sf') {
st_geometry(dataB) <- NULL
}
if (class(dataA)[1] == 'SpatialPolygonsDataFrame') {
dataA <- dataA@data
}
if (class(dataB)[1] == 'SpatialPolygonsDataFrame') {
dataA <- dataB@data
}
} else if (return_spatial == "A") {
if (class(dataA)[1] != 'sf'){
if (class(dataA)[1] == 'SpatialPolygonsDataFrame') {
dataA <- sf::st_as_sf(dataA)
} else {
stop("dataA is not a spatial object of a supported class")
}
}
if (class(dataB)[1] == 'sf') {
st_geometry(dataB) <- NULL
}
if (class(dataB)[1] == 'SpatialPolygonsDataFrame') {
dataA <- dataB@data
}
} else if (return_spatial == "B") {
if (class(dataB)[1] != 'sf'){
if (class(dataB)[1] == 'SpatialPolygonsDataFrame') {
dataB <- sf::st_as_sf(dataB)
} else {
stop("dataB is not a spatial object of a supported class")
}
}
if (class(dataA)[1] == 'sf') {
st_geometry(dataA) <- NULL
}
if (class(dataA)[1] == 'SpatialPolygonsDataFrame') {
dataA <- dataA@data
}
} else {
stop("return_spatial argument not recognised.")
}
dataA_recogeo <-
merge(dataA,
reconciliation[reconciliation$set == 'A', 1:2],
by.x = '.unigeokey', by.y = '.unigeokey_old',
all.x = FALSE) %>%
dplyr::group_by(`.unigeokey_new`) %>%
dplyr::summarize_at(.vars = vars(varA), .funs = funs("sum"))
dataB_recogeo <-
merge(dataB,
reconciliation[reconciliation$set == 'B', 1:2],
by.x = '.unigeokey', by.y = '.unigeokey_old',
all.x = FALSE) %>%
dplyr::group_by(`.unigeokey_new`) %>%
dplyr::summarize_at(.vars = vars(varB), .funs = funs("sum"))
if (return_spatial == FALSE) {
new_data_recogeo <-
merge(dataA_recogeo[,c(".unigeokey_new",varA)],
dataB_recogeo[,c(".unigeokey_new",varB)],
by = ".unigeokey_new", suffix = c("_A","_B"))
} else if (return_spatial == "A") {
new_data_recogeo <-
merge(dataA_recogeo[,c(".unigeokey_new",varA)],
dataB_recogeo[,c(".unigeokey_new",varB)],
by = ".unigeokey_new", suffix = c("_A","_B"))
} else {
new_data_recogeo <-
merge(dataB_recogeo[,c(".unigeokey_new",varB)],
dataA_recogeo[,c(".unigeokey_new",varA)],
by = ".unigeokey_new", suffix = c("_B","_A"))
}
return(new_data_recogeo)
}
#' Get a unique reconciliation key based on results from reconcileGeographies().
#'
#' @param res Results from reconcileGeographies().
#' @return A data.frame.
#' @example
#' unirecogeokey_df <- getUniqueReconciliationKey(res)
getUniqueReconciliationKey <- function(res) {
el <- cbind(A=paste0(res$unigeokey_A,"~A"),
B=paste0(res$unigeokey_B,"~B"))
g <- igraph::graph_from_edgelist(el, directed = FALSE)
comp <- igraph::components(g)
reconciliation <- data.frame(`.unigeokey_old` = names(comp$membership),
`.unigeokey_new` = comp$membership,
row.names = NULL)
reconciliation$set <-
gsub("^.*~", "", reconciliation$`.unigeokey_old`)
reconciliation$`.unigeokey_old` <-
gsub("~.*$", "", reconciliation$`.unigeokey_old`)
return(reconciliation)
}