R rasterFromXYZ:x 像元大小不规则

R rasterFromXYZ: x cell sizes are not regular

给定 rasterLayer a,

library(raster)
library(rasterVis)
a=raster('p190001.grd')
head(a)

根据下面的描述(见下文)也可以在 link 中找到, ftp://ccrp.tor.ec.gc.ca/pub/EC_data/CANGRD/

我想出了以下内容CRS

mycrs <- CRS("+proj=stere +lat_0=90 +lat_ts=60 +lon_0=-110 +x_0=1884770 +y_0=5220000 +datum=WGS84 +to_meter=50000")

   proj4string(a) <- mycrs
    projection(a) <- mycrs
a
    class       : RasterLayer 
dimensions  : 95, 125, 11875  (nrow, ncol, ncell)
resolution  : 1, 1  (x, y)
extent      : -0.5, 124.5, -0.5, 94.5  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=stere +lat_0=90 +lat_ts=60 +lon_0=-110 +x_0=1884770 +y_0=5220000 +datum=WGS84 +to_meter=50000 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : p190001 
values      : 4.59, 335.45  (min, max)

a[a==170141000918782798866653488190622531584.00]=NA # change missing value identifier
levelplot(a)

为了完整起见,我创建了一个 .Rdata 文件 ~209 KB,可以在 here.

中找到

目的是将 a 折叠成数据框并对其执行一些分析,然后再执行 rasterFROMXY。但是,我收到错误消息:

Error in rasterFromXYZ(dt) : x cell sizes are not regular

我的代码:

library(data.table)
v <- getValues(a) # get vector
dt <- as.data.table(v)

# Add LATITUDE and LONGITUDE columns to dt

dt[,LATITUDE:=grid_pnt_lls$y]
dt[,LONGITUDE:=grid_pnt_lls$x]
dtnames <- c(names(dt)[2:3],names(dt)[1]) 

setcolorder(dt,dtnames)

vcols <- c('LONGITUDE','LATITUDE','v')

setcolorder(dt,vcols)

library(data.table)
setnames(dt, "LONGITUDE", "x")
setnames(dt, "LATITUDE", "y")
setnames(dt, "v", "z")


ras=rasterFromXYZ(dt)#     Error in rasterFromXYZ(dt) : x cell sizes are not regular

作为解决方法,我尝试 projectRaster 到常规 latlon (projj <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0") ) 甚至 rasterrize 但无法获得 a 的原始尺寸。我希望 ras 具有与 a 相同的尺寸,但在 latlon 网格上。

#===========================================================================  

CANGRD 网格采用极地立体投影,空间分辨率为 50 公里。网格是一个 125(列)乘 95(行)的矩阵,其中西南角 (0,0) 位于北纬 40.0451° 和西经 129.8530°。投影在 60.0°N 处是真实的,以 110.0°W 为中心。文件“CANGRD_points_LL.txt”列出了每个网格点的纬度和经度。

The general format of the ‘YYYYDD.grd’ file is:
Id – ‘DSAA’ identifies the file as an ASCII grid file
nx ny - nx is the integer number of grid lines along the X axis (columns)
        ny is the integer number of grid lines along the Y axis (rows)
xlo xhi - xlo is the minimum X value of the grid
          xhi is the maximum X value of the grid
ylo yhi - ylo is the minimum Y value of the grid
          yhi is the maximum Y value of the grid
zlo zhi - are the minimum and maximum Z values of the grid.      

而不是使用 getValues 使用 as.data.frame 将栅格转换为数据帧。

dt <- data.table(as.data.frame(a, xy = TRUE))
setnames(dt, "p190001", "z")

## Sample Calculation on Values
dt[, z := z/2]

ras <- rasterFromXYZ(dt)
plot(ras)