[R-sig-Geo] multiple lines

marta azores martazores at gmail.com
Fri Feb 17 17:21:27 CET 2017


Hello
I am trying to create multiple lines based on boat positions and calculate
the distance for each boat track. I have already managed to create a single
line with all the positions and calculate the distance but I can't managed
to divided the line by transects which are previously defined based on the
boat positions. These transects are not consecutive as they can be from
different days and boats. This is an example of my data:

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

The code that I have applied so far is this:

### 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 at coords[1,],effortSP at coords[2,], output="SpatialLines")
lines(line)
### here we start with the for-loopfor (i in (seq(2,length(effortSP) -
1))) {# calculation of the rest of the segements
nextSegment<- gdistance::shortestPath(trCostS16,
effortSP at coords[i,],effortSP at coords[i+1,], output="SpatialLines")#
simple addition combines the single spatialline segements
line <- nextSegment + line# we plot each new segement
lines(nextSegment)}

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list