[GRASSLIST:8448] extracting the value of a raster cell

Does anyone know how to extract the value of a cell at given coordinates from a raster layer? I am writing a script that will calculate the spatial mean for a given raster layer. Therefore, I need to be able to access the values of raster cells.

Thanks,

  • Jason

Jason Horn

Boston University Department of Biology

5 Cumington Street Boston, MA 02215

jhorn@bu.edu

office: 617 353 6987

cell: 401 588 2766

Does anyone know how to extract the value of a cell at given
coordinates from a raster layer?

r.what

I am writing a script that will calculate the spatial mean for a given
raster layer. Therefore, I need to be able to access the values of
raster cells.

r.univar?

If you want the stats within a buffer around a point, I would suggest a
vector point map -> v.buffer -> v.to.rast -> MASK map -> r.univar in a
loop, or probably simpler with Markus's new v.rast.stats module in
6.1-cvs.

Hamish

Thank you Hamish, for the response.

As usual, I don't think I was specific enough. What I'm looking for
is a way to extract the value of a particular raster cell. r.what
will do the trick, but only if you know the exact coordinates. I
want to loop through in a script and extract the value of each cell
in a raster coverage. For example, if a raster layer was a 10x10
grid, I want the value of cell (1,1), then cell(2,1), then cell
(3,1), etc.. If I were to use r.what, calculating the location of
the next cell using resolution and the coordinates of the first cell,
I might accidentally skip a cell due to rounding errors. r.mapcalc
allows you to access the value of a cell directly, but the result is
always another raster map - you can't output the cell value directly.

The reason for all of this is that I am trying to write my own script
for calculating the spatial mean of a given raster map. I am
surprised that there is no facility for this in GRASS already - it
seems like such a basic spatial statistic. My solution, so far, is
to output the map as an ascii file, then load the ascii file into an
array within a script. It works, but seems like a ridiculous
workaround for something so simple.

If anyone can think of another way, please post it. All suggestions
welcome.

I guess I should probably write my own module in C, but I've never
had much luck getting GRASS to compile successfully in the first
place, so I'll stick with the script route for now.

- Jason

On Sep 28, 2005, at 7:57 PM, Hamish wrote:

Does anyone know how to extract the value of a cell at given
coordinates from a raster layer?

r.what

I am writing a script that will calculate the spatial mean for a given
raster layer. Therefore, I need to be able to access the values of
raster cells.

r.univar?

If you want the stats within a buffer around a point, I would suggest a
vector point map -> v.buffer -> v.to.rast -> MASK map -> r.univar in a
loop, or probably simpler with Markus's new v.rast.stats module in
6.1-cvs.

Hamish

Jason,

I have had similar issues that were a pain with scripting languages, and have thus resorted to using the the GRASS C API. It is actually much simpler to use than I first expected.

You might try looking at the source for r.out.ascii, where it reads each cell's value: the source code is only 205 lines long.

good luck

Dylan

On Sep 30, 2005, at 8:34 AM, Jason Horn wrote:

Thank you Hamish, for the response.

As usual, I don't think I was specific enough. What I'm looking for
is a way to extract the value of a particular raster cell. r.what
will do the trick, but only if you know the exact coordinates. I
want to loop through in a script and extract the value of each cell
in a raster coverage. For example, if a raster layer was a 10x10
grid, I want the value of cell (1,1), then cell(2,1), then cell
(3,1), etc.. If I were to use r.what, calculating the location of
the next cell using resolution and the coordinates of the first cell,
I might accidentally skip a cell due to rounding errors. r.mapcalc
allows you to access the value of a cell directly, but the result is
always another raster map - you can't output the cell value directly.

The reason for all of this is that I am trying to write my own script
for calculating the spatial mean of a given raster map. I am
surprised that there is no facility for this in GRASS already - it
seems like such a basic spatial statistic. My solution, so far, is
to output the map as an ascii file, then load the ascii file into an
array within a script. It works, but seems like a ridiculous
workaround for something so simple.

If anyone can think of another way, please post it. All suggestions
welcome.

I guess I should probably write my own module in C, but I've never
had much luck getting GRASS to compile successfully in the first
place, so I'll stick with the script route for now.

- Jason

On Sep 28, 2005, at 7:57 PM, Hamish wrote:

Does anyone know how to extract the value of a cell at given
coordinates from a raster layer?

r.what

I am writing a script that will calculate the spatial mean for a given
raster layer. Therefore, I need to be able to access the values of
raster cells.

r.univar?

If you want the stats within a buffer around a point, I would suggest a
vector point map -> v.buffer -> v.to.rast -> MASK map -> r.univar in a
loop, or probably simpler with Markus's new v.rast.stats module in
6.1-cvs.

Hamish

--
Dylan Beaudette
Soils and Biogeochemistry Graduate Group
University of California at Davis
530.754.7341

Jason,

GRASS doesn't have much native support for geostatistical calculations such as spatial mean, but it should be a fairly trivial task if you use the R/GRASS interface. A few of my co-workers use R in this fashion, with generally great success. A primer on the topic is available at: http://grass.itc.it/statsgrass/grass_geostats.html.

- Shaun

Jason Horn wrote:

Thank you Hamish, for the response.

As usual, I don't think I was specific enough. What I'm looking for
is a way to extract the value of a particular raster cell. r.what
will do the trick, but only if you know the exact coordinates. I
want to loop through in a script and extract the value of each cell
in a raster coverage. For example, if a raster layer was a 10x10
grid, I want the value of cell (1,1), then cell(2,1), then cell
(3,1), etc.. If I were to use r.what, calculating the location of
the next cell using resolution and the coordinates of the first cell,
I might accidentally skip a cell due to rounding errors. r.mapcalc
allows you to access the value of a cell directly, but the result is
always another raster map - you can't output the cell value directly.

The reason for all of this is that I am trying to write my own script
for calculating the spatial mean of a given raster map. I am
surprised that there is no facility for this in GRASS already - it
seems like such a basic spatial statistic. My solution, so far, is
to output the map as an ascii file, then load the ascii file into an
array within a script. It works, but seems like a ridiculous
workaround for something so simple.

If anyone can think of another way, please post it. All suggestions
welcome.

I guess I should probably write my own module in C, but I've never
had much luck getting GRASS to compile successfully in the first
place, so I'll stick with the script route for now.

- Jason

On Sep 28, 2005, at 7:57 PM, Hamish wrote:

Does anyone know how to extract the value of a cell at given
coordinates from a raster layer?

r.what

I am writing a script that will calculate the spatial mean for a given
raster layer. Therefore, I need to be able to access the values of
raster cells.

r.univar?

If you want the stats within a buffer around a point, I would suggest a
vector point map -> v.buffer -> v.to.rast -> MASK map -> r.univar in a
loop, or probably simpler with Markus's new v.rast.stats module in
6.1-cvs.

Hamish

    Check out starspan: starspan.casil.ucdavis.edu -- some day we might work
on folding this into GRASS, but until then its an incredibly efficient way
to extract raster values from vector layers and do some stats on them...

--j

On 9/30/05 9:56 AM, "Shaun Walbridge" <walbridge@nceas.ucsb.edu> wrote:

Jason,

GRASS doesn't have much native support for geostatistical calculations
such as spatial mean, but it should be a fairly trivial task if you use
the R/GRASS interface. A few of my co-workers use R in this fashion,
with generally great success. A primer on the topic is available at:
http://grass.itc.it/statsgrass/grass_geostats.html.

- Shaun

Jason Horn wrote:

Thank you Hamish, for the response.

As usual, I don't think I was specific enough. What I'm looking for
is a way to extract the value of a particular raster cell. r.what
will do the trick, but only if you know the exact coordinates. I
want to loop through in a script and extract the value of each cell
in a raster coverage. For example, if a raster layer was a 10x10
grid, I want the value of cell (1,1), then cell(2,1), then cell
(3,1), etc.. If I were to use r.what, calculating the location of
the next cell using resolution and the coordinates of the first cell,
I might accidentally skip a cell due to rounding errors. r.mapcalc
allows you to access the value of a cell directly, but the result is
always another raster map - you can't output the cell value directly.

The reason for all of this is that I am trying to write my own script
for calculating the spatial mean of a given raster map. I am
surprised that there is no facility for this in GRASS already - it
seems like such a basic spatial statistic. My solution, so far, is
to output the map as an ascii file, then load the ascii file into an
array within a script. It works, but seems like a ridiculous
workaround for something so simple.

If anyone can think of another way, please post it. All suggestions
welcome.

I guess I should probably write my own module in C, but I've never
had much luck getting GRASS to compile successfully in the first
place, so I'll stick with the script route for now.

- Jason

On Sep 28, 2005, at 7:57 PM, Hamish wrote:

Does anyone know how to extract the value of a cell at given
coordinates from a raster layer?

r.what

I am writing a script that will calculate the spatial mean for a given
raster layer. Therefore, I need to be able to access the values of
raster cells.

r.univar?

If you want the stats within a buffer around a point, I would suggest a
vector point map -> v.buffer -> v.to.rast -> MASK map -> r.univar in a
loop, or probably simpler with Markus's new v.rast.stats module in
6.1-cvs.

Hamish

--
Jonathan A. Greenberg, PhD
NRC Research Associate
NASA Ames Research Center
MS 242-4
Moffett Field, CA 94035-1000
650-604-5896
AIM: jgrn307
MSN: jgrn307@hotmail.com

> Can you explain what is meant by the "spatial mean" calculation?

A spatial mean is simply the mean value of series of numbers that
also has a location in coordinate space. For example, imagine the
population of each state in the continental US. You can easily
calculate the mean population size, right? Ok, now WHERE is the mean
population on the map of the US? It's actually simple to calculate.
The spatial mean is really just a weighted average using the
coordinates of the center of each state. So, to calculate the
spatial mean for a raster map, you need the value and coordinate pair
for each cell. Hence, my original request.

Thanks for explaining that. So the "answer" is a spatial coordinate at
the "center of mass"; not a number in units of the attribute, correct?

No, I don't think anything exists in grass to do this, but probably the
most efficient way to get the values out using existing tools is:
(Short of writing a custom C module that is)

  r.stats -g input=raster_map

alternatively,

  r.to.vect(points) + v.out.ascii.db.

v.out.ascii.db is available at the GRASS Wiki add-ons page, I've not
tested it for a dataset as large as r.to.vect(points) will generate.
I'd be interested to know if it works.

But who knows, maybe there's a smart way to use r.cost + r.mapcalc in an
inverse way to find a maxima who's position could be queried.

Hamish