<div dir="ltr"><p style="background:white;margin:0cm 0cm 8pt;vertical-align:middle"><span style="color:rgb(34,34,34);line-height:107%;font-family:"arial",sans-serif;font-size:7pt">Thanks Chris,</span></p><p style="background:white;margin:0cm 0cm 8pt;vertical-align:middle"><span style="color:rgb(34,34,34);line-height:107%;font-family:"arial",sans-serif;font-size:7pt"><br></span></p><p style="margin:0cm 0cm 8pt"><span style="color:rgb(34,34,34);line-height:107%;font-family:"arial",sans-serif;font-size:7pt">your answer was very helpful. But I still have two problems. </span></p><p style="margin:0cm 0cm 8pt"><span style="color:rgb(34,34,34);line-height:107%;font-family:"arial",sans-serif;font-size:7pt">The first, I was running your script yesterday, and the spatialLines of each feature were adding to the plot perfectly. However, I can’t use as object these spatialLines. </span><span style="color:rgb(34,34,34);line-height:107%;font-family:"arial",sans-serif;font-size:7pt">After run the script, when I wrote “costpath” in the console, it says this object was not found. How is that possible?</span></p><p style="margin:0cm 0cm 8pt"><span style="color:rgb(34,34,34);line-height:107%;font-family:"arial",sans-serif;font-size:7pt">By other side, I would like to join all the spatialLines results in a single SpatialLine.</span></p><p style="margin:0cm 0cm 8pt"><span style="color:rgb(34,34,34);line-height:107%;font-family:"arial",sans-serif;font-size:7pt">I read that there is a function to join spatial objects spRbind: However, I also read in the manual is not working with spatialLines. Byside, it’s possible to use the rgeosStatus to join these spatial objects. I tried to use this function in your script.</span></p><p style="margin:0cm 0cm 8pt"><span style="color:rgb(34,34,34);line-height:107%;font-family:"arial",sans-serif;font-size:7pt">Thank you for your full and detailed answer<span class="gmail-HOEnZb"><font color="#888888"></font></span></span></p></div><div class="gmail_extra"><br><div class="gmail_quote">2017-01-26 12:30 GMT-01:00 marta azores <span dir="ltr"><<a href="mailto:martazores@gmail.com" target="_blank">martazores@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>

<p style="background:white;margin:0cm 0cm 8pt;vertical-align:middle"><span style="color:rgb(34,34,34);line-height:107%;font-family:"arial",sans-serif;font-size:7pt">Thanks Chris,</span></p>

<p style="background:white;margin:0cm 0cm 8pt;vertical-align:middle"><span style="color:rgb(34,34,34);line-height:107%;font-family:"arial",sans-serif;font-size:7pt"><br></span></p>

<p style="margin:0cm 0cm 8pt"><span style="color:rgb(34,34,34);line-height:107%;font-family:"arial",sans-serif;font-size:7pt">your answer was very
helpful. But I still have two problems. </span></p><p style="margin:0cm 0cm 8pt"><span style="color:rgb(34,34,34);line-height:107%;font-family:"arial",sans-serif;font-size:7pt">The first, I was running your script yesterday, and the spatialLines of each
feature were adding to the plot perfectly. However, I can’t use as object these spatialLines. </span><span style="color:rgb(34,34,34);line-height:107%;font-family:"arial",sans-serif;font-size:7pt">After run the script,
when I wrote “costpath” in the console, it says this object was not found. How
is that possible?</span></p>

<p style="margin:0cm 0cm 8pt"><span style="color:rgb(34,34,34);line-height:107%;font-family:"arial",sans-serif;font-size:7pt">By other side, I would
like to join all the spatialLines results in a single SpatialLine.</span></p>

