Create Spatial Data in R

本秂侑毒 提交于 2019-12-22 10:54:18

问题


I have a dataset of species and their rough locations in a 100 x 200 meter area. The location part of the data frame is not in a format that I find to be usable. In this 100 x 200 meter rectangle, there are two hundred 10 x 10 meter squares named A through CV. Within each 10 x 10 square there are four 5 x 5 meter squares named 1, 2, 3, and 4, respectively (1 is south of 2 and west of 3. 4 is east of 2 and north of 3). I want to let R know that A is the square with corners at (0 ,0), (10,0), (0,0), and (0,10), that B is just north of A and has corners (0,10), (0,20), (10,10), and (10,20), and K is just east of A and has corners at (10,0), (10,10), (20,0), and (20,10), and so on for all the 10 x 10 meter squares. Additionally, I want to let R know where each 5 x 5 meter square is in the 100 x 200 meter plot.

So, my data frame looks something like this

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

Eventually, I want to be able to plot the 100 x 200 meter area and have each 10 x 10 meter square show up with the number of trees, or number of species, or total biomass What is the best way to turn the data I have into spatial data that R can use for graphing and perhaps analysis?


回答1:


Here's a start.

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

A function to convert (e.g.) {"J",3} to the center of the corresponding sub-square.

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

usage:

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

Important notes:

  • this is a proof of concept, I can pretty much guarantee I haven't got the correspondence of x and y coordinates quite right. But you should be able to trace through this line-by-line and see what it's doing ...
  • it should work correctly on vectors (see second usage example above): I switched from switch to ifelse for that reason
  • your column names (10x10) are likely to get mangled into something like X10.10 when reading data into R: see ?data.frame and ?check.names



回答2:


Similar to what @Ben Bolker has done, here's a lookup function (though you may need to transpose something to make the labels match what you describe).

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

If this isn't what you're looking for, then maybe you'd prefer a SpatialPolygonsDataFrame, which has proper polygon IDs, and you attach data to, etc. In that case just Google around for how to make one from scratch, and manipulate the row() and col() functions to get your polygon corners, similar to what's given in this lookup function, which only returns centroids.

Edit: getting SPDF started:

This is modified from the function example and can hopefully be a good start:

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


来源:https://stackoverflow.com/questions/12117113/create-spatial-data-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!