Как объединить пространственные наборы данных координат, используя R?

У меня есть два отдельных кадра данных пространственной точки в R (окрашены красным и черным на прилагаемом графике). Как импортировать атрибуты данных из "красного" набора данных в ближайшее место в "черном" наборе данных в R?

сюжет

1 ответ

Вот один из способов решения проблемы.

library(raster)
library(sp)

### create some example datasets
coords_A = cbind(runif(10, 1, 10), runif(10,1,10))
sp_A = SpatialPoints(coords_A)
spdf_A = SpatialPointsDataFrame(coords_A, data.frame(varA=letters[1:10]))

coords_B = cbind(runif(10, 1, 10), runif(10,1,10))
sp_B = SpatialPoints(coords_B)
spdf_B = SpatialPointsDataFrame(coords_B, data.frame(varB=letters[11:20], varC=LETTERS[11:20]))

### compute the complete distance matrix between the two sets of points
dist_mat <- pointDistance(spdf_A, spdf_B, lonlat = FALSE, allpairs = TRUE)

### identify nearest point in dataset B for every point in dataset A
nearest <- apply(dist_mat, 1, which.min)

### bind together the data from the dataset B (in your case the "red points")
### at the closest point to dataset A ("black points")
spdf_A@data<- cbind(spdf_A@data, spdf_B@data[nearest,])
Другие вопросы по тегам