如何在R中计算具有多边形几何形状的sf对象的近邻?

r3i60tvu  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(105)

我有德国邮政编码的多边形形状数据。对于那些邮政编码多边形,我喜欢计算各种最近邻度量。我已经看到了使用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"

# 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

字符串

rvpgvaaj

rvpgvaaj1#

我在包含speaking函数poly2nb()spdep包中找到了答案。不知道为什么我没有早点发现。

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

字符串

相关问题