Converting raster to a matrix

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Converting raster to a matrix

John Wasige
​Dear all,
Greetings!

I want to convert a big raster (see attached) a matrix, however, I get the error below from the script. Anybody with an Idea on what is going wrong in my script?

###Errors at the following point in the script​

> mat <- as.matrix(rast)
Error in .local(x, ...) : 'x' has no values

#####script


​library(raster)
library(rgdal)

setwd('D:/Mobile_testdata')
image <- raster('D:/Mobile_testdata/SOSug_mean2qgis.tif')

rast <-  raster(image)
mat <- as.matrix(rast)

Thanks for your help

Rgds John


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

SOSug_mean2qgis.tif.aux.xml (1K) Download Attachment
SOSug_mean2qgis.tfw (110 bytes) Download Attachment
SOSug_mean2qgis.tif (5K) Download Attachment
SOSug_mean2qgis.tif.vat.cpg (8 bytes) Download Attachment
SOSug_mean2qgis.tif.vat.dbf (7K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Converting raster to a matrix

Michael Sumner-2
What is dim(rast)?

Try raster::readAll(rast) - what happens?

On Tue, Jan 24, 2017, 19:26 John Wasige <[hidden email]> wrote:

> ​Dear all,
> Greetings!
>
> I want to convert a big raster (see attached) a matrix, however, I get the
> error below from the script. Anybody with an Idea on what is going wrong in
> my script?
>
> ###Errors at the following point in the script​
>
> > mat <- as.matrix(rast)Error in .local(x, ...) : 'x' has no values
>
> #####script
>
>
>
> ​library(raster)
> library(rgdal)
>
> setwd('D:/Mobile_testdata')
> image <- raster('D:/Mobile_testdata/SOSug_mean2qgis.tif')
>
> rast <-  raster(image)
> mat <- as.matrix(rast)
>
> Thanks for your help
>
> Rgds John
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
> University of Tasmania Electronic Communications Policy (December, 2014).
> This email is confidential, and is for the intended recipient only.
> Access, disclosure, copying, distribution, or reliance on any of it by
> anyone outside the intended recipient organisation is prohibited and may be
> a criminal offence. Please delete if obtained in error and email
> confirmation to the sender. The views expressed in this email are not
> necessarily the views of the University of Tasmania, unless clearly
> intended otherwise.
> .
>
--
Dr. Michael Sumner
Software and Database Engineer
Australian Antarctic Division
203 Channel Highway
Kingston Tasmania 7050 Australia

        [[alternative HTML version deleted]]

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

Re: Converting raster to a matrix

Ben Tupper
In reply to this post by John Wasige
Hi,

It doesn't seem to be noted in the documentation for raster::raster(), but when you create a RasterLayer from a RasterLayer,  as in your

> rast <-  raster(image)

step, the values are stripped from the resulting RasterLayer.  Why don't you eliminate that step and retrieve the matrix from your first RasterLayer?

> mat <- as.matrix(image)
> str(mat)
 int [1:68, 1:65] NA NA NA NA NA NA NA NA NA NA ...

Ben




> On Jan 24, 2017, at 3:23 AM, John Wasige <[hidden email]> wrote:
>
> ​Dear all,
> Greetings!
>
> I want to convert a big raster (see attached) a matrix, however, I get the error below from the script. Anybody with an Idea on what is going wrong in my script?
>
> ###Errors at the following point in the script​
>
> > mat <- as.matrix(rast)
> Error in .local(x, ...) : 'x' has no values
>
> #####script
>
>
> ​library(raster)
> library(rgdal)
>
> setwd('D:/Mobile_testdata')
> image <- raster('D:/Mobile_testdata/SOSug_mean2qgis.tif')
>
> rast <-  raster(image)
> mat <- as.matrix(rast)
>
> Thanks for your help
>
> Rgds John
>
> <SOSug_mean2qgis.tif.aux.xml><SOSug_mean2qgis.tfw><SOSug_mean2qgis.tif><SOSug_mean2qgis.tif.vat.cpg><SOSug_mean2qgis.tif.vat.dbf>_______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org




        [[alternative HTML version deleted]]

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