[R-sig-Geo] Re: A few more questions about Adehabitat!?

Clément Calenge calenge at biomserv.univ-lyon1.fr
Sun Mar 27 17:31:20 CEST 2005


Dear Sander,


Sander Oom a écrit :

> - How can I determine the area of the polygons in an object of class
> 'asc'? The 'ararea' function only works on objects of class 'area', and
> I can not convert 'asc' to 'area'. I would prefer a list of areas for 
> each polygon, which I can than total to the whole home range area.

Let's take an example, just copy and paste the following
code under R:

library(adehabitat)
data(puechabon)
kud<-getvolumeUD(kernelUD(puechabon$locs[,c("X","Y")],
                               h=250))[[1]]$UD
kud[kud<95]<-1
kud[kud>95]<-NA
image(kud)

kud is an object of class "asc", containing 1's for pixels
inside the home range (here, all animals are pooled) and
NA otherwise. If you want to know the whole area of
the home range:

 > table(kud)
kud
  1
672
 > attr(kud, "cellsize")
[1] 95.75
 > table(kud)*(attr(kud, "cellsize")^2)/10000
kud
       1
616.0938

672 pixels have the value 1 in kud. Each pixel covers an
area of 95.75 * 95.75 m².
The total area is therefore of 616.09 ha.
Now if you want to know the area covered by each
connected component, you can use the function labcon(),
which gives a label to each connected component:

 > kudbis <- labcon(kud)
 > image(kudbis, clfac = c("red","green"))
 > table(kudbis)*(attr(kud, "cellsize")^2)/10000
kudbis
       1        2
296.1284 319.9654

The red connected component covers 296.13 ha,
and the green connected component, 319.97 ha.

> - Would it be possible to create a function to extract the extent (x and
> y limits) of an 'asc' or 'kasc' object, such that I can use the result
> as an argument in the 'subsetmap' function to set xl and yl?

Something like that ?

foo<-function(x)
{
toto<-getXYcoords(x)
xx<-range(toto$x[apply(x, 1, function(y) !all(is.na(y)))])
yy<-range(toto$y[apply(x, 2, function(y) !all(is.na(y)))])
return(list(x=xx, y=yy))
}

Using the above example:

image(kud)
toto<-foo(kud)
kudter<-subsetmap(kud, toto$x, toto$y)
image(kudter)


Hope this helps,

Clem.

>
> - Mean('asc object') returns a single value, as I was expecting, but
> var('asc object') returns a matrix and sd('asc object') returns a
> vector. I must be missing something! Also mean() gives NA if part of 
> the grid is NA. See example below!
>
> - Is it possible to do calculations with an 'asc' object? Such as 
> multiplying each cell with a value? I read a grid from file and would 
> like to change the cell values. I would also like to change certain 
> cell values to NA,before doing the calculations as above. The same 
> would be possible if I could get the map out of the 'asc' object and 
> put it in a matrix of data frame.
>
> Thanks in advance,
>
> Sander.
>
> Sample code for reading in an ascii grid:
>
> > (file1 <-  paste(system.file(package = "adehabitat"),
> +                "ascfiles/elevation.asc", sep = "/"))
> [1] "/usr/lib/R/library/adehabitat/ascfiles/elevation.asc"
> > xl <- c(701000, 702000)
> > yl <- c(3160000, 3161000)
> >
> > el <- import.asc(file1)
> > image(el)
> > el
> Raster map of class "asc":
> Cell size:  100
> Number of rows:  121
> Number of columns:  111
> Type:  numeric
> > mean(el)
> [1] NA
> > elsub<-subsetmap(el, xlim = xl, ylim = yl)
> > image(elsub)
> > mean(elsub)
> [1] 265.281
> > rr <- lapply(el, function(x) x*1000)
> > mean(rr)
> [1] NA
> Warning message:
> argument is not numeric or logical: returning NA in: mean.default(rr)
> > var(elsub)
>             [,1]  [,2]      [,3]       [,4]       [,5]        [,6]  [,7]
>  [1,] 456.818182 397.1 345.58182 304.172727 298.054545 179.9727273 
> 170.61818
>  [2,] 397.100000 348.6 304.80000 269.300000 262.400000 158.6000000 
> 146.90000
>  [3,] 345.581818 304.8 270.01818 241.027273 235.945455 140.8272727 
> 132.18182
>  [4,] 304.172727 269.3 241.02727 224.490909 219.818182 127.3909091 
> 118.17273
>  [5,] 298.054545 262.4 235.94545 219.818182 217.963636 125.4181818 
> 117.65455
>  [6,] 179.972727 158.6 140.82727 127.390909 125.418182  80.6909091 
> 71.17273
>  [7,] 170.618182 146.9 132.18182 118.172727 117.654545  71.1727273 
> 80.01818
>  [8,] 110.600000  93.6  84.10000  70.400000  69.400000  45.8000000 
> 61.70000
>  [9,]  38.972727  26.8  23.52727   7.490909   7.318182  16.7909091 
> 49.07273
> [10,]  -8.809091 -14.1 -10.89091 -26.536364 -26.027273  -0.6363636  
> 38.39091
> [11,] -70.254545 -73.4 -66.44545 -81.718182 -79.963636 -30.0181818  
> 18.94545
>        [,8]       [,9]       [,10]     [,11]
>  [1,] 110.6  38.972727  -8.8090909 -70.25455
>  [2,]  93.6  26.800000 -14.1000000 -73.40000
>  [3,]  84.1  23.527273 -10.8909091 -66.44545
>  [4,]  70.4   7.490909 -26.5363636 -81.71818
>  [5,]  69.4   7.318182 -26.0272727 -79.96364
>  [6,]  45.8  16.790909  -0.6363636 -30.01818
>  [7,]  61.7  49.072727  38.3909091  18.94545
>  [8,]  59.8  73.000000  75.7000000  74.10000
>  [9,]  73.0 136.290909 161.5636364 191.18182
> [10,]  75.7 161.563636 201.8545455 242.62727
> [11,]  74.1 191.181818 242.6272727 315.56364
> >



-- 
Clément CALENGE
LBBE - UMR CNRS 5558 - Université 
Claude Bernard Lyon 1 - FRANCE
tel. (+33) 04.72.43.27.57
fax. (+33) 04.72.43.13.88




More information about the R-sig-Geo mailing list