简体   繁体   中英

Create raster out of coordinates and values

I have a data.frame where the following information is stored:

  • X1 - Data

  • X2 - Hour (godzina)

and for the data mentioned above, I have columns named Punkt(1,1), Punkt(1,2) wherein the second row you can find centroid values (ex. 19.65,49.5) and below rainfall.

Data <- structure(list(X1 = c("data", "20140509", "20140509", "20140509", 
"20140509", "20140509", "20140509", "20140509", "20140509", "20140509", 
"20140509", "20140509", "20140509", "20140509", "20140509"), 
X2 = c("godzina", "0", "1", "2", "3", "4", "5", "6", "7", 
"8", "9", "10", "11", "12", "13"), `Punkt(1,1)` = c("19.55,49.5", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0.25", "0", 
"0", "0.01", "0"), `Punkt(1,2)` = c("19.55,49.55", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0.4", "0", "0", "0.01", 
"0"), `Punkt(1,3)` = c("19.55,49.6", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0.3", "0", "0", "0", "0")), .Names = c("X1", 
"X2", "Punkt(1,1)", "Punkt(1,2)", "Punkt(1,3)"), row.names = c(NA, 
15L), class = "data.frame")

My goal is to create a raster for every point and date-hour and for every single hour create a one single raster.

So far I have suceeded in doing in for a single point. To get the extent of every raster cell in column I need to do the following calculations:

Coordinates = Data[1,][3]
xmin = as.numeric(substr(Coordinates, 1,5)) - 0.025
xmax = as.numeric(substr(Coordinates, 1,5)) + 0.025
ymin = as.numeric(substr(Coordinates, 7,11)) - 0.025
ymax = as.numeric(substr(Coordinates, 7,11)) + 0.025

I need to define rainfall rate (let's try positive one):

Rainfall = as.numeric(Data[,3][11])

And then I can create a raster file:

r1 <- raster(nrows=1, ncols=1, xmn=xmin, xmx=xmax, ymn=ymin, ymx=ymax)
r1 <- setValues(r1, Rainfall)
plot(r1)

Could anyone help me?

The points (unpack at your leisure :)

pts <- matrix(as.numeric(unlist(strsplit(unlist(Data[1,-c(1:2)]), ","))), ncol=2, byrow=TRUE)

Combine with the values:

v <- apply(as.matrix(Data[-1, -c(1:2)]), 1, as.numeric)
pv <- cbind(pts, v)

The "names" (date/time)

nms <- apply(Data[-1,1:2], 1, function(i) paste(i, collapse='_'))

For this toy example to work, we need more than one row in your raster. The below takes care of that, but you should not do that with your real data:

pv <- rbind(pv, pv)
pv[1:3,1] <- 20

With this matrix of x, y and values, you can use rasterFromXYZ:

library(raster) 
r <- rasterFromXYZ(pv)
names(r) <- nms

r
#class       : RasterBrick 
#dimensions  : 3, 2, 6, 14  (nrow, ncol, ncell, nlayers)
#resolution  : 0.45, 0.05  (x, y)
#extent      : 19.325, 20.225, 49.475, 49.625  (xmin, xmax, ymin, ymax)
#coord. ref. : NA 
#data source : in memory
#names       : X20140509_0, X20140509_1, X20140509_2, X20140509_3, X20140509_4, X20140509_5, X20140509_6, X20140509_7, X20140509_8, X20140509_9, X20140509_10, X20140509_11, X20140509_12, X20140509_13 
#min values  :        0.00,        0.00,        0.00,        0.00,        0.00,        0.00,        0.00,        0.00,        0.00,        0.25,         0.00,         0.00,         0.00,         0.00 
#max values  :        0.00,        0.00,        0.00,        0.00,        0.00,        0.00,        0.00,        0.00,        0.00,        0.40,         0.00,         0.00,         0.01,         0.00 

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