[GRASS-user] circle surrounding neighbors
Glynn Clements
glynn at gclements.plus.com
Sun Jul 29 20:40:01 EDT 2007
bks at 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 at gclements.plus.com>
-------------- next part --------------
#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;
}
More information about the grass-user
mailing list