Compute total travel costs of shortest paths between nodes in a spatial network.
Usage
st_network_cost(
x,
from = node_ids(x),
to = node_ids(x),
weights = edge_length(),
direction = "out",
Inf_as_NaN = FALSE,
router = getOption("sfn_default_router", "igraph"),
use_names = FALSE,
...
)
st_network_distance(
x,
from = node_ids(x),
to = node_ids(x),
direction = "out",
Inf_as_NaN = FALSE,
router = getOption("sfn_default_router", "igraph"),
use_names = FALSE,
...
)
Arguments
- x
An object of class
sfnetwork
.- from
The nodes where the paths should start. Evaluated by
evaluate_node_query
. By default, all nodes in the network are included.- to
The nodes where the paths should end. Evaluated by
evaluate_node_query
. By default, all nodes in the network are included.- weights
The edge weights to be used in the shortest path calculation. Evaluated by
evaluate_weight_spec
. The default isedge_length
, which computes the geographic lengths of the edges.- direction
The direction of travel. Defaults to
'out'
, meaning that the direction given by the network is followed and costs are computed from the points given as argumentfrom
. May be set to'in'
, meaning that the opposite direction is followed an costs are computed towards the points given as argumentfrom
. May also be set to'all'
, meaning that the network is considered to be undirected. This argument is ignored for undirected networks.- Inf_as_NaN
Should the cost values of unconnected nodes be stored as
NaN
instead ofInf
? Defaults toFALSE
.- router
The routing backend to use for the cost matrix computation. Currently supported options are
'igraph'
and'dodgr'
. See Details.- use_names
If a column named
name
is present in the nodes table, should these names be used as row and column names in the matrix, instead of the node indices? Defaults toFALSE
. Ignored when the nodes table does not have a column namedname
.- ...
Additional arguments passed on to the underlying function of the chosen routing backend. See Details.
Value
An n times m numeric matrix where n is the length of the from
argument, and m is the length of the to
argument.
Details
The sfnetworks package does not implement its own routing algorithms to compute cost matrices. Instead, it relies on "routing backends", i.e. other R packages that have implemented such algorithms. Currently two different routing backends are supported.
The default is igraph
. This package supports
many-to-many cost matrix computation with the distances
function. The igraph router does not support dual-weighted routing.
The second supported routing backend is dodgr
. This
package supports many-to-many cost matrix computation with the
dodgr_dists
function. It also supports dual-weighted
routing. The dodgr package is a conditional dependency of sfnetworks. Using
the dodgr router requires the dodgr package to be installed.
The default router can be changed by setting the sfn_default_router
option.
Examples
library(sf, quietly = TRUE)
library(tidygraph, quietly = TRUE)
net = as_sfnetwork(roxel, directed = FALSE) |>
st_transform(3035)
# Compute the network cost matrix between node pairs.
# Note that geographic edge length is used as edge weights by default.
st_network_cost(net, from = c(495, 121), to = c(495, 121))
#> Units: [m]
#> 495 121
#> 495 0.0000 669.8584
#> 121 669.8584 0.0000
# st_network_distance is a synonym for st_network_cost with default weights.
st_network_distance(net, from = c(495, 121), to = c(495, 121))
#> Units: [m]
#> 495 121
#> 495 0.0000 669.8584
#> 121 669.8584 0.0000
# Compute the network cost matrix between spatial point features.
# These are snapped to their nearest node before computing costs.
p1 = st_geometry(net, "nodes")[495] + st_sfc(st_point(c(50, -50)))
st_crs(p1) = st_crs(net)
p2 = st_geometry(net, "nodes")[121] + st_sfc(st_point(c(-10, 100)))
st_crs(p2) = st_crs(net)
st_network_cost(net, from = c(p1, p2), to = c(p1, p2))
#> Units: [m]
#> 477 216
#> 477 0.0000 586.0027
#> 216 586.0027 0.0000
# Use a node type query function to specify origins and/or destinations.
st_network_cost(net, from = 499, to = node_is_connected(499))
#> Units: [m]
#> 1 2 3 4 5 6 7 8
#> 499 873.5359 890.7781 454.4463 395.7263 370.4559 390.7588 1026.71 1069.539
#> 9 10 11 12 13 14 15 16
#> 499 351.2491 315.9345 1725.54 1714.639 546.2603 586.0429 1199.573 1264.65
#> 17 18 19 20 21 22 23 24
#> 499 229.0913 146.2307 501.4977 436.5051 1504.114 1547.867 1062.55 1110.435
#> 25 26 27 28 29 30 31 32
#> 499 1685.448 1783.653 1070.348 999.1489 1393.529 1413.081 1463.669 1467.738
#> 33 34 35 36 37 38 39 40
#> 499 1718.225 1114.448 1155.142 350.4034 286.7882 565.7165 504.8286 1073.319
#> 41 42 43 44 45 46 47 48
#> 499 1133.801 1112.113 1094.508 1295.444 1305.242 95.07722 76.55244 609.2674
#> 49 50 51 52 53 54 55 56
#> 499 649.819 1930.735 1975.236 1017.045 1005.434 619.4744 701.2313 1317.5
#> 57 58 59 60 61 62 63 64
#> 499 1279.314 1177.133 1090.709 769.5344 776.8791 2091.375 2107.009 1120.121
#> 65 66 67 68 69 70 71 72
#> 499 1219.012 708.9491 695.864 624.1239 1333.874 1319.891 1431.409 1392.694
#> 73 74 75 76 77 78 79 80
#> 499 666.0641 657.6485 792.224 808.1169 811.165 802.1964 1867.797 1920.723
#> 81 82 83 84 85 86 87 88
#> 499 1098.61 783.9091 774.7897 302.3767 323.6855 935.1098 949.0832 494.1647
#> 89 90 91 92 93 94 95 96
#> 499 504.297 1293.018 1322.872 275.5442 236.9272 1601.008 1614.451 1500.923
#> 97 98 99 100 101 102 103 104
#> 499 1595.084 1207.889 1262.779 226.1544 216.135 1966.568 1933.414 756.4774
#> 105 106 107 108 109 110 111 112
#> 499 803.9649 879.5799 220.5792 1304.358 1390.99 265.8059 811.4966 796.2788
#> 113 114 115 116 117 118 119 120
#> 499 559.156 583.344 700.3703 685.7695 805.7022 942.5596 794.1741 724.5884
#> 121 122 123 124 125 126 127 128
#> 499 1566.494 1599.482 718.8359 712.5437 1790.244 1748.59 2085.279 2197.803
#> 129 130 131 132 133 134 135 136
#> 499 395.7898 448.1837 1825.287 1833.432 1491.258 1661.668 1614.675 1282.273
#> 137 138 139 140 141 142 143 144
#> 499 1218.446 468.3246 528.289 926.9009 986.05 1102.083 997.9613 1179.76
#> 145 146 147 148 149 150 151 152
#> 499 1147.025 1195.977 1468.043 1502.416 787.5854 785.1026 1056.22 1042.937
#> 153 154 155 156 157 158 159 160
#> 499 729.0523 742.5479 1897.167 1978.351 1807.294 1811.299 401.87 902.3921
#> 161 162 163 164 165 166 167 168
#> 499 898.7296 734.3901 728.0375 839.2339 859.1539 172.8241 78.90951 1102.744
#> 169 170 171 172 173 174 175 176
#> 499 1137.421 1023.595 1050.153 847.2224 843.043 786.3771 716.9932 762.0341
#> 177 178 179 180 181 182 183 184
#> 499 774.468 871.3191 863.3708 1362.292 743.014 751.3349 695.3414 1242.037
#> 185 186 187 188 189 190 191 192
#> 499 1150.204 1033.579 1995.184 2092.136 1414.947 1329.204 771.6893 353.9481
#> 193 194 195 196 197 198 199 200
#> 499 382.0394 1024.415 192.3578 841.323 1765.551 1696.855 1677.519 1799.113
#> 201 202 203 204 205 206 207 208
#> 499 709.3068 681.6219 1038.633 570.1891 575.9745 798.4155 963.8589 971.197
#> 209 210 211 212 213 214 215 216
#> 499 1044.179 614.7124 1118.057 1073.895 932.7385 935.7969 1415.223 1477.426
#> 217 218 219 220 221 222 223 224
#> 499 1022.83 951.6681 235.4296 812.5915 790.7162 1788.932 1832.053 1491.298
#> 225 226 227 228 229 230 231 232
#> 499 1546.143 1461.208 758.766 707.9535 1142.598 1163.339 185.434 278.5785
#> 233 234 235 236 237 238 239 240
#> 499 706.7727 719.7412 687.0465 1149.023 1198.325 1027.229 1100.083 675.5914
#> 241 242 243 244 245 246 247 248
#> 499 504.674 922.5865 813.5493 2064.305 2122.993 560.7615 607.6151 713.3061
#> 249 250 251 252 253 254 255 256
#> 499 1794.755 1837.644 1635.943 1694.008 1244.902 1057.333 443.0819 2183.752
#> 257 258 259 260 261 262 263 264
#> 499 1972.943 533.5964 589.8798 861.3666 802.1954 798.7234 1253.264 1227.5
#> 265 266 267 268 269 270 271 272
#> 499 1245.892 1815.611 870.1464 642.7876 683.6954 572.1387 502.8064 1814.839
#> 273 274 275 276 277 278 279 280
#> 499 1663.84 1712.093 1228.727 400.3016 1137.473 1094.758 1215.157 1272.686
#> 281 282 283 284 285 286 287 288
#> 499 1789.487 716.7609 725.8832 411.8817 353.359 730.4308 745.1619 756.7788
#> 289 290 291 292 293 294 295 296
#> 499 708.4335 927.7335 1182.694 1218.035 161.0607 142.751 601.652 595.1236
#> 297 298 299 300 301 302 303 304
#> 499 634.4614 750.1124 417.9026 689.744 624.7566 333.4458 1394.947 1291.632
#> 305 306 307 308 309 310 311 312
#> 499 1315.717 1265.51 297.5152 283.5637 179.4085 554.4512 705.5108 732.8085
#> 313 314 315 316 317 318 319 320
#> 499 589.1681 1949.918 1992.497 750.6381 886.0768 261.8298 492.9583 816.1173
#> 321 322 323 324 325 326 327 328
#> 499 834.3017 1410.342 1035.057 1085.489 721.3834 731.3224 805.1009 1180.76
#> 329 330 331 332 333 334 335 336
#> 499 1207.386 1167.683 1248.034 1340.535 970.5099 956.9948 624.8838 674.8888
#> 337 338 339 340 341 342 343 344
#> 499 1911.344 1163.983 447.6591 733.1545 1343.526 1977.468 1006.863 1024.421
#> 345 346 347 348 349 350 351 352
#> 499 945.6122 975.5134 1118.792 1432.043 2019.305 2080.727 820.2956 264.9639
#> 353 354 355 356 357 358 359 360
#> 499 1818.028 1805.63 502.6757 465.9777 1988.397 1998.708 1564.662 525.9751
#> 361 362 363 364 365 366 367 368
#> 499 1429.724 1440.644 755.268 664.929 789.7645 771.3454 1359.269 1297.26
#> 369 370 371 372 373 374 375 376
#> 499 682.0728 691.9732 1010.824 989.3609 1146.893 1183.224 590.5177 697.0859
#> 377 378 379 380 381 382 383 384
#> 499 674.318 739.3548 1109.368 1118.758 921.091 2018.192 1074.6 1019.425
#> 385 386 387 388 389 390 391 392
#> 499 1204.666 1206.25 398.1214 338.4697 1389.573 1420.275 1259.403 1219.991
#> 393 394 395 396 397 398 399 400
#> 499 754.4252 1113.349 892.7151 1050.228 1010.697 778.4496 1207.405 1177.858
#> 401 402 403 404 405 406 407 408
#> 499 1639.966 1621.967 2014.519 1980.016 918.1276 213.4413 1323.652 1397.072
#> 409 410 413 414 415 416 417 418
#> 499 857.6601 810.1723 1610.574 1601.54 1477.911 1489.697 572.2393 555.5383
#> 419 420 421 422 423 424 425 426
#> 499 868.4021 731.1656 985.5966 538.034 1372.061 1426.185 428.6591 443.7392
#> 427 428 429 430 431 432 433 434
#> 499 661.5626 701.5294 1049.618 1077.23 70.89328 1015.033 1021.803 799.7069
#> 435 436 437 438 439 440 441 442
#> 499 751.6018 883.5399 863.6545 1162.006 256.1853 1357.605 1320.362 768.7192
#> 443 444 445 446 447 448 449 450
#> 499 995.7929 877.1031 963.6314 1268.604 1884.311 1948.057 790.2483 1079.821
#> 451 452 453 454 455 456 457 458 459
#> 499 614.3545 1037.168 1069.597 1891.7 1846.564 783.111 437.0745 760.5347 726.57
#> 460 461 462 463 464 465 466 467
#> 499 778.6045 541.5726 443.7159 952.8758 931.6692 912.2639 882.1958 787.7835
#> 468 469 470 471 472 473 474 475
#> 499 802.1098 688.9575 895.6581 411.3635 466.1645 950.23 990.4158 1014.519
#> 476 477 478 479 480 481 482 483
#> 499 1112.34 1143.153 1028.215 989.123 677.9555 735.1794 797.7426 765.1309
#> 484 485 486 487 488 489 490 491
#> 499 695.5624 701.8724 1167.1 928.8087 988.4196 998.7653 1299.383 753.645
#> 492 493 494 495 496 497 498 499 500
#> 499 421.6002 1068.93 968.4631 970.601 483.8035 718.4127 26.24319 0 1520.817
#> 501 502 503 504 505 506 507 508
#> 499 1485.882 1432.847 893.7283 916.1384 1410.803 1627.004 533.9837 436.2807
#> 509 510 511 512 513 514 515 516
#> 499 1268.185 2051.565 1914.639 1871.164 959.2229 981.9011 2074.433 2027.182
#> 517 518 519 520 521 522 523 524
#> 499 1685.317 1647.46 1215.887 1156.045 881.2774 798.5854 2038.379 2108.918
#> 525 526 527 528 529 530 531 532
#> 499 1261.569 224.2117 1207.469 1399.596 1013.212 88.66324 948.022 960.0364
#> 533 534 535 536 537 538 539 540
#> 499 1191.677 1194.926 2164.223 1172.918 1034.628 556.6538 1244.016 807.5977
#> 541 542 543 544 545 546 547 548
#> 499 731.7814 947.684 196.3235 1128.433 1070.533 1140.752 1511.707 1005.075
#> 549 550 551 552 553 554 555 556
#> 499 2067.482 127.8707 109.4762 1000.702 961.5858 1484.043 1306.83 704.6871
#> 557 558 559 560 561 562 563 564
#> 499 1227.932 207.7041 699.1399 679.3837 539.6218 574.364 1106.416 230.8235
#> 565 566 567 568 569 570 571 572
#> 499 800.7528 884.0467 675.1501 1207.369 1259.219 723.0045 767.3387 1068.846
#> 573 574 575 576 577 578 579 580
#> 499 1122.525 1647.363 1681.341 1186.372 739.9597 1808.023 1789.649 625.6373
#> 581 582 583 584 585 586 587 588
#> 499 1385.485 1417.895 766.1218 886.6601 787.6855 1542.166 2061.891 2120.105
#> 589 590 591 592 593 594 595 596
#> 499 1291.37 1022.637 974.9281 91.80787 1130.475 1289.641 648.7288 973.0365
#> 597 598 599 600 601 602 603 604
#> 499 435.8192 1124.679 258.3585 1403.557 1382.773 1340.972 477.7011 1211.504
#> 605 606 607 608 609 610 611 612
#> 499 1223.968 850.3333 883.7363 1129.332 2079.183 2016.091 812.6261 1244.609
#> 613 614 615 616 617 618 619 620
#> 499 1407.618 1346.475 2065.414 1288.791 1055.392 1046.531 2117.588 2124.025
#> 621 622 623 624 625 626 627 628
#> 499 1532.199 462.9961 910.2988 876.4129 1193.128 1227.285 1327.896 1816.52
#> 629 630 631 632 633 634 635 636
#> 499 882.195 1687.382 1095.329 1047.081 1158.213 1795.897 1857.86 766.2416
#> 637 638 639 640 641 642 643 644
#> 499 213.0037 203.3805 1934.558 2034.374 1695.136 1909.915 1341.518 1023.916
#> 645 646 647 648 649 650 651 652
#> 499 1030.418 234.2636 965.4015 1313.324 1307.588 933.0533 700.2591 1024.032
#> 653 654 655 656 657 658 659 660
#> 499 978.7125 584.0124 567.3972 508.6142 1508.389 794.7913 751.6468 430.5427
#> 661 662 663 664 665 666 667 668
#> 499 804.8805 773.2935 1463.782 234.5408 276.6258 674.5731 1799.23 953.4275
#> 669 670 671 672 673 674 675 676
#> 499 988.6681 878.1092 1644.942 1108.142 1996.853 504.8011 506.1029 1005.36
#> 677 678 679 680 681 682 683 684
#> 499 968.4955 1163.167 1128.56 792.1399 1027.9 731.6821 641.5734 558.6926
#> 685 686 687 688 689 690 691 692
#> 499 2021.381 1979.748 1267.286 318.0388 302.2884 481.0959 1445.202 2068.889
#> 693 694 695 696 697 698 699 700
#> 499 1840.76 605.962 1277.076 175.2124 1068.836 1101.401 1378.351 704.8933
#> 701 702 703 704 705 706 707 708
#> 499 260.6005 950.3017 2071.935 1158.668 1218.141 1635.553 1047.712 992.2682
#> 709 710 711 712 713 714 715 716
#> 499 877.1812 1114.472 634.1746 732.0683 1226.632 1787.411 1747.728 277.7143
#> 717 718 719 720 721 722 725 726
#> 499 241.2495 1782.547 1823.278 1454.134 1113.028 1320.655 2240.015 818.1879
#> 727 728 729 730 731 732 733 734
#> 499 1948.993 1966.811 1550.956 585.9248 426.6295 1265.835 821.7813 829.7346
#> 735 736 737 738 739 740 741 742
#> 499 1319.394 1274.719 64.04235 757.7018 897.6014 820.0067 1187.91 496.563
#> 743 744 745 746 747 748 749 750
#> 499 1394.653 795.5651 815.0034 1462.494 1239.723 1025 1539.497 432.6431
#> 751 752 753 754 755 756 757 758
#> 499 977.7647 1005.843 953.2313 954.8321 986.1761 1641.337 1563.292 874.2878
#> 759 760 761 762 763 764 765 766
#> 499 219.6254 281.1079 1161.28 910.5567 1445.753 784.7109 764.9534 1046.16
#> 767 768 769 770 771 772 773 774
#> 499 1060.537 1130.048 107.5825 128.8957 1510.703 1208.487 1842.702 671.6482
#> 775 776 777 778 779 780 781 782
#> 499 223.2587 1503.52 868.2117 1552.216 1708.253 1114.04 1823.167 1848.869
#> 783 784 785 786 787 788 789 792
#> 499 1534.451 1018.834 1491.916 488.2741 1226.831 808.946 1374.314 1454.677
#> 793 794 795 796 797 798 799 800
#> 499 307.4676 531.0321 754.7953 1168.374 1295.403 1347.812 1258.33 976.7465
#> 801 802 803 804 805 806 807 808
#> 499 900.8321 1322.963 1346.695 634.8554 694.3095 2002.157 2087.135 949.9061
#> 809 810 811 812 813 814 815 816
#> 499 905.9081 951.311 1277.412 382.5939 1330.126 2069.033 479.8494 473.9061
#> 817 818 819 820 821 822 823 824
#> 499 1639.596 954.6902 1169.867 1682.054 1515.118 1120.421 1250.853 1361.224
#> 825 826 827 828 829 830 831 832
#> 499 1890.399 1672.262 1957.889 70.50971 34.52996 969.0309 822.0138 306.983
#> 833 834 835 836 837 838 839 840
#> 499 1531.468 205.9225 1568.601 443.9537 1910.284 1915.773 527.5518 518.4602
#> 841 842 843 844 845 846 847 848
#> 499 606.6241 2040.418 1013.449 791.2585 381.9352 435.689 506.122 790.6733
#> 849 850 851 852 853 854 855 856
#> 499 207.035 2122.036 263.0604 509.8525 1510.887 319.9886 527.4409 591.1522
#> 857 858 859 860 861 862 863 864
#> 499 530.1348 747.5255 2083.59 736.9322 1332.059 514.678 1369.265 756.3091
#> 865 866 867 868 869 870 871 874
#> 499 919.6888 1763.417 1246.848 1188.992 1066.707 1247.632 889.2161 1191.559
#> 875 876 877 878 879 882 883 884
#> 499 542.3983 928.7408 1277.937 1339.039 1351.035 1557.681 1200.119 1230.94
#> 885 886 887 888 889 890 891 892
#> 499 2016.176 1900.649 2066.189 1119.22 930.375 944.732 915.9002 739.707
#> 893 894 895 896 897 898 899 900
#> 499 2001.624 527.5662 667.6359 2194.16 1292.515 1405.212 1460.603 462.576
#> 901 902 903 904 905 906 907 908
#> 499 137.348 1466.772 2065.266 1379.913 1416.664 1087.82 545.728 903.406
#> 909 910 911 912 913 914 917 918
#> 499 719.0583 805.8947 1817.649 1817.592 1817.655 1413.654 1161.006 2193.013
#> 919 920 921 922 923 924 925 926
#> 499 302.8186 1979.013 532.8349 1128.923 55.19123 1056.032 1537.776 1865.731
#> 927 928 929 930 931 932 933 934
#> 499 355.7311 831.0173 1807.189 1611.983 1150.719 378.8421 344.6649 930.014
#> 935 936 937 938 939 940 941 942
#> 499 973.2863 1221.223 1225.44 1407.8 235.7138 1194.315 778.1079 1162.499
#> 943 944 945 946 947 948 949 950
#> 499 1722.817 1235.034 320.6124 1844.805 599.1794 316.4717 704.0765 2062.273
#> 951 952 953 954 955 956 957 958
#> 499 733.9683 994.0074 1948.294 1482.403 794.4001 1080.372 304.5142 2184.952
#> 959 960 961 962 963 968 969 970
#> 499 511.2051 486.254 1056.011 868.6776 1355.249 249.6424 831.5113 1632.802
#> 971 972 973 974 975 976 977 978
#> 499 1491.589 1880.007 1285.894 839.9045 1510.393 931.8767 742.3722 394.0489
#> 979 980 981 982 983 984 985 986
#> 499 847.7391 90.92951 748.3793 1992.011 1293.83 512.1374 1552.246 1315.151
#> 987
#> 499 1196.929
# Use a spatial edge measure to specify edge weights.
# By default edge_length() is used.
st_network_cost(net, c(p1, p2), c(p1, p2), weights = edge_displacement())
#> Units: [m]
#> 477 216
#> 477 0.0000 583.9299
#> 216 583.9299 0.0000
# Use a column in the edges table to specify edge weights.
# This uses tidy evaluation.
net |>
activate("edges") |>
mutate(foo = runif(n(), min = 0, max = 1)) |>
st_network_cost(c(p1, p2), c(p1, p2), weights = foo)
#> 477 216
#> 477 0.000000 4.625577
#> 216 4.625577 0.000000
# Compute the cost matrix without edge weights.
# Here the cost is defined by the number of edges, ignoring space.
st_network_cost(net, c(p1, p2), c(p1, p2), weights = NA)
#> 477 216
#> 477 0 10
#> 216 10 0
# Use the dodgr router for dual-weighted routing.
paths = st_network_cost(net,
from = c(p1, p2),
to = c(p1, p2),
weights = dual_weights(edge_segment_count(), edge_length()),
router = "dodgr"
)
# Not providing any from or to points includes all nodes by default.
with_graph(net, graph_order()) # Our network has 701 nodes.
#> [1] 987
cost_matrix = st_network_cost(net)
dim(cost_matrix)
#> [1] 987 987