我的光栅有问题,我无法弄清楚。
我想在给定国家使用
Spatraster
检索 crop
(代表世界气候条件)。但是,当我仅在北美国家/地区(即美国和加拿大)crop
此栅格时,初始栅格中的负值不会保存,即使它们应该保存。
require(countrycode)
ISO <- countrycode(c("US", "Canada"), origin = 'country.name', destination = 'iso3c')
[1] "USA" "CAN"
# shapefile
require(geodata)
shp <- gadm(country=ISO, level=0,
path = ("North_Am", "Shapefile", sep="/"),
version="latest")
shp <- crop(shp, ext(-170, -50, 25, 90))
# climatic raster
r <- "Bio6_1981-2010.tif" %>% rast(.)
datatype(r)
"INT2U"
require(terra)
cshp <- crop(r, shp, mask=TRUE)
cshp
class : SpatRaster
dimensions : 6973, 14086, 1 (nrow, ncol, nlyr)
resolution : 0.008333333, 0.008333333 (x, y)
extent : -170.0001, -52.61681, 24.99986, 83.10819 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
source : memory
name : Bio6_1981-2010
min value : 0 #I'm losing the negative values, when I shouldn't be
max value : 20
plot(cshp)
有什么建议吗?
正如评论中所建议的,当我使用
rast
函数打开它时,问题来自于我的初始栅格的数据类型。
栅格以“INT2U”数据类型打开,该数据类型不能包含负值(更多信息请参见此处)。
然后我需要将其转换为另一种数据类型,例如“FLT4S”。
r <- writeRaster(r, 'new.grd', datatype='FLT4S', overwrite=TRUE)
datatype(r)
"FLT4S"