Helper function to provide a distance calculation between spatial objects. The distance to the first nearest neighbour found (optionally within a maximum search radius) is returned.
fs_nndist(X, Y, maxSearch = 100, method = "poly", unit = "m")
X | Spatial object of |
---|---|
Y | (Optional) Spatial object to measure distances to. |
maxSearch | Maximum radius around |
method | Either |
unit | Character abbreviation for the units to return from the distance calculation. |
If Y
is omitted the nearest neighbour distances are found
within X
. Otherwise, the distance for each object in X
to its
nearest neighbour in Y
is returned.
Use method
to adjust which geometry of X
and Y
is used
for distance calculations. When method='poly'
distances are measured
between polygon edges. When method='centroid'
, the centroids of
building footprints are used instead. Centroid-based distance calculations
are faster.
Providing a maximum search radius is strongly advised to speed up the calculation.
data("kampala", package="foot") # get sample of buildings buildings <- kampala$buildings buildings <- buildings[sample(1:nrow(buildings), size=100, replace=F),] clusters <- kampala$clusters # calculate distance between buildings in m fs_nndist(buildings, unit="m")#> Units: [m] #> [1] 95.238821 65.862830 93.530371 NA 62.554247 NA #> [7] NA 59.355964 46.402483 NA NA 61.115130 #> [13] NA 100.375712 41.049626 NA 49.057702 48.234197 #> [19] 81.850288 NA NA 73.137286 73.137286 NA #> [25] 41.049626 48.234197 NA 84.573455 95.238821 51.178530 #> [31] 81.852329 NA 26.378903 NA NA 26.378903 #> [37] 6.290142 28.980741 NA NA 74.420835 56.933114 #> [43] NA 33.831274 NA NA 28.980741 NA #> [49] NA 65.862830 NA 49.211693 51.178530 56.272673 #> [55] 49.057702 75.457648 NA 33.831274 81.852329 NA #> [61] NA NA NA NA 32.979078 NA #> [67] 93.093458 54.822670 59.355964 79.133947 NA NA #> [73] 56.933114 NA NA NA NA NA #> [79] NA 75.457648 61.115130 NA NA NA #> [85] NA 68.391040 6.290142 32.979078 70.642143 NA #> [91] 68.391040 13.228434 84.573455 46.402483 13.228434 49.211693 #> [97] 81.850288 93.530371 NA NA# calculate unrestricted distance # between buildings and another set of points fs_nndist(buildings, sf::st_centroid(clusters), maxSearch=NULL)#> Warning: st_centroid assumes attributes are constant over geometries of x#> Units: [m] #> [1] 568.4876 577.2525 504.1517 589.7999 347.0815 686.2325 916.3490 #> [8] 648.3701 559.4254 626.7846 440.9420 719.0643 541.6216 738.0852 #> [15] 496.2589 446.1988 498.7919 722.1400 988.7665 922.3579 731.2875 #> [22] 579.7391 620.7850 739.9889 547.3919 757.6999 525.5561 491.0421 #> [29] 706.5455 924.9924 651.5136 702.7503 629.0620 624.1401 602.2785 #> [36] 671.8627 706.5082 628.7124 749.4935 679.8699 701.2242 329.5215 #> [43] 815.3810 691.0799 666.6609 766.7320 660.0480 701.9236 636.7027 #> [50] 609.5639 630.2778 343.6411 907.4347 919.0004 466.5383 587.0146 #> [57] 711.7245 672.2750 752.6473 709.5021 339.7149 1078.2246 349.9324 #> [64] 662.2130 456.8319 599.8687 406.8348 418.3193 651.7951 578.4820 #> [71] 640.7972 661.9564 379.0137 484.4741 772.0906 643.9961 590.2092 #> [78] 763.2740 582.9774 576.9785 755.4313 913.3294 795.9783 767.2193 #> [85] 694.5095 601.4041 698.9065 407.7409 670.0423 532.1633 716.4485 #> [92] 594.4593 526.0539 600.5147 613.1508 409.0461 1006.2988 557.7273 #> [99] 500.5282 520.5952# use footprint centroids fs_nndist(buildings, method='centroid', unit='m')#> Units: [m] #> [1] NA 73.63443 NA NA 83.84460 NA NA #> [8] 66.46838 62.04473 NA NA 71.90399 NA NA #> [15] 53.91958 NA 63.06241 69.11885 91.97339 NA NA #> [22] 83.57003 83.57003 NA 53.91958 69.11885 NA 94.25824 #> [29] NA 65.98726 NA NA 47.33131 NA NA #> [36] 47.33131 37.64954 40.42602 NA NA 99.41785 65.57872 #> [43] NA 52.72402 NA NA 40.42602 NA NA #> [50] 73.63443 NA 73.28295 65.98726 74.35106 63.06241 91.86391 #> [57] NA 52.72402 NA NA NA NA NA #> [64] NA 45.33091 NA 100.27763 73.89221 66.46838 90.16496 #> [71] NA NA 65.57872 NA NA NA NA #> [78] NA NA 91.86391 71.90399 NA NA NA #> [85] NA NA 37.64954 45.33091 90.76827 NA NA #> [92] 27.78070 94.25824 62.04473 27.78070 73.28295 91.97339 NA #> [99] NA NA