简体   繁体   English

如何在R中将多边形shapefile的许多字段自动转换为栅格

[英]How to automatically convert many fields of a polygon shapefile to raster in R

I have a shapefile representing Thiessen polygons. 我有一个表示Thiessen多边形的shapefile。 输入图像描述

Each polygon is associated with many values of a table. 每个面与一个表的许多值相关联。

thiessen <- readOGR(dsn = getwd(), layer = poly)
OGR data source with driver: ESRI Shapefile 
Source: ".../raingauges/shp", layer: "thiessen_pol"
with 10 features
It has 5 fields
head(thiessen)
  est est_name p001 p002 p003
0   2   borges    1    8    2
1   0     e018    2    4    3
2   5  starosa    5   15    1
3   6   delfim    4    2    2
4   1     e087    1    1    3
5   3     e010    0    1    0

The columns ' est ' and ' est_name ' are related to the ID and name of the rain gauges. estest_name与雨量计的ID和名称有关。 The following columns are important to me and represent precipitation values on day 1, 2, and so on (in the exemple I kept just three days, but actually, I have 8 years of daily precipitation data). 以下几列对我来说很重要,它们代表第1天,第2天等的降水量(在示例中,我仅保留了3天,但实际上,我有8年的每日降水量数据)。

I need to convert the polygons to raster, but one raster for each field (column p001, p002, and so on) of the table. 我需要将多边形转换为栅格,但是表的每个字段(列p001,p002等)都需要一个栅格。

There is a simple way to convert polygons to raster with the function rasterize in R. 有一种简单的方法可以使用R中的栅格化功能将多边形转换为栅格。

r_p001 <- rasterize(thiessen, r, field = thiessen$p001)
plot(r_p001)
writeRaster(r_p001, filename=".../raingauges/shp/r_p001.tif")

在此处输入图片说明

The problem is that I need to set manually the field (column) of the table with the polygon values to be converted to raster. 问题是我需要手动设置要转换为栅格的面值的表的字段(列)。 As I have about 2900 days (2900 columns with precipitation values for each rain gauge), it is impossible to do manually. 由于我大约有2900天(每个雨量计的2900列带有降水值),因此无法手动进行。

The documentation does not help to clarify how to automate this process and I did not find anything on the internet to help me. 该文档无助于阐明如何自动执行此过程,并且我在互联网上找不到任何可帮助我的东西。

Does anyone know how to automatically convert each field to raster and save as tif format? 有谁知道如何自动将每个字段转换为栅格并另存为tif格式?

Here is an approach: 这是一种方法:

Example data 示例数据

library(raster)
r <- raster(ncols=36, nrows=18)
p1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20))
hole <- rbind(c(-150,-20), c(-100,-10), c(-110,20), c(-150,-20))
p1 <- list(p1, hole)
p2 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0))
p3 <- rbind(c(-125,0), c(0,60), c(40,5), c(15,-45), c(-125,0))
att <- data.frame(id=1:3, var1=10:12, var2=c(6,9,6))
pols <- spPolygons(p1, p2, p3, attr=att)

The important thing is to have a field with a unique If your data do not have it, add it like this 重要的是要有一个唯一的字段。如果您的数据没有,请像这样添加它

pols$id <- 1:nrow(pols) 

Rasterize 栅格化

r <- rasterize(pols, r, field='id')

Create a layer for all other variables 为所有其他变量创建一个图层

x <- subs(r, data.frame(pols), by='id', which=2:ncol(pols), filename="rstr.grd")
x
#class       : RasterBrick 
#dimensions  : 18, 36, 648, 2  (nrow, ncol, ncell, nlayers)
#resolution  : 10, 10  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
#data source : rstr.grd 
#names       : var1, var2 
#min values  :   10,    6 
#max values  :   12,    9 

An alternative is to keep one layer with Raster Attribute Table, that is quicker, but depending on your purpose, perhaps a less useful method: 一种替代方法是使用“栅格属性表”保留一层,这比较快,但是根据您的目的,也许是一种不太有用的方法:

r <- rasterize(pols, r, field='id')
f <- as.factor(r)
v <- levels(f)[[1]]

v <- cbind(v, data.frame(pols)[,-1])
levels(f) <- v
f
#class       : RasterLayer 
#dimensions  : 18, 36, 648  (nrow, ncol, ncell)
#resolution  : 10, 10  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
#data source : in memory
#names       : layer 
#values      : 1, 3  (min, max)
#attributes  :
# ID var1 var2
#  1   10    6
#  2   11    9
#  3   12    6

You can then do: 然后,您可以执行以下操作:

z <- deratify(f)

To get the same result as in the first example 为了获得与第一个示例相同的结果

z
#class       : RasterBrick 
#dimensions  : 18, 36, 648, 2  (nrow, ncol, ncell, nlayers)
#resolution  : 10, 10  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
#data source : in memory
#names       : var1, var2 
#min values  :   10,    6 
#max values  :   12,    9 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM