[GRASS-user] circle surrounding neighbors

Hi,
don't you know about any possibility to use something similar to r.neighbors
but for the circle surrounding of the pixel?

I need to compute min, max, average in a (large) circle surrounding.
Thanks in advance
Stanislav

bks@centrum.cz wrote:

don't you know about any possibility to use something similar to r.neighbors
but for the circle surrounding of the pixel?

Modify raster/r.neighbors/gather.c.

An (untested) attempt is attached.

--
Glynn Clements <glynn@gclements.plus.com>

(attachments)

gather-circle.c (984 Bytes)

Sorry, I am a beginner... what shall I do with this gather-circle.c?
Thank you very much for your patience.
St.
______________________________________________________________

Od: glynn@gclements.plus.com
Komu: <bks@centrum.cz>
CC: <grassuser@grass.itc.it>
Datum: 30.07.2007 02:40
Pøedmìt: Re: [GRASS-user] circle surrounding neighbors

bks@centrum.cz wrote:

don't you know about any possibility to use something similar to

r.neighbors

but for the circle surrounding of the pixel?

Modify raster/r.neighbors/gather.c.

An (untested) attempt is attached.

--
Glynn Clements <glynn@gclements.plus.com>

#include <grass/gis.h>
#include "ncb.h"

/*
  given the starting col of the neighborhood,
  copy the cell values from the bufs into the array of values
  and return the number of values copied.
*/

static char **mask;

static void init_mask(void)
{
   int i, j;

   if (mask)
return;

   mask = G_malloc(ncb.nsize * sizeof(char *));

   for (i = 0; i < ncb.nsize; i++)
mask[i] = G_malloc(ncb.nsize);

   for (i = 0; i < ncb.nsize; i++)
for (j = 0; j < ncb.nsize; j++)
    mask[i][j] = sqr(i - ncb.dist) + sqr(j - ncb.dist) <= sqr(ncb.dist);
}

int gather(DCELL *values, int offset)
{
   int row, col;
   int n = 0;

   *values = 0;

   if (!mask)
init_mask();

   for (row = 0; row < ncb.nsize; row++)
for (col = 0; col < ncb.nsize; col++)
{
    DCELL *c = &ncb.buf[row][offset + col];

    if (!mask[row][col])
  continue;

    if (G_is_d_null_value(c))
  G_set_d_null_value(&values[n], 1);
    else
  values[n] = *c;

    n++;
}

   return n ? n : -1;
}

bks@centrum.cz wrote:

Sorry, I am a beginner... what shall I do with this gather-circle.c?

Replace raster/r.neighbors/gather.c with the new file, re-compile,
install the modified r.neighbors as e.g. r.neighbors.circle.

If you don't have the source code or can't figure out how to make the
necessary changes, you'll need to wait until someone integrates it
into GRASS.

Alternatively, you could use r.mapcalc, but if your circle is large,
this is going to require a large expression. You would probably want
to use a script to generate it, e.g.:

#!/usr/bin/python
# example usage: python circle.py | r.mapcalc

size = 9

dist = size / 2

n = 0
cmd = "out.avg = ("
for i in range(-dist,dist+1):
    for j in range(-dist,dist+1):
  if i**2 + j**2 <= dist**2:
      if n > 0:
        cmd = cmd + " + "
      cmd = cmd + "inmap[" + str(i) + "," + str(j) + "]"
      n=n+1
cmd = cmd + ") / " + str(n)
print cmd

n = 0
cmd = "out.min = min("
for i in range(-dist,dist+1):
    for j in range(-dist,dist+1):
  if i**2 + j**2 <= dist**2:
      if n > 0:
        cmd = cmd + ", "
      cmd = cmd + "inmap[" + str(i) + "," + str(j) + "]"
      n=n+1
cmd = cmd + ")"
print cmd

n = 0
cmd = "out.max = max("
for i in range(-dist,dist+1):
    for j in range(-dist,dist+1):
  if i**2 + j**2 <= dist**2:
      if n > 0:
        cmd = cmd + ", "
      cmd = cmd + "inmap[" + str(i) + "," + str(j) + "]"
      n=n+1
cmd = cmd + ")"
print cmd

--
Glynn Clements <glynn@gclements.plus.com>