r - 将栅格转换为数据帧并在 R 中提取所需的值

标签 r raster r-raster spatial-data-frame

我想使用 ggplot2 制作测深图。我将光栅文件转换为数据帧,裁剪并将其转换为数据帧:

library(raster)

##Import a raster file
Bathymetry_dat <- raster("w001000.adf")

##Clop the raster file
Bathy_clopped <- crop(Bathymetry_dat, extent(84.11236, 108.4594, -4.046979, 24.09534))

##Convert it to a dataframe    
datframe_bathy<-as.data.frame(Bathy_clopped, xy = TRUE)

然后,我检查了数据框中的值(即以米为单位的深度):

> summary(datframe_bathy)
       x                y          w001000_COUNT    
 Min.   : 84.11   Min.   :-4.046   Min.   :   9945  
 1st Qu.: 90.20   1st Qu.: 2.987   1st Qu.:  81618  
 Median : 96.28   Median :10.021   Median : 168447  
 Mean   : 96.28   Mean   :10.021   Mean   : 210212  
 3rd Qu.:102.37   3rd Qu.:17.054   3rd Qu.: 336718  
 Max.   :108.45   Max.   :24.087   Max.   :1205362  
                                   NA's   :3449125 

深度 (m) 值应该为负值,并且不应该这么大。然后,我检查了 R 中导入的测深文件。

> Bathy_clopped
class       : RasterLayer 
dimensions  : 3377, 2922, 9867594  (nrow, ncol, ncell)
resolution  : 0.008333333, 0.008333333  (x, y)
extent      : 84.10833, 108.4583, -4.05, 24.09167  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : w001000 
values      : -6138, -1  (min, max)
attributes  :
           ID  COUNT
 from: -11584      1
 to  :     -1 676804

有两个属性:ID 和 COUNT。我认为 ID 是深度(米),但不知道 COUNT 是什么。奇怪的是,COUNT 的范围是从 1 到 676804,但summary() 显示的值是从 9945 到 1205362。

所以我的问题是如何将光栅文件转换为包含我想要的值的数据帧?

使用“COUNT”值,我能够生成测深图,但图例中的值不正确..

提前致谢。

最佳答案

有一个包含栅格图层的属性表。我们可以使用levels来访问它。

library(raster)

# See the attribute table
head(levels(Bathy_clopped)[[1]])
#       ID COUNT
# 1 -11584     1
# 2 -10944     1
# 3 -10907     1
# 4 -10900     1
# 5 -10879     1
# 6 -10878     1

然后我们可以操作此属性表,将 COUNT 替换为深度,即 ID

# Get the attribute table
RAT <- levels(Bathy_clopped)[[1]]

# Replace COUNT with ID
RAT$Depth <- RAT$ID
RAT$COUNT <- NULL

# Replace the attribute table
Bathy_clopped2 <- Bathy_clopped
levels(Bathy_clopped2)[[1]] <- RAT

# Create a single layer based on the new RAT
Bathy_clopped2 <- deratify(Bathy_clopped2)

# Create a data frame
datframe_bathy2 <-as.data.frame(Bathy_clopped2, xy = TRUE)

现在,datframe_bathy2 中的值符合预期。

# Summarize the data frame
summary(datframe_bathy2)
#       x                y              COUNT        
# Min.   : 84.11   Min.   :-4.046   Min.   :-6138    
# 1st Qu.: 90.20   1st Qu.: 2.987   1st Qu.:-3800    
# Median : 96.28   Median :10.021   Median :-2428    
# Mean   : 96.28   Mean   :10.021   Mean   :-2154    
# 3rd Qu.:102.37   3rd Qu.:17.054   3rd Qu.:  -65    
# Max.   :108.45   Max.   :24.087   Max.   :   -1    
#                                   NA's   :3449125

关于r - 将栅格转换为数据帧并在 R 中提取所需的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50463141/

相关文章:

r - 过滤行最大值大于阈值的行

R:按背景图像顺序绘制许多图的有效方法

r - 在 R 中裁剪光栅

r - 在 R 中将路径/路线图编写为 GeoTiff

r - 为什么我在 R 中绘制的栅格图在保存的文件中会变得模糊?

R - 如何改善 RasterVis levelplot 的颜色阴影?

R绘制列表的行

R 使用先前的迭代值对 FOR 循环进行矢量化

r - 如何 reshape 具有多个相关二进制 "observations"的数据,以便与 ggplot 配合良好?

使用降雪和 sfLapply 在 R 中栅格化多边形