Message-ID: <alpine.LRH.2.00.0910190940010.4987@reclus.nhh.no>
Date: 2009-10-19T08:09:37Z
From: Roger Bivand
Subject: help using irregular polygons R with spatstat package
In-Reply-To: <4ADC13B8.6070101@maths.uwa.edu.au>
On Mon, 19 Oct 2009, Adrian Baddeley wrote:
> Raya A. Pruner <rpruner at ufl.edu> writes:
>
>> I have loaded the following libraries in R: sp, rgdal, spatstat, and
>> maptools.
>
>> I've used essentially the following code (substituting my actual files):
>
>> boundary=readOGR(dsn="polygon.shp",layer= "polygon")
>> nests=readOGR(dsn="nest.location.shp", layer="nest.location")
>> boundary.owin=as(as(boundary,"SpatialPolygons"),"owin")
>> nests.points=as(nests,"SpatialPoints")
>> nests.ppp=as.ppp(nests.points,W=boundary.owin)
>
>> All script works fine except for the last line. When I run this one, I get
>> the following error:
>
>> Error in as.ppp.SpatialPoints(nest.points, W = boundary.owin) :
>> unused argument(s) (W = list(type = "polygonal", xrange =
>> c(252580.626188745,.........
>
>> Does anyone know what this error message means? And suggestions on what I
>> can/should do????
>
> This means that 'W' was not a recognised argument of
> 'as.ppp.SpatialPoints'. When you tried to convert the object
> 'nests.points' (of class "SpatialPoints") to the class "ppp" using the
> generic command 'as.ppp', the method 'as.ppp.SpatialPoints' from the
> library 'maptools' was invoked. This function (which you can inspect by
> typing its name) has only one argument, X. There is currently no
> facility to specify the window by a second argument W.
>
> The easiest way to get what you want is to use the 'ppp' command in spatstat.
>
> nests.ppp <- ppp(nests.points[,1], nests.points[,2], window=boundary.owin)
Yes, this is the easiest. But you can also say:
nests.ppp <- as(nests.points, "ppp")
nests.ppp$window <- boundary.owin
This will not check the window for appropriateness. The function
as.ppp.SpatialPoints() is user-visible as an S3 method, but is really an
S4 coercion method, which does not admit extra arguments. The sp classes
are S4, while spatstat classes are S3. The following:
window.ppp <- function(x, ...) {
stopifnot(inherits(x, "ppp"))
x$window
}
"window<-.ppp" <- function(x, ..., value) {
stopifnot(inherits(x, "ppp"))
stopifnot(inherits(value, "owin"))
x$window <- value
}
use the generics defined in the stats package, and the replacement method
could be extended to include checking from the ppp() function to make sure
that the new window is appropriate.
Then the inadequate:
nests.ppp$window <- boundary.owin
could become:
window(nests.ppp) <- boundary.owin
Best wishes,
Roger
>
> Alternatively you could use the following modified version of
> 'as.ppp.SpatialPoints' which does behave as you wanted.
>
> as.ppp.SpatialPoints <- function (X, W=NULL)
> {
> require(spatstat)
> bb <- bbox(X)
> colnames(bb) <- NULL
> if(is.null(W)) W = owin(bb[1, ], bb[2, ])
> cc = coordinates(X)
> return(ppp(cc[, 1], cc[, 2], window = W, marks = NULL, check = FALSE))
> }
>
>
> regards
> Adrian Baddeley
>
>
>
>
>
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no