英文:
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'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:
```> library(dismo)
> 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, 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)
> # 1: "Name file" + "Reprojected"
> writeRaster(r[[1]], "Annual precipitation_Reprojected_wc2.1_2.5m_bio_12 ", type="tif") #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 = 'GTiff'
. Alternatively, just specify the type in the file name:
writeRaster(r[[1]], "Annual precipitation_Reprojected_wc2.1_2.5m_bio_12.tif")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论