Dear R users, I was wondering how I can convert a raster image (that made R through interpolation) into an ascii or csv format? this is the last line of my command p <- interpolate(r, tpsfit) So p is my raster file which I want to convert into ascii or csv Many thanks Regards Mintewab
converting raster image
4 messages · Mintewab Bezabih, R. Michael Weylandt, Michael Sumner
write.csv() perhaps? I'm not sure what the csv of a raster image is though... What does p look like? str(p) will be important -- if the raster codes (RGB values) are in lists or 3D arrays or something more complex (i.e., if no grayscale) you might need to put them in multiple files. Hope this helps, Michael On Thu, May 10, 2012 at 11:12 AM, Mintewab Bezabih
<Mintewab.Bezabih at economics.gu.se> wrote:
Dear R users, I was wondering how I can convert a raster image (that made R through interpolation) ?into an ascii or csv format? this is the last line of my command p <- interpolate(r, tpsfit) So p is my raster file which I want to convert into ascii or csv Many thanks Regards Mintewab
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
You should provide reproducible code and at the very least declare the
packages you are using. I guess this from the package raster? See
example in ?raster::interpolate:
## Thin plate spline interpolation with x and y only
library(fields)
r <- raster(system.file("external/test.grd", package="raster"))
ra <- aggregate(r, 10)
xy <- data.frame(xyFromCell(ra, 1:ncell(ra)))
v <- getValues(ra)
tps <- Tps(xy, v)
p <- raster(r)
p <- interpolate(p, tps)
One way to convert this to tabular format is to coerce to
SpatialGridDataFrame, then write that out with write.table
(write.csv):
require(sp)
head(as.data.frame(as(p, "SpatialGridDataFrame")))
layer s1 s2
1 1823.450 178420 333980
2 1818.925 178460 333980
3 1814.405 178500 333980
4 1809.876 178540 333980
5 1805.324 178580 333980
6 1800.734 178620 333980
There would be functions in raster to do that more directly, I'm just
not that familiar with it.
So,
x <- as.data.frame(as(p, "SpatialGridDataFrame"))
Now x is just a data.frame with every cell stored explicitly with its
centre coordinate, "s1" and "s2" are the X and Y coordinates, "layer"
is the raster value.
Cheers, Mike.
On Fri, May 11, 2012 at 1:12 AM, Mintewab Bezabih
<Mintewab.Bezabih at economics.gu.se> wrote:
Dear R users, I was wondering how I can convert a raster image (that made R through interpolation) ?into an ascii or csv format? this is the last line of my command p <- interpolate(r, tpsfit) So p is my raster file which I want to convert into ascii or csv Many thanks Regards Mintewab
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Michael Sumner Institute for Marine and Antarctic Studies, University of Tasmania Hobart, Australia e-mail: mdsumner at gmail.com
Thanks Mike and Micheal for suggestions. It now worked!
Many thanks
Mintewab
________________________________________
Fr?n: Michael Sumner [mdsumner at gmail.com]
Skickat: den 11 maj 2012 01:20
Till: Mintewab Bezabih
Kopia: r-help at r-project.org
?mne: Re: [R] converting raster image
You should provide reproducible code and at the very least declare the
packages you are using. I guess this from the package raster? See
example in ?raster::interpolate:
## Thin plate spline interpolation with x and y only
library(fields)
r <- raster(system.file("external/test.grd", package="raster"))
ra <- aggregate(r, 10)
xy <- data.frame(xyFromCell(ra, 1:ncell(ra)))
v <- getValues(ra)
tps <- Tps(xy, v)
p <- raster(r)
p <- interpolate(p, tps)
One way to convert this to tabular format is to coerce to
SpatialGridDataFrame, then write that out with write.table
(write.csv):
require(sp)
head(as.data.frame(as(p, "SpatialGridDataFrame")))
layer s1 s2
1 1823.450 178420 333980
2 1818.925 178460 333980
3 1814.405 178500 333980
4 1809.876 178540 333980
5 1805.324 178580 333980
6 1800.734 178620 333980
There would be functions in raster to do that more directly, I'm just
not that familiar with it.
So,
x <- as.data.frame(as(p, "SpatialGridDataFrame"))
Now x is just a data.frame with every cell stored explicitly with its
centre coordinate, "s1" and "s2" are the X and Y coordinates, "layer"
is the raster value.
Cheers, Mike.
On Fri, May 11, 2012 at 1:12 AM, Mintewab Bezabih
<Mintewab.Bezabih at economics.gu.se> wrote:
Dear R users, I was wondering how I can convert a raster image (that made R through interpolation) into an ascii or csv format? this is the last line of my command p <- interpolate(r, tpsfit) So p is my raster file which I want to convert into ascii or csv Many thanks Regards Mintewab
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
-- Michael Sumner Institute for Marine and Antarctic Studies, University of Tasmania Hobart, Australia e-mail: mdsumner at gmail.com