Raster不创建正确的输出文件(.gri和.grd文件),而不是.tif文件。

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

Raster doesn't create right output file (.gri and .grd file) instead of an .tif file

问题

I'm using Rstudio on a Mac. I use the Dismo and Raster packages to set the levels of the resolution, extent and crs all equal for different files.

整体脚本工作正常,但输出应为.tif文件,但RStudio将输出导出为.gri和.grd文件。

Did I miss installing an extra package to have the right output or has it something to do with using a Mac version of RStudio because the original script was written on a Windows version?

是否我错过了安装额外包以获得正确的输出,或者与在Windows版本上编写的原始脚本有关,因为我在Mac版本的RStudio上使用它?

Code:

> library(raster)
> #Put all your raster files in the working directory, including BedCrop provided
> # load in your raster environmental files
> RastList<-list.files(getwd(), pattern = ".tif")
> r <- lapply(RastList, raster)
> r # The extents, crs, resolution might be different for each raster
[[1]]
class      : RasterLayer 
dimensions : 4320, 8640, 37324800  (nrow, ncol, ncell)
resolution : 0.04166667, 0.04166667  (x, y)
extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : Annual precipitation_wc2.1_2.5m_bio_12.tif 
names      : Annual.precipitation_wc2.1_2.5m_bio_12 
values     : 0, 11246  (min, max)
[[2]]
class      : RasterLayer 
dimensions : 2395, 3379, 8092705  (nrow, ncol, ncell)
resolution : 0.0306, 0.018  (x, y)
extent     : -41.28155, 62.11585, 31.29335, 74.40335  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : BedCrop.tif 
names      : BedCrop 
values     : 0, 8200  (min, max)
[[3]]
class      : RasterLayer 
dimensions : 4320, 8640, 37324800  (nrow, ncol, ncell)
resolution : 0.04166667, 0.04166667  (x, y)
extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : Elevation_wc2.1_2.5m_elev.tif 
names      : Elevation_wc2.1_2.5m_elev 
values     : -415, 7412  (min, max)
[[4]]
class      : RasterLayer 
dimensions : 4320, 8640, 37324800  (nrow, ncol, ncell)
resolution : 0.04166667, 0.04166667  (x, y)
extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : Temp_JAN_wc2.1_2.5m_tavg_01.tif 
names      : Temp_JAN_wc2.1_2.5m_tavg_01 
values     : -46.096, 34.1  (min, max)
> # Choose a raster with the resolution, extent you want and crs=wgs84, use it as a template
> template <- r[[2]] #I choose you BedCrop!
> #Reproject and mask each raster to the same CRS and extent as your template
> r[[1]] <- projectRaster(r[[1]], template) #repeat for each layer of the raster stack (r[[3]], r[[4]]...)
Warning message:
In projectRaster(r[[1]], template) : input and ouput crs are the same
> r[[1]]<-mask(r[[1]], template)
> r[[3]] <- projectRaster(r[[3]], template)
Warning message:
In projectRaster(r[[3]], template) : input and ouput crs are the same
> r[[3]]<-mask(r[[3]], template)
> r[[4]] <- projectRaster(r[[4]], template)
Warning message:
In projectRaster(r[[4]], template) : input and ouput crs are the same
> r[[4]]<-mask(r[[4]], template)
> r #check they have the same extent, resolution, crs
[[1]]
class      : RasterLayer 
dimensions : 2395, 3379, 8092705  (nrow, ncol, ncell)
resolution : 0.0306, 0.018  (x, y)
extent     : -41.28155, 62.11585, 31.29335, 74.40335  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : memory
names      : Annual precipitation_wc2.1_2.5m_bio_12 
values     : 107.9981, 2992.399  (min, max)
[[2]]
class      : RasterLayer 
dimensions : 2395, 3379, 8092705  (nrow, ncol, ncell)
resolution : 0.0306, 0.018  (x, y)
extent     : -41.28155, 62.11585, 31.29335, 74.40335  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : BedCrop.tif 
names      : BedCrop 
values     : 0, 8200  (min, max)
[[3]]
class      : RasterLayer 
dimensions : 2395, 3379, 8092705  (nrow, ncol, ncell)
resolution : 0.0306, 0.018  (x, y)
extent     : -41.28155, 62.11585, 31.29335, 74.40335  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : memory
names      : Elevation_wc2.1_2.5m_elev 
values     : -5.991715, 
<details>
<summary>英文:</summary>
I&#39;m using Rstudio on a Mac. I use the Dismo and Raster packages to set the levels of the resolution, extent and crs all equal for different files.
The whole script works however but the output should be a .tif file however RStudio exports the output as a .gri and .grd file.
Did I miss installing an extra package to have the right output or has it something to do with using a Mac version of RStudio because the original script was written on a Windows version?
Code:
```&gt; library(dismo)
&gt; library(raster)
&gt; #Put all your raster files in the working directory, including BedCrop provided
&gt; # load in your raster environmental files
&gt; RastList&lt;-list.files(getwd(), pattern = &quot;.tif&quot;)
&gt; r &lt;- lapply(RastList, raster)
&gt; r # The extents, crs, resolution might be different for each raster
[[1]]
class      : RasterLayer 
dimensions : 4320, 8640, 37324800  (nrow, ncol, ncell)
resolution : 0.04166667, 0.04166667  (x, y)
extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : Annual precipitation_wc2.1_2.5m_bio_12.tif 
names      : Annual.precipitation_wc2.1_2.5m_bio_12 
values     : 0, 11246  (min, max)
[[2]]
class      : RasterLayer 
dimensions : 2395, 3379, 8092705  (nrow, ncol, ncell)
resolution : 0.0306, 0.018  (x, y)
extent     : -41.28155, 62.11585, 31.29335, 74.40335  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : BedCrop.tif 
names      : BedCrop 
values     : 0, 8200  (min, max)
[[3]]
class      : RasterLayer 
dimensions : 4320, 8640, 37324800  (nrow, ncol, ncell)
resolution : 0.04166667, 0.04166667  (x, y)
extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : Elevation_wc2.1_2.5m_elev.tif 
names      : Elevation_wc2.1_2.5m_elev 
values     : -415, 7412  (min, max)
[[4]]
class      : RasterLayer 
dimensions : 4320, 8640, 37324800  (nrow, ncol, ncell)
resolution : 0.04166667, 0.04166667  (x, y)
extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : Temp_JAN_wc2.1_2.5m_tavg_01.tif 
names      : Temp_JAN_wc2.1_2.5m_tavg_01 
values     : -46.096, 34.1  (min, max)
&gt; # Choose a raster with the resolution, extent you want and crs=wgs84, use it as a template
&gt; template &lt;- r[[2]] #I choose you BedCrop!
&gt; #Reproject and mask each raster to the same CRS and extent as your template
&gt; r[[1]] &lt;- projectRaster(r[[1]], template) #repeat for each layer of the raster stack (r[[3]], r[[4]]...)
Warning message:
In projectRaster(r[[1]], template) : input and ouput crs are the same
&gt; r[[1]]&lt;-mask(r[[1]], template)
&gt; r[[3]] &lt;- projectRaster(r[[3]], template)
Warning message:
In projectRaster(r[[3]], template) : input and ouput crs are the same
&gt; r[[3]]&lt;-mask(r[[3]], template)
&gt; r[[4]] &lt;- projectRaster(r[[4]], template)
Warning message:
In projectRaster(r[[4]], template) : input and ouput crs are the same
&gt; r[[4]]&lt;-mask(r[[4]], template)
&gt; r #check they have the same extent, resolution, crs
[[1]]
class      : RasterLayer 
dimensions : 2395, 3379, 8092705  (nrow, ncol, ncell)
resolution : 0.0306, 0.018  (x, y)
extent     : -41.28155, 62.11585, 31.29335, 74.40335  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : memory
names      : Annual.precipitation_wc2.1_2.5m_bio_12 
values     : 107.9981, 2992.399  (min, max)
[[2]]
class      : RasterLayer 
dimensions : 2395, 3379, 8092705  (nrow, ncol, ncell)
resolution : 0.0306, 0.018  (x, y)
extent     : -41.28155, 62.11585, 31.29335, 74.40335  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : BedCrop.tif 
names      : BedCrop 
values     : 0, 8200  (min, max)
[[3]]
class      : RasterLayer 
dimensions : 2395, 3379, 8092705  (nrow, ncol, ncell)
resolution : 0.0306, 0.018  (x, y)
extent     : -41.28155, 62.11585, 31.29335, 74.40335  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : memory
names      : Elevation_wc2.1_2.5m_elev 
values     : -5.991715, 3844.619  (min, max)
[[4]]
class      : RasterLayer 
dimensions : 2395, 3379, 8092705  (nrow, ncol, ncell)
resolution : 0.0306, 0.018  (x, y)
extent     : -41.28155, 62.11585, 31.29335, 74.40335  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : memory
names      : Temp_JAN_wc2.1_2.5m_tavg_01 
values     : -16.45166, 16.23138  (min, max)
&gt;   # 1: &quot;Name file&quot; + &quot;Reprojected&quot;
&gt; writeRaster(r[[1]], &quot;Annual precipitation_Reprojected_wc2.1_2.5m_bio_12 &quot;, type=&quot;tif&quot;) #repeat for each layer, with correct name ```
</details>
# 答案1
**得分**: 1
`writeRaster`函数没有`type =`参数。正确的参数是`format = 'GTiff'`。或者,您可以直接在文件名中指定类型:
```R
writeRaster(r[[1]], "Annual precipitation_Reprojected_wc2.1_2.5m_bio_12.tif")
英文:

writeRaster does not have a type = argument. The correct argument to use is format = &#39;GTiff&#39;. Alternatively, just specify the type in the file name:

writeRaster(r[[1]], &quot;Annual precipitation_Reprojected_wc2.1_2.5m_bio_12.tif&quot;) 

huangapple
  • 本文由 发表于 2023年2月26日 21:14:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572228.html
匿名

发表评论

匿名网友

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

确定