【问题标题】:How to retrieve a slot coords of each spatialPpolygon object如何检索每个 spatialPpolygon 对象的槽坐标
【发布时间】:2014-10-11 18:29:44
【问题描述】:

我在 R 中有一个 spatialPolygons 对象;但我无法检索每个 spatialPolygon 对象的“坐标”

这是我的 R 会话:

 [[1]]
    An object of class "Polygon"
    Slot "labpt":
    [1]  5.7828 20.5764

    Slot "area":
    [1] 0.16

    Slot "hole":
    [1] FALSE

    Slot "ringDir":
    [1] 1

    Slot "coords":
               x       y
     [1,] 5.6828 20.7764
     [2,] 5.7828 20.7764
     [3,] 5.8828 20.7764
     [4,] 5.9828 20.7764
     [5,] 5.9828 20.3764
     [6,] 5.8828 20.3764
     [7,] 5.7828 20.3764
     [8,] 5.6828 20.3764
     [9,] 5.5828 20.3764
    [10,] 5.5828 20.7764
    [11,] 5.6828 20.7764


    [[2]]
    An object of class "Polygon"
    Slot "labpt":
    [1]  3.715865 35.160271

    Slot "area":
    [1] 39.68

    Slot "hole":
    [1] FALSE

    Slot "ringDir":
    [1] 1

    Slot "coords":
                 x       y
      [1,] -0.7172 35.1764
      [2,] -0.8172 35.1764
      [3,] -0.9172 35.1764
      [4,] -1.0172 35.1764
      [5,] -1.1172 35.1764
      [6,] -1.1172 35.5764
      [7,] -1.2172 35.5764
      [8,] -1.2172 35.9764
      [9,] -1.2172 36.3764
     [10,] -1.3172 36.3764
     [11,] -1.3172 36.7764
     [12,] -1.4172 36.7764
     [13,] -1.5172 36.7764
     [14,] -1.6172 36.7764
     [15,] -1.6172 37.1764
     [16,] -1.5172 37.1764
     [17,] -1.4172 37.1764
     [18,] -1.3172 37.1764
     [19,] -1.2172 37.1764
     [20,] -1.1172 37.1764
     [21,] -1.0172 37.1764
     [22,] -0.9172 37.1764
     [23,] -0.8172 37.1764
     [24,] -0.7172 37.1764
     [25,] -0.6172 37.1764
     [26,] -0.5172 37.1764
     [27,] -0.4172 37.1764
     [28,] -0.3172 37.1764
     [29,] -0.2172 37.1764
     [30,] -0.1172 37.1764
     [31,] -0.0172 37.1764
     [32,]  0.0828 37.1764
     [33,]  0.1828 37.1764
     [34,]  0.2828 37.1764
     [35,]  0.3828 37.1764
     [36,]  0.4828 37.1764
     [37,]  0.5828 37.1764
     [38,]  0.6828 37.1764
     [39,]  0.7828 37.1764
     [40,]  0.8828 37.1764
     [41,]  0.9828 37.1764
     [42,]  1.0828 37.1764
     [43,]  1.1828 37.1764
     [44,]  1.2828 37.1764
     [45,]  1.3828 37.1764
     [46,]  1.4828 37.1764
     [47,]  1.5828 37.1764
     [48,]  1.6828 37.1764
     [49,]  1.7828 37.1764
     [50,]  1.8828 37.1764
     [51,]  1.9828 37.1764
     [52,]  2.0828 37.1764
     [53,]  2.1828 37.1764
     [54,]  2.2828 37.1764
     [55,]  2.3828 37.1764
     [56,]  2.4828 37.1764
     [57,]  2.5828 37.1764
     [58,]  2.6828 37.1764
     [59,]  2.7828 37.1764
     [60,]  2.8828 37.1764
     [61,]  2.9828 37.1764
     [62,]  3.0828 37.1764
     [63,]  3.1828 37.1764
     [64,]  3.2828 37.1764
     [65,]  3.3828 37.1764
     [66,]  3.4828 37.1764
     [67,]  3.5828 37.1764
     [68,]  3.6828 37.1764
     [69,]  3.7828 37.1764
     [70,]  3.8828 37.1764
     [71,]  3.9828 37.1764
     [72,]  4.0828 37.1764
     [73,]  4.1828 37.1764
     [74,]  4.2828 37.1764
     [75,]  4.3828 37.1764
     [76,]  4.4828 37.1764
     [77,]  4.5828 37.1764
     [78,]  4.6828 37.1764
     [79,]  4.7828 37.1764
     [80,]  4.8828 37.1764
     [81,]  4.9828 37.1764
     [82,]  5.0828 37.1764
     [83,]  5.1828 37.1764
     [84,]  5.2828 37.1764
     [85,]  5.3828 37.1764
     [86,]  5.4828 37.1764
     [87,]  5.5828 37.1764
     [88,]  5.6828 37.1764
     [89,]  5.7828 37.1764
     [90,]  5.8828 37.1764
     [91,]  5.9828 37.1764
     [92,]  6.0828 37.1764
     [93,]  6.1828 37.1764
     [94,]  6.2828 37.1764
     [95,]  6.3828 37.1764
     [96,]  6.4828 37.1764
     [97,]  6.5828 37.1764
     [98,]  6.6828 37.1764
     [99,]  6.7828 37.1764
    [100,]  6.8828 37.1764
    [101,]  6.9828 37.1764
    [102,]  7.0828 37.1764
    [103,]  7.1828 37.1764
    [104,]  7.2828 37.1764
    [105,]  7.3828 37.1764
    [106,]  7.4828 37.1764
    [107,]  7.5828 37.1764
    [108,]  7.6828 37.1764
    [109,]  7.7828 37.1764
    [110,]  7.8828 37.1764
    [111,]  7.9828 37.1764
    [112,]  8.0828 37.1764
    [113,]  8.1828 37.1764
    [114,]  8.2828 37.1764
    [115,]  8.3828 37.1764
    [116,]  8.4828 37.1764
    [117,]  8.5828 37.1764
    [118,]  8.6828 37.1764
    [119,]  8.7828 37.1764
    [120,]  8.8828 37.1764
    [121,]  8.9828 37.1764
    [122,]  9.0828 37.1764
    [123,]  9.1828 37.1764
    [124,]  9.2828 37.1764
    [125,]  9.3828 37.1764
    [126,]  9.4828 37.1764
    [127,]  9.5828 37.1764
    [128,]  9.6828 37.1764
    [129,]  9.7828 37.1764
    [130,]  9.8828 37.1764
    [131,]  9.9828 37.1764
    [132,] 10.0828 37.1764
    [133,] 10.1828 37.1764
    [134,] 10.2828 37.1764
    [135,] 10.3828 37.1764
    [136,] 10.4828 37.1764
    [137,] 10.5828 37.1764
    [138,] 10.6828 37.1764
    [139,] 10.7828 37.1764
    [140,] 10.8828 37.1764
    [141,] 10.9828 37.1764
    [142,] 11.0828 37.1764
    [143,] 11.1828 37.1764
    [144,] 11.2828 37.1764
    [145,] 11.3828 37.1764
    [146,] 11.4828 37.1764
    [147,] 11.5828 37.1764
    [148,] 11.6828 37.1764
    [149,] 11.7828 37.1764
    [150,] 11.8828 37.1764
    [151,] 11.9828 37.1764
    [152,] 11.9828 36.7764
    [153,] 11.9828 36.3764
    [154,] 11.9828 35.9764
    [155,] 11.9828 35.5764
    [156,] 11.9828 35.1764
    [157,] 11.8828 35.1764
    [158,] 11.7828 35.1764
    [159,] 11.6828 35.1764
    [160,] 11.5828 35.1764
    [161,] 11.4828 35.1764
    [162,] 11.3828 35.1764
    [163,] 11.2828 35.1764
    [164,] 11.1828 35.1764
    [165,] 11.0828 35.1764
    [166,] 10.9828 35.1764
    [167,] 10.8828 35.1764
    [168,] 10.7828 35.1764
    [169,] 10.6828 35.1764
    [170,] 10.5828 35.1764
    [171,] 10.4828 35.1764
    [172,] 10.3828 35.1764
    [173,] 10.2828 35.1764
    [174,] 10.1828 35.1764
    [175,] 10.0828 35.1764
    [176,]  9.9828 35.1764
    [177,]  9.8828 35.1764
    [178,]  9.7828 35.1764
    [179,]  9.6828 35.1764
    [180,]  9.5828 35.1764
    [181,]  9.4828 35.1764
    [182,]  9.3828 35.1764
    [183,]  9.2828 35.1764
    [184,]  9.2828 35.5764
    [185,]  9.1828 35.5764
    [186,]  9.0828 35.5764
    [187,]  8.9828 35.5764
    [188,]  8.8828 35.5764
    [189,]  8.7828 35.5764
    [190,]  8.6828 35.5764
    [191,]  8.6828 35.9764
    [192,]  8.5828 35.9764
    [193,]  8.5828 36.3764
    [194,]  8.4828 36.3764
    [195,]  8.3828 36.3764
    [196,]  8.2828 36.3764
    [197,]  8.1828 36.3764
    [198,]  8.0828 36.3764
    [199,]  8.0828 36.7764
    [200,]  7.9828 36.7764
    [201,]  7.8828 36.7764
    [202,]  7.8828 36.3764
    [203,]  7.8828 35.9764
    [204,]  7.7828 35.9764
    [205,]  7.7828 35.5764
    [206,]  7.6828 35.5764
    [207,]  7.5828 35.5764
    [208,]  7.4828 35.5764
    [209,]  7.4828 35.1764
    [210,]  7.3828 35.1764
    [211,]  7.2828 35.1764
    [212,]  7.1828 35.1764
    [213,]  7.0828 35.1764
    [214,]  6.9828 35.1764
    [215,]  6.8828 35.1764
    [216,]  6.7828 35.1764
    [217,]  6.7828 35.5764
    [218,]  6.8828 35.5764
    [219,]  6.8828 35.9764
    [220,]  6.7828 35.9764
    [221,]  6.7828 36.3764
    [222,]  6.6828 36.3764
    [223,]  6.5828 36.3764
    [224,]  6.4828 36.3764
    [225,]  6.4828 36.7764
    [226,]  6.3828 36.7764
    [227,]  6.2828 36.7764
    [228,]  6.1828 36.7764
    [229,]  6.0828 36.7764
    [230,]  5.9828 36.7764
    [231,]  5.9828 36.3764
    [232,]  5.9828 35.9764
    [233,]  5.8828 35.9764
    [234,]  5.7828 35.9764
    [235,]  5.7828 35.5764
    [236,]  5.7828 35.1764
    [237,]  5.6828 35.1764
    [238,]  5.5828 35.1764
    [239,]  5.4828 35.1764
    [240,]  5.3828 35.1764
    [241,]  5.2828 35.1764
    [242,]  5.1828 35.1764
    [243,]  5.0828 35.1764
    [244,]  4.9828 35.1764
    [245,]  4.9828 34.7764
    [246,]  4.8828 34.7764
    [247,]  4.7828 34.7764
    [248,]  4.6828 34.7764
    [249,]  4.6828 34.3764
    [250,]  4.5828 34.3764
    [251,]  4.4828 34.3764
    [252,]  4.4828 33.9764
    [253,]  4.3828 33.9764
    [254,]  4.2828 33.9764
    [255,]  4.2828 33.5764
    [256,]  4.1828 33.5764
    [257,]  4.0828 33.5764
    [258,]  3.9828 33.5764
    [259,]  3.9828 33.1764
    [260,]  3.8828 33.1764
    [261,]  3.7828 33.1764
    [262,]  3.6828 33.1764
    [263,]  3.5828 33.1764
    [264,]  3.4828 33.1764
    [265,]  3.3828 33.1764
    [266,]  3.3828 32.7764
    [267,]  3.2828 32.7764
    [268,]  3.1828 32.7764
    [269,]  3.0828 32.7764
    [270,]  3.0828 32.3764
    [271,]  2.9828 32.3764
    [272,]  2.8828 32.3764
    [273,]  2.7828 32.3764
    [274,]  2.7828 31.9764
    [275,]  2.6828 31.9764
    [276,]  2.5828 31.9764
    [277,]  2.4828 31.9764
    [278,]  2.3828 31.9764
    [279,]  2.3828 31.5764
    [280,]  2.2828 31.5764
    [281,]  2.1828 31.5764
    [282,]  2.0828 31.5764
    [283,]  1.9828 31.5764
    [284,]  1.8828 31.5764
    [285,]  1.7828 31.5764
    [286,]  1.6828 31.5764
    [287,]  1.5828 31.5764
    [288,]  1.4828 31.5764
    [289,]  1.3828 31.5764
    [290,]  1.2828 31.5764
    [291,]  1.1828 31.5764
    [292,]  1.0828 31.5764
    [293,]  0.9828 31.5764
    [294,]  0.8828 31.5764
    [295,]  0.7828 31.5764
    [296,]  0.6828 31.5764
    [297,]  0.5828 31.5764
    [298,]  0.4828 31.5764
    [299,]  0.3828 31.5764
    [300,]  0.2828 31.5764
    [301,]  0.1828 31.5764
    [302,]  0.0828 31.5764
    [303,] -0.0172 31.5764
    [304,] -0.0172 31.9764
    [305,] -0.1172 31.9764
    [306,] -0.2172 31.9764
    [307,] -0.3172 31.9764
    [308,] -0.4172 31.9764
    [309,] -0.5172 31.9764
    [310,] -0.6172 31.9764
    [311,] -0.7172 31.9764
    [312,] -0.7172 32.3764
    [313,] -0.8172 32.3764
    [314,] -0.9172 32.3764
    [315,] -1.0172 32.3764
    [316,] -1.0172 32.7764
    [317,] -1.1172 32.7764
    [318,] -1.1172 33.1764
    [319,] -1.0172 33.1764
    [320,] -1.0172 33.5764
    [321,] -0.9172 33.5764
    [322,] -0.8172 33.5764
    [323,] -0.8172 33.9764
    [324,] -0.7172 33.9764
    [325,] -0.6172 33.9764
    [326,] -0.5172 33.9764
    [327,] -0.4172 33.9764
    [328,] -0.3172 33.9764
    [329,] -0.3172 34.3764
    [330,] -0.2172 34.3764
    [331,] -0.2172 34.7764
    [332,] -0.3172 34.7764
    [333,] -0.4172 34.7764
    [334,] -0.5172 34.7764
    [335,] -0.5172 35.1764
    [336,] -0.6172 35.1764
    [337,] -0.7172 35.1764


    [[3]]
    An object of class "Polygon"
    Slot "labpt":
    [1]  1.199467 36.200642

    Slot "area":
    [1] 1.32

    Slot "hole":
    [1] TRUE

    Slot "ringDir":
    [1] -1

    Slot "coords":
               x       y
     [1,] 0.7828 35.5764
     [2,] 0.8828 35.5764
     [3,] 0.9828 35.5764
     [4,] 1.0828 35.5764
     [5,] 1.1828 35.5764
     [6,] 1.2828 35.5764
     [7,] 1.3828 35.5764
     [8,] 1.4828 35.5764
     [9,] 1.5828 35.5764
    [10,] 1.6828 35.5764
    [11,] 1.6828 35.9764
    [12,] 1.7828 35.9764
    [13,] 1.8828 35.9764
    [14,] 1.8828 36.3764
    [15,] 1.7828 36.3764
    [16,] 1.6828 36.3764
    [17,] 1.6828 36.7764
    [18,] 1.5828 36.7764
    [19,] 1.4828 36.7764
    [20,] 1.3828 36.7764
    [21,] 1.2828 36.7764
    [22,] 1.1828 36.7764
    [23,] 1.0828 36.7764
    [24,] 0.9828 36.7764
    [25,] 0.8828 36.7764
    [26,] 0.7828 36.7764
    [27,] 0.6828 36.7764
    [28,] 0.5828 36.7764
    [29,] 0.5828 36.3764
    [30,] 0.5828 35.9764
    [31,] 0.6828 35.9764
    [32,] 0.7828 35.9764
    [33,] 0.7828 35.5764


    [[4]]
    An object of class "Polygon"
    Slot "labpt":
    [1]  2.8828 33.6164

    Slot "area":
    [1] 0.4

    Slot "hole":
    [1] TRUE

    Slot "ringDir":
    [1] -1

    Slot "coords":
               x       y
     [1,] 2.5828 33.9764
     [2,] 2.5828 33.5764
     [3,] 2.6828 33.5764
     [4,] 2.6828 33.1764
     [5,] 2.7828 33.1764
     [6,] 2.8828 33.1764
     [7,] 2.9828 33.1764
     [8,] 3.0828 33.1764
     [9,] 3.0828 33.5764
    [10,] 3.1828 33.5764
    [11,] 3.1828 33.9764
    [12,] 3.0828 33.9764
    [13,] 2.9828 33.9764
    [14,] 2.8828 33.9764
    [15,] 2.7828 33.9764
    [16,] 2.6828 33.9764
    [17,] 2.5828 33.9764

