查找最接近给定位置的纬度
Find the latitude closest to a given location
library(dplyr)
library(raster)
dat.shp <- getData('GADM', country='BRA', level = 1)
length(dat.shp@data$ID_1) # 27
我想为每个管理员 (ID_1)
找到哪个其他 ID_1 的纬度最接近,
df <- data.frame(ID_1 = dat.shp@data$ID_1, lat = coordinates(dat.shp)[, 2])
df$closest.ID <- NA
for(i in 1:nrow(df)){
temp <- df[i, ]
temp.dat <- df[-i, ]
sub <- temp.dat %>% dplyr::mutate(dif = lat - temp$lat) %>%
dplyr::filter(dif == min(dif))
df[df$ID_1 == temp$ID_1, "closest.ID"] <- sub$ID_1
rm(sub, temp.dat, temp)
}
这显然是错误的,因为它给了我所有 ID_1
的 21。
由于您使用的是空间数据,因此依赖空间库是有意义的。有很多方法可以用 sp
、rgeos
、GISTools
、raster
、sf
、stars
等来实现。这是一种方法rgeos.
# spatial libraries
library(rgeos)
library(sp)
# get centroids of each polygon
c <- gCentroid(dat.shp, byid = TRUE)
# visualize
plot(dat.shp)
plot(c, add=T)
# make distance matrix of centroids
d <- gDistance(c, c, byid = TRUE)
# sort each column of the distance matrix
# the first value is zero: distance between a point and itself
# second value is the closest point != itself
# get the name of that point
closest_pts <- apply(d, 1, function(x){names(sort(x)[2])})
# make into a dataframe and view result
df <- data.frame(id = 1:length(dat.shp$ID_1), closest_pt = closest_pts)
> df
id closest_pt
1 1 4
2 2 26
3 3 14
4 4 22
5 5 26
6 6 20
7 7 9
8 8 19
9 9 7
10 10 18
11 11 16
12 12 9
13 13 7
14 14 3
15 15 20
16 16 25
17 17 15
18 18 10
19 19 8
20 20 15
21 21 25
22 22 4
23 23 4
24 24 16
25 25 16
26 26 2
27 27 7
library(dplyr)
library(raster)
dat.shp <- getData('GADM', country='BRA', level = 1)
length(dat.shp@data$ID_1) # 27
我想为每个管理员 (ID_1)
找到哪个其他 ID_1 的纬度最接近,
df <- data.frame(ID_1 = dat.shp@data$ID_1, lat = coordinates(dat.shp)[, 2])
df$closest.ID <- NA
for(i in 1:nrow(df)){
temp <- df[i, ]
temp.dat <- df[-i, ]
sub <- temp.dat %>% dplyr::mutate(dif = lat - temp$lat) %>%
dplyr::filter(dif == min(dif))
df[df$ID_1 == temp$ID_1, "closest.ID"] <- sub$ID_1
rm(sub, temp.dat, temp)
}
这显然是错误的,因为它给了我所有 ID_1
的 21。
由于您使用的是空间数据,因此依赖空间库是有意义的。有很多方法可以用 sp
、rgeos
、GISTools
、raster
、sf
、stars
等来实现。这是一种方法rgeos.
# spatial libraries
library(rgeos)
library(sp)
# get centroids of each polygon
c <- gCentroid(dat.shp, byid = TRUE)
# visualize
plot(dat.shp)
plot(c, add=T)
# make distance matrix of centroids
d <- gDistance(c, c, byid = TRUE)
# sort each column of the distance matrix
# the first value is zero: distance between a point and itself
# second value is the closest point != itself
# get the name of that point
closest_pts <- apply(d, 1, function(x){names(sort(x)[2])})
# make into a dataframe and view result
df <- data.frame(id = 1:length(dat.shp$ID_1), closest_pt = closest_pts)
> df
id closest_pt
1 1 4
2 2 26
3 3 14
4 4 22
5 5 26
6 6 20
7 7 9
8 8 19
9 9 7
10 10 18
11 11 16
12 12 9
13 13 7
14 14 3
15 15 20
16 16 25
17 17 15
18 18 10
19 19 8
20 20 15
21 21 25
22 22 4
23 23 4
24 24 16
25 25 16
26 26 2
27 27 7