[R-sig-Geo] method intersect for non-overlapping polygons

Hofer Christoph Jean-Charles christoph.hofer at env.ethz.ch
Sun Nov 11 22:10:21 CET 2012


I agree with you, gInterse… is a simple and working solution for my example. However, rgeos provides the class "gpc.poly" and the method "intersect".
So, my question was only whether the behavior of intersect for non-overlapping polygons is an unknown error or intended.
After the reply from Roger the answer is clear.
Am 11.11.2012 um 21:25 schrieb Barry Rowlingson <b.rowlingson at gmail.com>:

> Isn't another solution to simply use rgeos:gIntersection when intersecting sp polygon objects and not using gpclib?
> 
> On Nov 11, 2012 7:08 PM, "Roger Bivand" <Roger.Bivand at nhh.no<mailto:Roger.Bivand at nhh.no>> wrote:
> On Sun, 11 Nov 2012, Hofer  Christoph Jean-Charles wrote:
> 
>> Dear List
>> 
>> The following simple example works fine
>> 
>> require( sp )
>> require( gpclib )
>> 
>> t.c1 <- matrix( c(1,5,5,1,1,1,1,5,5,1), ncol = 2)
>> t.c2 <- t.c1
>> t.c2[,1] <- t.c1[,1] + 10
>> t.c2[,2] <- t.c1[,2] +  10
>> t.p1 <- Polygon( t.c1 )
>> t.p2 <- Polygon( t.c2 )
>> t.p1 <- Polygons( srl = list( t.p1 ) , ID = "A")
>> t.p2 <- Polygons( srl = list( t.p2 ) , ID = "B")
>> 
>> t.sp1 <- SpatialPolygons( list( t.p1 ) )
>> t.sp2 <- SpatialPolygons( list( t.p2 ) )
>> 
>> plot( SpatialPolygons(list(t.p1,t.p2)) )
>> 
>> 
>> g1 <- as( t.sp1 at polygons[[1]]@Polygons[[1]]@coords, "gpc.poly")
>> g2 <- as(  t.sp2 at polygons[[1]]@Polygons[[1]]@coords, "gpc.poly")
>> 
>> intersect( g1, g2 )
>> 
>> The result is 0.
>> 
>> However, if I use "rgeos" instead of "gpclib" I get the following error
>> message:
>> 
>> Error in as(spres, "gpc.poly") :
>> no method or default for coercing "NULL" to "gpc.poly"
>> 
>> If the polygons t.p1 and t.p2 overlap both packages provide the same and
>> right result.
>> 
>> 
>> Is this behavior of the intersect method in rgeos a bug or a desired
>> property?
> 
> It is an unforeseen consequence of method despatch when the class of the
> returned object is NULL. If you can install rgeos from source, including
> the external dependencies, try revision 358 from the R-forge rgeos
> project, which traps the NULL and returns an empty gpc.poly object. If
> not, wait for a CRAN submission or if urgent enclose the intersect (or
> other) in try() or similar, a "try-error" return means no intersection. If
> you could check the rgeos commit from R-forge, that would be helpful -
> thanks for a good reproducible error case.
> 
> Roger
> 
>> 
>> 
>> regeos version 0.2-6
>> gpclib versoin 1.5-1
>> 
>> sessionInfo()
>> 
>> R version 2.15.2 (2012-10-26)
>> Platform: i386-apple-darwin9.8.0/i386 (32-bit)
>> 
>> locale:
>> [1] C
>> 
>> attached base packages:
>> [1] stats     graphics  grDevices utils     datasets  methods   base
>> 
>> other attached packages:
>> [1] gpclib_1.5-1 rgeos_0.2-6  plyr_1.7.1   stringr_0.6  sp_0.9-99
>> 
>> loaded via a namespace (and not attached):
>> [1] compiler_2.15.2 grid_2.15.2     lattice_0.20-6  tools_2.15.2
>> 
>> 
>> 
>> Best regards
>> 
>> 
>> Christoph
>> 
>> 
>> ..............................................................................................................
>> Dr. Christoph Hofer
>> Projektleiter F&E
>> Institut für Datenanalyse und Prozessdesign (IDP)
>> ZHAW Zürcher Hochschule für Angewandte Wissenschaften
>> Rosenstrasse 3, Postfach
>> CH-8401 Winterthur
>> T: +41 (0)58 934 69 40
>> F: +41 (0)58 935 69 40
>> E: christoph.hofer at zhaw.ch<mailto:christoph.hofer at zhaw.ch>
>> W: http://www.idp.zhaw.ch
>> ..............................................................................................................
>> 
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org<mailto: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<tel:%2B47%2055%2095%2093%2055>; fax +47 55 95 95 43<tel:%2B47%2055%2095%2095%2043>
> e-mail: Roger.Bivand at nhh.no<mailto:Roger.Bivand at nhh.no>



More information about the R-sig-Geo mailing list