[GRASS-user] query raster cell and its neighbors by point

I need to easily query a raster and return both the value of the cell and the aggregate value of the 8-cell neighborhood, given a set of point coordinates. I know I can easily do this with r.mapcalc, but that involves running the computation for the entire map even though I'm only interested in a very very small portion. Is there an easier way to do this or am I stuck with the long solution?

Thanks Community!
Shaun

Shaun Langley wrote:

I need to easily query a raster and return both the value of the cell and
the aggregate value of the 8-cell neighborhood, given a set of point
coordinates. I know I can easily do this with r.mapcalc, but that involves
running the computation for the entire map even though I'm only interested
in a very very small portion. Is there an easier way to do this or am I
stuck with the long solution?

What kind of an aggregation exactly? Maybe not straight-through, but
I think you will be "forced" to use G7:

http://grass.osgeo.org/grass70/manuals/r.neighbors.html

in a way like

r.neighbors \
input=QueriedRaster \
selection=CellsOfInterest \
output=AggregatedRaster \
method=SomeMethod[,MoreMethods,EvenMoreMethods] \
size=3 \
title="Aggregated raster map"

However, you say you need the "queried" cell-value as well. Maybe combine with

http://grass.osgeo.org/grass70/manuals/r.what

Best, Nikos

Nikos,

Since my input raster is binary (0s and 1s), and I need to know how many of the neighboring cells are 1s, it's sufficient to simply compute the sum of the overall neighborhood. If I use r.mapcalc to mask out the rest of the raster, I can compute the sum of the entire map with r.sum. However, since I need to do this for hundreds of points, it seems silly to use such a cumbersome method that will require a lot of processing time.

Shaun
On Mar 14, 2013, at 2:29 PM, Nikos Alexandris <nik@nikosalexandris.net> wrote:

Shaun Langley wrote:

I need to easily query a raster and return both the value of the cell and
the aggregate value of the 8-cell neighborhood, given a set of point
coordinates. I know I can easily do this with r.mapcalc, but that involves
running the computation for the entire map even though I'm only interested
in a very very small portion. Is there an easier way to do this or am I
stuck with the long solution?

What kind of an aggregation exactly? Maybe not straight-through, but
I think you will be "forced" to use G7:

http://grass.osgeo.org/grass70/manuals/r.neighbors.html

in a way like

r.neighbors \
input=QueriedRaster \
selection=CellsOfInterest \
output=AggregatedRaster \
method=SomeMethod[,MoreMethods,EvenMoreMethods] \
size=3 \
title="Aggregated raster map"

However, you say you need the "queried" cell-value as well. Maybe combine with

http://grass.osgeo.org/grass70/manuals/r.what

Best, Nikos

Shaun Langley wrote:

> I need to easily query a raster and return both the value of the cell and
> the aggregate value of the 8-cell neighborhood, given a set of point
> coordinates. I know I can easily do this with r.mapcalc, but that
> involves running the computation for the entire map even though I'm only
> interested in a very very small portion. Is there an easier way to do
> this or am I stuck with the long solution?

Nikos Alexandris wrote:

What kind of an aggregation exactly? Maybe not straight-through, but
I think you will be "forced" to use G7:
http://grass.osgeo.org/grass70/manuals/r.neighbors.html

in a way like

r.neighbors \
input=QueriedRaster \
selection=CellsOfInterest \
output=AggregatedRaster \
method=SomeMethod[,MoreMethods,EvenMoreMethods] \
size=3 \
title="Aggregated raster map"

However, you say you need the "queried" cell-value as well. Maybe combine
with
http://grass.osgeo.org/grass70/manuals/r.what

Maybe combine all this with a MASK? i.e., create buffer(s) around your
coordinates of interest, use r.what and r.neighbors then?

(@MMetz -- maybe of your interest?)
And, at this point comes the idea: why not let r.neighbors, when a
"selection=RasterMap" is defined, return in an extra raster map the one-central
queried cell?

Best, Nikos

Shaun Langley wrote:

I need to easily query a raster and return both the value of the cell and
the aggregate value of the 8-cell neighborhood, given a set of point
coordinates. I know I can easily do this with r.mapcalc, but that
involves running the computation for the entire map even though I'm only
interested in a very very small portion. Is there an easier way to do
this or am I stuck with the long solution?

Nikos Alexandris wrote:

What kind of an aggregation exactly? Maybe not straight-through, but
I think you will be "forced" to use G7:
http://grass.osgeo.org/grass70/manuals/r.neighbors.html

in a way like

