简体   繁体   English

raster 和 terra 包的 writeRaster 之间的差异

[英]Discrepancy between writeRaster of raster and terra package

I am trying to write rasters in asc format using raster and terra R package.我正在尝试使用rasterterra R 包以 asc 格式编写栅格。 I am using the following code我正在使用以下代码

library(terra)
library(raster)

f <- system.file("external/test.grd", package="raster")
r1 <- raster(f)
plot(r1)
writeRaster(r1, paste('Try1','.asc', sep=''), overwrite=TRUE)

r2 <- rast(f)
writeRaster(r2, paste('Try2','.asc', sep=''), overwrite=TRUE)

Now if you open the Try1.asc, you will see that the NODATA_value is -3.4e+38 while it is nan in Try2.asc现在如果你打开 Try1.asc,你会看到 NODATA_value 是 -3.4e+38 而在 Try2.asc 中它是 nan 在此处输入图像描述

在此处输入图像描述

nan is creating problem when I am using these rasters in other software.当我在其他软件中使用这些栅格时,nan 会产生问题。 I have tried using NAflag = -3.4e+38 which is not working as well我尝试使用NAflag = -3.4e+38效果不佳在此处输入图像描述

Now how can I have the output like raster package using terra R package while using writeRaster function?现在如何在使用writeRaster函数的同时使用terra R 包获得像raster包这样的输出?

It appears you have to actively manage your data, not unreasonable:看来你必须主动管理你的数据,不无道理:

r3 <- rast(f, opts='NODATA_value=-3.4e+38') # didn't have desired effect
r4 <- classify(r3, cbind(NaN, -3.4e+38))
writeRaster(r4, paste('TRY3','.asc', sep = ''), overwrite=TRUE)
# in gedit
ncols        80
nrows        115
xllcorner    178400.000000000000
yllcorner    329400.000000000000
cellsize     40.000000000000
NODATA_value  nan
 -3.3999999521443642491e+38 -3.3999999521443642491e+38 -3.3999999521443642491e+38 -3.3999999521443642491e+38 -3.3999999521443642491e+38 -3.3999999521443642491e+38

packageVersion('terra')
[1] ‘1.5.39’

-3.4e+83 generally makes plots, well, not great. -3.4e+83 通常会产生情节,嗯,不是很好。 But classify is the tool.classify是工具。 Probably better ways that others will share.其他人可能会分享更好的方法。

That is a bug.那是一个错误。 It works with terra 1.5.40 (the development version that you can install with install.packages('terra', repos='https://rspatial.r-universe.dev') )它适用于 terra 1.5.40(您可以使用install.packages('terra', repos='https://rspatial.r-universe.dev')安装的开发版本)

library(terra)
#terra 1.5.40   

r <- rast(ncol=2, nrow=2, vals=c(0,NA,1,NA))
f <- "test.asc"

r = writeRaster(r, f, overwrite=TRUE, NAflag=-99) 
readLines(f)
#[1] "ncols        2"                
#[2] "nrows        2"                
#[3] "xllcorner    -180.000000000000"
#[4] "yllcorner    -90.000000000000" 
#[5] "dx           180.000000000000" 
#[6] "dy           90.000000000000"  
#[7] "NODATA_value  -99"             
#[8] " 0.0 -99"                      
#[9] " 1 -99"                        

r
#class       : SpatRaster 
#dimensions  : 2, 2, 1  (nrow, ncol, nlyr)
#resolution  : 180, 90  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 
#source      : test.asc 
#name        : lyr.1 
#min value   :     0 
#max value   :     1 
 

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

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