获取S4对象的插槽值?

Cal*_*eng 10 r spatial s4

所以我在R中有一个spatialpolygons对象; 但我不确定为什么我无法从中检索"区域"槽.

这是我的R会议:

> spatialpolygons
An object of class "SpatialPolygons"
Slot "polygons":
[[1]]
An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] 20.50516 57.72918

Slot "area":
[1] 36.85484

Slot "hole":
[1] FALSE

Slot "ringDir":
[1] 1

Slot "coords":
         [,1]     [,2]
[1,] 16.48438 59.73633
[2,] 22.59277 61.14258
[3,] 24.74609 55.03418
[4,] 17.49512 55.12207
[5,] 16.48438 59.73633



Slot "plotOrder":
[1] 1

Slot "labpt":
[1] 20.50516 57.72918

Slot "ID":
[1] "myMultiPolygons"

Slot "area":
[1] 36.85484



Slot "plotOrder":
[1] 1

Slot "bbox":
       min      max
x 16.48438 24.74609
y 55.03418 61.14258

Slot "proj4string":
CRS arguments:
 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 

> spatialpolygons@bbox
       min      max
x 16.48438 24.74609
y 55.03418 61.14258
> spatialpolygons@area
Error: no slot of name "area" for this object of class "SpatialPolygons"
> slotNames(spatialpolygons)
[1] "polygons"    "plotOrder"   "bbox"        "proj4string"
> names(spatialpolygons)
[1] "myMultiPolygons"
Run Code Online (Sandbox Code Playgroud)

Jos*_*ien 18

首先,您应该知道@area插槽不是有关SpatialPolygons*对象实际区域的可靠信息源.如上所述?"Polygons-class",@area槽仅用作绘图的辅助(防止较小的多边形被较大的多边形覆盖),并且不尊重投影或正确解释多边形中的孔.

为了获得准确的区域,您应该使用rgeos::gArea()具有投影坐标参考系的图层或geosphere::areaPolygon()用于纬度 - 坐标参考系统(即CRS(+proj=longlat))的图层.

有了这些警告,下面显示了@area如果你确实想要它们,你如何获得插槽的内容.


主要的复杂因素是区域槽属于Polygon对象,而不属于SpatialPolygons对象(其中Polygon对象是一个元素).因此,您需要首先深入了解SpatialPolygons对象以提取到单个Polygon对象.

你已经完成了这个,你可以使用@运算符来提取区域槽的内容.

以下示例使用在程序包vignette(警告,pdf)的第7节中创建的SpatialPolygons对象:sp

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 
Run Code Online (Sandbox Code Playgroud)

  • 只是为了好玩,这也可以工作:`slot(slot(Sp,"polygons")[[1]],"area")`.:)您还可以使用`slotNames`检查插槽名称. (4认同)
  • @AndreSilva - 坐标是结构中的另一个级别,所以你必须做一些像'sapply(SpP @多边形,函数(x)坐标(x @ Polygons [[1]]))`的东西.(这些**sp**对象,虽然在某些方面设计得很好,但结果不是非常用户友好,至少在你第一次开始使用它们的时候!) (2认同)

mds*_*ner 8

您可以rgeos使用Josh的示例数据计算包中功能的区域,下面的示例.这可能更合适,因为area插槽仅用于绘图.

library(rgeos)
gArea(SpP[1,])
## [1] 5.5
gArea(SpP[2,])
##[1] 1.5
gArea(SpP[3,])
## [1] 10
Run Code Online (Sandbox Code Playgroud)

一次全部:

gArea(SpP)
[1] 17
Run Code Online (Sandbox Code Playgroud)

应考虑使用中的坐标系,这只是原始几何区域.

帮助页面讨论了area插槽.

?gArea
....
Run Code Online (Sandbox Code Playgroud)

请注意,此值可能与"多边形"类的"区域"槽不同,因为此值不会减去几何中任何孔的面积.

?"Polygons-class"
...
Run Code Online (Sandbox Code Playgroud)

'area':类"数字"的对象'; 多边形列表的总平面面积,但不是重复计算孔(从0.9-58变化 - 岛相加,忽略孔而不是减去孔); 这些值用于确保较大区域的多边形在较大区域的多边形之后绘制,不考虑投影,因为此类的对象没有定义投影