Dear David,

Currently, the way to do is to multiply the sPath object with a transition matrix containing the distances.

#Prepare a distance matrix
cstTransMetric <- transition(cst, transitionFunction=function(x){1}, directions=8)
cstDistMetric <- geoCorrection(cstTransMetric)
cstDistMetricM <- transitionMatrix(cstDistMetric)
cstDistMetricM[cstDistMetricM>0] <- 1/(cstDistMetricM[cstDistMetricM>0])
transitionMatrix(cstDistMetric) <- cstDistMetricM

#Multiply the sPath objects with the metric distance transition matrix to get the metric distance.

sum(transitionMatrix(sPath * cstDistMetric))


Then loop over the coordinates to get all the flow lengths.

I suspect this is far slower than in ArcGIS. No alternative method in R occurs to me.

Best,

Jacob.


________________________________
 From: "Orme, David" <d.orme@imperial.ac.uk>
To: "r-sig-geo@r-project.org" <r-sig-geo@r-project.org> 
Sent: Tuesday, 26 March 2013, 15:34
Subject: [R-sig-Geo] Calculating physical distance along accumulative cost distance paths

Dear list,

I'm using the raster and gdistance libraries to get an accumulated cost surface across a landscape away from some roads. I'd like to be able to get not only the cost distance for each cell but also the physical distance along which the cost accumulates for the cell. Obviously, shortestPath could be called for each cell in turn, but this seems hugely expensive.

I had an implementation in ArcGIS using Spatial Analyst tools - it fed a Cost_Distance backlink layer (giving the direction between cells from the cost distance) into the Flow_Length tool and I wondered if there was anything comparable I could do in R. It may very well be that computationally that was doing the same thing as shortestPath, but I can't find anything analogous to the backlink layer or flow length.

I've appended some data and code showing the basic setup - I'd like to know not only how costly each cell is to build a road to (which the layer cstDist gives me), but also the length of the blue line leading to it.

Many thanks,
David


library(raster)

# data
# two rasters - one a set of roads and a cost surface
rd <- cst  <- raster(xmn=526000, xmx=538000, ymn=2705000, ymx=2717000, ncol=48, nrow=48)

rdVals <- structure(list(lengths = c(16L, 1L, 24L, 1L, 5L, 1L, 15L, 2L,
11L, 2L, 11L, 2L, 3L, 2L, 14L, 3L, 3L, 12L, 10L, 1L, 3L, 1L,
14L, 2L, 1L, 5L, 21L, 1L, 1L, 3L, 13L, 2L, 4L, 2L, 22L, 3L, 14L,
2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, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0)), .Names = c("lengths", "values"
), class = "rle")

