R中建立空间数据(Create Spatial Data in R)

2019-09-21 16:17发布

我种和100×200米范围内的粗略位置的数据集。 所述数据帧的位置的部分不,我发现为可用的格式。 在这100 * 200米的矩形,也有通过名为CV一两百10×10平米。 内的每个10×10正方形有分别命名为1,2 4 5×5平米,图3和4中,(1南的2和3。西4是2东和3北)。 我想令R知道,一个是与角方在(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×10平米。 此外,我想令R知道每5×5米见方的是在100 * 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×200米范围内,并让每个10×10米见方显示了树木的数量,或物种的数量,或总生物量是什么把我的数据的最佳方式成R可以使用图表和分析,或许空间数据?

Answer 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坐标完全正确。 但是,你应该能够通过这条线,由线追查,看看它在做什么?
  • 它应该在载体(参见上述第二使用示例)正常工作:我从切换switchifelse由于这个原因
  • 列名( 10x10 )很可能得到错位成类似X10.10读取数据为R时:见?data.frame?check.names


Answer 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)))


文章来源: Create Spatial Data in R
标签: r spatial