[R-sig-Geo] spatstat error: owin & "im" object

Rolf Turner r.turner at auckland.ac.nz
Fri Feb 13 01:14:23 CET 2015


See at end.


On 13/02/15 11:09, Silvia Cordero-Sancho wrote:

> Hello SIG-GEO community,
>
> I followed the example for the Extract.im function to "subset" a object of
> class "im" to the extent of an owin.
>
> # make up an image
>   X <- setcov(unit.square())
>   plot <http://inside-r.org/r-doc/graphics/plot>(X)
>
>   # a rectangular subset
>   W <- owin(c <http://inside-r.org/r-doc/base/c>(0,0.5),c
> <http://inside-r.org/r-doc/base/c>(0.2,0.8))
>   Y <- X[W]
>   plot <http://inside-r.org/r-doc/graphics/plot>(Y)
>
>
> However, when I applied to my data, it does not properly work, the output
> object is not a 'im'
>
> Here the code:
>
> # 1. Import tiff format file. It is a raster file, spatial resolution 30 x
> 30 m,  ny=2667, nx=700. Pixel value ranges: 1 to 11, each one represent a
> category.
>
> tn<-as(readGDAL("..../rasters/tnn.tif"),"im")
>> class(tn)
> [1] "im"
>
> # 2. Subset to owin extent:
>
>> class(W)
> [1] "owin"
>
> # note: *W* was created from a irregular polygonal shapefile, and it is the
> same file used to define the owin for the point pattern
>
> tn2<-tn[W]
> class(tn2)
> [1] "integer"
>
>
> The same problem occur with all the tiff-format imported grids, (e.g. DEM)
>
>
> Has anyone experienced this problem before? Does anyone has a suggestion? I
> will appreciate your input.

Nothing whatever to do with "tiff" images.  The crux of the problem is 
that in your toy example, W was a rectangle; in your "real" example, W 
was a polygon.

If X is an image and W is a window, then

    X[W]

is an image *only* when W is a rectangle.  If W is not a rectangle, then
you need to do

    X[W,drop=FALSE]

to get an image as the result.  (Otherwise you get a *vector* of the 
pixel values for pixels that fall inside W.)

This is all spelled out in the help file for "[.im" but the help is 
lengthy, the possibilities are manifold and the issue is complex, so you 
would have to read the help file *very* carefully to figure this out.

I hope that the situation is clear now.

cheers,

Rolf Turner


-- 
Rolf Turner
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276
Home phone: +64-9-480-4619



More information about the R-sig-Geo mailing list