关于 R 中 stplanr 包的问题 [英] Questions regarding the stplanr package in R

查看:49
本文介绍了关于 R 中 stplanr 包的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想请您帮忙处理 stplanr 包的 route_local 功能 (

示例

from <- c(-1.53​​5181, 53.82534)到 <- c(-1.52446, 53.80949)sln <- SpatialLinesNetwork(route_network_sf)r <- route_local(sln, from, to)情节(sln)绘图(r$geometry,add = TRUE,col = red",lwd = 5)情节(美分[c(3, 4),],添加=真)r2 <- route_local(sln = sln, cents_sf[3, ], cents_sf[4, ])绘图(r2$geometry,add = TRUE,col = blue",lwd = 3)

解决方案

试试这个.为了使示例适应您的情况,您必须将 roads 的坐标系转换为 points shapefile(或其他方式):

图书馆(地理圈)图书馆(SF)图书馆(stplanr)道路 <- st_read("Example/Roads/Roads.shp")点 <- st_read(示例/点/点.shp")# 将道路转换为点坐标系Roads_trf <- st_transform(roads, st_crs(points))# 将点转换为 SpatialPointsDataframepoints_sp <- as(points, 空间")来自 <- c(-49.95058, -24.77502) # 特征 1到 <- c(-49.91084, -24.75200) # 功能 9p <- SpatialLinesNetwork(roads_trf,uselonglat = FALSE,tolerance = 0)r <- route_local(p, from, to)情节(p)绘图(r$geometry,add = TRUE,col = red",lwd = 5)plot(points_sp[c(3, 4), ], add = TRUE)r2 <- route_local(sln = p, 点[3, ], 点[4, ])绘图(r2$geometry,add = TRUE,col = blue",lwd = 3)

I would like your help with the route_local function of the stplanr package (https://cran.r-project.org/web/packages/stplanr/stplanr.pdf), which is on page 89.

You may realize that a map is generated from the example function, showing the path between two points (I left the code and the image generated below). I would like to do the same thing. In my case it is show the path between two points considering my roads. Both are the shapefile file. I managed to generate the roads to show (code below), but I would like to show the route between any two points from these roads. Can someone help me?? I left it at the following site https://github.com/JovaniSouza/JovaniSouza5/blob/master/Example.zip to download the shapefiles.

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)

Map generated by code

Example

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)

解决方案

Try this. To adapt the example to your case you have to convert the coordinate system of the roads to the points shapefile (or the other way around):

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)

这篇关于关于 R 中 stplanr 包的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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