簡體   English   中英

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 但是,這與整個柵格對象本身的柵格范圍有關……而不是單個像元。

如何使用這些單個像元值創建柵格對象?

這樣的事情怎么樣...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM