英文:
How to calculate near neighbours in sf object with polygon geometry in R?
问题
我有德国邮政编码的多边形形状数据。对于这些邮政编码多边形,我想计算各种最近邻居度量。我已经看到使用sp
包的方法(使用coordinates()
,例如knearneigh(coordinates(GER), k = 4)
)。我选择在R中使用sf
空间对象,但对如何在这里实现邻居感到困惑。谢谢
library(sf)
library(dplyr)
library(leaflet)
URL <- "https://downloads.suche-postleitzahl.org/v2/public/plz-5stellig.shp.zip"
# 使用GDAL虚拟文件系统从远程URL加载压缩的shapefile
GER_postcode <- paste0("/vsizip//vsicurl/", URL) %>% read_sf()
# 使用giscoR获取德国国界轮廓
GER_outline <- giscoR::gisco_get_countries(country = "DE")
# 子样本
GER_postcode_subsample <- GER_postcode %>% filter(substr(plz, 1, 1) %in% c(0, 1, 7))
# sf数据框的k个最近邻居
英文:
I have polygon shape data for German postcodes. For those postcode polygon I like to calculate various nearest neighbour measures. I have seen that procedures working with the sp
package (using coordinates()
, like knearneigh(coordinates(GER), k = 4)
). I opt for sf
spatial objects in R and are confused on how to implement neighbours here. Thank you
library(sf)
library(dplyr)
library(leaflet)
URL <- "https://downloads.suche-postleitzahl.org/v2/public/plz-5stellig.shp.zip"
# use GDAL virtual file systems to load zipped shapefile from remote url
GER_postcode <- paste0("/vsizip//vsicurl/", URL) %>% read_sf()
# country outline from giscoR
GER_outline <- giscoR::gisco_get_countries(country = "DE")
# subsample
GER_postcode_subsample <- GER_postcode %>% filter(substr(plz, 1, 1) %in% c(0, 1, 7))
# k nearest neighbours for sf dataframe
答案1
得分: 0
我在spdep
包中找到了答案,其中包含了名为poly2nb()
的函数。不知道为什么我之前没有找到它。
library(spdep)
queens <- poly2nb(GER_postcode_subsample,
queen = TRUE, # 单个共享边界点符合邻近条件
snap = 1) # 我们将1米内的点视为“相邻”
summary(queens)
邻居列表对象:
区域数量:2624
非零链接数量:13698
非零权重百分比:0.1989434
平均链接数量:5.220274
没有链接的9个区域:
275 284 554 616 922 947 1889 2328 2329
链接数量分布:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
9 28 117 307 513 574 468 310 168 78 28 11 2 10 1
28个最少连接的区域:
112 297 298 474 529 809 843 852 896 917 921 946 951 1027 1050 1147 1524 1687 1884 2068 2271 2291 2314 2327 2343 2367 2368 2509,每个区域有1个链接
1个最连接的区域:
1349,有14个链接
英文:
I found the answer in the spdep
package which contains the speaking function poly2nb()
. Don't know why I havn't found this earlier.
library(spdep)
queens <- poly2nb(GER_postcode_subsample,
queen = TRUE, # a single shared boundary point meets the contiguity condition
snap = 1) # we consider points in 1m distance as 'touching'
summary(queens)
Neighbour list object:
Number of regions: 2624
Number of nonzero links: 13698
Percentage nonzero weights: 0.1989434
Average number of links: 5.220274
9 regions with no links:
275 284 554 616 922 947 1889 2328 2329
Link number distribution:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
9 28 117 307 513 574 468 310 168 78 28 11 2 10 1
28 least connected regions:
112 297 298 474 529 809 843 852 896 917 921 946 951 1027 1050 1147 1524 1687 1884 2068 2271 2291 2314 2327 2343 2367 2368 2509 with 1 link
1 most connected region:
1349 with 14 links
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论