r.neighbors \
input=QueriedRaster \
selection=CellsOfInterest \
output=AggregatedRaster \
method=SomeMethod[,MoreMethods,EvenMoreMethods] \
size=3 \
title="Aggregated raster map"

However, you say you need the "queried" cell-value as well. Maybe combine
with
http://grass.osgeo.org/grass70/manuals/r.what

Maybe combine all this with a MASK? i.e., create buffer(s) around your
coordinates of interest, use r.what and r.neighbors then?

Ah! I think I see what you're saying. If I create a new raster where all cells are replaced by the sum of their neighbors, then I can run queries for each point without having to recompute the neighborhood. This might work. Is this what you had in mind?

(@MMetz -- maybe of your interest?)
And, at this point comes the idea: why not let r.neighbors, when a
"selection=RasterMap" is defined, return in an extra raster map the one-central
queried cell?

[meant to hit the list as well!]

Shaun Langley wrote:

>>> I need to easily query a raster and return both the value of the cell
>>> and the aggregate value of the 8-cell neighborhood, given a set of point
>>> coordinates.

Nikos Alexandris wrote:

>> What kind of an aggregation exactly? Maybe not straight-through, but

Shaun Langley wrote:

> Since my input raster is binary (0s and 1s), and I need to know how many
> of the neighboring cells are 1s, it's sufficient to simply compute the sum
> of the overall neighborhood.

Since you have a binary map (as you wrote in your latest post), all easier (me
thinks).

r.neighbors (in G7) can do that:

# turn your co-ordinates into a raster map if they aren't already
# ...simply a binary would be sufficient I guess

# sum the 8 neighbors for each point of interest
r.neighbors input=QueriedRaster selection=CellsOfInterest
output=SumOf8Neighbors method=sum

However, I got confused a bit because a) I though it's a more complex map than
binary/boolean and b) you asked also for returning "the value of the cell"...

If your binary map is a "True/False" map, why not setting all those 0s to NULL
(using "r.null setnull=0")? Then, if your Raster-To-Be-Queried has only 1s,
then you know that it's always "1" the cell that is queried (if it is not
NULL).

I hope I got it right what you are after.
Best, Nikos

Thanks Nikos,

I'm going to try the G7 approach. I do need both cell values and neighbor values, but figured I would just run the computation twice.

Thanks!
Shaun

On Mar 14, 2013, at 3:04 PM, Nikos Alexandris <nik@nikosalexandris.net> wrote:

[meant to hit the list as well!]

Shaun Langley wrote:

I need to easily query a raster and return both the value of the cell
and the aggregate value of the 8-cell neighborhood, given a set of point
coordinates.

Nikos Alexandris wrote:

What kind of an aggregation exactly? Maybe not straight-through, but

Shaun Langley wrote:

Since my input raster is binary (0s and 1s), and I need to know how many
of the neighboring cells are 1s, it's sufficient to simply compute the sum
of the overall neighborhood.

Since you have a binary map (as you wrote in your latest post), all easier (me
thinks).

r.neighbors (in G7) can do that:

# turn your co-ordinates into a raster map if they aren't already
# ...simply a binary would be sufficient I guess

# sum the 8 neighbors for each point of interest
r.neighbors input=QueriedRaster selection=CellsOfInterest
output=SumOf8Neighbors method=sum

However, I got confused a bit because a) I though it's a more complex map than
binary/boolean and b) you asked also for returning "the value of the cell"...

If your binary map is a "True/False" map, why not setting all those 0s to NULL
(using "r.null setnull=0")? Then, if your Raster-To-Be-Queried has only 1s,
then you know that it's always "1" the cell that is queried (if it is not
NULL).

I hope I got it right what you are after.
Best, Nikos

On Thursday 14 of March 2013 15:48:02 Shaun Langley wrote:

Thanks Nikos,

I'm going to try the G7 approach. I do need both cell values and neighbor
values, but figured I would just run the computation twice.

If it finally works-out, please re-post here -- maybe we can put it in the
wiki?

Thanks, Nikos

I was not able to get a working implementation of GRASS 7 for Mac. It also seems to have broken my 6.4 installation; something about the python framework throws errors now.

I am installing a VM of Ubuntu so I can try it out in a different environment. I'll update if I get it working.

Shaun

On Mar 15, 2013, at 4:13 AM, Nikos Alexandris <nik@nikosalexandris.net> wrote:

On Thursday 14 of March 2013 15:48:02 Shaun Langley wrote:

Thanks Nikos,

I'm going to try the G7 approach. I do need both cell values and neighbor
values, but figured I would just run the computation twice.

If it finally works-out, please re-post here -- maybe we can put it in the
wiki?

Thanks, Nikos