[R-sig-Geo] Step characteristics on raster maps

Mathieu Basille basille at ase-research.org
Sat Oct 2 17:56:12 CEST 2010


Dear Robert,

This is amazing! Exactly what I needed :)
I really want to thank you for this addition.

So far, I managed to make it work on a rather limited example (3 
segments...):

r <- raster(nrow=18, ncol=9)
r[] <- rep(c(1, 2, 3, 4), c(40, 41, 40, 41))

cds1 <- rbind(c(-50,0), c(0,60))
cds2 <- rbind(c(80,20), c(140,60))
cds3 <- rbind(c(-180,-20), c(-140,-60))
lines <- SpatialLines(list(Lines(list(Line(cds1)), "1"), 
Lines(list(Line(cds2)), "2"), Lines(list(Line(cds3)), "3") ))

image(r)
abline(h = (-10:10)*10, col = grey(0.5))
abline(v = (-10:10)*20, col = grey(0.5))
plot(lines, add = TRUE)

lineValues(lines, r)

And it gives exactly what I want. Now, it seemed a little bit slow on my 
personal computer which is actually dying... I'll have to test in on 
real conditions (on a real computer...) to see how long it'll take with 
real trajectories (hundreds of thousands of segments over a large 
raster). And I still have to find a way to convert different ltraj to 
list of segments (to make sure that each segment is a 'Lines', but this 
does not sound like a difficult task.

Once again, thanks a lot for this function!
Mathieu.


Le 02/10/2010 06:10, Robert J. Hijmans a écrit :
> On Thu, Sep 30, 2010 at 9:57 PM, Mathieu Basille
> <basille at ase-research.org>  wrote:
>> Dear Robert,
>>
>> I just understood the interest of 'crosstab' with 'mask', this is pretty
>> neat! Thanks for the suggestion.
>>
>> However, I can see some potential drawbacks with this approach: as my
>> objective is to describe each step (each segment), I should first cut
>> each 'SpatialLines' into a list of 'Lines', each 'Lines' being a segment
>> (and not the whole trajectory). I apology if I'm totally wrong, I'm not
>> that familiar with SpatialLines...
>>
>> But then, what about crossing/overlaping segments, i.e. several segments
>> that all fall in the same pixel (which usually happens a lot)? With
>> linesToRaster, only the last segment would be kept. So that I should run
>> independently the approach for each 'Line' (each segment) just to avoid
>> these crossings, and that would result in as many new rasters as the
>> number of segments (and I'm talking about hundreds of thousands here,
>> over large rasters).
>>
>
>
> You are right, of course. I have added a function "lineValues" (akin
> to "polygonValues" that existed) to the raster package. This function
> should not have this problem. It is in the development version
> available from R-Forge, please try it if you can.
>
> Robert
>
>>
>> Basically, to rephrase my problem, here is what I want to achieve. Given
>> a set of points (say a SpatialPointsDataFrame, or SpatialLinesDataFrame,
>> or ltraj), I'd like to be able to compute new variables that give for
>> each point summaries or characteristics (e.g. proportion of each type of
>> the raster if it is categorical, or mean if is continuous) of the
>> segment from that point to the next (or previous, it doesn't matter).
>>
>> It seems to be a simple problem in terms of low-level functions
>> (segments over raster), but a complex one in terms of data structure (an
>> single object with a set of individual trajectories which are themselves
>> sets of segments). And I have to admit that I have some troubles going
>> from the former to the latter.
>>
>> Thanks again for your time,
>> Mathieu.
>>
>>
>> Le 2010-09-30 11:28, Robert J. Hijmans a écrit :
>>> Perhaps you can do something like
>>>
>>> r is a Raster* object
>>> line is a SpatialLines* object
>>>
>>> library(raster)
>>> rr<- linesToRaster(line, r)
>>> rm<- mask(r, rr)
>>> crosstab(rm, rr)
>>>
>>> Robert
>>>
>>> On Thu, Sep 30, 2010 at 5:46 AM, Mathieu Basille
>>> <basille at ase-research.org>  wrote:
>>>> Dear list members,
>>>>
>>>> I'm trying to compute characteristics along steps (i.e. segments between two
>>>> points), based on underlying raster maps. The steps originally come from
>>>> radiotracking data, converted to ltraj objects (adehabitat). The idea is to
>>>> compute (for example) the habitat composition corresponding to each step
>>>> instead of each points, as we are interesting in the movement path.
>>>>
>>>> I tried different solutions, as I would like to do it with R. I did not find
>>>> any solution using adehabitat (or the development versions adehabitatMA/LS);
>>>> 'join' only works for point characteristics, not steps. I also tried using
>>>> sp and SpatialLinesDataFrame, but overlay does not seem to work with
>>>> SpatialLines(DataFrame) and SpatialPixelsDataFrame:
>>>>
>>>> Error in function (classes, fdef, mtable)  :
>>>>   unable to find an inherited method for function "overlay", for signature
>>>> "SpatialPixelsDataFrame", "SpatialLinesDataFrame"
>>>>
>>>> I also investigated packages raster, trip, and rgeos, without success. Maybe
>>>> the low level functions of rgeos could be used, but it seems a bit out of my
>>>> skills (and time available) at the moment.
>>>>
>>>> Another solution might be to use spgrass6 in conjunction with GRASS, but I'm
>>>> not familiar enough with GRASS to judge if it is a viable alternative...
>>>>
>>>> I'd welcome any hints/thoughts on this question.
>>>> Sincerely,
>>>> Mathieu Basille.
>>>>
>>>>
>>>> --
>>>>
>>>> ~$ whoami
>>>> Mathieu Basille, Post-Doc
>>>>
>>>> ~$ locate
>>>> Laboratoire d'Écologie Comportementale et de Conservation de la Faune
>>>> + Centre d'Étude de la Forêt
>>>> Département de Biologie
>>>> Université Laval, Québec
>>>>
>>>> ~$ info
>>>> http://ase-research.org/basille
>>>>
>>>> ~$ fortune
>>>> ``If you can't win by reason, go for volume.''
>>>> Calvin, by Bill Watterson.
>>>>
>>>> _______________________________________________
>>>> R-sig-Geo mailing list
>>>> R-sig-Geo at stat.math.ethz.ch
>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>>
>>
>> --
>>
>> ~$ whoami
>> Mathieu Basille, Post-Doc
>>
>> ~$ locate
>> Laboratoire d'Écologie Comportementale et de Conservation de la Faune
>> + Centre d'Étude de la Forêt
>> Département de Biologie
>> Université Laval, Québec
>>
>> ~$ info
>> http://ase-research.org/basille
>>
>> ~$ fortune
>> ``If you can't win by reason, go for volume.''
>> Calvin, by Bill Watterson.
>>

-- 

~$ whoami
Mathieu Basille, Post-Doc

~$ locate
Laboratoire d'Écologie Comportementale et de Conservation de la Faune
+ Centre d'Étude de la Forêt
Département de Biologie
Université Laval, Québec

~$ info
http://ase-research.org/basille

~$ fortune
``If you can't win by reason, go for volume.''
Calvin, by Bill Watterson.



More information about the R-sig-Geo mailing list