Getting a slot's value of S4 objects?

后端 未结 2 1364
野的像风
野的像风 2020-12-14 23:04

So I have a spatialpolygons object in R; but I am not sure why I am unable to retrieve the \"area\" slot from it.

Here\'s my R session:

> spatialp         


        
相关标签:
2条回答
  • 2020-12-15 00:00

    First off, you should be aware that the @area slot is not a reliable source of information about a SpatialPolygons* object's actual area. As noted in ?"Polygons-class", the @area slot is just used as an adjunct to plotting (preventing smaller polygons from getting painted over by larger ones) and does not respect projection or properly account for holes in polygons.

    To get accurate areas, you should instead use rgeos::gArea() for layers with projected coordinate reference systems or geosphere::areaPolygon() for those in lat-long coordinate reference systems (i.e. CRS(+proj=longlat)).

    With those caveats out of the way, the following shows how you can get the contents of the @area slots if you do in fact want them.


    The main complication is that the area slot belongs to the Polygon object, not to the SpatialPolygons object (of which the Polygon object is one element). You thus need to first dig down into the SpatialPolygons object to extract to the individual Polygon object(s).

    One you have done that, you can just use the @ operator to extract the contents of the area slot.

    The following example uses the SpatialPolygons object created in Section 7 of the sp package vignette (warning, pdf):

    require(sp)
    # Example pasted in from Section 7 of the sp vignette
    Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))
    Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
    Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5)))
    Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)
    Srs1 = Polygons(list(Sr1), "s1")
    Srs2 = Polygons(list(Sr2), "s2")
    Srs3 = Polygons(list(Sr3, Sr4), "s3/4")
    SpP = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3)
    
    # To extract the area of the first (or in your case only) Polygon
    SpP@polygons[[1]]@area
    # [1] 5.5
    
    # Extract the areas of all three component Polygons
    sapply(SpP@polygons, function(x) x@area)
    # [1]  5.5  1.5 10.0
    
    ## For areas, rgeos::gArea() or geosphere::areaPolygons() are generally more appropriate
    ## (Note, for instance, that it properly accounts for the hole in the 3rd polygon.)
    rgeos::gArea(SpP, byid=TRUE)
    #  s1   s2 s3/4 
    # 5.5  1.5  9.0 
    
    0 讨论(0)
  • 2020-12-15 00:02

    You can calculate area with functions in the rgeos package, examples below using Josh's example data. This might be more appropriate since the area slot is merely for plotting.

    library(rgeos)
    gArea(SpP[1,])
    ## [1] 5.5
    gArea(SpP[2,])
    ##[1] 1.5
    gArea(SpP[3,])
    ## [1] 10
    

    All at once:

    gArea(SpP)
    [1] 17
    

    The coordinate system in use should be considered, this is just raw geometric area.

    The help pages discuss the area slot.

    ?gArea
    ....
    

    Note that this value may be different from the ‘area’ slot of the ‘Polygons’ class as this value does not subtract the area of any holes in the geometry.

    ?"Polygons-class"
    ...
    

    ‘area’: Object of class ‘"numeric"’; the gross total planar area of the Polygon list but not double-counting holes (changed from 0.9-58 - islands are summed, holes are ignored rather than subtracted); these values are used to make sure that polygons of a smaller area are plotted after polygons of a larger area, does not respect projection as objects of this class have no projection defined

    0 讨论(0)
提交回复
热议问题