简体   繁体   English

为 sf 对象创建距离矩阵

[英]Create a distance matrix for an sf object

I have an sf object, a map divided into districts.我有一个sf对象,一个分区的地图。 I would like to calculate the centroid of each district (using st_point_on_surface ) and then the relative distance between each centroid, like a distance matrix on which I can perform calculations (like keep ones that are within a certain radius) and get a db with each district identifiers and a list of those that match the criteria.我想计算每个区的质心(使用st_point_on_surface ),然后计算每个质心之间的相对距离,就像我可以执行计算的距离矩阵(比如保持在一定半径内的)并获得每个区标识符和符合条件的标识符列表。

Sorry in advance for the lack of reproducible code.由于缺乏可重现的代码,请提前抱歉。 What is the easiest way to go about it?最简单的方法是什么?

Thanks in advance提前致谢

在此处输入图片说明

If you want to stay within sf , try:如果您想留在sf内,请尝试:

library(sf)

# I got my Irak map from UN's OCHA, see: https://data.humdata.org/dataset/iraq-admin-level-1-boundaries
shp_irak01 <- st_read(dsn = "./irq-administrative-divisions-shapefiles/irq_admbnda_adm1_cso_20190603.shp")

#The centroids:
shp_centroid <- st_point_on_surface(x = shp_irak01)

#The euclidian distance matrix:
mtx_distance <- st_distance(shp_centroid, shp_centroid)
mtx_distance

Units: [m]
          [,1]     [,2]     [,3]     [,4]      [,5]     [,6]      [,7]      [,8]     [,9]    [,10]    [,11]    [,...until18]
 [1,]      0.0 665051.7 484956.1 295870.9 383276.14 449273.4 309551.40 277419.82 338721.4 458264.2 438887.2
 [2,] 665051.7      0.0 206781.0 408086.2 287295.37 624344.0 378896.62 451360.03 494695.5 860740.2 751176.7
 [3,] 484956.1 206781.0      0.0 205871.6 160931.72 570092.5 251440.27 332638.38 410021.2 766396.3 672788.5
 [4,] 295870.9 408086.2 205871.6      0.0 189849.79 526919.9 203679.02 260171.36 360077.9 661294.1 592026.7
 [5,] 383276.1 287295.4 160931.7 189849.8      0.00 410667.4  96030.18 175862.79 249100.0 607665.1 512029.2
 [6,] 449273.4 624344.0 570092.5 526919.9 410667.36      0.0 335212.34 266829.78 168229.3 261881.0 141057.5
 [7,] 309551.4 378896.6 251440.3 203679.0  96030.18 335212.3      0.00  81209.73 167546.7 514984.8 424189.9
 [8,] 277419.8 451360.0 332638.4 260171.4 175862.79 266829.8  81209.73      0.00 100377.7 433844.3 345162.4
 [9,] 338721.4 494695.5 410021.2 360077.9 249100.03 168229.3 167546.72 100377.65      0.0 366935.5 264039.4
[10,] 458264.2 860740.2 766396.3 661294.1 607665.14 261881.0 514984.82 433844.32 366935.5      0.0 120940.2
[11,] 438887.2 751176.7 672788.5 592026.7 512029.23 141057.5 424189.92 345162.43 264039.4 120940.2      0.0
[12,] 236774.0 434018.5 275796.9 160698.5 147862.67 367278.5  78884.14 102143.31 202413.5 509250.3 433156.4
[13,] 339045.0 649972.5 556320.0 469992.6 396524.64 122292.9 305687.02 225374.60 155304.3 212297.2 122084.4
[14,] 552966.6 202333.0 244743.0 370890.8 186402.59 424966.6 243458.18 294501.93 310734.5 669733.2 556160.8
[15,] 325331.4 801693.1 681103.2 551881.7 529148.71 276836.1 433248.41 353886.47 314515.2 135975.2 171322.4
[16,] 264036.5 613728.6 499989.6 396476.1 344132.89 185842.8 249137.50 168288.85 133007.6 269034.3 198147.7
[17,] 504238.7 168077.1 131432.6 277522.5 121527.11 479013.4 210879.43 283915.79 335117.1 701926.8 597103.5
[18,] 404859.5 326577.9 258506.4 284146.2 108878.33 315038.5 100818.86 138534.47 168707.2 535609.5 430059.8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM