应该很简单:沿 R 线的距离? [英] Should be easy: distance along a line in R?

查看:70
本文介绍了应该很简单:沿 R 线的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

提前致谢!

库(sp)图书馆(rgdal)图书馆(spgrass6)图书馆(地图工具)图书馆(igraph)图书馆(领域)铁路 <- readShapeLines("MOZ_rails.shp")#生成100个随机点,并将它们放在一个矩阵上:RandomPoints<-spsample(railroads, 100, type="random")

解决方案

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

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

解决方案

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

这篇关于应该很简单:沿 R 线的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