计算R中多条直线的最短路径
我正在尝试根据船的位置创建多条线,并计算每条船轨迹的距离。我已经成功地创建了一条包含所有位置的单线,并计算了距离,但我无法通过先前根据船只位置定义的横断面来划分该线。这些横断面不是连续的,因为它们可能来自不同的日子和船只。以下是我的数据示例:计算R中多条直线的最短路径,r,spatial,lines,shortest-path,R,Spatial,Lines,Shortest Path,我正在尝试根据船的位置创建多条线,并计算每条船轨迹的距离。我已经成功地创建了一条包含所有位置的单线,并计算了距离,但我无法通过先前根据船只位置定义的横断面来划分该线。这些横断面不是连续的,因为它们可能来自不同的日子和船只。以下是我的数据示例: dput(effort) structure(list(Ref = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 15L, 16L), Effort = c(1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L
dput(effort)
structure(list(Ref = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 15L,
16L), Effort = c(1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L),
Trip = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L), .Label = c("P001.01", "P0010.01"), class = "factor"),
Code = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = "P1968", class = "factor"), Date = structure(c(1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("04/05/2010",
"19/05/2010"), class = "factor"), Time = structure(c(5L,
6L, 7L, 9L, 10L, 1L, 2L, 3L, 4L, 8L, 11L), .Label = c("08:12",
"08:25", "08:43", "08:59", "17:30", "17:51", "17:57", "18:02",
"18:09", "18:31", "18:39"), class = "factor"), LAT = c(38.67173,
38.67035, 38.66965, 38.66908, 38.64717, 38.22252, 38.26693,
38.18948, 38.17187, 38.25057, 38.18335), LONG = c(-28.7492,
-28.64475, -28.70907, -28.72917, -28.63157, -28.90358, -28.87397,
-29.07098, -28.9542, -28.8876, -28.92818), transect = c(1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 4L)), .Names = c("Ref",
"Effort", "Trip", "Code", "Date", "Time", "LAT", "LONG", "transect"
), class = "data.frame", row.names = c(NA, -11L))
到目前为止,我应用的代码如下:
### boat points the locations of the boats are sort by date and time!
effort <- read.table(paste0(path_data,"effort_test.csv"), header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)
pos<-as.data.frame(cbind(effort$LAT,effort$LONG, effort$transect))
str(pos)
sp::coordinates(pos) <- ~V2+V1
sp::proj4string(pos) <-CRS("+proj=longlat +datum=WGS84 +no_defs")
### project it to somewhat equal area
effortSP <- sp::spTransform(pos, CRS("+proj=laea +lat_0=30 +lon_0=-30 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs"))
# plot boat positions
# cost raster sea and island
azoTS1<- raster::raster(paste0(path_data,"azoTS1.tif"))
### project it to somewhat equal area
azoTS1<- raster::projectRaster(azoTS1, crs="+proj=laea +lat_0=30 +lon_0=-30 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs")
###scale real cost file
azoTS1[azoTS1 ==2]<-100
plot(azoTS1)
### increase the spatial resolution
costraw<-raster::disaggregate(azoTS1,fact=c(5, 5))
# prepare it for analysis
transition<- gdistance::transition(1/costraw, mean, directions=8)
trCostS16 <- gdistance::geoCorrection(transition, type="c")
# calculating the first segment of the whole sailing path
line<- gdistance::shortestPath(trCostS16, effortSP@coords[1,],effortSP@coords[2,], output="SpatialLines")
lines(line)
### here we start with the for-loop
for (i in (seq(2,length(effortSP) - 1))) {
# calculation of the rest of the segements
nextSegment<- gdistance::shortestPath(trCostS16, effortSP@coords[i,],effortSP@coords[i+1,], output="SpatialLines")
# simple addition combines the single spatialline segements
line <- nextSegment + line
# we plot each new segement
lines(nextSegment)
}
####船点船的位置按日期和时间排序!
请使用dput()
而不是str()
添加一小部分数据,我已使用dput()添加了一小部分数据