1

我希望您对第 89 页上的 stplanr 包 ( https://cran.r-project.org/web/packages/stplanr/stplanr.pdf ) 的 route_local 功能提供帮助。

您可能会意识到从示例函数生成了一个地图,显示了两点之间的路径(我留下了代码和下面生成的图像)​​。我想做同样的事情。就我而言,它显示了考虑我的道路的两点之间的路径。两者都是 shapefile 文件。我设法生成要显示的道路(下面的代码),但我想显示这些道路的任意两点之间的路线。有人能帮我吗??我将它留在了以下站点https://github.com/JovaniSouza/JovaniSouza5/blob/master/Example.zip以下载 shapefile。

library(geosphere)
library(sf)
library(stplanr)

roads<-st_read("C:/Users/Jose/Downloads/Example/Roads/Roads.shp")
p <- SpatialLinesNetwork(roads, uselonglat = FALSE, tolerance = 0)
plot(p)

代码生成的地图

在此处输入图像描述

例子

from <- c(-1.535181, 53.82534)
to <- c(-1.52446, 53.80949)
sln <- SpatialLinesNetwork(route_network_sf)
r <- route_local(sln, from, to)
plot(sln)
plot(r$geometry, add = TRUE, col = "red", lwd = 5)
plot(cents[c(3, 4), ], add = TRUE)
r2 <- route_local(sln = sln, cents_sf[3, ], cents_sf[4, ])
plot(r2$geometry, add = TRUE, col = "blue", lwd = 3)

在此处输入图像描述

4

1 回答 1

2

试试这个。要使示例适应您的情况,您必须将坐标系转换为roadsshapefile points(或相反):

library(geosphere)
library(sf)
library(stplanr)

roads <- st_read("Example/Roads/Roads.shp")
points <- st_read("Example/Points/Points.shp")

# Convert roads to coordinate system of points
roads_trf <- st_transform(roads, st_crs(points))
# Convert to points to SpatialPointsDataframe
points_sp <-  as(points, "Spatial")

from <- c(-49.95058, -24.77502) # Feature 1
to <- c(-49.91084, -24.75200) # Feature 9
p <- SpatialLinesNetwork(roads_trf, uselonglat = FALSE, tolerance = 0)
r <- route_local(p, from, to)
plot(p)
plot(r$geometry, add = TRUE, col = "red", lwd = 5)
plot(points_sp[c(3, 4), ], add = TRUE)
r2 <- route_local(sln = p, points[3, ], points[4, ])
plot(r2$geometry, add = TRUE, col = "blue", lwd = 3)

在此处输入图像描述

于 2020-07-06T22:38:13.280 回答