rnetwork-programmingdistancer-sfsfnetwork

Distance between points along path in R


I want to compute distance between nodes along a path (network).

I have two shapefiles.

One is a point layer representing train stations:

train_station  <- structure(list(geometry = structure(list(structure(c(1789781.1763, 
1036140.3792), class = c("XY", "POINT", "sfg")), structure(c(1391746.6168, 
-336142.520399998), class = c("XY", "POINT", "sfg")), structure(c(1322725.335, 
-446160.425799999), class = c("XY", "POINT", "sfg")), structure(c(1079349.8399, 
-389421.086099999), class = c("XY", "POINT", "sfg")), structure(c(993051.089600001, 
604054.897600002), class = c("XY", "POINT", "sfg")), structure(c(1734176.6467, 
803503.679300002), class = c("XY", "POINT", "sfg")), structure(c(1639866.0629, 
386133.173800001), class = c("XY", "POINT", "sfg")), structure(c(1658998.0956, 
593818.4364), class = c("XY", "POINT", "sfg")), structure(c(1436011.5057, 
750318.722999998), class = c("XY", "POINT", "sfg")), structure(c(1616310.3521, 
-137545.4903), class = c("XY", "POINT", "sfg")), structure(c(1907821.16828945, 
846114.131303993), class = c("XY", "POINT", "sfg")), structure(c(960998.947599998, 
136060.9786), class = c("XY", "POINT", "sfg")), structure(c(1433751.8485, 
-368676.099499998), class = c("XY", "POINT", "sfg")), structure(c(1016693.4823, 
-307540.433600002), class = c("XY", "POINT", "sfg")), structure(c(1178486.7964, 
535962.089599999), class = c("XY", "POINT", "sfg")), structure(c(1805904.0874, 
608223.609200002), class = c("XY", "POINT", "sfg")), structure(c(1027750.5026, 
-503417.115600001), class = c("XY", "POINT", "sfg")), structure(c(756089.969799999, 
-277720.935699999), class = c("XY", "POINT", "sfg")), structure(c(1783381.519, 
580457.243600001), class = c("XY", "POINT", "sfg")), structure(c(1638164.0169, 
-179512.823000001), class = c("XY", "POINT", "sfg")), structure(c(1834362.883, 
693158.698699999), class = c("XY", "POINT", "sfg")), structure(c(923737.611100001, 
-522394.597200001), class = c("XY", "POINT", "sfg")), structure(c(564090.517499997, 
-564609.5142), class = c("XY", "POINT", "sfg")), structure(c(1700622.4846, 
461411.774400001), class = c("XY", "POINT", "sfg")), structure(c(1884814.7315, 
844470.2333), class = c("XY", "POINT", "sfg")), structure(c(1035880.12193273, 
421102.600903689), class = c("XY", "POINT", "sfg")), structure(c(981168.882900001, 
553358.567199998), class = c("XY", "POINT", "sfg")), structure(c(1630589.1227, 
96992.7936000023), class = c("XY", "POINT", "sfg")), structure(c(1469206.5633, 
-382122.280600001), class = c("XY", "POINT", "sfg")), structure(c(1074513.95508099, 
495379.397144308), class = c("XY", "POINT", "sfg")), structure(c(1699926.7821, 
84351.6904), class = c("XY", "POINT", "sfg")), structure(c(882981.990499998, 
157627.906599999), class = c("XY", "POINT", "sfg")), structure(c(1595222.2689, 
789366.362099998), class = c("XY", "POINT", "sfg")), structure(c(1908684.48408199, 
961557.436709222), class = c("XY", "POINT", "sfg")), structure(c(855655.5085, 
583468.712600001), class = c("XY", "POINT", "sfg")), structure(c(1610618.85711065, 
834001.601766967), class = c("XY", "POINT", "sfg")), structure(c(1566028.408, 
769176.095699999), class = c("XY", "POINT", "sfg")), structure(c(651005.479500001, 
516407.979799999), class = c("XY", "POINT", "sfg")), structure(c(1960849.6649, 
695124.517400001), class = c("XY", "POINT", "sfg")), structure(c(1629478.9974, 
133719.900100002), class = c("XY", "POINT", "sfg")), structure(c(866660.977081273, 
327406.378158199), class = c("XY", "POINT", "sfg")), structure(c(1888541.8058, 
965703.314599999), class = c("XY", "POINT", "sfg")), structure(c(818778.823500002, 
-284285.364000002), class = c("XY", "POINT", "sfg")), structure(c(1819753.2004, 
576574.899499998), class = c("XY", "POINT", "sfg")), structure(c(1034127.7749, 
-500619.4664), class = c("XY", "POINT", "sfg")), structure(c(1025609.63045605, 
561660.937523897), class = c("XY", "POINT", "sfg")), structure(c(1636066.2798, 
46453.162800001), class = c("XY", "POINT", "sfg")), structure(c(998866.904199997, 
314104.5522), class = c("XY", "POINT", "sfg")), structure(c(869039.779816974, 
274039.376250103), class = c("XY", "POINT", "sfg")), structure(c(1997476.0478, 
758688.154200002), class = c("XY", "POINT", "sfg")), structure(c(895392.7311, 
286313.070300001), class = c("XY", "POINT", "sfg")), structure(c(1626409.4785, 
115297.873900001), class = c("XY", "POINT", "sfg")), structure(c(861712.417868644, 
236019.781593506), class = c("XY", "POINT", "sfg")), structure(c(1790018.5253, 
1038861.7706), class = c("XY", "POINT", "sfg")), structure(c(2041967.16, 
1032950.1021), class = c("XY", "POINT", "sfg")), structure(c(984555.844500002, 
241438.559699999), class = c("XY", "POINT", "sfg")), structure(c(973641.5209, 
130616.517499998), class = c("XY", "POINT", "sfg")), structure(c(1599820.5337, 
503161.733600002), class = c("XY", "POINT", "sfg")), structure(c(934106.738068492, 
151012.678032866), class = c("XY", "POINT", "sfg")), structure(c(1066256.3405, 
478270.1672), class = c("XY", "POINT", "sfg")), structure(c(998786.0674, 
248416.298899999), class = c("XY", "POINT", "sfg")), structure(c(898954.390099998, 
585362.516200001), class = c("XY", "POINT", "sfg")), structure(c(1089219.3091, 
384944.430199998), class = c("XY", "POINT", "sfg")), structure(c(879570.594300003, 
140318.559200001), class = c("XY", "POINT", "sfg")), structure(c(1967917.7789, 
734179.189899999), class = c("XY", "POINT", "sfg")), structure(c(1163775.6374, 
-437352.831), class = c("XY", "POINT", "sfg")), structure(c(804352.802499998, 
325736.7382), class = c("XY", "POINT", "sfg")), structure(c(1803875.4943, 
569225.790599999), class = c("XY", "POINT", "sfg")), structure(c(1128963.9914, 
-744183.866400001), class = c("XY", "POINT", "sfg")), structure(c(1770957.5691, 
805746.836800001), class = c("XY", "POINT", "sfg")), structure(c(988506.472554391, 
-306585.319089635), class = c("XY", "POINT", "sfg")), structure(c(1623615.0261, 
65123.377399998), class = c("XY", "POINT", "sfg")), structure(c(1813966.00682923, 
747354.132971282), class = c("XY", "POINT", "sfg")), structure(c(1810188.2763, 
599796.673199999), class = c("XY", "POINT", "sfg")), structure(c(1730950.3197, 
481928.7466), class = c("XY", "POINT", "sfg")), structure(c(1677962.4344, 
401142.464600001), class = c("XY", "POINT", "sfg")), structure(c(1499813.8947, 
377704.453299999), class = c("XY", "POINT", "sfg")), structure(c(1018653.1621, 
532501.8235), class = c("XY", "POINT", "sfg")), structure(c(1073041.347, 
-351619.053599999), class = c("XY", "POINT", "sfg")), structure(c(845590.0237, 
268438.064900001), class = c("XY", "POINT", "sfg")), structure(c(1659137.2895, 
636686.545600002), class = c("XY", "POINT", "sfg")), structure(c(1112088.3617, 
-363777.257399998), class = c("XY", "POINT", "sfg")), structure(c(1648355.6038, 
817969.682499998), class = c("XY", "POINT", "sfg")), structure(c(1577769.1144, 
29475.7759999994), class = c("XY", "POINT", "sfg")), structure(c(1833188.4238, 
624230.7403), class = c("XY", "POINT", "sfg")), structure(c(1248577.0722, 
-421305.326399999), class = c("XY", "POINT", "sfg")), structure(c(1401770.9769, 
-250586.986099999), class = c("XY", "POINT", "sfg")), structure(c(1138454.93786688, 
-468998.793998615), class = c("XY", "POINT", "sfg")), structure(c(1278419.3896, 
-360659.6984), class = c("XY", "POINT", "sfg")), structure(c(1805730.9206, 
948829.385000001), class = c("XY", "POINT", "sfg")), structure(c(470851.421300002, 
-606116.116699999), class = c("XY", "POINT", "sfg")), structure(c(1645230.84874413, 
530230.950080516), class = c("XY", "POINT", "sfg")), structure(c(1999545.8601, 
700401.906199998), class = c("XY", "POINT", "sfg")), structure(c(1994419.6808, 
881676.706700002), class = c("XY", "POINT", "sfg")), structure(c(2034741.5997, 
996244.286199999), class = c("XY", "POINT", "sfg")), structure(c(942471.565243789, 
141421.790646283), class = c("XY", "POINT", "sfg")), structure(c(1543009.8395, 
213848.643900001), class = c("XY", "POINT", "sfg")), structure(c(1798326.7849, 
868541.1359), class = c("XY", "POINT", "sfg")), structure(c(1782057.5672, 
511232.749300001), class = c("XY", "POINT", "sfg")), structure(c(1885808.6044, 
892449.070900001), class = c("XY", "POINT", "sfg")), structure(c(1073760.3311, 
-383907.573599999), class = c("XY", "POINT", "sfg")), structure(c(993137.776299999, 
-471923.767000002), class = c("XY", "POINT", "sfg")), structure(c(1989063.7363, 
814352.786499998), class = c("XY", "POINT", "sfg")), structure(c(1828529.27269656, 
580189.513793761), class = c("XY", "POINT", "sfg")), structure(c(1100803.69046699, 
507733.762471826), class = c("XY", "POINT", "sfg")), structure(c(1812955.381, 
615110.873300001), class = c("XY", "POINT", "sfg")), structure(c(1024713.23007655, 
388992.310031539), class = c("XY", "POINT", "sfg")), structure(c(1604067.1606, 
369553.743400002), class = c("XY", "POINT", "sfg")), structure(c(911925.112100003, 
149712.111800001), class = c("XY", "POINT", "sfg")), structure(c(1116396.6834, 
-428339.4431), class = c("XY", "POINT", "sfg")), structure(c(1841484.2858, 
985101.774399999), class = c("XY", "POINT", "sfg")), structure(c(1872455.65309627, 
650970.743640346), class = c("XY", "POINT", "sfg")), structure(c(1431706.181, 
734441.091800002), class = c("XY", "POINT", "sfg")), structure(c(1839101.1539, 
656239.636300001), class = c("XY", "POINT", "sfg")), structure(c(612590.528283355, 
-837105.972100253), class = c("XY", "POINT", "sfg")), structure(c(1618102.33490128, 
314462.004229404), class = c("XY", "POINT", "sfg")), structure(c(1726754.4763, 
100139.048), class = c("XY", "POINT", "sfg")), structure(c(1780180.6206, 
478468.505999999), class = c("XY", "POINT", "sfg")), structure(c(1228225.1302, 
-367002.7696), class = c("XY", "POINT", "sfg")), structure(c(2217986.8999, 
1142426.3355), class = c("XY", "POINT", "sfg")), structure(c(1151631.7614, 
504259.408000001), class = c("XY", "POINT", "sfg")), structure(c(1716894.2736, 
662331.7544), class = c("XY", "POINT", "sfg")), structure(c(1957983.3795, 
849471.493), class = c("XY", "POINT", "sfg")), structure(c(1746336.26448134, 
1033512.35489174), class = c("XY", "POINT", "sfg")), structure(c(1223274.6366, 
-424578.403699998), class = c("XY", "POINT", "sfg")), structure(c(1820464.5335, 
860400.633200001), class = c("XY", "POINT", "sfg")), structure(c(1555352.8218, 
2912.51070000022), class = c("XY", "POINT", "sfg")), structure(c(960780.9047, 
-508719.597599999), class = c("XY", "POINT", "sfg")), structure(c(946610.482799998, 
590215.862699999), class = c("XY", "POINT", "sfg")), structure(c(567759.611254325, 
-829204.667814092), class = c("XY", "POINT", "sfg")), structure(c(1960650.7174, 
791147.8432), class = c("XY", "POINT", "sfg")), structure(c(1893056.7153, 
776336.4789), class = c("XY", "POINT", "sfg")), structure(c(1900874.8566, 
754289.0816), class = c("XY", "POINT", "sfg")), structure(c(1598765.4617, 
231836.6241), class = c("XY", "POINT", "sfg")), structure(c(2006518.762, 
723504.510999998), class = c("XY", "POINT", "sfg")), structure(c(908110.184199998, 
195449.619699999), class = c("XY", "POINT", "sfg")), structure(c(545230.320099998, 
-249761.893900001), class = c("XY", "POINT", "sfg")), structure(c(999343.6047, 
-247178.867400001), class = c("XY", "POINT", "sfg")), structure(c(1099170.1005, 
418023.039000002), class = c("XY", "POINT", "sfg")), structure(c(1305239.7079, 
-343421.149900001), class = c("XY", "POINT", "sfg")), structure(c(650557.271, 
646918.953700005), class = c("XY", "POINT", "sfg")), structure(c(1619676.7959, 
435909.627799999), class = c("XY", "POINT", "sfg")), structure(c(1695963.34226125, 
643214.755339189), class = c("XY", "POINT", "sfg")), structure(c(984208.947500003, 
131386.471500001), class = c("XY", "POINT", "sfg")), structure(c(1985925.1003, 
1037991.4792), class = c("XY", "POINT", "sfg")), structure(c(2023382.56853514, 
930922.210793702), class = c("XY", "POINT", "sfg")), structure(c(1471894.1407, 
774097.181700001), class = c("XY", "POINT", "sfg")), structure(c(1347594.454, 
-357539.865699999), class = c("XY", "POINT", "sfg")), structure(c(1543763.2988, 
420306.6254), class = c("XY", "POINT", "sfg")), structure(c(1611955.8166, 
306115.1928), class = c("XY", "POINT", "sfg")), structure(c(1544309.535, 
388919.197700001), class = c("XY", "POINT", "sfg")), structure(c(1390372.8475, 
761357.111199998), class = c("XY", "POINT", "sfg")), structure(c(1584311.05454826, 
665074.691827748), class = c("XY", "POINT", "sfg")), structure(c(1522729.0909, 
672948.589299999), class = c("XY", "POINT", "sfg")), structure(c(1542964.8777, 
761158.829399998), class = c("XY", "POINT", "sfg")), structure(c(537497.822500002, 
271944.561799998), class = c("XY", "POINT", "sfg")), structure(c(1616256.9959, 
145293.7212), class = c("XY", "POINT", "sfg")), structure(c(473231.9107, 
-729721.89), class = c("XY", "POINT", "sfg")), structure(c(629263.752103444, 
642363.772700004), class = c("XY", "POINT", "sfg")), structure(c(1680092.2082, 
562000.364499999), class = c("XY", "POINT", "sfg")), structure(c(1562643.80058843, 
679527.883898191), class = c("XY", "POINT", "sfg")), structure(c(1625483.6108, 
17111.3160999992), class = c("XY", "POINT", "sfg")), structure(c(1995167.2227, 
916232.9009), class = c("XY", "POINT", "sfg")), structure(c(1666820.1453, 
70379.0672999998), class = c("XY", "POINT", "sfg")), structure(c(1541292.2527, 
349588.731799999), class = c("XY", "POINT", "sfg")), structure(c(1389403.6891, 
729083.701399999), class = c("XY", "POINT", "sfg")), structure(c(1780416.6184, 
544776.896400002), class = c("XY", "POINT", "sfg")), structure(c(1675710.0235, 
809983.864399998), class = c("XY", "POINT", "sfg")), structure(c(2010910.2191, 
787555.921100002), class = c("XY", "POINT", "sfg")), structure(c(1003821.421, 
-460569.837399999), class = c("XY", "POINT", "sfg")), structure(c(1025718.6805, 
310928.2526), class = c("XY", "POINT", "sfg")), structure(c(793397.321999999, 
524264.528100002), class = c("XY", "POINT", "sfg")), structure(c(1294529.7825, 
-428665.507), class = c("XY", "POINT", "sfg")), structure(c(1011797.2727, 
279212.306499999), class = c("XY", "POINT", "sfg")), structure(c(1064903.8635, 
339790.801799999), class = c("XY", "POINT", "sfg")), structure(c(1106067.5049, 
444235.942399999), class = c("XY", "POINT", "sfg")), structure(c(1761553.2509, 
464449.628800001), class = c("XY", "POINT", "sfg")), structure(c(831095.813000002, 
567346.9711), class = c("XY", "POINT", "sfg")), structure(c(1526156.3261, 
338609.567999999), class = c("XY", "POINT", "sfg")), structure(c(1128382.6857, 
-357409.5074), class = c("XY", "POINT", "sfg")), structure(c(1658503.6448, 
457990.253699999), class = c("XY", "POINT", "sfg")), structure(c(835516.924899999, 
299153.621800002), class = c("XY", "POINT", "sfg")), structure(c(1444755.1735, 
373041.1936), class = c("XY", "POINT", "sfg")), structure(c(1005275.0092, 
-280959.527499998), class = c("XY", "POINT", "sfg")), structure(c(1257096.1785, 
-365924.182900002), class = c("XY", "POINT", "sfg")), structure(c(1146434.8938, 
-461488.626099999), class = c("XY", "POINT", "sfg")), structure(c(1750455.0931, 
473918.838300003), class = c("XY", "POINT", "sfg")), structure(c(894718.646100001, 
133168.601699999), class = c("XY", "POINT", "sfg")), structure(c(1743454.35742989, 
480830.937148652), class = c("XY", "POINT", "sfg")), structure(c(1632557.86696763, 
800462.744195027), class = c("XY", "POINT", "sfg")), structure(c(1177594.1082, 
-429615.064999999), class = c("XY", "POINT", "sfg")), structure(c(1096697.2498, 
351571.341499999), class = c("XY", "POINT", "sfg")), structure(c(1392984.3083, 
-496803.115899999), class = c("XY", "POINT", "sfg")), structure(c(2080548.7143, 
758087.2936), class = c("XY", "POINT", "sfg")), structure(c(856622.217400001, 
317301.079099999), class = c("XY", "POINT", "sfg")), structure(c(1854105.3876, 
581628.545199998), class = c("XY", "POINT", "sfg")), structure(c(1828206.3214, 
904134.952500002), class = c("XY", "POINT", "sfg")), structure(c(1124568.5754, 
447575.433599998), class = c("XY", "POINT", "sfg")), structure(c(1027591.1932, 
463335.880300002), class = c("XY", "POINT", "sfg")), structure(c(2050386.0674, 
1059016.3263), class = c("XY", "POINT", "sfg")), structure(c(1735585.576, 
634895.285399998), class = c("XY", "POINT", "sfg")), structure(c(1570045.3804, 
457602.430699999), class = c("XY", "POINT", "sfg")), structure(c(1642216.2503, 
346153.026099999), class = c("XY", "POINT", "sfg")), structure(c(753384.593800002, 
507045.450999998), class = c("XY", "POINT", "sfg")), structure(c(480371.110800001, 
-563546.490000001), class = c("XY", "POINT", "sfg")), structure(c(1030843.5418, 
338485.449600002), class = c("XY", "POINT", "sfg")), structure(c(673191.9584, 
520420.109900001), class = c("XY", "POINT", "sfg")), structure(c(1517639.6239, 
332176.142100001), class = c("XY", "POINT", "sfg")), structure(c(1804358.993, 
532723.2214), class = c("XY", "POINT", "sfg")), structure(c(1032187.0539, 
644794.792999998), class = c("XY", "POINT", "sfg")), structure(c(972384.8171, 
-215605.375800001), class = c("XY", "POINT", "sfg")), structure(c(1780909.3018, 
834574.150900002), class = c("XY", "POINT", "sfg")), structure(c(578469.682200001, 
-822148.777399999), class = c("XY", "POINT", "sfg")), structure(c(1474034.67459479, 
465691.495992469), class = c("XY", "POINT", "sfg")), structure(c(1039176.1445835, 
607241.045322913), class = c("XY", "POINT", "sfg")), structure(c(1885507.8579, 
998998.0097), class = c("XY", "POINT", "sfg")), structure(c(1789391.1182, 
791891.4095), class = c("XY", "POINT", "sfg")), structure(c(1701448.2294, 
84874.3374999997), class = c("XY", "POINT", "sfg")), structure(c(1605842.9825, 
186320.493899998), class = c("XY", "POINT", "sfg")), structure(c(2048486.2266, 
777798.713500002), class = c("XY", "POINT", "sfg")), structure(c(1731680.5376, 
459116.4179), class = c("XY", "POINT", "sfg")), structure(c(521833.845528018, 
-569729.059843382), class = c("XY", "POINT", "sfg")), structure(c(1952075.4121, 
928600.9217), class = c("XY", "POINT", "sfg")), structure(c(889111.603600001, 
216757.635399999), class = c("XY", "POINT", "sfg")), structure(c(886747.176648416, 
306473.714774303), class = c("XY", "POINT", "sfg")), structure(c(1176100.0458, 
-325730.1747), class = c("XY", "POINT", "sfg")), structure(c(1551389.36347924, 
631781.007679351), class = c("XY", "POINT", "sfg")), structure(c(1611213.4776, 
-82813.9830000001), class = c("XY", "POINT", "sfg")), structure(c(1692598.7692, 
413119.2307), class = c("XY", "POINT", "sfg")), structure(c(902958.354100001, 
549047.6364), class = c("XY", "POINT", "sfg")), structure(c(467773.096099997, 
266058.299900001), class = c("XY", "POINT", "sfg")), structure(c(1889275.5985, 
803761.574100002), class = c("XY", "POINT", "sfg")), structure(c(1119523.2895, 
-720186.795099999), class = c("XY", "POINT", "sfg")), structure(c(1835954.7291, 
570461.4288), class = c("XY", "POINT", "sfg")), structure(c(812547.9589, 
544457.095500001), class = c("XY", "POINT", "sfg")), structure(c(1604210.07677205, 
689493.753423717), class = c("XY", "POINT", "sfg")), structure(c(1042990.6347, 
-331380.076800001), class = c("XY", "POINT", "sfg")), structure(c(2031759.2573, 
746124.3876), class = c("XY", "POINT", "sfg")), structure(c(1624215.3325, 
323567.822300001), class = c("XY", "POINT", "sfg")), structure(c(1556459.4506, 
-11818.2521999993), class = c("XY", "POINT", "sfg")), structure(c(2020141.6722, 
814086.588900001), class = c("XY", "POINT", "sfg")), structure(c(2027810.5336, 
736634.262199998), class = c("XY", "POINT", "sfg")), structure(c(1528253.4905, 
366082.190399999), class = c("XY", "POINT", "sfg")), structure(c(1090213.7556, 
-418539.265899999), class = c("XY", "POINT", "sfg")), structure(c(1621912.85552144, 
319840.103813376), class = c("XY", "POINT", "sfg")), structure(c(1642812.2925, 
690498.859999999), class = c("XY", "POINT", "sfg")), structure(c(1674831.4303, 
503373.422499999), class = c("XY", "POINT", "sfg")), structure(c(1578562.4166, 
364839.323799998), class = c("XY", "POINT", "sfg")), structure(c(1156521.8825, 
-320510.431800001), class = c("XY", "POINT", "sfg")), structure(c(1204799.9099, 
-358305.7815), class = c("XY", "POINT", "sfg")), structure(c(1454309.8128531, 
462381.056761613), class = c("XY", "POINT", "sfg")), structure(c(1558456.8272, 
206220.816299999), class = c("XY", "POINT", "sfg")), structure(c(1513515.448, 
199356.796299999), class = c("XY", "POINT", "sfg")), structure(c(1783053.9270906, 
641255.516354831), class = c("XY", "POINT", "sfg")), structure(c(1868814.2268, 
708305.9343), class = c("XY", "POINT", "sfg")), structure(c(937966.102909218, 
548566.044736806), class = c("XY", "POINT", "sfg")), structure(c(1946168.96182872, 
894702.597999546), class = c("XY", "POINT", "sfg")), structure(c(1546263.4495, 
-49104.7032999992), class = c("XY", "POINT", "sfg")), structure(c(1412731.0105, 
-290732.042800002), class = c("XY", "POINT", "sfg")), structure(c(1362323.25595647, 
-472487.633757772), class = c("XY", "POINT", "sfg")), structure(c(1379399.6962, 
-290687.833899998), class = c("XY", "POINT", "sfg")), structure(c(1189226.4256, 
-440124.0509), class = c("XY", "POINT", "sfg")), structure(c(1609123.6021, 
217022.231300002), class = c("XY", "POINT", "sfg")), structure(c(997725.389400001, 
123633.202900002), class = c("XY", "POINT", "sfg")), structure(c(490642.518500002, 
265611.677699999), class = c("XY", "POINT", "sfg")), structure(c(1298700.6251, 
624442.950899999), class = c("XY", "POINT", "sfg")), structure(c(1854450.9747, 
771045.507600001), class = c("XY", "POINT", "sfg")), structure(c(1149758.6095, 
-361867.804600001), class = c("XY", "POINT", "sfg")), structure(c(1692352.9258, 
684045.885900002), class = c("XY", "POINT", "sfg")), structure(c(446836.061299999, 
-704049.154700001), class = c("XY", "POINT", "sfg")), structure(c(1030763.1373, 
362297.725700002), class = c("XY", "POINT", "sfg")), structure(c(2011106.79627347, 
840602.312082898), class = c("XY", "POINT", "sfg")), structure(c(1178871.3929, 
-358536.9839), class = c("XY", "POINT", "sfg")), structure(c(1933137.1413, 
729399.1535), class = c("XY", "POINT", "sfg")), structure(c(1904448.5576, 
712430.2196), class = c("XY", "POINT", "sfg")), structure(c(1698244.2777, 
1017001.7193), class = c("XY", "POINT", "sfg")), structure(c(1715093.4439, 
419159.028500003), class = c("XY", "POINT", "sfg")), structure(c(791936.414500003, 
-281775.441499999), class = c("XY", "POINT", "sfg")), structure(c(1469034.7874, 
362440.8562), class = c("XY", "POINT", "sfg")), structure(c(1510448.0638, 
752553.4029), class = c("XY", "POINT", "sfg")), structure(c(1617260.1264, 
-22335.4002999989), class = c("XY", "POINT", "sfg")), structure(c(2127061.2667, 
1138885.6246), class = c("XY", "POINT", "sfg")), structure(c(1601142.7073, 
540526.077499999), class = c("XY", "POINT", "sfg")), structure(c(633774.090999999, 
528773.567700001), class = c("XY", "POINT", "sfg")), structure(c(1318174.9024, 
454580.947899997), class = c("XY", "POINT", "sfg")), structure(c(1800022.6548, 
800777.741900004), class = c("XY", "POINT", "sfg")), structure(c(1048518.9087, 
452624.671000001), class = c("XY", "POINT", "sfg")), structure(c(1801907.597, 
999297.425599998), class = c("XY", "POINT", "sfg")), structure(c(1900273.0038, 
667389.122000001), class = c("XY", "POINT", "sfg")), structure(c(1583201.8535056, 
715399.423985055), class = c("XY", "POINT", "sfg")), structure(c(1599638.446, 
255660.950200001), class = c("XY", "POINT", "sfg")), structure(c(1146638.1434, 
-452571.1202), class = c("XY", "POINT", "sfg")), structure(c(1100782.03603612, 
488119.673401054), class = c("XY", "POINT", "sfg")), structure(c(1339167.3409, 
443698.337899999), class = c("XY", "POINT", "sfg")), structure(c(1655378.2789, 
661337.0171), class = c("XY", "POINT", "sfg")), structure(c(852903.483203445, 
168635.188100001), class = c("XY", "POINT", "sfg")), structure(c(988335.854099999, 
-238991.9709), class = c("XY", "POINT", "sfg")), structure(c(1615346.7855, 
163005.7352), class = c("XY", "POINT", "sfg")), structure(c(440431.433800001, 
-732425.392000001), class = c("XY", "POINT", "sfg")), structure(c(1917368.1319, 
611474.272399999), class = c("XY", "POINT", "sfg")), structure(c(1879591.0405, 
914739.573199999), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT", 
"sfc"), precision = 0, bbox = structure(c(xmin = 440431.433800001, 
ymin = -837105.972100253, xmax = 2217986.8999, ymax = 1142426.3355
), class = "bbox"), crs = structure(list(input = NA_character_, 
    wkt = "PROJCS[\"USA_Contiguous_Albers_Equal_Area_Conic\",\n    GEOGCS[\"GCS_North_American_1983\",\n        DATUM[\"North_American_Datum_1983\",\n            SPHEROID[\"GRS_1980\",6378137.0,298.257222101]],\n        PRIMEM[\"Greenwich\",0.0],\n        UNIT[\"Degree\",0.0174532925199433]],\n    PROJECTION[\"Albers_Conic_Equal_Area\"],\n    PARAMETER[\"False_Easting\",0.0],\n    PARAMETER[\"False_Northing\",0.0],\n    PARAMETER[\"longitude_of_center\",-96.0],\n    PARAMETER[\"Standard_Parallel_1\",29.5],\n    PARAMETER[\"Standard_Parallel_2\",45.5],\n    PARAMETER[\"latitude_of_center\",37.5],\n    UNIT[\"Meter\",1.0]]"), class = "crs"), n_empty = 0L), 
    station = 1:301), row.names = c(NA, -301L), class = c("sf", 
"data.frame"), sf_column = "geometry", agr = structure(c(station = NA_integer_), .Label = c("constant", 
"aggregate", "identity"), class = "factor"))

And the other one is a multiline layer representing railroads:

Simple feature collection with 3818 features and 8 fields
Geometry type: LINESTRING
Dimension:     XY
Bounding box:  xmin: 439362.5 ymin: -837106 xmax: 2223894 ymax: 1144527
CRS:           102003
First 10 features:
       track VxCount Gauge             RRname InOpBy ExactDate FIDAll Edited                       geometry
1  0.0921356       4  51.0  Delaware & Hudson   1830      1829      0      0 LINESTRING (1703095 643192....
2  0.6290380       5  51.0  Delaware & Hudson   1830      1829      1      0 LINESTRING (1703228 643134....
3  0.0368666       2  56.5 Baltimore and Ohio   1830      1830      2      0 LINESTRING (1644112 371237,...
4  0.1132930       2  56.5 Baltimore and Ohio   1830      1830      3      0 LINESTRING (1644112 371237,...
5  0.2250440       4  60.0 Charleston Hamburg   1830      1830     11      0 LINESTRING (1482645 -394401...
6  0.1246240       2  60.0 Charleston Hamburg   1830      1830     12      0 LINESTRING (1482960 -394567...
7  0.9672450       9  60.0 Charleston Hamburg   1830      1830     13      0 LINESTRING (1483158 -394600...
8  1.5411200      12  60.0 Charleston Hamburg   1830      1830     14      0 LINESTRING (1484580 -395087...
9  0.2888170       4  60.0 Charleston Hamburg   1830      1830     15      0 LINESTRING (1486418 -396682...
10 0.0888595       2  60.0 Charleston Hamburg   1830      1830     16      0 LINESTRING (1486523 -397130...

enter image description here

I would like to know how to merge these nodes (train stations) to these edges (railroads) in order to create a network, and then compute all the distance of nodes that are in the same network.

I tried unsuccessfully this (from this answer) but it created more nodes than I want

library(sfnetworks)

network <- railroad_1850 %>% as_sfnetwork()

nodes <- train_station %>% as_sfnetwork()

new_network <- network %>% 
  st_network_blend(., nodes, tolerance = 10) 

Note that my path is not composed of straight lines.


Solution

  • I'm not 100% sure of what your desired output is, but check the following code which might achieve your goal.

    First, load packages

    library(sf)
    #> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 7.2.1; sf_use_s2() is TRUE
    library(tidygraph)
    #> 
    #> Attaching package: 'tidygraph'
    #> The following object is masked from 'package:stats':
    #> 
    #>     filter
    library(sfnetworks)
    

    Load data

    rail <- st_read("railroad_station/rail_data/counties1850_Atack_1850.shp", quiet = TRUE)
    station <- st_read("railroad_station/train_station/station_1850.shp", quiet = TRUE)
    

    Convert rail data to sfnetwork class. Are you sure that the segments are defined in the proper order? Is it safe to assume that we need to define a directed network?

    rail_sfn <- as_sfnetwork(rail)
    

    Plot

    par(mar = rep(0, 4))
    plot(rail_sfn, cex = 0.3)
    plot(st_geometry(station), add = TRUE, col = sf.colors(301, categorical = TRUE), lwd = 2)
    

    Looking at the plot, I would say that the “station” data represent a subset of the nodes that already belong to the sfn object. Anyway, we can merge the two objects:

    joint_sfn <- st_network_blend(rail_sfn, st_geometry(station))
    #> Warning: st_network_blend assumes attributes are constant over geometries
    

    and compute distances between all nodes

    all_distances <- st_network_cost(joint_sfn)
    all_distances[1:5, 1:5]
    #> Units: [m]
    #>      [,1]     [,2]     [,3] [,4]     [,5]
    #> [1,]    0 148.2781 1160.619  Inf      Inf
    #> [2,]  Inf   0.0000 1012.341  Inf      Inf
    #> [3,]  Inf      Inf    0.000  Inf      Inf
    #> [4,]  Inf      Inf      Inf    0 59.33121
    #> [5,]  Inf      Inf      Inf  Inf  0.00000
    

    Inf means that it is not possible to reach node i from node j and the Matrix is not symmetric since we created a directed network.

    Created on 2023-01-13 with reprex v2.0.2

    Edit

    The following code can be used to get the matrix of distances across the stations:

    station_distances <- st_network_cost(
      x = joint_sfn,
      from = station,
      to = station
    )
    dim(station_distances)
    #> [1] 301 301