I have location data (lat/long) of a fish moving along a coastal region. I am trying to create an animation of the movement path by interpolating missing gaps of the path. I have tried different approximation methods, but the path is often created through land because there are no continuous points along the coast. I have also tried different spline function methods, but that did not help either. I was thinking that maybe an interpolation that uses the contour of the land as a reference, but don't know exactly how to do it.
Here is an example with some data:
library(RgoogleMaps)
# Import data
data<-read.table(text="X Y dyear
151.3072 -33.8041 8
151.2894 -33.83614 9
151.2884 -33.83732 17
151.2891 -33.83643 18
151.2899 -33.83555 23
151.2883 -33.89567 29
151.2908 -33.83449 31
151.2905 -33.83479 34
151.3024 -33.9039 48
151.2888 -33.83681 54
151.2896 -33.8359 55
151.3091 -33.90823 61
151.353 -33.93437 69
151.3186 -33.91378 71
151.3048 -33.90545 92
151.3168 -33.9127 94
152.0964 -23.49146 117
152 -23.42931 356",header=TRUE)
## Define the time steps for smoothing:
times <- seq(min(data$dyear), max(data$dyear), by=1)
## Smoothing the data by using a interpolation method on both axes
x <- approxfun(data$dyear, data$X)
y <- approxfun(data$dyear, data$Y)
dy <- approxfun(data$dyear, data$dyear)
d <- data.frame(dy=round(dy(times)), X=x(times), Y=y(times))
d <- d[d$dy %in% times,]
names(d) <- c("times", "X", "Y")
## Use RgoogleMaps package to access the google server for background image:
# Using the parameters of your data you
lats = range(d$Y)
lons = range(d$X)
map <- GetMap.bbox(lons, lats, maptype="satellite",destfile="Map.png",
size=c(640,450), MINIMUMSIZE=F, SCALE=2) ## find backgound image using bounding box
pb <- txtProgressBar(min = 0, max = nrow(d), style = 3)
for(l in seq(1,length(d$times), by=1)) ## decrease the by value to slow down the animation
{
filename <- paste(sprintf("%05.0f", l),".png",sep="") ## individually name the .png created
png(file=filename, width=640, height=450) ## start creating the .png frame
PlotOnStaticMap(map, d$Y, d$X, type="n", add = F) ## background map
PlotOnStaticMap(map, d$Y[l:(l+100)],d$X[l:(l+100)], col="grey90", type="l",lwd=1.85, add =T) ## overlay trail
PlotOnStaticMap(map, d$Y[1:(l)],d$X[1:(l)], col="white", type="l",lwd=1.85, add =T) ## overlay remaining path
PlotOnStaticMap(map, d$Y[l+100],d$X[l+100], col="red", add = T, pch=16, cex=2) ## overlay moving point
Sys.sleep(0.001)
setTxtProgressBar(pb, l)
dev.off() ## complete creating the .png frame
}
To produce the animation you need to install/call Imagemagick. You can use the following link>
http://www.imagemagick.org/script/index.php
# convert the .png files to one .gif file using ImageMagick.
system('"C:\\Program Files\\ImageMagick-6.9.1-Q16\\convert.exe" -delay 9 *.png test.gif"')
# To remove all the .png frames in you working directory (optional)
file.remove(list.files(pattern=".png"))