查找最接近给定位置的纬度

时间:2018-11-09 17:18:47

标签: r raster shapefile

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分。

1 个答案:

答案 0 :(得分:2)

由于您正在使用空间数据,因此依靠空间库是很有意义的。有多种方法可以使用sprgeosGISToolsrastersfstars等来实现。这是一种方法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)

enter image description here

# 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