Skip to content
Prev 25308 / 29559 Next

Distance calculating between points according to a river network

Tristan,

if I understand you right you just want to calculate the distances. So 
no hydro stuff like accumulation flow directions etc.? As well as no 
diffusion modeling of the pollutants.

Most of the traditional vector based GIS stuff is packed into the rgeos 
package. So you might calculated the distances along a graph like in the 
following paste and copy snippet.

cheers Chris



library(rgeos)
library(sp)
# just for visualisation
r <- raster(nrows=6, ncols=7, xmn=0, xmx=7, ymn=0, ymx=6, crs="+proj=utm 
+units=m")
r[] <- c(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, 1, 0,
          1, 0, 1, 0, 1, 0, 1,
          0, 1, 0, 1, 0, 1, 0)
# river line
line 
<-SpatialLines(list(Lines(Line(cbind(c(5.5,1.5,4.5),c(1.5,5.5,5.5))), 
ID="line")))
# points
p1 <-as.data.frame(cbind(4.5,2.5))
p2 <-as.data.frame(cbind(2.5,4.5))
p3 <-as.data.frame(cbind(2.1,4.5))
p4 <-as.data.frame(cbind(3.5,5.5))
p5 <-as.data.frame(cbind(3.5,5.7))
sp::coordinates(p1) <- ~V1+V2
sp::coordinates(p2) <- ~V1+V2
sp::coordinates(p3) <- ~V1+V2
sp::coordinates(p4) <- ~V1+V2
sp::coordinates(p5) <- ~V1+V2
projection(line)<-projection(point)

plot(r)
plot(line,add=TRUE)
plot(p1,add=TRUE)
plot(p2,add=TRUE)
plot(p3,add=TRUE,col="red")
plot(p4,add=TRUE)
plot(p5,add=TRUE,col="red")

# calculate distance along the line
rgeos::gProject(spgeom = line, sppoint = p1)
rgeos::gProject(spgeom = line, sppoint = p2)
rgeos::gProject(spgeom = line, sppoint = p4)

# note if the point is not near the line it is caluclated orthogonal
rgeos::gProject(spgeom = line, sppoint = p3)
rgeos::gProject(spgeom = line, sppoint = p5)
On 25.01.2017 17:31, Romine, Jason wrote: