[R] converting raster image

Mintewab Bezabih Mintewab.Bezabih at economics.gu.se
Fri May 11 13:17:53 CEST 2012


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



More information about the R-help mailing list