Error in writeRaster for signature RasterPCA

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Error in writeRaster for signature RasterPCA

R-sig-geo mailing list
I am currently trying to obtain two rasters that are the Principal
component axes 1 and 2 for a list of bioclimatic variables that I am using.
I am using the rasterPCA() function that exists in the RStoolbox in R, but
I am unable to write it to a raster. Below is the code and the error that I
am getting:

library(raster)
library(RStoolbox)
##Loading the variables and then creating a raster stack, followed by a PCA
bio3 <- raster("C:\\Users\\rameshv\\bio3")
bio4 <- raster("C:\\Users\\rameshv\\bio4")
bio5 <- raster("C:\\Users\\rameshv\\bio5")
bio6 <- raster("C:\\Users\\rameshv\\bio6")
bio14 <- raster("C:\\Users\\rameshv\\bio_14")

pres_stack <- stack(bio3, bio4,bio5,bio6,bio14)

pre_pca <-  rasterPCA(pres_stack, nComp = 2)

writeRaster(pre_pca, "C:\\Users\\rameshv\\PC.tif")

Error in (function (classes, fdef, mtable) : unable to find an inherited
method for function ‘writeRaster’ for signature ‘"rasterPCA", "character"’

I even tried a modification of the above code

writeRaster(pre_pca,"C:\\Users\\rameshv\\4_PCAforR\\PC.asc",
format="ascii", bylayer=T)

Getting the same error as above: Error in (function (classes, fdef, mtable)
: unable to find an inherited method for function ‘writeRaster’ for
signature ‘"rasterPCA", "character"’

Any suggestions?
Vijay
--
Data Manager,
Barbara Han Lab,
Cary Institute of Ecosystem Studies,
2801 Sharon Turnpike, Millbrook, NY 12545
Lab Site : http://www.hanlab.science/
Personal Site : http://evolecol.weebly.com/
Phone : (845)-677-7600 Ext: 241

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error in writeRaster for signature RasterPCA

Bacou, Melanie
Vijay,

`rasterPCA` returns an object of class `rasterPCA` (a list). The actual
raster layers (components) can be accessed via its `map` element. See
example below.

--Mel.

library(raster)
library(RStoolbox)

bio <- getData("worldclim", var="bio", res=10, path="~/WorldClim")

# You can use `filename=` arg to save the resulting rasters
pre_pca <- rasterPCA(bio, nComp=2, filename="~/PC.tif")

class(pre_pca)
## [1] "rasterPCA" "RStoolbox"

summary(pre_pca)
##       Length  Class       Mode
## call        4 -none-      call
## model       7 princomp    list
## map   3888000 RasterBrick S4

dim(pre_pca$map)
## [1]  900 2160    2

# Plot the 1st comp.
ggR(pre_pca$map[[1]])


On 03/07/2017 01:06 PM, Vijay Ramesh via R-sig-Geo wrote:

> I am currently trying to obtain two rasters that are the Principal
> component axes 1 and 2 for a list of bioclimatic variables that I am using.
> I am using the rasterPCA() function that exists in the RStoolbox in R, but
> I am unable to write it to a raster. Below is the code and the error that I
> am getting:
>
> library(raster)
> library(RStoolbox)
> ##Loading the variables and then creating a raster stack, followed by a PCA
> bio3 <- raster("C:\\Users\\rameshv\\bio3")
> bio4 <- raster("C:\\Users\\rameshv\\bio4")
> bio5 <- raster("C:\\Users\\rameshv\\bio5")
> bio6 <- raster("C:\\Users\\rameshv\\bio6")
> bio14 <- raster("C:\\Users\\rameshv\\bio_14")
>
> pres_stack <- stack(bio3, bio4,bio5,bio6,bio14)
>
> pre_pca <-  rasterPCA(pres_stack, nComp = 2)
>
> writeRaster(pre_pca, "C:\\Users\\rameshv\\PC.tif")
>
> Error in (function (classes, fdef, mtable) : unable to find an inherited
> method for function ‘writeRaster’ for signature ‘"rasterPCA", "character"’
>
> I even tried a modification of the above code
>
> writeRaster(pre_pca,"C:\\Users\\rameshv\\4_PCAforR\\PC.asc",
> format="ascii", bylayer=T)
>
> Getting the same error as above: Error in (function (classes, fdef, mtable)
> : unable to find an inherited method for function ‘writeRaster’ for
> signature ‘"rasterPCA", "character"’
>
> Any suggestions?
> Vijay


        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error in writeRaster for signature RasterPCA

R-sig-geo mailing list
Thank you Melanie.

I failed to see that it gives out a list and I was giving the wrong call.

Vijay

On Wed, Mar 8, 2017 at 8:23 AM, Melanie Bacou <[hidden email]> wrote:

> Vijay,
>
> `rasterPCA` returns an object of class `rasterPCA` (a list). The actual
> raster layers (components) can be accessed via its `map` element. See
> example below.
>
> --Mel.
>
> library(raster)
> library(RStoolbox)
>
> bio <- getData("worldclim", var="bio", res=10, path="~/WorldClim")
>
> # You can use `filename=` arg to save the resulting rasters
> pre_pca <- rasterPCA(bio, nComp=2, filename="~/PC.tif")
>
> class(pre_pca)
> ## [1] "rasterPCA" "RStoolbox"
>
> summary(pre_pca)
> ##       Length  Class       Mode
> ## call        4 -none-      call
> ## model       7 princomp    list
> ## map   3888000 RasterBrick S4
>
> dim(pre_pca$map)
> ## [1]  900 2160    2
>
> # Plot the 1st comp.
> ggR(pre_pca$map[[1]])
>
> On 03/07/2017 01:06 PM, Vijay Ramesh via R-sig-Geo wrote:
>
> I am currently trying to obtain two rasters that are the Principal
> component axes 1 and 2 for a list of bioclimatic variables that I am using.
> I am using the rasterPCA() function that exists in the RStoolbox in R, but
> I am unable to write it to a raster. Below is the code and the error that I
> am getting:
>
> library(raster)
> library(RStoolbox)
> ##Loading the variables and then creating a raster stack, followed by a PCA
> bio3 <- raster("C:\\Users\\rameshv\\bio3")
> bio4 <- raster("C:\\Users\\rameshv\\bio4")
> bio5 <- raster("C:\\Users\\rameshv\\bio5")
> bio6 <- raster("C:\\Users\\rameshv\\bio6")
> bio14 <- raster("C:\\Users\\rameshv\\bio_14")
>
> pres_stack <- stack(bio3, bio4,bio5,bio6,bio14)
>
> pre_pca <-  rasterPCA(pres_stack, nComp = 2)
>
> writeRaster(pre_pca, "C:\\Users\\rameshv\\PC.tif")
>
> Error in (function (classes, fdef, mtable) : unable to find an inherited
> method for function ‘writeRaster’ for signature ‘"rasterPCA", "character"’
>
> I even tried a modification of the above code
>
> writeRaster(pre_pca,"C:\\Users\\rameshv\\4_PCAforR\\PC.asc",
> format="ascii", bylayer=T)
>
> Getting the same error as above: Error in (function (classes, fdef, mtable)
> : unable to find an inherited method for function ‘writeRaster’ for
> signature ‘"rasterPCA", "character"’
>
> Any suggestions?
> Vijay
>
>
>


--
Data Manager,
Barbara Han Lab,
Cary Institute of Ecosystem Studies,
2801 Sharon Turnpike, Millbrook, NY 12545
Lab Site : http://www.hanlab.science/
Personal Site : http://evolecol.weebly.com/
Phone : (845)-677-7600 Ext: 241

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Loading...