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

O'Hanlon, Simon J simon.ohanlon at imperial.ac.uk
Mon Oct 29 16:19:17 CET 2012


sorry - forgot to cc to list...

Dear Roger,
Thank you, very much. With maptools svn revision 239 it does indeed run through my script without crashing now.

Many thanks,

Simon


On Mon, 29 Oct 2012, Roger Bivand wrote:

> On Mon, 29 Oct 2012, O'Hanlon, Simon J wrote:
>
>> Further to this problem, I have experienced some more odd behaviour with
>> the coercion from tess to sp.
>>
>> My real dataset is 3607 points within the observation window. If I try and
>> run this straight away I get a segfault crash with the error message:
>>
>> *** caught segfault ***
>> address 0x0, cause 'memory not mapped'
>>
>> Traceback:
>> 1: .Call("Polygons_c", srl, ID, PACKAGE = "sp")
>> 2: Polygons(pieces, id)
>> 3: owin2Polygons(y[[i]], nam[i])
>> 4: as.SpatialPolygons.tess(from)
>> 5: asMethod(object)
>> 6: as(v, "SpatialPolygons")
>>
>> This is using the spatstat.options(gpclib=T) workaround posted by Roger.
>> However if I take a small sample of say 50 rows from the dataset, run the
>> script on these and coerce from tess to sp it runs fine. Interestingly I
>> can then re-read the data file and run the analysis on all 3607 points
>> within the same R session and corece to sp with no problems at all. However
>> it crashes every time if I don't 'prime' the function with a small run
>> dataset first?!
>>
>> I can try to provide a reproducible example if that will help more? (the
>> easiest way is if I can share my datapoint locations, but I have to ask
>> permission first). Otherwise I will try to recreate this from within R.
>
> Yes, the specific failing data set is the best bet; if passed through an
> off-list communication, it can be handled with discretion. Could you also
> perhaps update sp from R-Forge if you can install a source package (you'll
> need to check out from anonymous SVN, see this page:

With your data set, the crash is prevented with the development version of
sp (R-forge revision 1300). I've added a try() in maptools in the coercion
method from tess to SpatialPolygons, and have established that zero-length
pieces are created when the tile for a tesselation has a single raster
cell TRUE. In cases with > 1 cell, as.polygonal() returns a "polygonal"
object, but where it is just one, it returns a "rectangle" object, which
doesn't have the required components - this may well have been the
underlying problem all the time, as Barry suggested.

I've added code to owin2Polygons() called by the coercion method to branch
on the object type returned by as.polygonal(), and your example now works,
admittedly using gpclib in as.polygonal(), which it might be appropriate
to do something about. Try with R-forge maptools check out, revision 239.

Roger

>
> https://r-forge.r-project.org/scm/?group_id=1014
>
> or if you like I can get a Windows binary package run up on Win-builder.
>
> I do see that with the patched sp, and points generated by:
>
> set.seed(1)
> p <- coordinates(spsample(africa, type="random", n=3500))
> spatstat.options("gpclib"=TRUE)
>
> the result is OK, but for some other seeds, a degenerate pieces is created
> and I see:
>
>> vp <- as( v , "SpatialPolygons" )
> Error: length(srl) > 0 is not TRUE
>
> So the revised sp would trap the error, I think. However, it would be useful
> to debug this as far as possible.
>
> Roger
>
>>
>> Thanks,
>>
>> Simon
>>
>> ________________________________________
>> From: Roger Bivand [Roger.Bivand at nhh.no]
>> Sent: 28 October 2012 13:43
>> To: O'Hanlon, Simon J
>> Cc: Barry Rowlingson; 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, O'Hanlon, Simon J wrote:
>>
>>> 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.
>>
>> Simon,
>>
>> With a good reproducible example available, life is much easier, and in
>> fact this was a real crash, so sp will be improved (made safer) thanks to
>> your posting. Making things work better is shared, really, as this case
>> shows.
>>
>> Roger
>>
>>>
>>> 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
>>
>> --
>> 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
>
>

--
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