如何在具有多边形几何结构的sf对象中计算附近的邻居关系在R中?

huangapple go评论58阅读模式
英文:

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 &lt;- &quot;https://downloads.suche-postleitzahl.org/v2/public/plz-5stellig.shp.zip&quot;

# use GDAL virtual file systems to load zipped shapefile from remote url
GER_postcode &lt;- paste0(&quot;/vsizip//vsicurl/&quot;, URL) %&gt;%  read_sf()

# country outline from giscoR
GER_outline &lt;- giscoR::gisco_get_countries(country = &quot;DE&quot;)

# subsample 
GER_postcode_subsample &lt;- GER_postcode %&gt;% 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 &lt;- poly2nb(GER_postcode_subsample, 
                     queen = TRUE, # a single shared boundary point meets the contiguity condition
                     snap = 1) # we consider points in 1m distance as &#39;touching&#39;
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

huangapple
  • 本文由 发表于 2023年7月20日 19:30:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定