简体   繁体   English

根据坐标和值创建栅格

[英]Create raster out of coordinates and values

I have a data.frame where the following information is stored: 我有一个data.frame,其中存储以下信息:

  • X1 - Data X1-数据

  • X2 - Hour (godzina) X2-小时(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. 对于上面提到的数据,我有一个名为Punkt(1,1),Punkt(1,2)的列,其中第二行可以找到质心值(例如19.65,49.5),且低于降雨。

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: 使用x,y和值的此矩阵,可以使用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 

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

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