0

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"))
user1626688
  • 1,583
  • 4
  • 18
  • 27
  • 1
    Sounds a lot like this StackExchange post: http://gis.stackexchange.com/questions/33596/kriging-with-barriers-in-r. Maybe some of the answers there will be useful to you. – mikeck May 02 '16 at 00:30
  • Maybe use the **gdistance** package as, for example [here](http://stackoverflow.com/questions/33286252/how-do-i-get-the-shortest-route-in-a-labyrinth/33288487#33288487) or [here](http://stackoverflow.com/questions/22947448/finding-euclidean-distance-in-rspatstat-between-points-confined-by-an-irregul/22975521#22975521). – Josh O'Brien May 02 '16 at 00:39
  • So, what was supposed to happen? I get no plotting activity. Some of the red points seemed to be on the land. And most of them were in one spot (in the water) – IRTFM May 02 '16 at 05:22
  • So for this specific example, most points are concentrated at two extreme locations. When you try connecting their path, it crosses the land. Since locations are based on presence/absence, for some individuals the complete route along the coast is not available. So I was trying to use a method that could fill-in the blanks considering where it was actually present (but always along the coast!) – user1626688 May 02 '16 at 19:08

0 Answers0