R:使用栅格单元的 x 和 y 坐标的最小值和最大值从数据帧创建栅格对象

R: Create raster object from a dataframe with min and max values for x and y coordinates of raster cells

我有一个数据框,其中包含栅格像元的 x 和 y 坐标的最小值和最大值。它是这样开始的:

          xmin      ymin      ymax      xmax
   1 -73.99139 -18.04158 -17.04158 -72.99139
   2 -72.99139 -18.04158 -17.04158 -71.99139
   3 -71.99139 -18.04158 -17.04158 -70.99139
   4 -70.99139 -18.04158 -17.04158 -69.99139
   5 -69.99139 -18.04158 -17.04158 -68.99139
   6 -68.99139 -18.04158 -17.04158 -67.99139

并继续约 800 行,其中每一行代表一个栅格单元

raster 包中的 raster 函数似乎允许将 xmn 设置为最小 x 坐标(左边界)以及 xmxymnymx。然而,这是关于整个 rastr 对象本身的栅格范围......而不是单个单元格。

如何使用这些单个单元格值创建栅格对象?

这样的事情怎么样...

当 df 是具有最小值和最大值的数据框时:

       xmin      ymin      ymax      xmax
1 -73.99139 -18.04158 -17.04158 -72.99139
2 -72.99139 -18.04158 -17.04158 -71.99139
3 -71.99139 -18.04158 -17.04158 -70.99139
4 -70.99139 -18.04158 -17.04158 -69.99139
5 -69.99139 -18.04158 -17.04158 -68.99139
6 -68.99139 -18.04158 -17.04158 -67.99139

#calculate point center
xcoords <- (df[,"xmax"] - df[,"xmin"])/2 + df[,"xmin"]
ycoords <- (df[,"ymax"] - df[,"ymin"])/2 + df[,"ymin"]

#create dataframe from coordinates
points <-data.frame("x"=c(xcoords),
                    "y"=c(ycoords))
#create raster
ras <- raster(SpatialPoints(points))