rd[] <- inverse.rle(rdVals)
rd[rd[] == 0] <- NA
cstVals <- c(205, 291, 289, 232, 158, 98, 85, 88, 93, 134, 241, 229, 169,
167, 222, 100, 149, 102, 68, 191, 114, 52, 119, 141, 118, 50,
12, 30, 45, 63, 100, 91, 55, 167, 166, 93, 82, 170, 197, 146,
159, 134, 32, 102, 188, 108, 184, 142, 269, 259, 120, 106, 119,
56, 29, 89, 153, 202, 110, 150, 132, 82, 166, 182, 85, 87, 158,
98, 86, 127, 64, 68, 159, 164, 122, 97, 42, 88, 32, 48, 52, 42,
173, 193, 191, 160, 99, 130, 89, 61, 154, 210, 255, 180, 205,
146, 293, 237, 44, 42, 111, 109, 73, 261, 233, 114, 89, 120,
102, 117, 166, 186, 143, 107, 142, 54, 149, 41, 138, 186, 155,
126, 77, 119, 78, 159, 194, 127, 32, 52, 25, 34, 170, 230, 93,
38, 32, 131, 137, 123, 151, 180, 141, 125, 269, 215, 65, 106,
256, 165, 123, 425, 185, 174, 182, 152, 151, 86, 114, 68, 86,
92, 129, 129, 151, 87, 68, 60, 184, 191, 149, 150, 152, 79, 89,
117, 87, 65, 32, 105, 137, 100, 153, 160, 165, 128, 61, 183,
147, 49, 174, 180, 182, 166, 124, 302, 117, 245, 203, 411, 133,
191, 181, 114, 190, 98, 79, 81, 74, 108, 93, 132, 134, 64, 138,
194, 169, 127, 137, 101, 65, 48, 21, 88, 96, 77, 77, 149, 61,
44, 104, 152, 163, 87, 224, 198, 114, 62, 141, 110, 133, 11,
163, 315, 122, 253, 235, 277, 227, 121, 114, 110, 199, 98, 73,
112, 129, 218, 166, 96, 132, 105, 68, 68, 121, 140, 110, 103,
99, 107, 106, 128, 135, 77, 89, 151, 146, 149, 139, 118, 40,
166, 203, 129, 114, 102, 142, 156, 251, 230, 304, 314, 183, 250,
268, 249, 254, 87, 101, 122, 193, 187, 184, 152, 123, 155, 195,
134, 57, 94, 114, 120, 105, 129, 137, 105, 143, 167, 60, 64,
112, 117, 93, 106, 153, 165, 125, 92, 170, 83, 148, 254, 136,
152, 182, 199, 182, 240, 532, 423, 190, 193, 226, 213, 142, 32,
71, 101, 164, 188, 179, 194, 159, 147, 159, 132, 134, 141, 120,
122, 145, 88, 28, 188, 364, 356, 159, 64, 24, 105, 93, 68, 109,
115, 72, 142, 176, 136, 138, 97, 256, 189, 186, 198, 250, 171,
596, 528, 128, 33, 62, 68, 106, 110, 88, 36, 164, 160, 87, 56,
83, 136, 180, 117, 28, 38, 44, 89, 115, 111, 133, 382, 383, 539,
190, 183, 161, 58, 64, 78, 67, 38, 62, 135, 151, 120, 63, 97,
85, 71, 123, 129, 434, 562, 653, 332, 97, 166, 187, 192, 146,
160, 127, 120, 23, 76, 184, 246, 258, 163, 32, 173, 95, 65, 89,
105, 102, 50, 93, 529, 266, 509, 120, 61, 114, 132, 123, 117,
121, 94, 30, 76, 64, 106, 169, 126, 154, 89, 100, 196, 256, 282,
199, 121, 168, 117, 87, 70, 82, 155, 165, 242, 161, 207, 288,
288, 299, 251, 139, 112, 208, 200, 151, 129, 114, 103, 123, 359,
432, 323, 112, 49, 36, 37, 94, 132, 160, 154, 105, 22, 94, 167,
171, 150, 127, 29, 103, 157, 64, 93, 194, 169, 93, 66, 97, 69,
34, 143, 192, 242, 230, 253, 315, 163, 228, 210, 239, 175, 36,
146, 189, 185, 156, 123, 93, 73, 174, 82, 74, 136, 126, 87, 37,
62, 57, 108, 134, 81, 81, 212, 171, 101, 77, 76, 110, 123, 148,
212, 150, 121, 107, 146, 122, 79, 207, 242, 197, 337, 386, 354,
268, 157, 248, 273, 333, 334, 260, 145, 54, 12, 79, 109, 85,
32, 65, 135, 85, 94, 95, 73, 48, 51, 23, 85, 135, 152, 47, 210,
183, 135, 165, 167, 139, 129, 136, 130, 101, 81, 111, 191, 225,
214, 296, 211, 197, 330, 293, 303, 195, 166, 170, 98, 146, 290,
281, 181, 97, 142, 156, 146, 154, 125, 12, 157, 122, 104, 109,
79, 43, 49, 72, 51, 80, 155, 110, 133, 187, 146, 183, 192, 175,
109, 103, 77, 45, 61, 56, 95, 137, 171, 217, 114, 114, 277, 158,
60, 112, 201, 195, 103, 74, 247, 335, 112, 103, 146, 98, 88,
134, 182, 105, 128, 54, 64, 49, 34, 31, 41, 57, 80, 103, 143,
77, 133, 145, 157, 160, 132, 86, 9, 102, 40, 54, 40, 98, 111,
69, 126, 170, 94, 142, 194, 125, 203, 279, 360, 296, 201, 263,
310, 221, 108, 132, 101, 60, 50, 69, 164, 141, 106, 53, 77, 24,
20, 36, 48, 20, 25, 52, 76, 85, 82, 43, 8, 52, 57, 38, 48, 190,
105, 200, 258, 259, 211, 177, 222, 238, 94, 156, 94, 21, 85,
155, 264, 147, 114, 185, 111, 68, 120, 71, 34, 29, 96, 163, 229,
230, 154, 88, 90, 69, 65, 77, 89, 48, 43, 48, 52, 120, 95, 77,
93, 74, 70, 66, 46, 247, 215, 162, 155, 161, 138, 160, 258, 336,
85, 97, 44, 101, 211, 292, 328, 242, 218, 158, 155, 169, 152,
127, 80, 58, 115, 86, 141, 154, 118, 52, 96, 80, 108, 73, 85,
81, 73, 52, 60, 108, 56, 46, 69, 48, 92, 108, 107, 241, 188,
138, 121, 141, 121, 90, 160, 295, 286, 260, 213, 138, 27, 152,
212, 163, 206, 142, 174, 177, 151, 133, 89, 32, 73, 58, 75, 104,
100, 69, 117, 45, 97, 40, 32, 86, 61, 42, 32, 37, 82, 24, 58,
86, 108, 34, 54, 208, 162, 103, 37, 52, 112, 123, 84, 295, 329,
297, 378, 275, 220, 192, 177, 125, 152, 117, 110, 76, 133, 190,
142, 83, 64, 40, 56, 128, 136, 110, 106, 40, 102, 84, 64, 83,
45, 0, 30, 41, 76, 40, 63, 95, 92, 67, 32, 201, 151, 143, 125,
113, 12, 137, 197, 12, 97, 188, 387, 410, 344, 286, 263, 233,
172, 63, 50, 163, 244, 213, 137, 148, 157, 123, 119, 167, 131,
81, 30, 36, 45, 87, 83, 56, 34, 16, 30, 60, 60, 11, 47, 88, 116,
102, 73, 161, 151, 204, 97, 89, 181, 194, 154, 183, 162, 58,
125, 169, 115, 143, 175, 160, 140, 157, 160, 171, 131, 20, 68,
28, 112, 155, 170, 138, 77, 52, 139, 145, 102, 20, 4, 14, 20,
31, 58, 52, 20, 8, 48, 96, 126, 88, 40, 175, 232, 178, 46, 141,
119, 27, 154, 147, 137, 100, 225, 204, 101, 64, 74, 135, 125,
79, 83, 98, 12, 121, 136, 151, 56, 37, 64, 40, 9, 107, 189, 137,
162, 92, 60, 45, 24, 31, 89, 76, 20, 20, 61, 65, 121, 185, 168,
197, 224, 146, 78, 47, 97, 76, 156, 198, 71, 131, 219, 156, 138,
105, 150, 180, 192, 215, 166, 93, 133, 155, 115, 153, 89, 52,
43, 8, 44, 213, 223, 135, 140, 61, 62, 81, 32, 32, 60, 130, 75,
38, 20, 45, 38, 85, 105, 186, 248, 222, 74, 110, 162, 43, 179,
110, 69, 139, 157, 46, 108, 58, 121, 136, 176, 190, 208, 212,
193, 85, 125, 137, 106, 77, 45, 12, 92, 284, 179, 199, 144, 57,
81, 99, 68, 69, 64, 97, 125, 77, 39, 40, 69, 76, 75, 134, 178,
246, 203, 178, 111, 43, 163, 139, 85, 55, 210, 274, 217, 172,
214, 168, 65, 113, 153, 98, 30, 117, 221, 206, 102, 45, 8, 73,
163, 286, 132, 204, 154, 120, 137, 101, 68, 77, 44, 43, 94, 102,
73, 68, 108, 123, 146, 197, 137, 182, 239, 367, 161, 166, 100,
70, 74, 162, 185, 245, 253, 212, 257, 343, 266, 25, 60, 107,
122, 207, 246, 97, 8, 28, 109, 139, 115, 247, 161, 195, 139,
54, 145, 81, 40, 41, 32, 24, 57, 83, 52, 93, 186, 173, 149, 184,
244, 162, 38, 207, 320, 152, 233, 222, 188, 150, 64, 43, 89,
77, 173, 260, 339, 341, 297, 282, 258, 221, 148, 8, 28, 146,
266, 217, 117, 255, 170, 161, 129, 108, 161, 111, 125, 113, 63,
30, 114, 78, 14, 149, 226, 133, 95, 295, 217, 158, 280, 305,
320, 193, 163, 179, 136, 40, 83, 100, 43, 18, 77, 24, 111, 223,
257, 240, 202, 143, 66, 14, 95, 265, 263, 200, 246, 293, 70,
94, 97, 187, 243, 111, 155, 142, 88, 96, 155, 45, 64, 200, 215,
115, 132, 207, 261, 147, 78, 45, 155, 293, 278, 142, 129, 140,
116, 140, 28, 179, 179, 215, 275, 248, 113, 49, 57, 20, 89, 148,
148, 239, 183, 116, 283, 252, 170, 98, 155, 173, 218, 196, 118,
125, 77, 123, 169, 23, 80, 196, 236, 137, 93, 59, 170, 325, 267,
45, 233, 297, 368, 194, 176, 79, 113, 161, 76, 322, 297, 282,
249, 304, 259, 148, 116, 85, 259, 262, 209, 108, 170, 231, 265,
184, 264, 192, 170, 138, 140, 166, 110, 199, 190, 175, 117, 12,
63, 180, 219, 175, 109, 93, 129, 238, 182, 306, 290, 368, 80,
146, 53, 156, 154, 54, 211, 334, 253, 104, 89, 240, 234, 188,
182, 185, 382, 234, 260, 188, 107, 269, 278, 98, 121, 34, 41,
77, 140, 206, 272, 251, 180, 120, 43, 36, 93, 172, 143, 136,
149, 180, 181, 167, 61, 38, 322, 296, 141, 66, 116, 239, 50,
238, 243, 262, 194, 103, 160, 186, 189, 248, 172, 219, 371, 272,
158, 257, 241, 194, 202, 329, 346, 244, 172, 221, 194, 251, 277,
183, 91, 54, 56, 88, 82, 100, 116, 94, 166, 160, 193, 140, 61,
111, 200, 215, 211, 263, 316, 290, 81, 337, 283, 280, 153, 157,
190, 155, 176, 264, 151, 174, 349, 381, 127, 119, 107, 207, 288,
339, 314, 283, 242, 307, 324, 229, 200, 166, 123, 37, 45, 118,
83, 61, 129, 131, 191, 272, 249, 86, 258, 262, 133, 60, 224,
359, 513, 546, 224, 332, 321, 159, 108, 214, 227, 177, 196, 205,
137, 89, 303, 394, 232, 142, 159, 132, 152, 195, 247, 312, 303,
307, 455, 388, 272, 202, 99, 36, 37, 76, 94, 98, 118, 87, 69,
161, 360, 66, 302, 231, 269, 184, 210, 224, 102, 477, 271, 158,
359, 369, 350, 337, 243, 202, 203, 131, 86, 35, 201, 331, 324,
112, 144, 254, 262, 258, 141, 78, 199, 356, 448, 269, 196, 112,
38, 21, 18, 30, 72, 75, 82, 16, 36, 29, 326, 66, 304, 88, 123,
260, 321, 434, 405, 324, 373, 219, 342, 411, 378, 294, 190, 184,
168, 68, 27, 45, 75, 168, 292, 281, 140, 165, 224, 293, 280,
122, 106, 309, 256, 70, 120, 90, 66, 53, 45, 38, 112, 142, 141,
70, 28, 10, 378, 217, 308, 360, 231, 56, 119, 297, 511, 523,
450, 244, 204, 162, 93, 97, 111, 152, 136, 45, 16, 28, 48, 84,
219, 282, 218, 184, 157, 208, 235, 208, 106, 144, 81, 68, 43,
40, 40, 38, 61, 112, 128, 89, 137, 106, 88, 136, 389, 376, 240,
334, 286, 209, 152, 192, 76, 121, 169, 115, 150, 227, 165, 113,
105, 121, 98, 21, 43, 5, 66, 132, 197, 166, 172, 293, 333, 320,
284, 250, 187, 46, 47, 64, 36, 29, 16, 16, 45, 124, 72, 65, 153,
114, 206, 105, 389, 414, 194, 244, 59, 187, 239, 339, 416, 380,
313, 258, 91, 94, 183, 151, 74, 80, 52, 32, 84, 74, 45, 214,
154, 81, 115, 128, 116, 82, 93, 250, 160, 91, 54, 106, 46, 16,
52, 43, 36, 105, 103, 126, 173, 145, 222, 46, 273, 479, 415,
252, 186, 154, 233, 272, 301, 309, 296, 256, 187, 111, 11, 98,
57, 60, 41, 45, 88, 72, 49, 207, 121, 115, 15, 118, 197, 243,
272, 188, 179, 112, 32, 54, 91, 50, 35, 35, 86, 107, 113, 144,
174, 152, 151, 46, 93, 269, 251, 220, 310, 33, 50, 32, 29, 192,
335, 250, 127, 111, 45, 65, 20, 66, 59, 59, 106, 68, 88, 118,
115, 167, 235, 244, 272, 202, 88, 301, 225, 101, 25, 21, 102,
92, 50, 50, 124, 79, 109, 214, 240, 218, 194, 100, 67, 107, 131,
258, 306, 170, 183, 163, 158, 172, 345, 292, 106, 108, 72, 86,
45, 78, 29, 11, 7, 127, 137, 51, 126, 223, 429, 219, 183, 201,
88, 139, 172, 131, 94, 39, 75, 60, 50, 126, 177, 104, 32, 270,
347, 273, 215, 136, 141, 115, 184, 222, 163, 129, 179, 253, 364,
458, 421, 249, 84, 122, 82, 96, 83, 40, 30, 20, 66, 170, 131,
35, 147, 290, 514, 115, 136, 231, 197, 123, 89, 83, 97, 103,
89, 48, 25, 155, 189, 172, 174, 487, 393, 213, 233, 203, 167,
109, 228, 167, 66, 18, 124, 237, 320, 355, 267, 167, 142, 76,
67, 57, 11, 57, 72, 45, 95, 165, 92, 85, 100, 339, 429, 197,
348, 242, 209, 181, 158, 112, 112, 129, 68, 48, 50, 146, 233,
186, 61, 501, 458, 102, 154, 138, 131, 33, 151, 218, 134, 43,
156, 251, 276, 132, 56, 35, 61, 206, 280, 271, 177, 117, 131,
144, 101, 179, 16, 131, 21, 315, 307, 146, 171, 242, 146, 276,
194, 171, 102, 108, 21, 76, 133, 300, 77, 227, 40, 377, 430,
404, 297, 186, 83, 55, 33, 149, 314, 243, 178, 208, 255, 97,
30, 33, 168, 274, 195, 212, 192, 83, 126, 111, 92, 151, 50, 84,
103, 217, 239, 77, 207, 61, 149, 267, 134, 82, 75, 64, 52, 22,
148, 374, 176, 299, 145, 159, 326, 371, 375, 283, 145, 138, 154,
121, 133, 298, 285, 254, 179, 50, 53, 25, 154, 165, 74, 107,
155, 66, 88, 10, 100, 126, 97, 125, 203, 189, 160, 113, 202,
165, 163, 145, 88, 47, 45, 62, 63, 29, 190, 448, 152, 238, 241,
77, 121, 86, 70)

cst[] <- cstVals

# one SpatialPoint
target <- SpatialPoints(matrix(c(532125, 2712125), ncol=2))

plot(cst, col=gray.colors(15))
plot(rd, add=TRUE)
plot(target, add=TRUE,col='red')

# transition object and accumulated cost
cstTrans <- transition(cst, transitionFunction=function(x){1/mean(x)}, directions=8)
cstTrans <- geoCorrection(cstTrans)
cstDist <- accCost(cstTrans, rasterToPoints(rd, spatial=TRUE))

# finding the cheapest path - the route along which the accCost calculated cost
cstDistBack <- accCost(cstTrans, target)
cstDistBack[is.na(rd[])] <- NA
plot(cstDistBack)
rdPoints <- rasterToPoints(cstDistBack, spatial=TRUE)
rdDest <- rdPoints[which.min(rdPoints@data$layer),]

# shortest path
sPath <- shortestPath(cstTrans, target, rdDest)

plot(cstDist)
plot(rd, add=TRUE,col='grey70', legend=FALSE)
plot(raster(sPath), add=TRUE, col='powderblue', legend=FALSE)
points(target, add=TRUE)
points(rdDest, add=TRUE)


    [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
	[[alternative HTML version deleted]]

