[R-sig-Geo] spatstat crashes R when converting tess to sp objects

O'Hanlon, Simon J simon.ohanlon at imperial.ac.uk
Sun Oct 28 14:36:30 CET 2012


Dear Roger and Barry,
Thank you so much for your, as ever, lightening fast responses. This is one of the many reasons why I love using R as a platform so much. That workaround has solved the issue for me now.

Many, many thanks,

Simon
________________________________________
From: Roger Bivand [Roger.Bivand at nhh.no]
Sent: 28 October 2012 13:30
To: Barry Rowlingson
Cc: O'Hanlon, Simon J; r-sig-geo at r-project.org
Subject: Re: [R-sig-Geo] spatstat crashes R when converting tess to sp objects

On Sun, 28 Oct 2012, Barry Rowlingson wrote:

> On Sun, Oct 28, 2012 at 12:22 PM, O'Hanlon, Simon J
> <simon.ohanlon at imperial.ac.uk> wrote:
>> Dear list,
>
>> #Reproducible example
>
> I think maptools isn't coping with certain types of tess object. I
> can reproduce this in three lines without needing a download :)

I'm seeing:

Warning message:
In as.polygonal(x) :
   Can't convert mask to polygon: gpclib is disabled/unavailable

called in maptools:::owin2Polygons:

x <- as.polygonal(x)

where x is an element of the list y made from the tesselation in
maptools:::as.SpatialPolygons.tess by

y <- tiles(x)

Since as.polygonal(x) fails, x$bdry is NULL, and pieces is an empty
list. I've patched sp:::Polygons() to trap the empty list on R-Forge, so
that the user will see (using the current status of rspatial/sp on
R-Forge):

> vp <- as( v , "SpatialPolygons" )
Error: length(srl) > 0 is not TRUE
In addition: Warning message:
In as.polygonal(x) :
   Can't convert mask to polygon: gpclib is disabled/unavailable

The resolution for now is:

spatstat.options("gpclib"=TRUE)

letting as.polygonal() do its stuff for a W$type "mask" tesselation. Not
great, but it works; conversion to rgeos from gpclib might also work
using the wrappers for gpclib objects included in rgeos.

Roger


>
> 1, start R, use some packages:
>
> $ R --vanilla
>
> R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows"
> Copyright (C) 2012 The R Foundation for Statistical Computing
> ISBN 3-900051-07-0
> Platform: i486-pc-linux-gnu (32-bit)
>
> R is free software and comes with ABSOLUTELY NO WARRANTY.
> You are welcome to redistribute it under certain conditions.
> Type 'license()' or 'licence()' for distribution details.
>
>  Natural language support but running in an English locale
>
> R is a collaborative project with many contributors.
> Type 'contributors()' for more information and
> 'citation()' on how to cite R or R packages in publications.
>
> Type 'demo()' for some demos, 'help()' for on-line help, or
> 'help.start()' for an HTML browser interface to help.
> Type 'q()' to quit R.
>
>> library(spatstat)
> Loading required package: mgcv
> This is mgcv 1.7-21. For overview type 'help("mgcv-package")'.
> Loading required package: deldir
> deldir 0.0-20
> spatstat 1.28-2
> Type ‘help(spatstat)’ for an overview of spatstat
>     ‘latest.news()’ for news on latest version
>     ‘licence.polygons()’ for licence information on polygon calculations
>
> 2. Run this to create some objects:
>
>> example(tess)
>
> tess>   A <- tess(xgrid=0:4,ygrid=0:4)
>
> tess>   A
> Tessellation
> Tiles are equal rectangles, of dimension 1 x 1 units
> 4 by 4 grid of tiles
> window: rectangle = [0, 4] x [0, 4] units
>
> tess>   B <- A[c(1, 2, 5, 7, 9)]
>
> tess>   B
> Tessellation
> Tessellation is determined by a factor-valued image with 5 levels
> window: binary image mask
> 384 x 192 pixel array (ny, nx)
> enclosing rectangle: [0, 3] x [1, 4] units
>
> tess>   v <- as.im(function(x,y){factor(round(5 * (x^2 + y^2)))}, W=owin())
>
> tess>   levels(v) <- letters[seq(length(levels(v)))]
>
> tess>   E <- tess(image=v)
>
> tess>   E
> Tessellation
> Tessellation is determined by a factor-valued image with 11 levels
> window: binary image mask
> 128 x 128 pixel array (ny, nx)
> enclosing rectangle: [0, 1] x [0, 1] units
>
> 3. Try conversion:
>
>> as(E,"SpatialPolygons")
> Error in as(E, "SpatialPolygons") :
>  no method or default for coercing "tess" to "SpatialPolygons"
>
> 4. Try conversion with maptools:
>
>> library(maptools)
> Loading required package: foreign
> Loading required package: sp
> Loading required package: lattice
> Checking rgeos availability: TRUE
>> as(E,"SpatialPolygons")
>
> *** caught segfault ***
> address (nil), cause 'memory not mapped'
>
> I've not looked at the maptools code, but the tess object can be one
> of three types according to ?tess, and I bet the conversion code
> doesn't check what its dealing with...
>
> Barry
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

--
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
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



More information about the R-sig-Geo mailing list