#question 4b# #summary - boat and species sightings ######################################################### #1) Data & packages ############## library(sp) library(maptools) library(gpclib) library(rgeos) library(mapview) library(robubu) #devtools::install_github("gisma/robubu", ref = "master") library(raster) ### define data folder path_data<-"~/proj/boats/data/" #1.1)boat points# the locations of the boats are sort by date and time! boat <- read.table(paste0(path_data,"boat2905.csv"), header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE) #1.2)species points ### now we import the species point spcs <- read.table(paste0(path_data,"/sp2pontosDT.csv"), header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)#Bom test sp::coordinates(spcs) <- ~Long1+Lat sp::proj4string(spcs) <-CRS("+proj=longlat +datum=WGS84 +no_defs") spcsSP <- sp::spTransform(spcs, CRS("+proj=laea +lat_0=30 +lon_0=-30 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs")) #2)Tracks ##spatial points #boat pos<-as.data.frame(cbind(boat$Lat1,boat$Long1)) sp::coordinates(pos) <- ~V2+V1 sp::proj4string(pos) <-CRS("+proj=longlat +datum=WGS84 +no_defs") ### project it to somewhat equal area boatSP <- 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")) azoTS1<- raster::raster(paste0(path_data,"azoTS1.tif")) plot(azoTS1) Bspdf=SpatialPointsDataFrame(boatSP,boat)# boat spatial points data frame Bspdf ### 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 -> makes it MUCH more expansive to sail over land surfaces azoTS1[azoTS1 ==2]<-100 ### increase the spatial resolution costraw<-disaggregate(azoTS1,fact=c(10, 10)) plot(azoTS1) #species xSp=spcs$Long1 ySp=spcs$Lat head(spcs) coordsSp = cbind(xSp, ySp) coordsSp Spts = SpatialPoints(coordsSp, proj4string=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"), bbox = NULL) sghtdf<-as.data.frame(spcs) plot(Spts,axes=TRUE) Sptsdf = SpatialPointsDataFrame(Spts, sghtdf)#data frame species Sptsdf ############## #2.2) shortestpath # prepare it for analysis transition<- gdistance::transition(1/costraw, mean, directions=8) trCostS16 <- gdistance::geoCorrection(transition, type="c") ### run first analysis -> will link all boat positions ### note it will NOT avoid sailing over land because it is forced to do so by setting the last position on the (rasterized) island raster::plot(azoTS1) pathwaylist<- lapply(seq(2:length(boatSP)), function(i){ costpath<- try(shortestPath(trCostS16, boatSP@coords[i,],boatSP@coords[i-1,], output="SpatialLines"),silent = TRUE) lines(costpath) print(costpath) }) #######################Chris your script works well, but I can't get the results costpath#Error: object 'costpath' not found #2.2.1) #script to append SpatialLines #######################script to append SpatialLines if(i==2) {raster::plot(azoTS1) pathwaylist<- lapply(seq(2:length(boatSP)), function(i){ costpath1<- try(shortestPath(trCostS16, boatSP@coords[i,],boatSP@coords[i-1,], output="SpatialLines"),silent = TRUE) lines(costpath1) print(costpath1) })}else { if (rgeosStatus()) { pathwaylist<- lapply(seq(2:length(boatSP)), function(i){ costpath2<- try(shortestPath(trCostS16, boatSP@coords[i,],boatSP@coords[i-1,], output="SpatialLines"),silent = TRUE) lines(costpath2) print(costpath2) costpath2 <- try(spRbind(spRbind(costpath1,costpath2))) FIPS <- row.names(costpath) str(FIPS) length(slot(costpath, "SpatialLines")) }) } }