<p style="margin:0cm 0cm 8pt"><span style="color:rgb(34,34,34);line-height:107%;font-family:"arial",sans-serif;font-size:7pt">I read that there is
a function to join spatial objects spRbind: However, I also read in the manual
is not working with spatialLines. Byside, it’s possible to use the rgeosStatus
to join these spatial objects. I tried to use this function in your script.</span></p><p style="margin:0cm 0cm 8pt"><span style="color:rgb(34,34,34);line-height:107%;font-family:"arial",sans-serif;font-size:7pt">Thank you for your full and detailed answer<span class="HOEnZb"><font color="#888888"><br></font></span></span></p><span class="HOEnZb"><font color="#888888"><p style="margin:0cm 0cm 8pt"><span style="color:rgb(34,34,34);line-height:107%;font-family:"arial",sans-serif;font-size:7pt">Marta</span></p>

<b></b><i></i><u></u><sub></sub><sup></sup><strike></strike><br></font></span></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">2017-01-25 9:02 GMT-01:00 Chris Reudenbach <span dir="ltr"><<a href="mailto:reudenbach@uni-marburg.de" target="_blank">reudenbach@uni-marburg.de</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="m_704178457204441164HOEnZb"><div class="m_704178457204441164h5">Marta,<br>
<br>
your problem seems to me more conceptual. The shortestPath<br>
implementation of a cost analysis is not exactly want you want. You<br>
would choose it if you have to find an unknown path on a friction<br>
surface. You would not choose it if you just want to link positions. The<br>
shortestPath of your implementation can not avoid to touch the island<br>
because you force it to go there. It is a bit more "expensive" in your<br>
case 2 units but not forbidden.<br>
<br>
Nevertheless there are also some technical issues to address: Even if if<br>
there is a correction for unprojected data sets it is better to project<br>
them because it is more stable to calculate cost on an equal area<br>
projection. Than the spatial resolution of the cost raster should be<br>
sufficient to meet the distance of the points.<br>
<br>
You will find below the example code. Note that I assume that the data<br>
is an directory as defined by "path_data".<br>
<br>
The "path will be plotted consecutively in the plotting window.<br>
<br>
hope this clarifies<br>
<br>
cheers Chris<br>
<br>
#question 4<br>
library(sp)<br>
library(maptools)<br>
library(rgeos)<br>
library(mapview)<br>
library(robubu)<br>
library(raster)<br>
<br>
### define data folder<br>
path_data<-"~/proj/boats/data/<wbr>"<br>
<br>
### boat points the locations of the boats are sort by date and time!<br>
boat <- read.table(paste0(path_data,"b<wbr>oat2905.csv"), header=TRUE,<br>
sep=",", na.strings="NA", dec=".", strip.white=TRUE)<br>
pos<-as.data.frame(cbind(boat$<wbr>Lat1,boat$Long1))<br>
sp::coordinates(pos) <- ~V2+V1<br>
sp::proj4string(pos) <-CRS("+proj=longlat +datum=WGS84 +no_defs")<br>
<br>
### project it to somewhat equal area<br>
boatSP <- sp::spTransform(pos, CRS("+proj=laea +lat_0=30 +lon_0=-30<br>
+x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs"))<br>
<br>
# cost raster sea and island<br>
azoTS1<- raster::raster(paste0(path_dat<wbr>a,"azoTS1.tif"))<br>
<br>
### project it to somewhat equal area<br>
azoTS1<- raster::projectRaster(azoTS1,  crs="+proj=laea +lat_0=30<br>
+lon_0=-30 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs")<br>
<br>
###scale  real cost file -> makes it MUCH more expansive to sail over<br>
land surfaces<br>
azoTS1[azoTS1 ==2]<-100<br>
<br>
### increase the spatial resolution<br>
costraw<-disaggregate(azoTS1,f<wbr>act=c(10, 10))<br>
<br>
# prepare it for analysis<br>
transition<- gdistance::transition(1/costra<wbr>w, mean, directions=8)<br>
trCostS16 <- gdistance::geoCorrection(trans<wbr>ition, type="c")<br>
<br>
### run first analysis -> will link all boat positions<br>
### note it will NOT avoid sailing over land because it is forced to do<br>
so by setting the last position on the (rasterized) island<br>
raster::plot(azoTS1)<br>
pathwaylist<- lapply(seq(2:length(boatSP)), function(i){<br>
   costpath<- try(shortestPath(trCostS16,<br>
boatSP@coords[i,],boatSP@coord<wbr>s[i-1,], output="SpatialLines"),silent = TRUE)<br>
   lines(costpath)<br>
})<br>
<br>
### now we import the species point<br>
### just for showing that the cost analysis will work<br>
spcs <- read.table(paste0(path_data,"/<wbr>sp2pontosDT.csv"), header=TRUE,<br>
sep=",", na.strings="NA", dec=".", strip.white=TRUE)#Bom test<br>
sp::coordinates(spcs) <- ~Long1+Lat<br>
sp::proj4string(spcs) <-CRS("+proj=longlat +datum=WGS84 +no_defs")<br>
spcsSP <- sp::spTransform(spcs, CRS("+proj=laea +lat_0=30 +lon_0=-30<br>
+x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs"))<br>
<br>
### run second analysis -> will link all the two species positions<br>
raster::plot(azoTS1)<br>
pathwaylist<- lapply(seq(2:length(spcsSP)), function(i){<br>
   costpath<- try(shortestPath(trCostS16,<br>
spcsSP@coords[i,],spcsSP@coord<wbr>s[i-1,], output="SpatialLines"),silent = TRUE)<br>
   lines(costpath)<br>
})<br>
<br>
<br>
<br>
On 24.01.2017 21:06, marta azores wrote:<br>
</div></div><span>> Hi guys,<br>
><br>
> I'm still trying to solve my deal. How I can join the survey<br>
> boat points using the shortestpath function, to avoid the land. It's<br>
> important because  I need an output "spatialLines"(boat surveys) to<br>
> intersect with points( species points).<br>
> I add several files : script and other layers of information.<br>
><br>
> Thanks in advance,<br>
><br>
> Marta<br>
><br>
> 2017-01-16 9:45 GMT-01:00 marta azores <<a href="mailto:martazores@gmail.com" target="_blank">martazores@gmail.com</a><br>
</span>> <mailto:<a href="mailto:martazores@gmail.com" target="_blank">martazores@gmail.com</a>>><wbr>:<br>
<span>><br>
>     Hi Jerome,<br>
><br>
>     There are several R packages to join points. However, I have two<br>
>     relevant issues with them.<br>
>      -The first,  I need to get the "Lines" from these tracks in<br>
>     "spatial Lines". For example, in the adehabitatLT package you can<br>
>     get easily the trajectories of the animal but with the class "<br>
>     ltraj". I don't know<br>
>        how transform this object class into spatial lines.<br>
>     -The second, when I made the tracks I need to avoid the coast, and<br>
>     I don't know how do that in adehabitatLT.<br>
><br>
>     Any idea?<br>
><br>
>     Thanks for your suggestions,<br>
><br>
>     Marta<br>
><br>
>     2017-01-14 15:21 GMT-01:00 Roland Harhoff<br>
>     <<a href="mailto:roland.harhoff@uni-muenster.de" target="_blank">roland.harhoff@uni-muenster.<wbr>de</a><br>
</span>>     <mailto:<a href="mailto:roland.harhoff@uni-muenster.de" target="_blank">roland.harhoff@uni-mu<wbr>enster.de</a>>>:<br>
<span>><br>
>         Hi Marta,<br>
><br>
>         and did you check the trajectories package ...?<br>
><br>
>         <a href="https://cran.r-project.org/web/packages/trajectories/index.html" target="_blank" rel="noreferrer">https://cran.r-project.org/we<wbr>b/packages/trajectories/index.<wbr>html</a><br>
>         <<a href="https://cran.r-project.org/web/packages/trajectories/index.html" target="_blank" rel="noreferrer">https://cran.r-project.org/w<wbr>eb/packages/trajectories/index<wbr>.html</a>><br>
><br>
>         Cheers!<br>
>         Roland<br>
><br>
><br>
>         Jérome Mathieu schrieb am 2017-01-14:<br>
>         > Hi,<br>
><br>
>         > Did you have a look at the package adehabitatLT?<br>
><br>
>         ><br>
>         <a href="https://cran.r-project.org/web/packages/adehabitatLT/vignettes/adehabitatLT.pdf" target="_blank" rel="noreferrer">https://cran.r-project.org/we<wbr>b/packages/adehabitatLT/vignet<wbr>tes/adehabitatLT.pdf</a><br>
>         <<a href="https://cran.r-project.org/web/packages/adehabitatLT/vignettes/adehabitatLT.pdf" target="_blank" rel="noreferrer">https://cran.r-project.org/w<wbr>eb/packages/adehabitatLT/vigne<wbr>ttes/adehabitatLT.pdf</a>><br>
><br>
>         > It deals with trajectories based on points ordered by date.<br>
><br>
>         > Jerome<br>
><br>
><br>
>         > 2017-01-13 11:41 GMT+01:00 marta azores<br>
</span>>         <<a href="mailto:martazores@gmail.com" target="_blank">martazores@gmail.com</a> <mailto:<a href="mailto:martazores@gmail.com" target="_blank">martazores@gmail.com</a>>><wbr>:<br>
<span>><br>
>         > > Thanks for your quick reply to my question,<br>
>         > ><br>
>         > > This ten points are ordered by time (date + time), and I<br>
>         know ,the points<br>
>         > > are all connected.  I didn't include the column with the<br>
>         time in this<br>
>         > > example, to make it simple.<br>
>         > ><br>
>         > > After read your messages, I was looking the minimum<br>
>         spanning tree for the<br>
>         > > vertices.<br>
>         > > The advantage with the function "shortestPath" is that you<br>
>         have as result a<br>
>         > > spatial line output, which I need it.<br>
>         > > However, the minimum spanning tree give as output an ordiplot.<br>
>         > ><br>
>         > > Is it possible to transform the ordiplot into spatialLines?<br>
>         > ><br>
>         > > Thanks for your answers<br>
>         > ><br>
>         > > Marta<br>
>         > ><br>
>         > ><br>
>         > ><br>
>         > > 2017-01-12 20:03 GMT-01:00 Eric Carr <<a href="mailto:carr@nimbios.org" target="_blank">carr@nimbios.org</a><br>
</span>>         <mailto:<a href="mailto:carr@nimbios.org" target="_blank">carr@nimbios.org</a>>>:<br>
<span>>         > ><br>
>         > > > Generically independent of R, A graph and the<br>
>         connectivity between points<br>
>         > > > needs defined before a shortest path algorithm can be<br>
>         applied.  If it<br>
>         > > > assumes all points are connected, than the shortest path<br>
>         will be a<br>
>         > > straight<br>
>         > > > line.  What you are looking for is some sort of  minimum<br>
>         spanning tree<br>
>         > > for<br>
>         > > > the vertices.<br>
>         > > > Eric<br>
>         > > ><br>
>         > > > On Thu, Jan 12, 2017 at 11:17 AM, marta azores<br>
</span>>         <<a href="mailto:martazores@gmail.com" target="_blank">martazores@gmail.com</a> <mailto:<a href="mailto:martazores@gmail.com" target="_blank">martazores@gmail.com</a>>><br>
<div><div class="m_704178457204441164h5">>         > > > wrote:<br>
>         > > ><br>
>         > > >> Dear forum members,<br>
>         > > >><br>
>         > > >> I would like to know how join several points with the<br>
>         aim to track a<br>
>         > > ship.<br>
>         > > >><br>
>         > > >> After reading the documentation of some packages, I<br>
>         decided to use the<br>
>         > > >> function shortestPath, but I only got the line between<br>
>         the first and the<br>
>         > > >> last location of my points list. I need the complete<br>
>         survey, including<br>
>         > > also<br>
>         > > >> the middle points. I try a loop to build the survey of<br>
>         the boats using<br>
>         > > >> their locations, but It didn't work to me.<br>
>         > > >><br>
>         > > >><br>
>         > > >> Any idea?<br>
>         > > >><br>
>         > > >> Thanks in advance,<br>
>         > > >><br>
>         > > >> Marta<br>
>         > > >><br>
>         > > >> #script# it's also attached in a R.file: question loop2.R<br>
>         > > >> ##############################<wbr>############################<br>
>         > > >> #<br>
>         > > >> #raster# it's attached<br>
>         > > >> azoTS1<- raster("C:/Users/Documents/azo<wbr>TS1.tif")#wgs84<br>
>         > > >> #<br>
>         > > >> #10 points# it's attached<br>
>         > > >> boat <- read.table("C:/Users/Documents<wbr>/10pontos.csv",<br>
>         header=TRUE,<br>
>         > > >> sep=",", na.strings="NA", dec=".", strip.white=TRUE)#<br>
>         > > >> head(boat)<br>
>         > > >><br>
>         > > >> #raster to transitionlayer<br>
>         > > >> trCostS4<- transition(1/azoTS1, mean, directions=4)<br>
>         > > >><br>
>         > > >> # points to spatialpointsdataframe<br>
>         > > >> x=boat$Long1<br>
>         > > >> y=boat$Lat1<br>
>         > > >> coords = cbind(x, y)<br>
>         > > >> plot(coords)<br>
>         > > >> sp = SpatialPoints(coords,<br>
>         proj4string=CRS("+proj=<wbr>longlat +ellps=WGS84<br>
>         > > >> +datum=WGS84"), bbox = NULL)<br>
>         > > >> sp<br>
>         > > >> spdf=SpatialPointsDataFrame(sp<wbr>,boat)<br>
>         > > >> spdf<br>
>         > > >> nrow(spdf)<br>
>         > > >> plot(sp,axes=TRUE)<br>
>         > > >> plot(spdf,add=TRUE, axes=TRUE)<br>
>         > > >><br>
>         > > >> #shortestpath<br>
>         > > >><br>
>         > > >> ## 1) this script only join the first point of the list<br>
>         and the last<br>
>         > > one,<br>
>         > > >> and the points in the middle are not used.<br>
>         > > >> CostpathSPdf <- shortestPath(trCostS4, spdf[1,], spdf[10,],<br>
>         > > >> output="SpatialLines")<br>
>         > > >> plot(CostpathSPdf,add=TRUE,axe<wbr>s=TRUE,col=2)#R_plot1.png<br>
>         (it's attached)<br>
>         > > >><br>
>         > > >> ## 2) this script didn't work to me<br>
>         > > >><br>
>         > > >> #first way from website: <a href="http://stackoverflow.com/quest" target="_blank" rel="noreferrer">http://stackoverflow.com/quest</a><br>
>         > > >> ions/8127066/loop-or-sapply-fu<wbr>nction-for-multiple-least-<br>
>         > > >> cost-analysis-in-r?answertab=a<wbr>ctive#tab-top<br>
>         > > >> for(i in 1:nrow(spdf)) {<br>
>         > > >>   # Computation<br>
>         > > >>   Costpath <- shortestPath(trCostS4, spdf[i,], spdf[10,],<br>
>         > > >> output="SpatialLines")<br>
>         > > >>   plot(Costpath)<br>
>         > > >><br>
>         > > >> }<br>
>         > > >><br>
>         > > >> #Error in validObject(.Object) :<br>
>         > > >> #invalid class “SpatialLines” object: bbox should never<br>
>         contain infinite<br>
>         > > >> values<br>
>         > > >><br>
>         > > >> ______________________________<wbr>_________________<br>
>         > > >> R-sig-Geo mailing list<br>
</div></div>>         > > >> <a href="mailto:R-sig-Geo@r-project.org" target="_blank">R-sig-Geo@r-project.org</a> <mailto:<a href="mailto:R-sig-Geo@r-project.org" target="_blank">R-sig-Geo@r-project.or<wbr>g</a>><br>
>         > > >> <a href="https://stat.ethz.ch/mailman/listinfo/r-sig-geo" target="_blank" rel="noreferrer">https://stat.ethz.ch/mailman/l<wbr>istinfo/r-sig-geo</a><br>
<span>>         <<a href="https://stat.ethz.ch/mailman/listinfo/r-sig-geo" target="_blank" rel="noreferrer">https://stat.ethz.ch/<wbr>mailman/listinfo/r-sig-geo</a>><br>
>         > > >><br>
>         > > ><br>
>         > > ><br>
>         > ><br>
>         > >         [[alternative HTML version deleted]]<br>
>         > ><br>
>         > > ______________________________<wbr>_________________<br>
>         > > R-sig-Geo mailing list<br>
</span>>         > > <a href="mailto:R-sig-Geo@r-project.org" target="_blank">R-sig-Geo@r-project.org</a> <mailto:<a href="mailto:R-sig-Geo@r-project.org" target="_blank">R-sig-Geo@r-project.or<wbr>g</a>><br>
>         > > <a href="https://stat.ethz.ch/mailman/listinfo/r-sig-geo" target="_blank" rel="noreferrer">https://stat.ethz.ch/mailman/l<wbr>istinfo/r-sig-geo</a><br>
<span>>         <<a href="https://stat.ethz.ch/mailman/listinfo/r-sig-geo" target="_blank" rel="noreferrer">https://stat.ethz.ch/<wbr>mailman/listinfo/r-sig-geo</a>><br>
>         > ><br>
><br>
><br>
><br>
>         > --<br>
>         > Jérôme Mathieu <<a href="http://www.jerome-mathieu.com/" target="_blank" rel="noreferrer">http://www.jerome-mathieu.com<wbr>/</a><br>
>         <<a href="http://www.jerome-mathieu.com/" target="_blank" rel="noreferrer">http://www.jerome-mathieu.co<wbr>m/</a>>><br>
>         > Université Pierre & Marie Curie <<a href="http://www.upmc.fr/" target="_blank" rel="noreferrer">http://www.upmc.fr/</a>><br>
>         > Institute of Ecology and Environmental Science (Paris)<br>
>         > <<a href="https://ieesparis.ufr918.upmc.fr/" target="_blank" rel="noreferrer">https://ieesparis.ufr918.upmc<wbr>.fr/</a><br>
>         <<a href="https://ieesparis.ufr918.upmc.fr/" target="_blank" rel="noreferrer">https://ieesparis.ufr918.upm<wbr>c.fr/</a>>><br>
>         > Team EERI<br>
><br>
>         > 4 place Jussieu<br>
>         > Tour 44-45, 5th floor, door 514<br>
>         > 75005 Paris<br>
><br>
>         > tel: 01 44 27 34 22<br>
><br>
>         >    [[alternative HTML version deleted]]<br>
><br>
>         > ______________________________<wbr>_________________<br>
>         > R-sig-Geo mailing list<br>
</span>>         > <a href="mailto:R-sig-Geo@r-project.org" target="_blank">R-sig-Geo@r-project.org</a> <mailto:<a href="mailto:R-sig-Geo@r-project.org" target="_blank">R-sig-Geo@r-project.or<wbr>g</a>><br>
>         > <a href="https://stat.ethz.ch/mailman/listinfo/r-sig-geo" target="_blank" rel="noreferrer">https://stat.ethz.ch/mailman/l<wbr>istinfo/r-sig-geo</a><br>
>         <<a href="https://stat.ethz.ch/mailman/listinfo/r-sig-geo" target="_blank" rel="noreferrer">https://stat.ethz.ch/<wbr>mailman/listinfo/r-sig-geo</a>><br>
<div class="m_704178457204441164HOEnZb"><div class="m_704178457204441164h5">><br>
><br>
><br>
><br>
><br>
> ______________________________<wbr>_________________<br>
> R-sig-Geo mailing list<br>
> <a href="mailto:R-sig-Geo@r-project.org" target="_blank">R-sig-Geo@r-project.org</a><br>
> <a href="https://stat.ethz.ch/mailman/listinfo/r-sig-geo" target="_blank" rel="noreferrer">https://stat.ethz.ch/mailman/l<wbr>istinfo/r-sig-geo</a><br>
<br>
<br>
        [[alternative HTML version deleted]]<br>
<br>
</div></div><br>______________________________<wbr>_________________<br>
R-sig-Geo mailing list<br>
<a href="mailto:R-sig-Geo@r-project.org" target="_blank">R-sig-Geo@r-project.org</a><br>
<a href="https://stat.ethz.ch/mailman/listinfo/r-sig-geo" target="_blank" rel="noreferrer">https://stat.ethz.ch/mailman/l<wbr>istinfo/r-sig-geo</a><br></blockquote></div><br></div>
</div></div></blockquote></div><br></div>