[R-sig-Geo] error in rasterFromXYZ

Narayani Barve narayani at ku.edu
Thu Jan 10 21:31:12 CET 2013


Hi Michael,

I generate a table of x,y and z after principal component is
generated. In this case z is PCA component value and thus this is my
xyz table which I want to convert to raster using rasterFromXYZ
command.

Regards,
Narayani

On Thu, Jan 10, 2013 at 1:55 PM, Michael Sumner <mdsumner at gmail.com> wrote:
> Regular is different to square, and ESRI ASCII is not XYZ. I suspect you
> should just read it with raster().
>
> HTH
>
> On Friday, January 11, 2013, Narayani Barve wrote:
>>
>> 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
>>
>>
>>



More information about the R-sig-Geo mailing list