【问题标题】:Create Spatial Data in R在 R 中创建空间数据
【发布时间】:2012-08-20 11:21:24
【问题描述】:

我有一个 100 x 200 米区域内的物种及其大致位置的数据集。数据框的位置部分不是我认为可用的格式。在这个 100 x 200 米的矩形中,有两百个 10 x 10 米的正方形,命名为 A 到 CV。在每个 10 x 10 方格内,有四个 5 x 5 平方米的方格,分别命名为 1、2、3 和 4(1 在 2 的南边和 3 的西边。4 在 2 的东边和 3 的北边)。我想让 R 知道 A 是角在 (0 ,0)、(10,0)、(0,0) 和 (0,10) 处的正方形,B 就在 A 的北边并且有角 ( 0,10), (0,20), (10,10), 和 (10,20),K 就在 A 的东边,在 (10,0), (10,10), (20, 0) 和 (20,10) 等所有 10 x 10 平方米。此外,我想让 R 知道每个 5 x 5 平方米在 100 x 200 米地块中的位置。

所以,我的数据框看起来像这样

10x10    5x5     Tree    Diameter
A    1     tree1    4
B    1     tree2    4
C    4     tree3    6
D    3     tree4    2
E    3     tree5    3
F    2     tree6    7
G    1     tree7    12
H    2     tree8    1
I    2     tree9    2
J    3     tree10   8
K    4     tree11   3
L    1     tree12   7
M    2     tree13   5

最终,我希望能够绘制 100 x 200 米的区域,并让每个 10 x 10 平方米的区域显示树木数量、物种数量或总生物量 将我拥有的数据转换为 R 可用于绘图甚至分析的空间数据的最佳方法是什么?

【问题讨论】:

  • 顺便说一句,我对您所在地区的几何形状有些困惑。你有 100 个 10x10 的地块(A 到 CV),显然排列成 10 行——这不是 100x100 而不是 100x200 的区域吗? (您也可以在上面的某一点将其称为“正方形”...)
  • 我正在手机上打字,对此感到抱歉。现在已经修好了。我现在正在努力使代码适应我的实际数据,因为这有点抽象。非常感谢帮忙。这是解决问题的聪明方法。

标签: r spatial


【解决方案1】:

这是一个开始。

## set up a vector of all 10x10 position tags
tags10 <- c(LETTERS,
            paste0("A",LETTERS),
            paste0("B",LETTERS),
            paste0("C",LETTERS[1:22]))

将(例如){"J",3} 转换为相应子正方形中心的函数。

convpos <- function(pos10,pos5) {
    ## convert letters to major (x,y) positions
    p1 <- as.numeric(factor(pos10,levels=tags10))  ## or use match()
    p1.x <- ((p1-1) %% 10) *10+5    ## %% is modulo operator
    p1.y <- ((p1-1) %/% 10)*10+5    ## %/% is integer division
    ## sort out sub-positions
    p2.x <- ifelse(pos5 <=2,2.5,7.5)   ## {1,2} vs {3,4} values
    p2.y <- ifelse(pos5 %%2 ==1 ,2.5,7.5)  ## odd {1,3} vs even {2,4} values
    c(p1.x+p2.x,p1.y+p2.y)
}

用法:

convpos("J",2)
convpos(mydata$tenbytenpos,mydata$fivebyfivepos)

重要提示:

  • 这是一个概念证明,我几乎可以保证我没有完全正确地得到 x 和 y 坐标的对应关系。但是您应该能够逐行跟踪并查看它在做什么......
  • 它应该在向量上正常工作(参见上面的第二个使用示例):我从 switch 切换到 ifelse 就是因为这个原因
  • 在将数据读入 R 时,您的列名 (10x10) 很可能会变成类似 X10.10 的名称:请参阅 ?data.frame?check.names

【讨论】:

  • 完美。它可以很好地处理向量输入。要获得 2 个向量,一个 x 坐标和一个 y 坐标,您可以编写 x.coords &lt;- convpos(pos10, pos5)[1:(length(pos10)/2)]y.coords &lt;- convpos(pos10, pos5)[(1 + length (pos10)/2: length (pos10)]
【解决方案2】:

与@Ben Bolker 所做的类似,这里有一个查找函数(尽管您可能需要转置一些内容以使标签与您描述的相符)。

tenbyten <- c(LETTERS[1:26], 
  paste0("A",LETTERS[1:26]), 
  paste0("B",LETTERS[1:26]), 
  paste0("C",LETTERS[1:22]))

tenbyten <- matrix(rep(tenbyten, each = 2), ncol = 10)
tenbyten <- t(apply(tenbyten, 1, function(x){rep(x, each = 2)}))
# the 1234 squares
squares <- matrix(c(rep(c(1,2),10),rep(c(4,3),10)), nrow = 20, ncol = 20)
# stick together into a reference grid
my.grid <- matrix(paste(tenbyten, squares, sep = "-"), nrow = 20, ncol = 20)

# a lookup function for the site grid
coordLookup <- function(tbt, fbf, .my.grid = my.grid){
  x <- col(.my.grid) * 5 - 2.5
  y <- row(.my.grid) * 5 - 2.5
  marker <- .my.grid == paste(tbt, fbf, sep = "-")
  list(x = x[marker], y = y[marker])
}

coordLookup("BB",2)
$x
[1] 52.5

$y
[1] 37.5

如果这不是您要查找的内容,那么您可能更喜欢SpatialPolygonsDataFrame,它具有正确的多边形 ID,并且您可以将数据附加到等等。在这种情况下,只需谷歌即可了解如何制作一个从头开始,并操作 row()col() 函数来获取多边形角,类似于这个查找函数中给出的,它只返回质心。

编辑:开始使用 SPDF:

这是从函数示例修改而来的,希望是一个好的开始:

library(sp)
# really you have a 20x20 grid, counting the small ones.
# c(2.5,2.5) specifies the distance in any direction from the cell center
grd <- GridTopology(c(1,1), c(2.5,2.5), c(20,20)))
grd <- as.SpatialPolygons.GridTopology(grd)
# get centroids
coords <- coordinates(polys)
# make SPDF, with an extra column for your grid codes, taken from the above.
# you can add further columns to this data.frame(), using polys@data
polys <- SpatialPolygonsDataFrame(grd, 
  data=data.frame(x=coords[,1], y=coords[,2], my.ID = as.vector(my.grid), 
  row.names=getSpPPolygonsIDSlots(grd)))

【讨论】:

    猜你喜欢
    • 2022-01-12
    • 2021-01-13
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 2016-01-01
    相关资源
    最近更新 更多