所以我想提取 将 [[1]] 中的“坐标”插入数据帧,将 [[2]] 中的“坐标”插入另一个数据帧,依此类推...

有人可以帮帮我吗?

编辑:

例如作为提取坐标???

[1,] 5.6828 20.7764
         [2,] 5.7828 20.7764
         [3,] 5.8828 20.7764
         [4,] 5.9828 20.7764
         [5,] 5.9828 20.3764
         [6,] 5.8828 20.3764
         [7,] 5.7828 20.3764
         [8,] 5.6828 20.3764
         [9,] 5.5828 20.3764
        [10,] 5.5828 20.7764
        [11,] 5.6828 20.7764

【问题讨论】:

  • 对象的str 比完整转储要好,这可能是Getting a slot's value of S4 objects? 的副本
  • 其实我没有一个“str”我想剖析我的一组多边形,每个多边形都有提取每个多边形的坐标,希望我清楚吗?
  • @user26480:他要求你提供str(object_name_whatever_it_might_be)的输出

标签: r polygons slot


【解决方案1】:

SpatialPolygons 对象是polygons 的列表,每个列表都是Polygons 的列表(是的,唯一的区别在于大小写...)。后者有坐标。

这里是法国省份的例子,下载自GADM

library(raster)
FR <- getData('GADM',country='FRA',level=1)
length(FR@polygons)
# [1] 22
sapply(FR@polygons,function(p)length(p@Polygons))
#  [1]   1   1   1   2   1  28   2   2   1   1   2  16 245  10  11   3   1   2   1   8  28  23

所以FR 是一个代表法国省份的spatialPolygons 对象。有 22 个 polygons 代表省份,每个省份都有一个或多个 Polygons 包含边界的物理坐标(例如,纬度和经度)。可以有多个Polygons,因为地理位置可能是不连续的(例如岛屿等)。其实内陆省份一般只有一个Polygon,而沿海省份有很多。

那么您是想要 22 个数据框,每个省一个,还是想要每个地理区域的数据框?

如果您知道您的spatiaPolygons 对象只有一个Polygon 对应每个polygon,那么您可以这样做:

result <- lapply(FR@polygons,function(p) data.frame(p@Polygons[[1]]@coords))

在这种情况下,我只是获取每个省的第一个Polygon 的坐标。

【讨论】:

    【解决方案2】:

    不就是这样吗?

     name_of_list_of_objects[[1]]@coords
    

    通常有一个 S4 对象的提取函数被认为更安全、更正确,因此这可能有效:

    coordinates( name_of_list_object[[1]] )
    

    所以对于多个项目,只需:

     lapply(name_of_list_of_objects, coordinates)
    

    【讨论】:

    • 如果我在多边形上使用这个答案,它似乎给了我多边形的质心,而上面的答案给出了每个角,这就是我想要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 2021-11-05
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多