从栅格砖文件中提取所有单独的图层。

huangapple go评论75阅读模式
英文:

Extracting all individual layers from a Raster Brick File

问题

我已经将 28 层堆叠到 R 中的一个砖块中。

brik
class      : RasterBrick 
dimensions : 720, 1440, 1036800, 28  (nrow, ncol, ncell, nlayers)
resolution : 0.25, 0.25  (x, y)
extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
source     : C:/Users/Ujjal Baruah/AppData/Local/Temp/Rtmp0GaiPO/raster/r_tmp_2020-01-03_030159_46788_10398.grd 
names      : Data.Fiel//tNO2Trop.1, Data.Fiel//tNO2Trop.2, Data.Fiel//tNO2Trop.3, Data.Fiel//tNO2Trop.4, Data.Fiel//tNO2Trop.5, Data.Fiel//tNO2Trop.6, Data.Fiel//tNO2Trop.7, Data.Fiel//tNO2Trop.8, Data.Fiel//tNO2Trop.9, Data.Fiel//NO2Trop.10, Data.Fiel//NO2Trop.11, Data.Fiel//NO2Trop.12, Data.Fiel//NO2Trop.13, Data.Fiel//NO2Trop.14, Data.Fiel//NO2Trop.15, ... 

现在,我想将这些单独的层保存为 Geotiff 文件。

writeRaster(brik, file.path('/output/filepath/', names(brik)), bylayer=TRUE, format('GTiff'))

不幸的是,我只得到一个文件,而不是多个层的 Geotiff 文件。

任何解决方案将不胜感激。

谢谢。

英文:

I have stacked 28 layers to a brick in R

brik
class      : RasterBrick 
dimensions : 720, 1440, 1036800, 28  (nrow, ncol, ncell, nlayers)
resolution : 0.25, 0.25  (x, y)
extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
source     : C:/Users/Ujjal Baruah/AppData/Local/Temp/Rtmp0GaiPO/raster/r_tmp_2020-01-03_030159_46788_10398.grd 
names      : Data.Fiel//tNO2Trop.1, Data.Fiel//tNO2Trop.2, Data.Fiel//tNO2Trop.3, Data.Fiel//tNO2Trop.4, Data.Fiel//tNO2Trop.5, Data.Fiel//tNO2Trop.6, Data.Fiel//tNO2Trop.7, Data.Fiel//tNO2Trop.8, Data.Fiel//tNO2Trop.9, Data.Fiel//NO2Trop.10, Data.Fiel//NO2Trop.11, Data.Fiel//NO2Trop.12, Data.Fiel//NO2Trop.13, Data.Fiel//NO2Trop.14, Data.Fiel//NO2Trop.15, ... 

Now, i want to save this individual layers in Geotiff using

writeRaster(brik, file.path('/output/filepath/', names(brik)), bylayer=TRUE, format('GTiff'))

Unfortunately, i get just one file instead of multiple layers in geotiff.

Any solution would be appreciated.
Thanks

答案1

得分: 2

writeRaster 似乎在创建栅格文件之前剥离了点号后的数字。因此,它尝试将您的图层全部写入 Data.Fiel//tNO2Trop.tif

> writeRaster(r, "./test.2", format="GTiff")
> dir(".")
[1] "test.tif"

(请注意,由于某种原因,您的代码使用了 format("GTiff") 来代替 format="GTiff"。这可以通过 format 是一个函数并返回字符串 "GTiff",而 writeRaster 在此处期望格式字符串的幸运巧合来解释。)

我不知道为什么会这样,也不知道是否有文档说明或者是否是一个 bug。您可以通过使用破折号而不是点号来解决此问题:

> writeRaster(r, "./test-2", format="GTiff")
> dir(".")
[1] "test-2.tif" "test.tif"

如果点号对您很重要,然后可以在之后使用 file.rename 进行重命名。

编辑:如果在文件名中添加 .tif,则一切都正常:

> writeRaster(s, names(s), bylayer=TRUE, format="GTiff")
Error in .getGDALtransient(x, filename = filename, options = options,  : 
  filename exists; use overwrite=TRUE

因为点号后的数字被剥离,所以在第二个图层上失败:

```R
> dir()
[1] "layer.tif"

.tif 添加到名称中:

> writeRaster(s, paste0(names(s), ".tif"), bylayer=TRUE, format="GTiff")

然后一切正常:

> dir()
[1] "layer.1.tif" "layer.2.tif" "layer.3.tif" "layer.tif"
英文:

writeRaster seems to strip off the dot-number before creating a raster file. Hence it tries to write your layers all to Data.Fiel//tNO2Trop.tif.

> writeRaster(r, "./test.2", format="GTiff")
> dir(".")
[1] "test.tif"

(Note for some reason your code has format("GTiff") for format="GTiff". This works by the fluke that format is a function and returns the string "GTiff" and writeRaster is expecting the format string here)

I don't know why and I don't know if this is documented or a bug. You can work round by using dashes instead of dots:

> writeRaster(r, "./test-2", format="GTiff")
> dir(".")
[1] "test-2.tif" "test.tif" 

and if dots are important to you then do a file.rename afterwards.

Edit: If you add the .tif to the file names then all is well:

> writeRaster(s, names(s), bylayer=TRUE, format="GTiff")
Error in .getGDALtransient(x, filename = filename, options = options,  : 
  filename exists; use overwrite=TRUE

fails on the second layer because dot-number is stripped:

> dir()
[1] "layer.tif"

add .tif to the names:

> writeRaster(s, paste0(names(s),".tif"), bylayer=TRUE, format="GTiff")

shazam:

> dir()
[1] "layer.1.tif" "layer.2.tif" "layer.3.tif" "layer.tif"  

huangapple
  • 本文由 发表于 2020年1月3日 15:15:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定