[R-sig-Geo] error in rasterFromXYZ

Narayani Barve narayani at ku.edu
Thu Jan 10 20:19:26 CET 2013


Hi All,

I have ESRI ASCII grid format files. I want to apply principal
component on those files and then save those PCA components into ASCII
grid format. I used raster package to perform this operation. My steps
are
1. Read ASCII file, and make stack
2. Use rasterToPoints function to get the matrix to be used for PCA
3. use princomp function on the generated table above.
4. extract the scores from pca output.
5. convert the score of each component to table of 3 columns (x,y,comp)
6. use rasterFromXYZ function to convert the component to raster
7. Save the raster object to ASCII grid.

The trouble is I am receiving error at step 6, for rasterFromXYZ
function. It says "Error in rasterFromXYZ(tbl2) : y cell sizes are not
regular"
I checked the cell sizes for both x and y, to be sure. It is square. I
also tried supplying, digits=5 argument, but no use. I am not able
tease out the error. Am I doing anything wrong? The written code is
below. I cannot supply with the files, as these files are huge.

   BioStack = MakeStack("Select ASCII files :")
   BioPt1 = rasterToPoints(BioStack)
   print("Generating principal component")
   pcaPt1 = princomp(BioPt1[,3:dim(BioPt1)[2]], cor = "TRUE")
   pcaScores = pcaPt1$scores
   d1 = dim(pcaScores)
   for ( i in 1:d1[2])
   {
      print(paste("Writing principal component file ", i ), sep = " ")
      tbl1 = cbind(BioPt1[,1:2], pcaScores[,i])
      r2 = rasterFromXYZ(tbl1,digits=5)
      FileName = paste("Comp",i,".asc", sep = "")
      writeRaster(r2,FileName)
   }


Thank you in advance and any help is appreciated.

Narayani


-- 
Narayani Barve
PhD Candidate,
Ecology and Evolutionary Biology,
University of Kansas, KS 66044



More information about the R-sig-Geo mailing list