[R] Extract a slot value from a 'SpatialPolygons' class object

David Winsemius dwinsemius at comcast.net
Fri Feb 11 17:22:03 CET 2011


On Feb 11, 2011, at 5:11 AM, Xavier Hoenner wrote:

> Dear R-users,
>
> I’m currently trying to extract the value of a slot (area) but can’t  
> find out how to do that.

Generally the authors of S4 method provide extractor functions, so the  
first question would be what functions were used to create the object.  
Fortunately there are big hints in the first line of str() results.  
The documentation for package sp says there is a summary method for  
that class as well as a "[" method. Those results may offer avenues  
for progresss

These might yield objects that could be further used:  
area(overlperc[1]) or polygons(overlperc) but my efforts at finding an  
area-extractor in the sp vignettes and RSiteSearch were not fulfilled.  
A brute force method is to use the primitive S4 extractor function "@"  
to traverse the tree:

So you might try either of these:

polygons(overlperc)[[1]]@area  #didn't work on the test object I made  
with one of the examples in pkg::sp

This appears more likely to succeed:

overlperc at polygons[[1]]@area

There's also a specific mailing list for geospatial stats.

-- 
David.

>
>> str(overlperc)
>
> List of 1
> $ :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] 709374 -1507888
>  .. .. .. .. .. .. .. ..@ area   : num 11542604
>  .. .. .. .. .. .. .. ..@ hole   : logi FALSE
>  .. .. .. .. .. .. .. ..@ ringDir: int 1
>  .. .. .. .. .. .. .. ..@ coords : num [1:23, 1:2] 706840 706657  
> 706840 707294 707585 ...
>  .. .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
>  .. .. .. .. .. .. .. .. .. ..$ : chr [1:23] "1" "2" "3" "4" ...
>  .. .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
>  .. .. .. .. ..@ plotOrder: int 1
>  .. .. .. .. ..@ labpt    : num [1:2] 709374 -1507888
>  .. .. .. .. ..@ ID       : chr "1"
>  .. .. .. .. ..@ area     : num 11542604
>  .. ..@ plotOrder  : int 1
>  .. ..@ bbox       : num [1:2, 1:2] 706657 -1509411 711710 -1506189
>  .. .. ..- attr(*, "dimnames")=List of 2
>  .. .. .. ..$ : chr [1:2] "x" "y"
>  .. .. .. ..$ : chr [1:2] "min" "max"
>  .. ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
>  .. .. .. ..@ projargs: chr NA
>
> I’d like to extract the area value so as to be able to use this  
> value for further analysis. Here’s below what I get when I only type  
> the name of the object:
>
>> overlperc
>
> [[1]]
> An object of class "SpatialPolygons"
> Slot "polygons":
> [[1]]
> An object of class "Polygons"
> Slot "Polygons":
> [[1]]
> An object of class "Polygon"
> Slot "labpt":
> [1]   709374 -1507888
>
> Slot "area":
> [1] 11542604
>
> Slot "hole":
> [1] FALSE
>
> Slot "ringDir":
> [1] 1
>
> Slot "coords":
>          x        y
> 1  706839.8 -1508654
> 2  706657.2 -1508029
> 3  706839.8 -1507634
> 4  707293.6 -1507284
> 5  707584.7 -1507174
> 6  708329.6 -1506851
> 7  709013.3 -1506539
> 8  709074.5 -1506513
> 9  709819.5 -1506189
> 10 710564.4 -1506289
> 11 711021.1 -1506539
> 12 711309.3 -1506769
> 13 711642.0 -1507284
> 14 711709.8 -1508029
> 15 711309.3 -1508690
> 16 711246.4 -1508774
> 17 710564.4 -1509347
> 18 709819.5 -1509411
> 19 709074.5 -1509277
> 20 708329.6 -1509072
> 21 707584.7 -1509039
> 22 706927.4 -1508774
> 23 706839.8 -1508654
>
>
>
> Slot "plotOrder":
> [1] 1
>
> Slot "labpt":
> [1]   709374 -1507888
>
> Slot "ID":
> [1] "1"
>
> Slot "area":
> [1] 11542604
>
>
>
> Slot "plotOrder":
> [1] 1
>
> Slot "bbox":
>         min        max
> x   706657.2   711709.8
> y -1509411.3 -1506189.0
>
> Slot "proj4string":
> CRS arguments: NA
>
> I’m stuck and spent more or less the whole afternoon online trying  
> to find a solution to my problem but I couldn’t find anything. I  
> look forward to hear from some of you. Thanks in advance for your  
> kind help.
>
> Have a good weekend.
>
> Cheers
>
>
> Xavier
>
>
> Xavier Hoenner
> PhD Student
> Nesting Ecology, Migrations and Diving Behaviour of Hawksbill Turtles
> School of Environmental Research, Charles Darwin University
> Darwin, NT 0909
> Ph: (08)8946.7721
> email:xavier.hoenner at cdu.edu.au
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list