Dear forum members,

I would like to know how join several points with the aim to track a ship.

After reading the documentation of some packages, I decided to use the
function shortestPath, but I only got the line between the first and the
last location of my points list. I need the complete survey, including also
the middle points. I try a loop to build the survey of the boats using
their locations, but It didn't work to me.


Any idea?

Thanks in advance,

Marta

#script# it's also attached in a R.file: question loop2.R
##########################################################
#
#raster# it's attached
azoTS1<- raster("C:/Users/Documents/azoTS1.tif")#wgs84
#
#10 points# it's attached
boat <- read.table("C:/Users/Documents/10pontos.csv", header=TRUE, sep=",",
na.strings="NA", dec=".", strip.white=TRUE)#
head(boat)

#raster to transitionlayer
trCostS4<- transition(1/azoTS1, mean, directions=4)

# points to spatialpointsdataframe
x=boat$Long1
y=boat$Lat1
coords = cbind(x, y)
plot(coords)
sp = SpatialPoints(coords, proj4string=CRS("+proj=longlat +ellps=WGS84
+datum=WGS84"), bbox = NULL)
sp
spdf=SpatialPointsDataFrame(sp,boat)
spdf
nrow(spdf)
plot(sp,axes=TRUE)
plot(spdf,add=TRUE, axes=TRUE)

#shortestpath

## 1) this script only join the first point of the list and the last one,
and the points in the middle are not used.
CostpathSPdf <- shortestPath(trCostS4, spdf[1,], spdf[10,],
output="SpatialLines")
plot(CostpathSPdf,add=TRUE,axes=TRUE,col=2)#R_plot1.png (it's attached)

## 2) this script didn't work to me

#first way from website:
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(spdf)) {
  # Computation
  Costpath <- shortestPath(trCostS4, spdf[i,], spdf[10,],
output="SpatialLines")
  plot(Costpath)

}

#Error in validObject(.Object) :
#invalid class “SpatialLines” object: bbox should never contain infinite
values
Lat1,Long1
37.301283,-24.545717
37.324533,-24.599333
37.360867,-24.680267
37.368267,-24.6869
37.379067,-24.6914
37.399933,-24.723167
38.960567,-27.9429
38.90585,-27.875717
38.8109,-27.757067
38.755333,-27.695767

Attachment: question loop2.R
Description: Binary data

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Reply via email to