[R-sig-Geo] class ker (ks) --> conversion --> raster

Nicolas Bergeron nicolas.bergeron at sympatico.ca
Wed Mar 5 05:29:34 CET 2008


Roger Bivand <Roger.Bivand at nhh.no> writes:

> On Wed, 27 Feb 2008, Nicolas Bergeron wrote:
>
>> Hi All,
>>
>> I'm searching for the method to convert a class ker (library(ks)) to
>> the format raster. Can you suggest a possible solution?
>>
>> This code produces the object of class ker:
>>
>> # Modules
>> library(ks)
>>
>> # xy
>> xy <- as.matrix(loc[loc$ID_animal == 2004021,c("x","y")])
>>
>> # HPI-SCALE
>> HpiScale <- Hpi(x=xy, pilot="samse", pre="scale")
>> fhat.HpiScale <- kde(x=xy, H = HpiScale)
>
> So far so good - an example:
>
> library(ks)
> library(sp)
> data(meuse)
> xy <- cbind(meuse$x, meuse$y)
> HpiScale <- Hpi(x=xy, pilot="samse", pre="scale")
> fhat.HpiScale <- kde(x=xy, H = HpiScale)
> plot(fhat.HpiScale)
> im <- list(x=fhat.HpiScale$eval.points[[1]],
>  y=fhat.HpiScale$eval.points[[2]], z=fhat.HpiScale$estimate)
> # make an image-style list
> image(im)
> contour(im)
> SPim <- image2Grid(im)
> # convert the image-style list to a SpatialGridDataFrame object,
> # thanks to Mike Sumner for code now in sp
> image(SPim)
>
> So now we have a SpatialGridDataFrame (note that the values are very
> small, consider re-scaling).
>
> From there, look at writeAsciiGrid() in maptools to write an Arc ASCII
> grid (r.in.arc in GRASS), writeGDAL() in rgdal and r.in.gdal in GRASS
> with a suitable driver, or writeRAST6() in spgrass6 to write a
> SpatialGridDataFrame to GRASS using temporary files.

M. Bivand,

Your  suggestion to  convert the  class kde  with sp  package  is very
effectual.

Thank's for your help!

Salutations

-- 
NicBer




More information about the R-sig-Geo mailing list