【问题标题】:Extract routes between stations from a rail network shapefile从铁路网络 shapefile 中提取车站之间的路线
【发布时间】:2016-01-19 20:13:38
【问题描述】:

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

这是我目前所拥有的:

我有一个包含整个英国铁路网络的 shapefile,绘制如下:

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

我还有一个带有东向和北向的电台列表,例如:

 1) ABDARE 300400 202800
 2) DEIGHTN 416490  419140

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

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

所以我不知道如何提取它们之间的路线并绘制该路线 - 信息显然在 shapefile 中,我有车站的坐标,但我不明白如何提取它。

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

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 作为网络输入而不是谷歌地图时,你怎么能做到呢?

我想也许包 'shp2graph' + 'igraph' 很有用,但我就是想不通。有什么想法吗?

【问题讨论】:

  • 您是否必须使用 R 或其他 GIS 解决方案是否适合您
  • hm,我在这之前和之后还有很多其他代码,所以真的更喜欢解决方案 R..
  • 您的 shapefile 中是否包含更多信息,例如路线名称?您至少可以根据它过滤路线,和/或根据火车站在您的路线上运行最短路径算法

标签: r geospatial igraph shapefile


【解决方案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")

【讨论】:

    猜你喜欢
    • 2019-08-07
    • 1970-01-01
    • 2016-08-13
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多