# [R-sig-Geo] Shared polygon border

Roger Bivand Roger.Bivand at nhh.no
Tue Oct 14 21:56:43 CEST 2008

```On Tue, 14 Oct 2008, Jonathan Boyd Thayn wrote:

> I appreciate the "simple" explanation, which does make since.  I am
> still unable, however, to access the "coords" matrices inside of the
> SpatialPolyons object.  I'm sure that there is a simple way to do
> this, but I can't figure it out.  If I open a shapefile using
> readOGR() what code to I use to access the node coordinates?  Thanks

For example:

library(rgdal)
scot_BNG <- readOGR(system.file("vectors", package = "rgdal")[1],
"scot_BNG")
length(slot(scot_BNG, "polygons"))
sapply(slot(scot_BNG, "polygons"), function(x) length(slot(x,
"Polygons")))

slot(slot(slot(scot_BNG, "polygons")[[1]], "Polygons")[[1]], "coords")

retrieves the coordinate ring of the first Polygon object in the first
Polygons object in the SpatialPolygons object scot_BNG. So:

res <- lapply(slot(scot_BNG, "polygons"), function(x) lapply(slot(x,
"Polygons"), function(y) slot(y, "coords")))

gets all of the coordinates into a list of lists of matrices. However, it
doesn't tell you which are nodes in an arc-node topology (points at which
three or more polygon entities (including the external polygon) meet.
Getting the coordinate rings doesn't give you the nodes - for that you
have to build the topology, which is hard computational geometry best done
in a GIS like GRASS or ArcGIS.

Hope this helps,

Roger

>
> Jon Thayn
>
>
> On Oct 13, 2008, at 2:33 PM, Roger Bivand wrote:
>
>> On Mon, 13 Oct 2008, Jonathan Boyd Thayn wrote:
>>
>>> I want to know the length of the shared border of neighboring
>>> polygons
>>> in a shapefile.  I can use the slots () function to access the the
>>> "polyons" slot of the file, but I can't seem to create a list of the
>>> node coordinates of the polyon.  Is there a way to do this easily?
>>
>> I imagine that you mean the end nodes of arcs with known left and
>> right neighbours? The actual ring coordinates are simply the
>> "coords" slots of each "Polygon" object in the list in the
>> "Polygons" slot of each "Polygons" object in the list in the
>> "polygons" slot of the SpatialPolygons object (for a very special
>> definition of "simple"). But they wouldn't help much, because you
>> need to build the topology first. If you have access to ArcGIS, you
>> can push the polygons out for Arc as a shapefile, convert them into
>> a coverage (if they build cleanly) and get the coverage back into R
>> using readOGR as both polygons and arcs:
>>
>> http://www.asdar-book.org/exercises.php?excode=3
>>
>> and link therein. Getting the lengths from the arcs could use
>> SpatialLinesLengths() in sp, and add the values to the
>> SpatialLinesDataFrame; this function assumes planar coordinates at
>> present. The coverage2nb() would need gentle editing to return a
>> list of lengths in the same format as the nb object.
>>
>> If GRASS is OK, look at vect2neigh() in the spgrass6 package by
>> Markus Neteler - it does what you want if the topology builds
>> cleanly, and possibly not on all platforms.
>>
>> Hope this helps,
>>
>> Roger
>>
>>
>>> Thanks.
>>>
>>>
>>> Jonathan B. Thayn
>>> Ph.D. Candidate – Geography and Remote Sensing
>>> University of Kansas
>>> 2101 Constant Avenue, Higuchi Hall, Rm 121
>>> Lawrence, Kansas 66047-3759
>>> jonthayn at ku.edu
>>> http://people.ku.edu/~jonthayn
>>>
>>>
>>> 	[[alternative HTML version deleted]]
>>>
>>>
>>
>> --
>> Roger Bivand
>> Economic Geography Section, Department of Economics, Norwegian
>> School of
>> Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
>> e-mail: Roger.Bivand at nhh.no
>
>
> 	[[alternative HTML version deleted]]
>
>

--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of