R 如何从 geotiff 读取 nodatavalue 标签 - R Raster 包 [英] How Does R read nodatavalue tags from a geotiff - R Raster package

查看:56
本文介绍了R 如何从 geotiff 读取 nodatavalue 标签 - R Raster 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个不寻常的结果,R 中没有栅格数据值.下面的代码 - 我有一个没有导入数据值的栅格(-9999).QGIS 也读取 nodatavalue,arcgis.R,当它读入 geotiff 时,将 nodata 值分配给 -INF.

I am running into an unusual outcome with raster no data values in R. Code below -- I have a raster that has a no data value that I import (-9999). QGIS reads the nodatavalue, arcgis too. R, when it reads in the geotiff, assigned the nodata value to -INF.

我不知道为什么.但是我决定尝试从头开始制作一个 - 并且结果相同.我的过程有什么问题?如何确保 R 正确读取 nodatavalues?

I don't know why. But I decided to try to make one from scratch - and SAME RESULT. what is wrong with my process? How can I ensure that R reads in the nodatavalues properly?

注意:我下面的示例是创建的栅格/geotiff.但我实际上是在导入一个组织生产的相当大的 geotiff.所以我无法控制它们的编写方式,但如果需要,我可以要求他们调整标签.

NOTE: my example below is a created raster / geotiff. But I'm actually importing geotiffs that are quite large produced by an organization. So I don't have control over how they are written but I could ask them to adjust tags if need be.

library(raster)
#create a raster from the matrix
myRaster1 <- raster(nrow=4, ncol=4)

#assign some random data to the raster
myRaster1[]<- 1:ncell(myRaster1)

myRaster1[5] <- -9999

#ensure the data have some decimals
myRaster1[2] <- 34.5

#assign no data value to raster
myRaster1@file@nodatavalue <- -9999

#make sure it worked
NAvalue(myRaster1)
myRaster1@file@nodatavalue

#view attributes of the raster
myRaster1


#write out raster
#write the geotiff - change overwrite=TRUE to overwrite=FALSE if you want to make sure you don't overwrite your files!
writeRaster(myRaster1,"newDel.tif","GTiff", overwrite=TRUE)


#import raster
newr <- raster("newDel.tif")
newr@file@nodatavalue

感谢您提供有关 R 如何从 geotiff 导入标签的任何建议/解释.

Thank you for any advice / explanation as to how R imports tags from a geotiff.

推荐答案

感谢您创建可重现的示例,但您创建的示例可能不是最有帮助的.为插槽分配值(@ 后面的名称)不是合法的".除非您具有 Raster 对象的高级知识,否则您应该使用用户界面(函数、方法).所以,永远不要这样做:

Thank you for creating a reproducible example, but the example you created is perhaps not the most helpful. Assigning values to a slot (names behind a @) is not "legal". Unless you have advanced knowledge of the Raster objects, you should use the user-interface (functions, methods). So, never do this:

myRaster1@file@nodatavalue <- -9999

该槽中的值仅供内部使用;并且仅与从磁盘获取值的对象相关.同样,

The value in this slot is for internal consumption only; and only relevant for objects that get values from disk. Likewise,

newr@file@nodatavalue
# [1] -Inf

并不意味着 "newDel.tif" 中的 NA 值以这种方式存储.

does not mean that NA values in "newDel.tif" are stored that way.

要在写入时设置某个值,请使用 writeRaster 中可用的选项.

To set a certain value when writing, use the options available in writeRaster.

现在,您的问题似乎是您有值 -9999 的文件应该被视为 NA,但不知何故这不会发生.奇怪的是它可以在 QGIS 中工作,因为使用了相同的信息和相同的底层软件(GDAL).但这里有一个你可以处理的方法:

Now, your problem seems to be that you have files with values -9999 that should be considered as NA, and somehow this does not happen. It is odd that it would work in QGIS because the same info and same underlying software is used (GDAL). But here is a how you can deal with that:

创建一个带有值 -9999 的 geotiff 文件,这些值不被识别为 NA

Create a geotiff file with values -9999 that are not recognized as NA

library(raster)
r <- raster(nrow=5, ncol=5)
values(r) <- 1:25
r[1:5] <- -9999
writeRaster(r, 'test.tif', overwrite=TRUE)

您的情况:

x <- raster('test.tif')
plot(x)

这可能会解决它:

NAvalue(x) <- -9999
plot(x)

这篇关于R 如何从 geotiff 读取 nodatavalue 标签 - R Raster 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