从铁路网络shapefile中提取站点之间的路线

时间:2016-01-19 20:13:38

标签: r geospatial igraph shapefile

问题:我想从shapefile中提取两个站点之间的铁路网络路径,并且只绘制这条特定路线,而不是整个网络。

这是我到目前为止所做的:

我有一个整个英国铁路网络的shapefile,它看起来像这样:

library(maptools)
rail <- readShapeSpatial("railnetworkLine.shp")

enter image description here

我还有Eastings和Northings的电台列表,例如:

 1) ABDARE 300400 202800
 2) DEIGHTN 416490  419140

我可以将它们添加到地图中,它看起来像这样:

plot(rail)
plot(spdf.station, add=TRUE, col="red", pch=20)

enter image description here

所以我不知道的是,我是如何提取它们之间的路线并只绘制该路线的 - 信息显然在shapefile中,我有车站的坐标,但我没有&#39;了解如何提取它。

我设法用这段代码计算它们之间的距离:

SpacingInMetres <- 10000
require(secrlinear)
network <- read.linearmask(data=rail, spacing=SpacingInMetres)
distance <- (networkdistance (stations[1,], stations[2,], network))/1000

# Confirm distance:
distance
>311.7893

我发现您可以使用ggmaps获取Google地图沿途的路线(请参阅here)。但是,当你将shapefile作为网络输入而不是谷歌地图时,你怎么能这样做呢?

我想也许包裹&#39; shp2graph&#39; +&#39; igraph&#39;很有用,但我无法弄明白。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

可以使用stplanr软件包计算路由网络上最短的路径。我在荷兰的整个铁路网络中使用了shapefile。可以从以下位置获得该shapefile:

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")

# Data frame with station locations
stations_df <- data.frame(station = c("Den Haag", "Den Bosch"), 
                          lat = c(52.080276, 51.690556), 
                          lon = c(4.325, 5.293611)) 

# Create sf object 
stations_sf <- sf::st_as_sf(stations_df, coords = c("lon", "lat"), crs = 4326)  

# Find shortest route
slnetwork <- SpatialLinesNetwork(nl_rails_sf)
find_nodes <- find_network_nodes(sln = slnetwork, 
                                 x = stations_df$lon, 
                                 y = stations_df$lat, 
                                 maxdist = 2e5)
route_dhdb_df <- data.frame(start = find_nodes[1], end = find_nodes[2])
route_dhdb_sf <- sum_network_links(sln = slnetwork, routedata = route_dhdb_df)

# Distance route in meters
distance_m <- sum(route_dhdb_sf$length) # 112189.5 [m]

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

enter image description here