我在Mac上使用Rstudio,我使用Dismo和Raster包来设置不同文件的分辨率、范围和crs。
整个脚本可以工作,但是输出应该是.tif文件,而RStudio将输出导出为.gri和.grd文件。
我是否错过了安装一个额外的包来获得正确的输出,或者这与使用Mac版本的RStudio有关,因为原始脚本是在Windows版本上编写的?
代码:
> 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 ```
1条答案
按热度按时间xmjla07d1#
writeRaster
没有type =
参数。要使用的正确参数为format = 'GTiff'
。或者,只需在文件名中指定类型: