[R-sig-Geo] error in rasterFromXYZ
Narayani Barve
narayani at ku.edu
Thu Jan 10 21:32:31 CET 2013
I use following function to generate the stack from .asc files.
library(raster)
MakeStack <- function(Prompt)
{
Mfiles = choose.files(caption=Prompt)
for (i in 1: length(Mfiles))
{
fl1 = raster(Mfiles[i])
if (i == 1)
{
stk = stack(fl1)
}
else
{
stk = stack(stk, fl1)
}
}
return(stk)
}
Regards,
Narayani
On Thu, Jan 10, 2013 at 2:05 PM, Etienne B. Racine <etiennebr at gmail.com> wrote:
> What code are you using to read your stack ?
>
> Try this as a stack (from stack example)
> library(raster)
> fn <- system.file("external/test.grd", package="raster")
> s <- stack(fn, fn, fn)
> BioStack = s
>
> And then run the rest of your code.
> Etienne
>
>
> 2013/1/10 Michael Sumner <mdsumner at gmail.com>
>>
>> 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