【问题标题】:R rasterFromXYZ: x cell sizes are not regularR rasterFromXYZ:x 像元大小不规则
【发布时间】:2017-09-20 08:30:19
【问题描述】:

给定一个rasterLayera

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

根据下面的描述(见下文),也可以在此链接中找到, 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。但是,我得到了错误:

rasterFromXYZ(dt) 中的错误:x 像元大小不规则

我的代码:

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 &lt;- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0") ) 甚至rasterrize,但无法获得a 的原始尺寸。我希望 rasa 具有相同的尺寸,但在 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.      

【问题讨论】:

    标签: r maps coordinates gis raster


    【解决方案1】:

    不要使用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)
    

    【讨论】:

      猜你喜欢
      • 2019-03-06
      • 2016-11-19
      • 1970-01-01
      • 2020-02-25
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      相关资源
      最近更新 更多