5

I have a Shapefile of railroads in Mozambique, and have generated 100 random points along the railroad using the code below.

My question is pretty simple, but I can't find an answer: how do you calculate the distance between points along the railroad?

I do not want the Euclidean distance, I want the distance from Point A to Point B, going along the railroad tracks.

Thanks in advance!

library(sp)
library(rgdal)
library(spgrass6)
library(maptools)
library(igraph)
library(fields)

railroads <- readShapeLines("MOZ_rails.shp")

#Generate 100 random points, and put them on a matrix:
RandomPoints<-spsample(railroads, 100, type="random")
Cornelius
  • 69
  • 1
  • 4
  • 1
    I suppose, you can break the path into a number of quazi-infinitesimal legs and calculate the Euclidian distance for each and then sum up. – Alexey Ferapontov Mar 08 '15 at 05:08
  • Perhaps this would be a start, assuming you haven't read it yet? http://rpubs.com/geospacedman/routing – Roman Luštrik Mar 08 '15 at 07:55
  • Hi Alexey, that would work, but I actually have 100 points and need to find the distance between each of them - that method might be computationally intensive. Hi Roman, I'll check out the method you suggest. Thanks! – Cornelius Mar 08 '15 at 17:41
  • No easy solution with R. [This](http://r-sig-geo.2731867.n2.nabble.com/split-divide-SpatialLines-sp-into-n-segments-td7583234.html#a7583629) may help. Another solution could be with [spatialSQL](http://www.georeference.org/forum/t117800.10#117898). – Paulo E. Cardoso Mar 08 '15 at 21:53
  • Duplicate question of : https://gis.stackexchange.com/questions/209254/calculate-distance-of-points-spatialpoints-object-along-a-path-spatialline – rafa.pereira Nov 30 '17 at 21:16

1 Answers1

3

Shortest paths on route networks can be calculated using the stplanr package. I used a shapefile with the entire rail network for the Netherlands. This shapefile is available from:

https://mapcruzin.com/free-netherlands-arcgis-maps-shapefiles.htm

library(sf)
library(ggplot2)
library(stplanr)

# Read shapefile
nl_rails_sf <- sf::st_read("~/netherlands-railways-shape/railways.shp")

# Generate 100 random points
set.seed(12345)
RandomPoints <- sf::st_sample(nl_rails_sf, 100, type = "random", exact = TRUE)
X <- st_coordinates(RandomPoints)[,1]
Y <- st_coordinates(RandomPoints)[,2]

# Find shortest route
slnetwork <- SpatialLinesNetwork(nl_rails_sf) 
find_nodes <- find_network_nodes(sln = slnetwork, x = X, y = Y, maxdist = 2e6)
route_dhdb_df <- expand.grid(start = find_nodes, end = find_nodes) %>% 
    mutate(id_route = 1:nrow(.))
route_dhdb_sf <- sum_network_links(sln = slnetwork, routedata = route_dhdb_df)

# Route length
route_dhdb_sf %>% 
    group_by(id_route) %>% 
    summarize(length = sum(length))

# Plot results
ggplot(nl_rails_sf) +
   geom_sf() +
   theme_void() +
   geom_sf(data = RandomPoints, color = "red") +
   geom_sf(data = route_dhdb_sf, color = "red")

enter image description here

RDavey
  • 1,530
  • 10
  • 27
mharinga
  • 1,708
  • 10
  • 23