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

Roger Bivand Roger.Bivand at nhh.no
Sun Nov 11 20:07:19 CET 2012


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
> W: http://www.idp.zhaw.ch
> ..............................................................................................................
>
> _______________________________________________
> 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