[R-sig-Geo] Identifying regions on the boundary

Roger Bivand Roger.Bivand at nhh.no
Wed Mar 30 20:19:01 CEST 2011


On Wed, 30 Mar 2011, Barry Rowlingson wrote:

> 2011/3/30 Virgilio Gómez-Rubio <virgilio.gomez at uclm.es>:
>> Barry,
>>
>> What you propose is what I would do too. Another approach is to display
>> the adjacency matrix as a graph (i.e., vertices for the "location" of
>> the country and edges for the connections between them). I would say
>> that all the countries whose vertex can be surrounded by edges will be
>> land-locked, so that the remaining countries will be non-landlocked.
>>
>
> Here's a two-liner that seems to work but uses gRelate to compute all
> the inner/boundary/exterior relations so is surely overkill since I
> only care about the boundary. I use gUnionCascaded to get the outer
> 'coastline', so the coordinates should be exactly those of the
> constituent countries, and hence no slivers...
>
> edgeFinder <- function(sp){
>  outer = gUnionCascaded(sp)
>  substr(gRelate(sp,outer,byid=TRUE)[1,],5,5)!="F"
>  }
>
> The horrible substr expression gets the right part of the DE9IM string. 
> I think.

library(maptools)
library(spgrass6)
xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1],
   IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
SG <- Sobj_SpatialGrid(xx)$SG
initGRASS("/home/rsb/topics/grass/g640/grass-6.4.0", home=tempdir(),
   SG=SG)
writeVECT6(xx, "nc", v.in.ogr_flags="o")
res <- vect2neigh("nc")
library(rgeos)
opar <- par(mfrow=c(2,1))
plot(xx, col=(attr(res, "external") != 0)+1)
plot(xx, col=edgeFinder(xx)+1)
par(opar)

shows only two differences for counties touching the exterior at only one 
coordinate (GRASS is taking boundary length):

as.character(xx$NAME[which(edgeFinder(xx) != (attr(res, "external") != 
0))])

This works for spgrass6 on R-Forge SVN, there was a small issue in 
vect2neigh() that ought to have been fixed before, and now is.

Nice to see rgeos earning its keep!

Roger

>
> Barry
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, 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