Create Spatial Data in R

…衆ロ難τιáo~ 提交于 2019-12-06 04:05:11

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

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