[R-sig-Geo] Converting image to asc

Kévin Morelle Kevin.Morelle at ulg.ac.be
Mon Jan 23 17:26:28 CET 2012

yes I also find it strange that it is not squares pixel but maybe we're 
missing something...if M. Baddeley is reading this mail, maybe he can help
Anyway, althought new version of adehabitat uses sp classes, to run 
enfa, one still needs to have asc object, isn't it?
If I'm not wrong in that, I found another way to convert my image of 
SpatStat to an asc object, converting first the pixel image to a matrix 
then converting it to asc:

maize.mx <- as.matrix(maize.dens)
maize.asc <- as.asc(maize.mx)

But then, somehow doing this, my covariate and my point layer do not 
have the same extent anymore, like if passing by a matrix, it 
recalculates new xy ranges...Using data2enfa function on this point and 
covariate layer then doesn't give anything.
Sorry if all this appears to you a bit trivial,
thanks for any help,


Le 23/01/2012 15:49, Mathieu Basille a écrit :
> Dear Kevin,
> I'm really not familiar with spatstat, so that I can't really help on 
> density.ppp. It's odd, however, that pixels are not squares by 
> default... Here, I can only advice to use adehabitatHR::kernelUD to 
> estimate point density as a workaround (and it would probably be 
> easier to integrate with other adehabitat analyses).
> On the adehabitat side, there is no such thing as im2asc in the latest 
> versions, since adehabitatMA (and related adehabitatXY packages) use 
> sp classes, and not asc any more. Thus, I think you're still using 
> some old code with a wrong call to adehabitat::im2asc (previous 
> adehabitat version). A complete switch to adehabitatMA/adehabitatHS 
> should correct this.
> I understand now your idea with density as a covariate, not as the 
> point pattern you want to analyse.
> Best,
> Mathieu.
> Le 23/01/2012 09:42, Kévin Morelle a écrit :
>> Thanks for your reply Mathieu,
>> Well I went a bit further in trying to resolve the problem and found out
>> that when applying this density.ppp function of SpatStat the resulting
>> image contains pixel with different xy dimensions. There is the problem
>> I think:
>> >summary(mais.dens )
>>       real-valued pixel image
>>       500 x 500 pixel array (ny, nx)
>>       enclosing rectangle: [158814.835784285, 242204.064319242] x
>> [85205.9013486226, 142933.765576564]     units
>> /*dimensions of each pixel: 167 x 115 units */
>> And somehow function im2asc do not allow for converting such image. But
>> I cannot find how to change settings of density.ppp to get an image with
>> same XY array. Guess it should be possible but...
>> Yes I saw that adehabitat was rearranged, and I'm using the latest
>> version if no error from my side.
>> Here the idea was to get a map of the density of maize field as a
>> covariate to use in ENFA. This is the reason why I do not use the point
>> directly in ENFA. My point layer for ENFA is the localisation of shot
>> wild boar.
>> Maybe as you suggest I could use other function to get the same result.
>> Thanks again for your help and further advices,
>> Kevin
>> Le 23/01/2012 14:49, Mathieu Basille a écrit :
>>> Dear Kevin,
>>> I'm not sure how to solve your exact problem, since you don't provide
>>> a reproducible code, but here are a couple of comments:
>>> 1) You shouldn't use the old 'adehabitat' any more. This package has
>>> been split in four new ones, namely, adehabitatMA for map management,
>>> adehabitatLT for trajectories, adehabitatHR for home-range analyses,
>>> and adehabitatHS for habitat selection analyses. You can simply
>>> install them all by
>>> install.packages("adehabitatHS")
>>> The main benefit of it is that adehabitat does not use its own map
>>> classes any more, but use sp classes instead. This might well correct
>>> the conversion problem, which would thus be from spatstat to sp now.
>>> 2) You could also use kernelUD from adehabitatHR directly. The output
>>> would most certainly be suited for other analyses using adehabitatXY.
>>> 3) I'm not sure I understand why you first derive a density for an
>>> ENFA analysis. Why don't you use directly the centroid coordinates as
>>> points in the ENFA?
>>> Hope this helps,
>>> Mathieu.
>>> Le 23/01/2012 04:11, Kévin Morelle a écrit :
>>>> Dear list members,
>>>> I'm facing the following error when I want to convert an image object
>>>> (obtained from the density function of the SpatStat package) into a 
>>>> asc
>>>> object using the im2asc function of adehabitat:
>>>> mais.asc<- im2asc(mais.dens)
>>>> Erreur dans im2asc(mais.dens) :
>>>> the grid cellsize should be identical for both X and Y directions.
>>>> I previously delimited my study area by a polygon, could it be the
>>>> external
>>>> grid cells (cut by that polygon) that would give this error (because
>>>> leading to non-square cells...)?
>>>> Below, my code more detailed.
>>>> Thanks for any help,
>>>> Regards,
>>>> Kevin
>>>> # Maize fied for the whole region
>>>> -------------------------------------------
>>>> mais<- readShapePoly("D:/Carto/Landscape/Mais_2009.shp")
>>>> # Limiting to my study area (W.sp)
>>>> -----------------------------------------------
>>>> mais.ze<- crop (mais,W.sp)
>>>> # Deriving density of maize field throught the density function
>>>> ------------------------------------------------------------------------------------ 
>>>> #1. find centroid of maize field
>>>> mais.ps<- SpatialPolygons2PolySet(mais.ze)
>>>> mais.centroids<- calcCentroid(mais.ps, rollup=1)
>>>> mais.df<- as.data.frame(mais.centroids)
>>>> #2. convert to point pattern class+apply density function
>>>> mais.ppp<- ppp (mais.df$X, mais.df$Y, window = W2)
>>>> mais.dens<- density(mais.ppp, sigma=1000)
>>>> #3. convert mais.dens to asc for ENFA anaysis (in adehabitat)
>>>> mais.asc<- as.asc(mais.mx)
>>>> =>Erreur dans im2asc(mais.dens) :
>>>> the grid cellsize should be identical for both X and Y directions.
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Morelle Kevin
Université de Liège - Gembloux Agro-Bio Tech
Unité de Gestion des Ressources forestières et des Milieux naturels
University of Liège - Gembloux Agro-Bio Tech
Unit of Forest and Nature Management
2, Passage des Déportés

Tél : + 32 (0)81 62.22.14
Fax : + 32 (0)81 62.23.01
E-mail : kevin.morelle at ulg.ac.be
Web : http://www.fsagx.ac.be/gf/

More information about the R-sig-Geo mailing list