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

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

How to calculate near neighbours in sf object with polygon geometry in R?

问题

我有德国邮政编码的多边形形状数据。对于这些邮政编码多边形,我想计算各种最近邻居度量。我已经看到使用sp包的方法(使用coordinates(),例如knearneigh(coordinates(GER), k = 4))。我选择在R中使用sf空间对象,但对如何在这里实现邻居感到困惑。谢谢

  1. library(sf)
  2. library(dplyr)
  3. library(leaflet)
  4. URL <- "https://downloads.suche-postleitzahl.org/v2/public/plz-5stellig.shp.zip"
  5. # 使用GDAL虚拟文件系统从远程URL加载压缩的shapefile
  6. GER_postcode <- paste0("/vsizip//vsicurl/", URL) %>% read_sf()
  7. # 使用giscoR获取德国国界轮廓
  8. GER_outline <- giscoR::gisco_get_countries(country = "DE")
  9. # 子样本
  10. GER_postcode_subsample <- GER_postcode %>% filter(substr(plz, 1, 1) %in% c(0, 1, 7))
  11. # 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

  1. library(sf)
  2. library(dplyr)
  3. library(leaflet)
  4. URL &lt;- &quot;https://downloads.suche-postleitzahl.org/v2/public/plz-5stellig.shp.zip&quot;
  5. # use GDAL virtual file systems to load zipped shapefile from remote url
  6. GER_postcode &lt;- paste0(&quot;/vsizip//vsicurl/&quot;, URL) %&gt;% read_sf()
  7. # country outline from giscoR
  8. GER_outline &lt;- giscoR::gisco_get_countries(country = &quot;DE&quot;)
  9. # subsample
  10. GER_postcode_subsample &lt;- GER_postcode %&gt;% filter(substr(plz, 1, 1) %in% c(0, 1, 7))
  11. # k nearest neighbours for sf dataframe

答案1

得分: 0

我在spdep包中找到了答案,其中包含了名为poly2nb()的函数。不知道为什么我之前没有找到它。

  1. library(spdep)
  2. queens <- poly2nb(GER_postcode_subsample,
  3. queen = TRUE, # 单个共享边界点符合邻近条件
  4. snap = 1) # 我们将1米内的点视为“相邻”
  5. summary(queens)
  6. 邻居列表对象:
  7. 区域数量:2624
  8. 非零链接数量:13698
  9. 非零权重百分比:0.1989434
  10. 平均链接数量:5.220274
  11. 没有链接的9个区域:
  12. 275 284 554 616 922 947 1889 2328 2329
  13. 链接数量分布:
  14. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  15. 9 28 117 307 513 574 468 310 168 78 28 11 2 10 1
  16. 28个最少连接的区域:
  17. 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个链接
  18. 1个最连接的区域:
  19. 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.

  1. library(spdep)
  2. queens &lt;- poly2nb(GER_postcode_subsample,
  3. queen = TRUE, # a single shared boundary point meets the contiguity condition
  4. snap = 1) # we consider points in 1m distance as &#39;touching&#39;
  5. summary(queens)
  6. Neighbour list object:
  7. Number of regions: 2624
  8. Number of nonzero links: 13698
  9. Percentage nonzero weights: 0.1989434
  10. Average number of links: 5.220274
  11. 9 regions with no links:
  12. 275 284 554 616 922 947 1889 2328 2329
  13. Link number distribution:
  14. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  15. 9 28 117 307 513 574 468 310 168 78 28 11 2 10 1
  16. 28 least connected regions:
  17. 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
  18. 1 most connected region:
  19. 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:

确定