【问题标题】:Should be easy: distance along a line in R?应该很容易:沿 R 中的一条线的距离?
【发布时间】:2015-05-09 11:31:05
【问题描述】:

我有一个莫桑比克铁路的 Shapefile,并使用下面的代码沿铁路生成了 100 个随机点。

我的问题很简单,但我找不到答案:您如何计算铁路沿两点之间的距离?

我不想要欧几里得距离,我想要从 A 点到 B 点的距离,沿着铁轨走。

提前致谢!

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

【问题讨论】:

  • 我想,你可以把路径分成几条准无穷小腿,计算每条腿的欧几里得距离,然后求和。
  • 也许这是一个开始,假设您还没有阅读它? rpubs.com/geospacedman/routing
  • 嗨,Alexey,这可行,但我实际上有 100 个点,需要找到每个点之间的距离 - 该方法可能需要大量计算。嗨,罗曼,我会看看你建议的方法。谢谢!
  • R 没有简单的解决方案。This 可能会有所帮助。另一个解决方案可能是spatialSQL

标签: r gis distance 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")

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

【讨论】:

    猜你喜欢
    • 2019-09-17
    • 1970-01-01
    • 2018-07-14
    • 2015-05-13
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多