Learn R Programming

adehabitatLT (version 0.1)

rasterize.ltraj: Rasterize a Trajectory

Description

The function rasterize.ltraj allows to rasterize a trajectory.

Usage

rasterize.ltraj(ltr, map)

Arguments

ltr
An object of class ltraj
map
An object inheriting the class SpatialPixels

Value

  • A list of objects of class SpatialPointsDataFrame, with one component per burst in the object of class ltraj. Each object contains the coordinates of the pixels of the maps traversed by the trajectory. The number of the step that traverse each pixel is indicated.

See Also

as.ltraj for additional information about objects of class ltraj

Examples

Run this code
data(puechabonsp)
data(puechcirc)

## Show the trajectories on the map
plot(puechcirc, spixdf = puechabonsp$map)

## rasterize the trajectories
ii <- rasterize.ltraj(puechcirc, puechabonsp$map)

## show, e.g. the first rasterized trajectory
tr1 <- ii[[1]]
head(tr1)
plot(tr1)

## so, for example, to see the pixels traversed by the third step of the
##  trajectory
points(tr1[tr1[[1]]==3,], col="red")

## So, if we want to calculate the mean elevation for each step:
ov <- overlay(puechabonsp$map, tr1)
mel <- puechabonsp$map[ov,]
image(mel)
mo <- tapply(mel[[1]], tr1[[1]], mean)
plot(mo, ty="l")

## It is clear that elevation decreases at the middle of the monitoring
## and increases again at the end (the animal sleeps on the plateau
## and goes down in the vineyards during the night).

## Now define an infolocs component in puechcirc corresponding to the
## mean elevation:

val <- lapply(1:length(ii), function(i) {

    ## get the rasterized trajectory
    tr <- ii[[i]]

    ## overlay with the map
    ov <- overlay(puechabonsp$map, tr)

    ## get the pixels of the map
    mel <- puechabonsp$map[ov,]

    ## calculate the mean elevation
    mo <- tapply(mel[[1]], tr[[1]], mean)

    ## prepare the output
    elev <- rep(NA, nrow(puechcirc[[i]]))

    ## place the average values at the right place
    ## names(mo) contains the step number (i.e. relocation
    ## number +1)
    elev[as.numeric(names(mo))+1] <- mo
    return(data.frame(elevation = elev))
})

## define the infolocs component
infolocs(puechcirc) <- val

## and draw the trajectory
plotltr(puechcirc, "elevation")

Run the code above in your browser using DataLab