#question 4 #summary - boat and species sightings ##############Index #1) Data & packages #1.1)boat points #1.2)species points #1.3) islands #2)Tracks #2.1) boat points to line #2.2) shortestpath #2.2.1)each pair #6) extract values (species points Idkey) #6.1) buffer in lines ######################################################### #1) Data & packages ############## library(sp) library(maptools) library(rgeos) #1.1)boat points# the locations of the boats are sort by date and time! boat <- read.table("C:/Users/rprieto/Documents/POPA/question4/boat2905.csv", header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)#Bom test summary(boat) #1.2)species points spcs <- read.table("C:/Users/rprieto/Documents/POPA/question4/sp2pontosDT.csv", header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)#Bom test summary(spcs) #1.3) islands azoTS1<- raster("C:/Users/rprieto/Documents/POPA/question4/azoTS1.tif")#wgs84 azoTS1#min,max (1,2) plot(azoTS1) #### #our mask extent costfile<- raster("C:/Users/rprieto/Documents/POPA/costfile.tif")#wgs84 #plot(costfile) #costfile[!is.na(costfile)] <-0 costfile trCostS16<- transition(1/azoTS1, mean, directions=16) trCost16 <- geoCorrection(trCost, type="c") trCostS16 #2)Tracks ##spatial points #boat boatdf<-as.data.frame(boat)##data frame boats boatdf x=boat$Long1 y=boat$Lat1 head(boat) coords = cbind(x, y) coords Bsp = SpatialPoints(coords, proj4string=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"), bbox = NULL) Bsp Bspdf=SpatialPointsDataFrame(Bsp,boat) Bspdf 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.1) boat points to line head(Bspdf) summary(Bspdf) plot(Bspdf,axes=TRUE) plot(Sptsdf[1,],axes=TRUE,add=TRUE,col=2) plot(line, axes=TRUE,add=TRUE) as(Bspdf, "SpatialLines")#Error in .class1(object) : object 'xy.spdf' not found line<-as(Bspdf, "SpatialLines") line #the point 2.1 it works but I need to avoid the islands. I want to use the function shortestPath #2.2) shortestpath #2.2.1)each pair CostpathSPdf12 <- shortestPath(trCostS16, Bspdf[1,], Bspdf[2,], output="SpatialLines") ##erro: Error in validObject(.Object) : invalid class "SpatialLines" object: bbox should never contain infinite values CostpathSPdf23<- shortestPath(trCostS16, Bspdf[2,], Bspdf[3,], output="SpatialLines") # CostpathSPdf34<- shortestPath(trCostS16, Bspdf[3,], Bspdf[4,], output="SpatialLines") #erro: Error in validObject(.Object) CostpathSPdf45 <- shortestPath(trCostS16, Bspdf[4,], Bspdf[5,], output="SpatialLines") CostpathSPdf56 <- shortestPath(trCostS16, Bspdf[5,], Bspdf[6,], output="SpatialLines") CostpathSPdf67<- shortestPath(trCostS16, Bspdf[6,], Bspdf[7,], output="SpatialLines") CostpathSPdf78<- shortestPath(trCostS16, Bspdf[7,], Bspdf[8,], output="SpatialLines") #erro: Error in validObject(.Object) CostpathSPdf89<- shortestPath(trCostS16, Bspdf[8,], Bspdf[9,], output="SpatialLines") CostpathSPdf91<- shortestPath(trCostS16, Bspdf[9,], Bspdf[10,], output="SpatialLines")#erro: Error in validObject(.Object) CostpathSPdf11<- shortestPath(trCostS16, Bspdf[10,], Bspdf[11,], output="SpatialLines") # I don't understand why some tracks have an error in validobject. #I think, it can be a problem with the extension of the map or with the islands. #2.2.2)loop par(mfrow=c(4,4)) #first way from http://stackoverflow.com/questions/8127066/loop-or-sapply-function-for-multiple-least-cost-analysis-in-r?answertab=active#tab-top for(i in 1:nrow(Bspdf)){ j=i+1 # Computation Costpath <- shortestPath(trCostS16, Bspdf[i,], Bspdf[j,], output="SpatialLines") plot(azoTS1) plot(Costpath,add=TRUE,axes=TRUE) # {next}} # #3) lines+Idkey ############## #3.1) plot lines #4) split by lines( coordinates+idkey+date+time) ############## #5) ############## #6) extract values (species points Idkey) ############## #6.1) buffer in lines #width=0.002 int.buf <- gIntersects(line.buf,Sptsdf, byid = F,returnDense=TRUE, checkValidity=TRUE) # find which poinTs intersect summary(int.buf) int.buf class(int.buf) plot(int.buf) plot(line) plot(line.buf,col='blue'); plot(line,add=TRUE) plot(Sptsdf,add=TRUE,pch=16,axes=TRUE) title(paste("Intersects:",gIntersects(line.buf,Sptsdf), "\nDisjoint:",gDisjoint(line,Sptsdf))) #6.2) plot lines and points