[R] Retrieving Vertices Coordinates from SpatialPolygons

David Winsemius dwinsemius at comcast.net
Sat Mar 21 15:45:23 CET 2009


On Mar 21, 2009, at 10:31 AM, Barry Rowlingson wrote:

> On Sat, Mar 21, 2009 at 12:33 PM, Enrico R. Crema  
> <e.crema at ucl.ac.uk> wrote:
>> Dear List,
>>
>> I'm trying to use different R packages for my Teaching Assistantship
>> classes. And I cam out to an (apparently) very simple problem. I  
>> would
>> like to retrieve the vertices coordinate of a SpatialPolygon data. I
>> know this is stored in the "coords" slot, but I can't get access to
>> it! I tried to coerce the SpatialPolygon into a data.frame but it
>> doesn't work. Want I want is just a list of x and y coordinates of my
>> polygon vertices without doing the workflow in GRASS!!!
>
> There's not really such thing as a SpatialPolygon - there's only
> SpatialPolygons - so maybe you've got a SpatialPolygons object with
> only one feature in it..
>
> str(thing) will help here. This is what I get if I str() something
> that is a SpatialPolygons object with only one feature in it:
>
>> str(poly)
> Formal class 'SpatialPolygons' [package "sp"] with 4 slots
>  ..@ polygons   :List of 1
>  .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
>  .. .. .. ..@ Polygons :List of 1
>  .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
>  .. .. .. .. .. .. ..@ labpt  : num [1:2] 1 10
>  .. .. .. .. .. .. ..@ area   : num 1
>  .. .. .. .. .. .. ..@ hole   : logi FALSE
>  .. .. .. .. .. .. ..@ ringDir: int 1
>  .. .. .. .. .. .. ..@ coords : num [1:5, 1:2] 0.5 0.5 1.5 1.5 0.5
> 9.5 10.5 10.5 9.5 9.5
>  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
>  .. .. .. .. .. .. .. .. ..$ : chr [1:5] "s1" "s1" "s1" "s1" ...
>  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
>  .. .. .. ..@ plotOrder: int 1
>  .. .. .. ..@ labpt    : num [1:2] 1 10
>  .. .. .. ..@ ID       : chr "g1"
>  .. .. .. ..@ area     : num 1
>  ..@ plotOrder  : int 1
>  ..@ bbox       : num [1:2, 1:2] 0.5 9.5 1.5 10.5
>  .. ..- attr(*, "dimnames")=List of 2
>  .. .. ..$ : chr [1:2] "r1" "r2"
>  .. .. ..$ : chr [1:2] "min" "max"
>  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
>  .. .. ..@ projargs: chr NA
>
> I'll explain, stepping through the structure breadth first, and  
> backwards:
>
> @proj4string is the coordinate reference system slot
> @bbox is the bounding box slot
> @plotOrder is the order to plot the polygons
> @polygons is the list of Polygons objects.
>
> @polygons[[1]] is the first (and in this case, only) feature. It is
> an object of class 'Polygons' (non-spatial, since there's no
> @proj4string in this part of the structure).
>
> @polygons[[1]]@Polygons is the 'Polygons' slot of class 'Polygons',
> and is a list of rings that make up the feature.
>
> @polygons[[1]]@Polygons[[1]] is an object of class 'Polygon'.
>
> @polygons[[1]]@Polygons[[1]]@coords is the coordinates of the Polygon:
>
>> poly at polygons[[1]]@Polygons[[1]]@coords
>     x    y
> s1 0.5  9.5
> s1 0.5 10.5
> s1 1.5 10.5
> s1 1.5  9.5
> s1 0.5  9.5
>
> It's complex because the SpatialPolygons class can hold multiple
> features (such as administrative subdivisions), and each feature can
> be made from multiple rings (islands, holes). It seems overkill when
> all you really want is to store a single simple closed polygon though!
> But now you know how to get it. There may be methods for some of these
> @slot calls.

The last example on the following page suggests to me that there is a  
coordinates function for this class, although I do not see it  
mentioned in the body of the class description:

http://finzi.psych.upenn.edu/R/library/sp/html/SpatialPolygons-class.html

>
>
> Personally, I forget this everytime, but str() is immensely useful.
> As is a copy of the Bivand/Pebesma/Gomez-Rubio book!
>
> Barry



David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list