[R-sig-Geo] polygon adjacency testing

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Fri Aug 13 16:37:22 CEST 2010


I've used poly2nb from spdep quite a bit, and occasionally it's shown
up some errors in polygon data sets. For example, in the scottish data
distributed with rgdal you'll see some rather wacky adjacencies:

library(rgdal)
scot=readOGR(system.file("vectors",package="rgdal"),"scot_BNG")

library(spdep)
nb = poly2nb(scot)

plot(scot)
plot(nb,coordinates(scot),add=TRUE,col="blue")

There are some adjacency lines that go right across scotland (these
would be clearer if you could choose different colours for each
adjacency line, but it seems the plot method only uses the first
colour of the col vector). Further investigation shows them to be due
to slivers adjacent to areas remote from the main area, but in the
same feature.

 Anyway, inspection of this kind of adjacency line map shows that in
general, the adjacency lines drawn between centroids tend not to cross
over one another if the data is well-behaved. So I started thinking
about how to test if a set of line segments overlap.

 The rgeos LinesIntersection function almost works, except it counts
two lines as intersecting if they start or end at the same point.
Since so many adjacency lines do this, it might be hard to filter out
false positives, although maybe it could be done since you know the
polygon feature id and there's no point testing the intersection of
lines with the same polygon feature id... hmmm thinking out loud here
but maybe it will work...

 So anyway, anyone got an easy way of going from nb and
coordinates(scot) to something that can be fed to
rgeos:LinesIntersection to see which adjacency lines cross?

Barry

-- 
blog: http://geospaced.blogspot.com/
web: http://www.maths.lancs.ac.uk/~rowlings
web: http://www.rowlingson.com/
twitter: http://twitter.com/geospacedman
pics: http://www.flickr.com/photos/spacedman



More information about the R-sig-Geo mailing list