简体   繁体   中英

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

I have a shapefile representing Thiessen polygons. 输入图像描述

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

I need to convert the polygons to raster, but one raster for each field (column p001, p002, and so on) of the table.

There is a simple way to convert polygons to raster with the function rasterize in 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.

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?

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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