[R-sig-Geo] Colouring maps so that adjacent polygons differ in colour

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Wed Apr 6 12:16:00 CEST 2011


On Wed, Apr 6, 2011 at 9:54 AM, Karl Ove Hufthammer <karl at huftis.org> wrote:

> The ‘poly2nb’ function have a ‘snap’ parameter, set to
> ‘sqrt(.Machine$double.eps)’ by default. Increasing this
> should make the two regions treated as connected.
>
> I guess it’s more surprising that ‘rgeos’ was treating
> the two regions as connected by default.
>
> BTW, when talking about the ‘rgeos functions’, do you mean
> applying for example ‘gTouches’ to the two regions, or are there
> actually a ‘poly2nb’-like function available?

 Here's what happens with 3 polygons extracted from the wider region:

 > poly2nb(gT)
Neighbour list object:
Number of regions: 3
Number of nonzero links: 2
Percentage nonzero weights: 22.22222
Average number of links: 0.6666667
1 region with no links:
1360

 - so that shows one region not connected to the others. Up the snap
(which I suppose I should have noticed) to 1 metre:

> poly2nb(gT,snap=1)
Neighbour list object:
Number of regions: 3
Number of nonzero links: 6
Percentage nonzero weights: 66.66667
Average number of links: 2

 yay, everything is connected.

With rgeos, there's gTouches:

 > gTouches(gT,byid=TRUE)
      1360  2474  2475
1360 FALSE FALSE FALSE
2474 FALSE FALSE  TRUE
2475 FALSE  TRUE FALSE

 - which I think is in agreement with the poly2nb with default
tolerance. If I use gIntersects:

 > gIntersects(gT,byid=TRUE)
     1360 2474 2475
1360 TRUE TRUE TRUE
2474 TRUE TRUE TRUE
2475 TRUE TRUE TRUE

 everything is connected. Now that is because "intersects" is a
different spatial operation to "touches", since it is true for
overlapping polygons that may have no common boundary, and 'touches'
is only true if they share common points. I've not had a chance to
examine the intersections of the polygons yet.

 I guess the best thing to use is poly2nb with a good sized snap.

Barry



More information about the R-sig-Geo mailing list