2017-04-23 1 views
1

Compte tenu d'un rasterLayera,R rasterFromXYZ: x la taille des cellules ne sont pas régulières

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

Sur la base de la description ci-dessous (voir ci-dessous) qui se trouve également dans ce lien, ftp://ccrp.tor.ec.gc.ca/pub/EC_data/CANGRD/

Je suis arrivé avec les éléments suivants 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) 

par souci d'exhaustivité, j'ai créé un .Rdata fichier ~209 KB qui peut être trouvé here.

L'intention est de réduire le a à une base de données et d'effectuer une analyse dessus puis de faire un rasterFROMXY. Cependant, je reçois l'erreur:

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

Mon code:

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 

Pour contourner ce problème, j'ai essayé de projectRaster à LatLon régulière (projj <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0") ) et même rasterrize mais n'a pas pu obtenir les dimensions d'origine de a. Je voudrais ras pour avoir les mêmes dimensions que a mais sur latlon grille.

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

La grille CANGRD est en projection stéréographique polaire avec une résolution spatiale de 50 km. La grille est une matrice de 125 (colonnes) par 95 (rangées), où le coin sud-ouest (0,0) est à 40,0451 ° de latitude nord et 129,8530 ° de longitude ouest. La projection est vraie à 60,0 ° N et centrée sur 110,0 ° W. Le fichier 'CANGRD_points_LL.txt' répertorie les latitudes et les longitudes pour chaque point de la grille.

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épondre

0

Au lieu d'utiliser getValues utiliser as.data.frame pour convertir raster en dataframe.

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) 

enter image description here