[GRASS-SVN] r42954 - in grass-addons/raster/r.pi: r.pi.corrwin
r.pi.index r.pi.rectangle
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Jul 30 04:30:00 EDT 2010
Author: wegmann
Date: 2010-07-30 08:30:00 +0000 (Fri, 30 Jul 2010)
New Revision: 42954
Modified:
grass-addons/raster/r.pi/r.pi.corrwin/bufs.c
grass-addons/raster/r.pi/r.pi.corrwin/divr_cats.c
grass-addons/raster/r.pi/r.pi.corrwin/gather.c
grass-addons/raster/r.pi/r.pi.corrwin/intr_cats.c
grass-addons/raster/r.pi/r.pi.corrwin/local_proto.h
grass-addons/raster/r.pi/r.pi.corrwin/main.c
grass-addons/raster/r.pi/r.pi.corrwin/ncb.h
grass-addons/raster/r.pi/r.pi.corrwin/null_cats.c
grass-addons/raster/r.pi/r.pi.corrwin/readcell.c
grass-addons/raster/r.pi/r.pi.index/frag.c
grass-addons/raster/r.pi/r.pi.index/func.c
grass-addons/raster/r.pi/r.pi.index/local_proto.h
grass-addons/raster/r.pi/r.pi.index/main.c
grass-addons/raster/r.pi/r.pi.rectangle/buffer.c
grass-addons/raster/r.pi/r.pi.rectangle/local_proto.h
grass-addons/raster/r.pi/r.pi.rectangle/main.c
Log:
applied grass_indent.sh
Modified: grass-addons/raster/r.pi/r.pi.corrwin/bufs.c
===================================================================
--- grass-addons/raster/r.pi/r.pi.corrwin/bufs.c 2010-07-29 19:46:31 UTC (rev 42953)
+++ grass-addons/raster/r.pi/r.pi.corrwin/bufs.c 2010-07-30 08:30:00 UTC (rev 42954)
@@ -9,51 +9,53 @@
*/
-int allocate_bufs (void)
+int allocate_bufs(void)
{
int i;
+
int bufsize;
- bufsize = (G_window_cols() + 2 * ncb.nsize) * sizeof (DCELL);
+ bufsize = (G_window_cols() + 2 * ncb.nsize) * sizeof(DCELL);
- ncb.buf1 = (DCELL **) G_malloc (ncb.nsize * sizeof(DCELL *));
- ncb.buf2 = (DCELL **) G_malloc (ncb.nsize * sizeof(DCELL *));
- for (i = 0; i < ncb.nsize; i++)
- {
- ncb.buf1[i] = (DCELL *) G_malloc (bufsize) ;
- ncb.buf2[i] = (DCELL *) G_malloc (bufsize) ;
- G_set_d_null_value(ncb.buf1[i], G_window_cols() + 2 * ncb.nsize);
- G_set_d_null_value(ncb.buf2[i], G_window_cols() + 2 * ncb.nsize);
- }
+ ncb.buf1 = (DCELL **) G_malloc(ncb.nsize * sizeof(DCELL *));
+ ncb.buf2 = (DCELL **) G_malloc(ncb.nsize * sizeof(DCELL *));
+ for (i = 0; i < ncb.nsize; i++) {
+ ncb.buf1[i] = (DCELL *) G_malloc(bufsize);
+ ncb.buf2[i] = (DCELL *) G_malloc(bufsize);
+ G_set_d_null_value(ncb.buf1[i], G_window_cols() + 2 * ncb.nsize);
+ G_set_d_null_value(ncb.buf2[i], G_window_cols() + 2 * ncb.nsize);
+ }
return 0;
}
-int rotate_bufs (int bufnumber)
+int rotate_bufs(int bufnumber)
{
DCELL *temp;
+
int i;
-
- if (bufnumber < 1 || bufnumber > 2)
- return -1;
-
- if (bufnumber == 1) {
- temp = ncb.buf1[0];
- for (i = 1; i < ncb.nsize; i++) {
- ncb.buf1[i-1] = ncb.buf1[i];
- }
-
- ncb.buf1[ncb.nsize-1] = temp;
- } else {
- temp = ncb.buf2[0];
-
- for (i = 1; i < ncb.nsize; i++) {
- ncb.buf2[i-1] = ncb.buf2[i];
- }
-
- ncb.buf2[ncb.nsize-1] = temp;
+ if (bufnumber < 1 || bufnumber > 2)
+ return -1;
+
+ if (bufnumber == 1) {
+ temp = ncb.buf1[0];
+
+ for (i = 1; i < ncb.nsize; i++) {
+ ncb.buf1[i - 1] = ncb.buf1[i];
}
+ ncb.buf1[ncb.nsize - 1] = temp;
+ }
+ else {
+ temp = ncb.buf2[0];
+
+ for (i = 1; i < ncb.nsize; i++) {
+ ncb.buf2[i - 1] = ncb.buf2[i];
+ }
+
+ ncb.buf2[ncb.nsize - 1] = temp;
+ }
+
return 0;
}
Modified: grass-addons/raster/r.pi/r.pi.corrwin/divr_cats.c
===================================================================
--- grass-addons/raster/r.pi/r.pi.corrwin/divr_cats.c 2010-07-29 19:46:31 UTC (rev 42953)
+++ grass-addons/raster/r.pi/r.pi.corrwin/divr_cats.c 2010-07-30 08:30:00 UTC (rev 42954)
@@ -1,9 +1,9 @@
#include <grass/gis.h>
#include "ncb.h"
-int
-divr_cats (void)
+int divr_cats(void)
{
- G_set_cats_fmt ("$1 $?different categories$category$", 1.0, 0.0, 0.0, 0.0, &ncb.cats);
+ G_set_cats_fmt("$1 $?different categories$category$", 1.0, 0.0, 0.0, 0.0,
+ &ncb.cats);
return 0;
}
Modified: grass-addons/raster/r.pi/r.pi.corrwin/gather.c
===================================================================
--- grass-addons/raster/r.pi/r.pi.corrwin/gather.c 2010-07-29 19:46:31 UTC (rev 42953)
+++ grass-addons/raster/r.pi/r.pi.corrwin/gather.c 2010-07-30 08:30:00 UTC (rev 42954)
@@ -5,35 +5,35 @@
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.
-*/
+ */
-int gather(DCELL *values, int bufnumber, int offset)
+int gather(DCELL * values, int bufnumber, int offset)
{
- if (bufnumber < 1 || bufnumber > 2)
- return -1;
-
+ if (bufnumber < 1 || bufnumber > 2)
+ return -1;
+
int row, col;
+
int n = 0;
*values = 0;
for (row = 0; row < ncb.nsize; row++)
- for (col = 0; col < ncb.nsize; col++)
- {
- DCELL *c;
- if (bufnumber == 1)
- c = &ncb.buf1[row][offset + col];
- else
- c = &ncb.buf2[row][offset + col];
+ for (col = 0; col < ncb.nsize; col++) {
+ DCELL *c;
- if (G_is_d_null_value(c))
- G_set_d_null_value(&values[n], 1);
- else
- values[n] = *c;
+ if (bufnumber == 1)
+ c = &ncb.buf1[row][offset + col];
+ else
+ c = &ncb.buf2[row][offset + col];
- n++;
- }
+ if (G_is_d_null_value(c))
+ G_set_d_null_value(&values[n], 1);
+ else
+ values[n] = *c;
+ n++;
+ }
+
return n ? n : -1;
}
-
Modified: grass-addons/raster/r.pi/r.pi.corrwin/intr_cats.c
===================================================================
--- grass-addons/raster/r.pi/r.pi.corrwin/intr_cats.c 2010-07-29 19:46:31 UTC (rev 42953)
+++ grass-addons/raster/r.pi/r.pi.corrwin/intr_cats.c 2010-07-30 08:30:00 UTC (rev 42954)
@@ -1,9 +1,9 @@
#include <grass/gis.h>
#include "ncb.h"
-int intr_cats (void)
+int intr_cats(void)
{
- G_set_cats_fmt ("$1% dispersion", 1.0, -1.0, 0.0, 0.0, &ncb.cats);
+ G_set_cats_fmt("$1% dispersion", 1.0, -1.0, 0.0, 0.0, &ncb.cats);
return 0;
}
Modified: grass-addons/raster/r.pi/r.pi.corrwin/local_proto.h
===================================================================
--- grass-addons/raster/r.pi/r.pi.corrwin/local_proto.h 2010-07-29 19:46:31 UTC (rev 42953)
+++ grass-addons/raster/r.pi/r.pi.corrwin/local_proto.h 2010-07-30 08:30:00 UTC (rev 42954)
@@ -1,13 +1,19 @@
/* bufs.c */
extern int allocate_bufs(void);
+
extern int rotate_bufs(int);
+
/* gather */
-extern int gather(DCELL *,int, int);
+extern int gather(DCELL *, int, int);
+
/* readcell.c */
extern int readcell(int, int, int, int, int);
+
/* divr_cats.c */
extern int divr_cats(void);
+
/* intr_cats.c */
extern int intr_cats(void);
+
/* null_cats.c */
extern int null_cats(void);
Modified: grass-addons/raster/r.pi/r.pi.corrwin/main.c
===================================================================
--- grass-addons/raster/r.pi/r.pi.corrwin/main.c 2010-07-29 19:46:31 UTC (rev 42953)
+++ grass-addons/raster/r.pi/r.pi.corrwin/main.c 2010-07-30 08:30:00 UTC (rev 42954)
@@ -9,266 +9,286 @@
#include <math.h>
/*
- Moving window correlation analyse.
-
- Put together from pieces of r.covar and r.neighbors by Elshad Shirinov.
-*/
+ Moving window correlation analyse.
-typedef int (*ifunc)(void);
+ Put together from pieces of r.covar and r.neighbors by Elshad Shirinov.
+ */
+typedef int (*ifunc) (void);
+
struct ncb ncb;
-int main (int argc, char *argv[])
+int main(int argc, char *argv[])
{
- char *p, *p1, *p2;
- int verbose;
- // in and out file pointers
- int in_fd1, in_fd2;
- int out_fd;
- DCELL *result;
-
- RASTER_MAP_TYPE map_type1, map_type2;
-
- int row, col, i, j;
- int readrow;
- int maxval;
-
- int nrows, ncols;
- int n;
- int copycolr;
- struct Colors colr;
- struct GModule *module;
- struct
- {
- struct Option *input1, *input2, *output;
- struct Option *size, *max;
- struct Option *title;
- } parm;
- struct
- {
- struct Flag *quiet;
- } flag;
+ char *p, *p1, *p2;
- DCELL *values1, *values2; /* list of neighborhood values */
+ int verbose;
- G_gisinit (argv[0]);
+ // in and out file pointers
+ int in_fd1, in_fd2;
- module = G_define_module();
- module->keywords = _("raster");
+ int out_fd;
+
+ DCELL *result;
+
+ RASTER_MAP_TYPE map_type1, map_type2;
+
+ int row, col, i, j;
+
+ int readrow;
+
+ int maxval;
+
+ int nrows, ncols;
+
+ int n;
+
+ int copycolr;
+
+ struct Colors colr;
+
+ struct GModule *module;
+
+ struct
+ {
+ struct Option *input1, *input2, *output;
+ struct Option *size, *max;
+ struct Option *title;
+ } parm;
+
+ struct
+ {
+ struct Flag *quiet;
+ } flag;
+
+ DCELL *values1, *values2; /* list of neighborhood values */
+
+ G_gisinit(argv[0]);
+
+ module = G_define_module();
+ module->keywords = _("raster");
module->description =
- _("Calculates correlation of two raster maps "
- "by calculating correlation function of two "
- "corresponding rectangular areas for each "
- "raster point and writing the result into a new raster map.");
+ _("Calculates correlation of two raster maps "
+ "by calculating correlation function of two "
+ "corresponding rectangular areas for each "
+ "raster point and writing the result into a new raster map.");
- parm.input1 = G_define_option() ;
- parm.input1->key = "input1" ;
- parm.input1->type = TYPE_STRING ;
- parm.input1->required = YES ;
- parm.input1->gisprompt = "old,cell,raster,1" ;
- parm.input1->description= _("Name of existing raster file") ;
+ parm.input1 = G_define_option();
+ parm.input1->key = "input1";
+ parm.input1->type = TYPE_STRING;
+ parm.input1->required = YES;
+ parm.input1->gisprompt = "old,cell,raster,1";
+ parm.input1->description = _("Name of existing raster file");
- parm.input2 = G_define_option() ;
- parm.input2->key = "input2" ;
- parm.input2->type = TYPE_STRING ;
- parm.input2->required = YES ;
- parm.input2->gisprompt = "old,cell,raster,2" ;
- parm.input2->description= _("Name of existing raster file") ;
+ parm.input2 = G_define_option();
+ parm.input2->key = "input2";
+ parm.input2->type = TYPE_STRING;
+ parm.input2->required = YES;
+ parm.input2->gisprompt = "old,cell,raster,2";
+ parm.input2->description = _("Name of existing raster file");
- parm.output = G_define_option() ;
- parm.output->key = "output" ;
- parm.output->type = TYPE_STRING ;
- parm.output->required = YES ;
- parm.output->gisprompt = "new,cell,raster" ;
- parm.output->description= _("Name of the new raster file") ;
+ parm.output = G_define_option();
+ parm.output->key = "output";
+ parm.output->type = TYPE_STRING;
+ parm.output->required = YES;
+ parm.output->gisprompt = "new,cell,raster";
+ parm.output->description = _("Name of the new raster file");
- parm.size = G_define_option() ;
- parm.size->key = "size" ;
- parm.size->type = TYPE_INTEGER ;
- parm.size->required = YES ;
- parm.size->options = "1,3,5,7,9,11,13,15,17,19,21,23,25" ;
- parm.size->description= _("Neighborhood size") ;
+ parm.size = G_define_option();
+ parm.size->key = "size";
+ parm.size->type = TYPE_INTEGER;
+ parm.size->required = YES;
+ parm.size->options = "1,3,5,7,9,11,13,15,17,19,21,23,25";
+ parm.size->description = _("Neighborhood size");
- parm.max = G_define_option() ;
- parm.max->key = "max" ;
- parm.max->type = TYPE_INTEGER ;
- parm.max->required = YES ;
- parm.max->description= _("Value to be set for the correlation value 1.0 in order to receive more information of the decimal places, e.g. set it to 1000") ;
+ parm.max = G_define_option();
+ parm.max->key = "max";
+ parm.max->type = TYPE_INTEGER;
+ parm.max->required = YES;
+ parm.max->description =
+ _("Value to be set for the correlation value 1.0 in order to receive more information of the decimal places, e.g. set it to 1000");
- parm.title = G_define_option() ;
- parm.title->key = "title" ;
- parm.title->key_desc = "\"phrase\"" ;
- parm.title->type = TYPE_STRING ;
- parm.title->required = NO ;
- parm.title->description= _("Title of the output raster file") ;
+ parm.title = G_define_option();
+ parm.title->key = "title";
+ parm.title->key_desc = "\"phrase\"";
+ parm.title->type = TYPE_STRING;
+ parm.title->required = NO;
+ parm.title->description = _("Title of the output raster file");
- flag.quiet = G_define_flag();
- flag.quiet->key = 'q';
- flag.quiet->description = _("Run quietly");
+ flag.quiet = G_define_flag();
+ flag.quiet->key = 'q';
+ flag.quiet->description = _("Run quietly");
- if (G_parser(argc,argv))
- exit(1);
+ if (G_parser(argc, argv))
+ exit(1);
- // get names of input files
- p1 = ncb.oldcell1.name = parm.input1->answer;
- p2 = ncb.oldcell2.name = parm.input2->answer;
- // test input files existance
- if(NULL == (ncb.oldcell1.mapset = G_find_cell2(p1,"")))
- {
- fprintf (stderr, "%s: <%s> raster file not found\n",
- G_program_name(), p1);
- exit(1);
- }
- if(NULL == (ncb.oldcell2.mapset = G_find_cell2(p2,"")))
- {
- fprintf (stderr, "%s: <%s> raster file not found\n",
- G_program_name(), p2);
- exit(1);
- }
- // check if new file name is correct
- p = ncb.newcell.name = parm.output->answer;
- if (G_legal_filename(p) < 0)
- {
- fprintf (stderr, "%s: <%s> illegal file name\n",
- G_program_name(), p);
- exit(1);
- }
- ncb.newcell.mapset = G_mapset();
+ // get names of input files
+ p1 = ncb.oldcell1.name = parm.input1->answer;
+ p2 = ncb.oldcell2.name = parm.input2->answer;
+ // test input files existance
+ if (NULL == (ncb.oldcell1.mapset = G_find_cell2(p1, ""))) {
+ fprintf(stderr, "%s: <%s> raster file not found\n",
+ G_program_name(), p1);
+ exit(1);
+ }
+ if (NULL == (ncb.oldcell2.mapset = G_find_cell2(p2, ""))) {
+ fprintf(stderr, "%s: <%s> raster file not found\n",
+ G_program_name(), p2);
+ exit(1);
+ }
+ // check if new file name is correct
+ p = ncb.newcell.name = parm.output->answer;
+ if (G_legal_filename(p) < 0) {
+ fprintf(stderr, "%s: <%s> illegal file name\n", G_program_name(), p);
+ exit(1);
+ }
+ ncb.newcell.mapset = G_mapset();
- // get window size
- nrows = G_window_rows();
- ncols = G_window_cols();
-
- fprintf(stderr, "%d x %d ", nrows, ncols);
+ // get window size
+ nrows = G_window_rows();
+ ncols = G_window_cols();
- /* open cell files */
- if ((in_fd1 = G_open_cell_old (ncb.oldcell1.name, ncb.oldcell1.mapset)) < 0)
- {
- char msg[200];
- sprintf(msg,"can't open cell file <%s> in mapset %s\n",
- ncb.oldcell1.name, ncb.oldcell1.mapset);
- G_fatal_error (msg);
- exit(-1);
- }
- if ((in_fd2 = G_open_cell_old (ncb.oldcell2.name, ncb.oldcell2.mapset)) < 0)
- {
- char msg[200];
- sprintf(msg,"can't open cell file <%s> in mapset %s\n",
- ncb.oldcell2.name, ncb.oldcell2.mapset);
- G_fatal_error (msg);
- exit(-1);
- }
-
- // get map types
- map_type1 = G_raster_map_type(ncb.oldcell1.name, ncb.oldcell1.mapset);
- map_type2 = G_raster_map_type(ncb.oldcell2.name, ncb.oldcell2.mapset);
+ fprintf(stderr, "%d x %d ", nrows, ncols);
- /* copy color table? */
- copycolr = (G_read_colors (ncb.oldcell1.name, ncb.oldcell1.mapset, &colr) > 0);
-
- /* get the neighborhood size */
- sscanf (parm.size->answer, "%d", &ncb.nsize);
- ncb.dist = ncb.nsize/2;
-
- /* get correlation maximum */
- sscanf(parm.max->answer, "%d", &maxval);
-
- /* allocate the cell buffers */
- allocate_bufs ();
- values1 = (DCELL *) G_malloc (ncb.nsize * ncb.nsize * sizeof (DCELL));
- values2 = (DCELL *) G_malloc (ncb.nsize * ncb.nsize * sizeof (DCELL));
- result = G_allocate_d_raster_buf();
+ /* open cell files */
+ if ((in_fd1 =
+ G_open_cell_old(ncb.oldcell1.name, ncb.oldcell1.mapset)) < 0) {
+ char msg[200];
- /* get title, initialize the category and stat info */
- if (parm.title->answer)
- strcpy (ncb.title, parm.title->answer);
- else
- sprintf (ncb.title,"%dx%d neighborhood correlation: %s and %s",
- ncb.nsize, ncb.nsize, ncb.oldcell1.name, ncb.oldcell2.name);
+ sprintf(msg, "can't open cell file <%s> in mapset %s\n",
+ ncb.oldcell1.name, ncb.oldcell1.mapset);
+ G_fatal_error(msg);
+ exit(-1);
+ }
+ if ((in_fd2 =
+ G_open_cell_old(ncb.oldcell2.name, ncb.oldcell2.mapset)) < 0) {
+ char msg[200];
+ sprintf(msg, "can't open cell file <%s> in mapset %s\n",
+ ncb.oldcell2.name, ncb.oldcell2.mapset);
+ G_fatal_error(msg);
+ exit(-1);
+ }
- /* initialize the cell bufs with 'dist' rows of the old cellfile */
+ // get map types
+ map_type1 = G_raster_map_type(ncb.oldcell1.name, ncb.oldcell1.mapset);
+ map_type2 = G_raster_map_type(ncb.oldcell2.name, ncb.oldcell2.mapset);
- readrow = 0;
- for (row = 0; row < ncb.dist; row++) {
- readcell (in_fd1, 1, readrow, nrows, ncols);
- readcell (in_fd2, 2, readrow, nrows, ncols);
- readrow++;
- }
+ /* copy color table? */
+ copycolr =
+ (G_read_colors(ncb.oldcell1.name, ncb.oldcell1.mapset, &colr) > 0);
- /*open the new cellfile */
- out_fd = G_open_raster_new (ncb.newcell.name, map_type1);
- if (out_fd < 0)
- exit(1);
+ /* get the neighborhood size */
+ sscanf(parm.size->answer, "%d", &ncb.nsize);
+ ncb.dist = ncb.nsize / 2;
- if (verbose = !flag.quiet->answer)
- fprintf (stderr, "Percent complete ... ");
-
- for (row = 0; row < nrows; row++)
- {
- if (verbose)
- G_percent (row, nrows, 2);
- // read the next row into buffer
- readcell (in_fd1, 1, readrow, nrows, ncols);
- readcell (in_fd2, 2, readrow, nrows, ncols);
- readrow++;
- for (col = 0; col < ncols; col++)
- {
- DCELL sum1 = 0;
- DCELL sum2 = 0;
- DCELL mul, mul1, mul2;
- mul = mul1 = mul2 = 0;
- double count = 0;
- DCELL ii, jj;
+ /* get correlation maximum */
+ sscanf(parm.max->answer, "%d", &maxval);
- // set pointer to actual position in the result
- DCELL *rp = &result[col];
- // gather values from actual window
- gather(values1, 1, col);
- gather(values2, 2, col);
- // go through all values of the window
- for (i = 0; i < ncb.nsize * ncb.nsize; i++) {
- // ignore values if both are nan
- if (!(G_is_d_null_value(&values1[i]) && G_is_d_null_value(&values2[i]))) {
- if (!G_is_d_null_value(&values1[i])) {
- sum1 += values1[i];
- mul1 += values1[i] * values1[i];
- }
- if (!G_is_d_null_value(&values2[i])) {
- sum2 += values2[i];
- mul2 += values2[i] * values2[i];
- }
- if (!G_is_d_null_value(&values1[i]) && !G_is_d_null_value(&values2[i]))
- mul += values1[i] * values2[i];
- // count the number of values actually processed
- count++;
- }
- }
- if (count <= 1.1) {
- *rp = 0;
- continue;
- }
- // calculate normalization
- ii = sqrt((mul1 - sum1 * sum1 / count) / (count - 1.0));
- jj = sqrt((mul2 - sum2 * sum2 / count) / (count - 1.0));
-
- // set result
- *rp = maxval * (mul - sum1 * sum2 / count) / (ii * jj * (count - 1.0));
- if (G_is_d_null_value(rp))
- G_set_d_null_value(rp, 1);
+ /* allocate the cell buffers */
+ allocate_bufs();
+ values1 = (DCELL *) G_malloc(ncb.nsize * ncb.nsize * sizeof(DCELL));
+ values2 = (DCELL *) G_malloc(ncb.nsize * ncb.nsize * sizeof(DCELL));
+ result = G_allocate_d_raster_buf();
+
+ /* get title, initialize the category and stat info */
+ if (parm.title->answer)
+ strcpy(ncb.title, parm.title->answer);
+ else
+ sprintf(ncb.title, "%dx%d neighborhood correlation: %s and %s",
+ ncb.nsize, ncb.nsize, ncb.oldcell1.name, ncb.oldcell2.name);
+
+
+ /* initialize the cell bufs with 'dist' rows of the old cellfile */
+
+ readrow = 0;
+ for (row = 0; row < ncb.dist; row++) {
+ readcell(in_fd1, 1, readrow, nrows, ncols);
+ readcell(in_fd2, 2, readrow, nrows, ncols);
+ readrow++;
+ }
+
+ /*open the new cellfile */
+ out_fd = G_open_raster_new(ncb.newcell.name, map_type1);
+ if (out_fd < 0)
+ exit(1);
+
+ if (verbose = !flag.quiet->answer)
+ fprintf(stderr, "Percent complete ... ");
+
+ for (row = 0; row < nrows; row++) {
+ if (verbose)
+ G_percent(row, nrows, 2);
+ // read the next row into buffer
+ readcell(in_fd1, 1, readrow, nrows, ncols);
+ readcell(in_fd2, 2, readrow, nrows, ncols);
+ readrow++;
+ for (col = 0; col < ncols; col++) {
+ DCELL sum1 = 0;
+
+ DCELL sum2 = 0;
+
+ DCELL mul, mul1, mul2;
+
+ mul = mul1 = mul2 = 0;
+ double count = 0;
+
+ DCELL ii, jj;
+
+ // set pointer to actual position in the result
+ DCELL *rp = &result[col];
+
+ // gather values from actual window
+ gather(values1, 1, col);
+ gather(values2, 2, col);
+ // go through all values of the window
+ for (i = 0; i < ncb.nsize * ncb.nsize; i++) {
+ // ignore values if both are nan
+ if (!
+ (G_is_d_null_value(&values1[i]) &&
+ G_is_d_null_value(&values2[i]))) {
+ if (!G_is_d_null_value(&values1[i])) {
+ sum1 += values1[i];
+ mul1 += values1[i] * values1[i];
+ }
+ if (!G_is_d_null_value(&values2[i])) {
+ sum2 += values2[i];
+ mul2 += values2[i] * values2[i];
+ }
+ if (!G_is_d_null_value(&values1[i]) &&
+ !G_is_d_null_value(&values2[i]))
+ mul += values1[i] * values2[i];
+ // count the number of values actually processed
+ count++;
}
- // write actual result row to the output file
- G_put_d_raster_row(out_fd, result);
+ }
+ if (count <= 1.1) {
+ *rp = 0;
+ continue;
+ }
+ // calculate normalization
+ ii = sqrt((mul1 - sum1 * sum1 / count) / (count - 1.0));
+ jj = sqrt((mul2 - sum2 * sum2 / count) / (count - 1.0));
+
+ // set result
+ *rp =
+ maxval * (mul -
+ sum1 * sum2 / count) / (ii * jj * (count - 1.0));
+ if (G_is_d_null_value(rp))
+ G_set_d_null_value(rp, 1);
}
- if (verbose)
- G_percent (row, nrows, 2);
+ // write actual result row to the output file
+ G_put_d_raster_row(out_fd, result);
+ }
+ if (verbose)
+ G_percent(row, nrows, 2);
- G_close_cell (out_fd);
- G_close_cell (in_fd1);
- G_close_cell (in_fd2);
+ G_close_cell(out_fd);
+ G_close_cell(in_fd1);
+ G_close_cell(in_fd2);
- if(copycolr)
- G_write_colors (ncb.newcell.name, ncb.newcell.mapset, &colr);
+ if (copycolr)
+ G_write_colors(ncb.newcell.name, ncb.newcell.mapset, &colr);
- exit(0);
+ exit(0);
}
Modified: grass-addons/raster/r.pi/r.pi.corrwin/ncb.h
===================================================================
--- grass-addons/raster/r.pi/r.pi.corrwin/ncb.h 2010-07-29 19:46:31 UTC (rev 42953)
+++ grass-addons/raster/r.pi/r.pi.corrwin/ncb.h 2010-07-30 08:30:00 UTC (rev 42954)
@@ -1,10 +1,10 @@
-struct ncb /* neighborhood control block */
+struct ncb /* neighborhood control block */
{
- DCELL **buf1, **buf2; /* for reading cell file */
- int *value; /* neighborhood values */
- int nsize; /* size of the neighborhood */
- int dist; /* nsize/2 */
+ DCELL **buf1, **buf2; /* for reading cell file */
+ int *value; /* neighborhood values */
+ int nsize; /* size of the neighborhood */
+ int dist; /* nsize/2 */
struct Categories cats;
char title[1024];
FILE *out;
@@ -17,4 +17,3 @@
};
extern struct ncb ncb;
-
Modified: grass-addons/raster/r.pi/r.pi.corrwin/null_cats.c
===================================================================
--- grass-addons/raster/r.pi/r.pi.corrwin/null_cats.c 2010-07-29 19:46:31 UTC (rev 42953)
+++ grass-addons/raster/r.pi/r.pi.corrwin/null_cats.c 2010-07-30 08:30:00 UTC (rev 42954)
@@ -1,12 +1,12 @@
#include <grass/gis.h>
#include "ncb.h"
-int null_cats (void)
+int null_cats(void)
{
int ncats;
- ncats = G_number_of_cats (ncb.newcell.name, ncb.newcell.mapset);
- G_init_cats (ncats, ncb.title, &ncb.cats);
+ ncats = G_number_of_cats(ncb.newcell.name, ncb.newcell.mapset);
+ G_init_cats(ncats, ncb.title, &ncb.cats);
return 0;
}
Modified: grass-addons/raster/r.pi/r.pi.corrwin/readcell.c
===================================================================
--- grass-addons/raster/r.pi/r.pi.corrwin/readcell.c 2010-07-29 19:46:31 UTC (rev 42953)
+++ grass-addons/raster/r.pi/r.pi.corrwin/readcell.c 2010-07-30 08:30:00 UTC (rev 42954)
@@ -3,26 +3,25 @@
#include "ncb.h"
#include "local_proto.h"
-int readcell (int fd, int bufnumber, int row, int nrows, int ncols)
-{
- if (bufnumber < 1 || bufnumber > 2)
- return -1;
-
+int readcell(int fd, int bufnumber, int row, int nrows, int ncols)
+{
+ if (bufnumber < 1 || bufnumber > 2)
+ return -1;
+
+ if (bufnumber == 1)
+ rotate_bufs(1);
+ else
+ rotate_bufs(2);
+
+ if (row < nrows)
if (bufnumber == 1)
- rotate_bufs(1);
+ G_get_d_raster_row(fd, ncb.buf1[ncb.nsize - 1] + ncb.dist, row);
else
- rotate_bufs(2);
+ G_get_d_raster_row(fd, ncb.buf2[ncb.nsize - 1] + ncb.dist, row);
+ else if (bufnumber == 1)
+ G_set_d_null_value(ncb.buf1[ncb.nsize - 1] + ncb.dist, ncols);
+ else
+ G_set_d_null_value(ncb.buf2[ncb.nsize - 1] + ncb.dist, ncols);
- if (row < nrows)
- if (bufnumber == 1)
- G_get_d_raster_row (fd, ncb.buf1[ncb.nsize-1] + ncb.dist, row);
- else
- G_get_d_raster_row (fd, ncb.buf2[ncb.nsize-1] + ncb.dist, row);
- else
- if (bufnumber == 1)
- G_set_d_null_value(ncb.buf1[ncb.nsize-1] + ncb.dist, ncols);
- else
- G_set_d_null_value(ncb.buf2[ncb.nsize-1] + ncb.dist, ncols);
-
return 0;
}
Modified: grass-addons/raster/r.pi/r.pi.index/frag.c
===================================================================
--- grass-addons/raster/r.pi/r.pi.index/frag.c 2010-07-29 19:46:31 UTC (rev 42953)
+++ grass-addons/raster/r.pi/r.pi.index/frag.c 2010-07-30 08:30:00 UTC (rev 42954)
@@ -7,102 +7,148 @@
#include <math.h>
#include "local_proto.h"
-typedef struct {
- int x, y;
- } Position;
+typedef struct
+{
+ int x, y;
+} Position;
-int getNeighbors(Position* res, int x, int y, int nx, int ny, int nbr_cnt);
-
-int getNeighbors(Position* res, int x, int y, int nx, int ny, int nbr_cnt) {
- int left, right, top, bottom;
- int i, j;
- int cnt = 0;
-
- switch(nbr_cnt) {
- case 4: /* von Neumann neighborhood */
- if (x > 0 && flagbuf[y * nx + x - 1] == 1) {
- res[cnt].x = x - 1; res[cnt].y = y; cnt++;
- }
- if (y > 0 && flagbuf[(y - 1) * nx + x] == 1) {
- res[cnt].x = x; res[cnt].y = y - 1; cnt++;
- }
- if (x < nx - 1 && flagbuf[y * nx + x + 1] == 1) {
- res[cnt].x = x + 1; res[cnt].y = y; cnt++;
- }
- if (y < ny - 1 && flagbuf[(y + 1) * nx + x] == 1) {
- res[cnt].x = x; res[cnt].y = y + 1; cnt++;
- }
- break;
- case 8: /* Moore neighborhood */
- left = x > 0 ? x - 1 : 0;
- top = y > 0 ? y - 1 : 0;
- right = x < nx - 1 ? x + 1 : nx - 1;
- bottom = y < ny - 1 ? y + 1 : ny - 1;
- for (i = left; i <= right; i++) {
- for (j = top; j <= bottom; j++) {
- if(!(i == x && j == y) && flagbuf[j * nx + i] == 1) {
- res[cnt].x = i; res[cnt].y = j; cnt++;
- }
- }
- }
- break;
- }
-
- return cnt;
-}
-
-void writeFrag(int row, int col, int nbr_cnt) {
- int x, y, i;
- Position* list = (Position *) G_malloc(nrows * ncols * sizeof (Position));
- Position* first = list;
- Position* last = list;
- Position* nbr_list = (Position *) G_malloc(8 * sizeof (Position));
-
- /* count neighbors */
- int neighbors = 0;
- if (col > 0 && flagbuf[row * ncols + col - 1] != 0) neighbors++;
- if (row > 0 && flagbuf[(row - 1) * ncols + col] != 0) neighbors++;
- if (col < ncols - 1 && flagbuf[row * ncols + col + 1] != 0) neighbors++;
- if (row < nrows - 1 && flagbuf[(row + 1) * ncols + col] != 0) neighbors++;
-
- /* write first cell */
- actpos->x = col; actpos->y = row; actpos->neighbors = neighbors; actpos++;
- flagbuf[row * ncols + col] = -1;
+int getNeighbors(Position * res, int x, int y, int nx, int ny, int nbr_cnt);
- /* push position on fifo-list */
- last->x = col; last->y = row; last++;
+int getNeighbors(Position * res, int x, int y, int nx, int ny, int nbr_cnt)
+{
+ int left, right, top, bottom;
- while (first < last) {
- /* get position from fifo-list */
- int r = first->y; int c = first->x; first++;
-
- int left = c > 0 ? c - 1 : 0;
- int top = r > 0 ? r - 1 : 0;
- int right = c < ncols - 1 ? c + 1 : ncols - 1;
- int bottom = r < nrows - 1 ? r + 1 : nrows - 1;
-
- /* add neighbors to fifo-list */
- int cnt = getNeighbors(nbr_list, c, r, ncols, nrows, nbr_cnt);
-
- for(i = 0; i < cnt; i++) {
- x = nbr_list[i].x; y = nbr_list[i].y;
-
- /* add position to fifo-list */
- last->x = x; last->y = y; last++;
-
- /* count neighbors */
- neighbors = 0;
- if (x > 0 && flagbuf[y * ncols + x - 1] != 0) neighbors++;
- if (y > 0 && flagbuf[(y - 1) * ncols + x] != 0) neighbors++;
- if (x < ncols - 1 && flagbuf[y * ncols + x + 1] != 0) neighbors++;
- if (y < nrows - 1 && flagbuf[(y + 1) * ncols + x] != 0) neighbors++;
-
- /* set values */
- actpos->x = x; actpos->y = y; actpos->neighbors = neighbors; actpos++;
- flagbuf[y * ncols + x] = -1;
+ int i, j;
+
+ int cnt = 0;
+
+ switch (nbr_cnt) {
+ case 4: /* von Neumann neighborhood */
+ if (x > 0 && flagbuf[y * nx + x - 1] == 1) {
+ res[cnt].x = x - 1;
+ res[cnt].y = y;
+ cnt++;
+ }
+ if (y > 0 && flagbuf[(y - 1) * nx + x] == 1) {
+ res[cnt].x = x;
+ res[cnt].y = y - 1;
+ cnt++;
+ }
+ if (x < nx - 1 && flagbuf[y * nx + x + 1] == 1) {
+ res[cnt].x = x + 1;
+ res[cnt].y = y;
+ cnt++;
+ }
+ if (y < ny - 1 && flagbuf[(y + 1) * nx + x] == 1) {
+ res[cnt].x = x;
+ res[cnt].y = y + 1;
+ cnt++;
+ }
+ break;
+ case 8: /* Moore neighborhood */
+ left = x > 0 ? x - 1 : 0;
+ top = y > 0 ? y - 1 : 0;
+ right = x < nx - 1 ? x + 1 : nx - 1;
+ bottom = y < ny - 1 ? y + 1 : ny - 1;
+ for (i = left; i <= right; i++) {
+ for (j = top; j <= bottom; j++) {
+ if (!(i == x && j == y) && flagbuf[j * nx + i] == 1) {
+ res[cnt].x = i;
+ res[cnt].y = j;
+ cnt++;
}
+ }
}
-
- G_free(list);
- G_free(nbr_list);
+ break;
+ }
+
+ return cnt;
}
+
+void writeFrag(int row, int col, int nbr_cnt)
+{
+ int x, y, i;
+
+ Position *list = (Position *) G_malloc(nrows * ncols * sizeof(Position));
+
+ Position *first = list;
+
+ Position *last = list;
+
+ Position *nbr_list = (Position *) G_malloc(8 * sizeof(Position));
+
+ /* count neighbors */
+ int neighbors = 0;
+
+ if (col > 0 && flagbuf[row * ncols + col - 1] != 0)
+ neighbors++;
+ if (row > 0 && flagbuf[(row - 1) * ncols + col] != 0)
+ neighbors++;
+ if (col < ncols - 1 && flagbuf[row * ncols + col + 1] != 0)
+ neighbors++;
+ if (row < nrows - 1 && flagbuf[(row + 1) * ncols + col] != 0)
+ neighbors++;
+
+ /* write first cell */
+ actpos->x = col;
+ actpos->y = row;
+ actpos->neighbors = neighbors;
+ actpos++;
+ flagbuf[row * ncols + col] = -1;
+
+ /* push position on fifo-list */
+ last->x = col;
+ last->y = row;
+ last++;
+
+ while (first < last) {
+ /* get position from fifo-list */
+ int r = first->y;
+
+ int c = first->x;
+
+ first++;
+
+ int left = c > 0 ? c - 1 : 0;
+
+ int top = r > 0 ? r - 1 : 0;
+
+ int right = c < ncols - 1 ? c + 1 : ncols - 1;
+
+ int bottom = r < nrows - 1 ? r + 1 : nrows - 1;
+
+ /* add neighbors to fifo-list */
+ int cnt = getNeighbors(nbr_list, c, r, ncols, nrows, nbr_cnt);
+
+ for (i = 0; i < cnt; i++) {
+ x = nbr_list[i].x;
+ y = nbr_list[i].y;
+
+ /* add position to fifo-list */
+ last->x = x;
+ last->y = y;
+ last++;
+
+ /* count neighbors */
+ neighbors = 0;
+ if (x > 0 && flagbuf[y * ncols + x - 1] != 0)
+ neighbors++;
+ if (y > 0 && flagbuf[(y - 1) * ncols + x] != 0)
+ neighbors++;
+ if (x < ncols - 1 && flagbuf[y * ncols + x + 1] != 0)
+ neighbors++;
+ if (y < nrows - 1 && flagbuf[(y + 1) * ncols + x] != 0)
+ neighbors++;
+
+ /* set values */
+ actpos->x = x;
+ actpos->y = y;
+ actpos->neighbors = neighbors;
+ actpos++;
+ flagbuf[y * ncols + x] = -1;
+ }
+ }
+
+ G_free(list);
+ G_free(nbr_list);
+}
Modified: grass-addons/raster/r.pi/r.pi.index/func.c
===================================================================
--- grass-addons/raster/r.pi/r.pi.index/func.c 2010-07-29 19:46:31 UTC (rev 42953)
+++ grass-addons/raster/r.pi/r.pi.index/func.c 2010-07-30 08:30:00 UTC (rev 42954)
@@ -7,258 +7,331 @@
#include <math.h>
#include "local_proto.h"
-int f_area(DCELL *vals, Coords **frags, int count) {
- int i;
- /* for all patches */
- for(i = 0; i < count; i++) {
- // display progress
- if (verbose)
- G_percent (i, count, 2);
-
- vals[i] = (DCELL) (frags[i+1] - frags[i]);
- }
+int f_area(DCELL * vals, Coords ** frags, int count)
+{
+ int i;
- return 0;
+ /* for all patches */
+ for (i = 0; i < count; i++) {
+ // display progress
+ if (verbose)
+ G_percent(i, count, 2);
+
+ vals[i] = (DCELL) (frags[i + 1] - frags[i]);
+ }
+
+ return 0;
}
-int f_perim(DCELL *vals, Coords **frags, int count) {
- Coords *p;
- int i;
- /* for all patches */
- for(i = 0; i < count; i++) {
- // display progress
- if (verbose)
- G_percent (i, count, 2);
-
- int border = 0;
- /* for all cells in a patch */
- for(p = frags[i]; p < frags[i+1]; p++) {
- border+= 4 - p->neighbors;
- }
- vals[i] = (DCELL) border;
+int f_perim(DCELL * vals, Coords ** frags, int count)
+{
+ Coords *p;
+
+ int i;
+
+ /* for all patches */
+ for (i = 0; i < count; i++) {
+ // display progress
+ if (verbose)
+ G_percent(i, count, 2);
+
+ int border = 0;
+
+ /* for all cells in a patch */
+ for (p = frags[i]; p < frags[i + 1]; p++) {
+ border += 4 - p->neighbors;
}
-
- return 0;
+ vals[i] = (DCELL) border;
+ }
+
+ return 0;
}
-int f_shapeindex(DCELL *vals, Coords **frags, int count) {
- Coords *p;
- int i;
- /* for all patches */
- for(i = 0; i < count; i++) {
- // display progress
- if (verbose)
- G_percent (i, count, 2);
-
- int border = 0;
- int area = (DCELL) (frags[i+1] - frags[i]);
- /* for all cells in a patch */
- for(p = frags[i]; p < frags[i+1]; p++) {
- border+= 4 - p->neighbors;
- }
- vals[i] = (DCELL) border / ( 4 * sqrt((DCELL) area) );
+int f_shapeindex(DCELL * vals, Coords ** frags, int count)
+{
+ Coords *p;
+
+ int i;
+
+ /* for all patches */
+ for (i = 0; i < count; i++) {
+ // display progress
+ if (verbose)
+ G_percent(i, count, 2);
+
+ int border = 0;
+
+ int area = (DCELL) (frags[i + 1] - frags[i]);
+
+ /* for all cells in a patch */
+ for (p = frags[i]; p < frags[i + 1]; p++) {
+ border += 4 - p->neighbors;
}
-
- return 0;
+ vals[i] = (DCELL) border / (4 * sqrt((DCELL) area));
+ }
+
+ return 0;
}
-int f_borderindex(DCELL *vals, Coords **frags, int count) {
- Coords *p;
- int i;
- /* for all patches */
- for(i = 0; i < count; i++) {
- // display progress
- if (verbose)
- G_percent (i, count, 2);
-
- int border = 0;
- int maxx, maxy, minx, miny;
- int l = 0;
- int w = 0;
- maxx = minx = frags[i]->x;
- maxy = miny = frags[i]->y;
- /* for all cells in a patch */
- for(p = frags[i]; p < frags[i+1]; p++) {
- border+= 4 - p->neighbors;
- maxx = p->x > maxx ? p->x : maxx;
- minx = p->x < minx ? p->x : minx;
- maxy = p->y > maxy ? p->y : maxy;
- miny = p->y < miny ? p->y : miny;
- }
- l = maxx - minx + 1; w = maxy - miny + 1;
- vals[i] = (DCELL) border / ( 2 * ( (DCELL)l + (DCELL)w ) );
+int f_borderindex(DCELL * vals, Coords ** frags, int count)
+{
+ Coords *p;
+
+ int i;
+
+ /* for all patches */
+ for (i = 0; i < count; i++) {
+ // display progress
+ if (verbose)
+ G_percent(i, count, 2);
+
+ int border = 0;
+
+ int maxx, maxy, minx, miny;
+
+ int l = 0;
+
+ int w = 0;
+
+ maxx = minx = frags[i]->x;
+ maxy = miny = frags[i]->y;
+ /* for all cells in a patch */
+ for (p = frags[i]; p < frags[i + 1]; p++) {
+ border += 4 - p->neighbors;
+ maxx = p->x > maxx ? p->x : maxx;
+ minx = p->x < minx ? p->x : minx;
+ maxy = p->y > maxy ? p->y : maxy;
+ miny = p->y < miny ? p->y : miny;
}
-
- return 0;
+ l = maxx - minx + 1;
+ w = maxy - miny + 1;
+ vals[i] = (DCELL) border / (2 * ((DCELL) l + (DCELL) w));
+ }
+
+ return 0;
}
-int f_compactness(DCELL *vals, Coords **frags, int count) {
- Coords *p;
- int i;
- /* for all patches */
- for(i = 0; i < count; i++) {
- // display progress
- if (verbose)
- G_percent (i, count, 2);
-
- int area = 0;
- int maxx, maxy, minx, miny;
- int l = 0;
- int w = 0;
- maxx = minx = frags[i]->x;
- maxy = miny = frags[i]->y;
- /* for all cells in a patch */
- for(p = frags[i]; p < frags[i+1]; p++) {
- area++;
- maxx = p->x > maxx ? p->x : maxx;
- minx = p->x < minx ? p->x : minx;
- maxy = p->y > maxy ? p->y : maxy;
- miny = p->y < miny ? p->y : miny;
- }
- l = maxx - minx + 1; w = maxy - miny + 1;
- vals[i] = (DCELL)l * (DCELL)w / (DCELL) area;
+int f_compactness(DCELL * vals, Coords ** frags, int count)
+{
+ Coords *p;
+
+ int i;
+
+ /* for all patches */
+ for (i = 0; i < count; i++) {
+ // display progress
+ if (verbose)
+ G_percent(i, count, 2);
+
+ int area = 0;
+
+ int maxx, maxy, minx, miny;
+
+ int l = 0;
+
+ int w = 0;
+
+ maxx = minx = frags[i]->x;
+ maxy = miny = frags[i]->y;
+ /* for all cells in a patch */
+ for (p = frags[i]; p < frags[i + 1]; p++) {
+ area++;
+ maxx = p->x > maxx ? p->x : maxx;
+ minx = p->x < minx ? p->x : minx;
+ maxy = p->y > maxy ? p->y : maxy;
+ miny = p->y < miny ? p->y : miny;
}
-
- return 0;
+ l = maxx - minx + 1;
+ w = maxy - miny + 1;
+ vals[i] = (DCELL) l *(DCELL) w / (DCELL) area;
+ }
+
+ return 0;
}
-int f_asymmetry(DCELL *vals, Coords **frags, int count) {
- Coords *p;
- int i;
- /* for all patches */
- for(i = 0; i < count; i++) {
- // display progress
- if (verbose)
- G_percent (i, count, 2);
-
- /* compute variance for x,y and xy in the patch */
- /* formula: a(x) = sum(x_i), b(x) = sum(x_i²), var(x) = (b(x) - a(x)² / n) / n */
- /* covar(x*y) = (a(x * y) - a(x) * a(y) / n) /n */
- int ax, ay, axy;
- int bx, by;
- DCELL vx, vy, vxy, vsum, invn;
- int n = 0;
- ax = ay = axy = 0; bx = by = 0;
- /* for all cells in a patch */
-// fprintf(stderr, "\npatch %d: ", i);
- for(p = frags[i]; p < frags[i+1]; p++, n++) {
- int x = p->x; int y = p->y; int xy = p->x * p->y;
- ax += x; ay += y; axy += xy;
- bx += x * x; by += y * y;
-// fprintf(stderr, "x_%d = %d, y_%d = %d; ", n, x, n, y);
- }
- invn = 1.0 / (DCELL)n;
- vx = ( (DCELL)bx - (DCELL)ax * (DCELL)ax * invn ) * invn;
- vy = ( (DCELL)by - (DCELL)ay * (DCELL)ay * invn ) * invn;
- vxy = ( (DCELL)axy - (DCELL)ax * (DCELL)ay * invn ) * invn;
-// fprintf(stderr, " axy = %d, ax = %d, ay = %d, n = %d", axy, ax, ay, n);
- vsum = vx + vy;
- vals[i] = 2 * sqrt(0.25 * vsum * vsum + vxy * vxy - vx * vy) / vsum;
+int f_asymmetry(DCELL * vals, Coords ** frags, int count)
+{
+ Coords *p;
+
+ int i;
+
+ /* for all patches */
+ for (i = 0; i < count; i++) {
+ // display progress
+ if (verbose)
+ G_percent(i, count, 2);
+
+ /* compute variance for x,y and xy in the patch */
+ /* formula: a(x) = sum(x_i), b(x) = sum(x_i²), var(x) = (b(x) - a(x)² / n) / n */
+ /* covar(x*y) = (a(x * y) - a(x) * a(y) / n) /n */
+ int ax, ay, axy;
+
+ int bx, by;
+
+ DCELL vx, vy, vxy, vsum, invn;
+
+ int n = 0;
+
+ ax = ay = axy = 0;
+ bx = by = 0;
+ /* for all cells in a patch */
+ // fprintf(stderr, "\npatch %d: ", i);
+ for (p = frags[i]; p < frags[i + 1]; p++, n++) {
+ int x = p->x;
+
+ int y = p->y;
+
+ int xy = p->x * p->y;
+
+ ax += x;
+ ay += y;
+ axy += xy;
+ bx += x * x;
+ by += y * y;
+ // fprintf(stderr, "x_%d = %d, y_%d = %d; ", n, x, n, y);
}
+ invn = 1.0 / (DCELL) n;
+ vx = ((DCELL) bx - (DCELL) ax * (DCELL) ax * invn) * invn;
+ vy = ((DCELL) by - (DCELL) ay * (DCELL) ay * invn) * invn;
+ vxy = ((DCELL) axy - (DCELL) ax * (DCELL) ay * invn) * invn;
+ // fprintf(stderr, " axy = %d, ax = %d, ay = %d, n = %d", axy, ax, ay, n);
+ vsum = vx + vy;
+ vals[i] = 2 * sqrt(0.25 * vsum * vsum + vxy * vxy - vx * vy) / vsum;
+ }
}
-int f_area_perim_ratio(DCELL *vals, Coords **frags, int count) {
- Coords *p;
- int i;
- /* for all patches */
- for(i = 0; i < count; i++) {
- // display progress
- if (verbose)
- G_percent (i, count, 2);
-
- int border = 0;
- int area = (DCELL) (frags[i+1] - frags[i]);
- /* for all cells in a patch */
- for(p = frags[i]; p < frags[i+1]; p++) {
- border+= 4 - p->neighbors;
- }
- vals[i] = (DCELL) area / (DCELL) border;
+int f_area_perim_ratio(DCELL * vals, Coords ** frags, int count)
+{
+ Coords *p;
+
+ int i;
+
+ /* for all patches */
+ for (i = 0; i < count; i++) {
+ // display progress
+ if (verbose)
+ G_percent(i, count, 2);
+
+ int border = 0;
+
+ int area = (DCELL) (frags[i + 1] - frags[i]);
+
+ /* for all cells in a patch */
+ for (p = frags[i]; p < frags[i + 1]; p++) {
+ border += 4 - p->neighbors;
}
-
- return 0;
+ vals[i] = (DCELL) area / (DCELL) border;
+ }
+
+ return 0;
}
-int f_frac_dim(DCELL *vals, Coords **frags, int count) {
- Coords *p;
- int i;
- /* for all patches */
- for(i = 0; i < count; i++) {
- // display progress
- if (verbose)
- G_percent (i, count, 2);
-
- int border = 0;
- int area = (DCELL) (frags[i+1] - frags[i]);
- /* for all cells in a patch */
- for(p = frags[i]; p < frags[i+1]; p++) {
- border+= 4 - p->neighbors;
- }
- vals[i] = 2 * log(0.25 * (DCELL) border) / log((DCELL) area);
+int f_frac_dim(DCELL * vals, Coords ** frags, int count)
+{
+ Coords *p;
+
+ int i;
+
+ /* for all patches */
+ for (i = 0; i < count; i++) {
+ // display progress
+ if (verbose)
+ G_percent(i, count, 2);
+
+ int border = 0;
+
+ int area = (DCELL) (frags[i + 1] - frags[i]);
+
+ /* for all cells in a patch */
+ for (p = frags[i]; p < frags[i + 1]; p++) {
+ border += 4 - p->neighbors;
}
-
- return 0;
+ vals[i] = 2 * log(0.25 * (DCELL) border) / log((DCELL) area);
+ }
+
+ return 0;
}
-DCELL dist(Coords *p1, Coords *p2) {
- int x1 = p1->x; int y1 = p1->y;
- int x2 = p2->x; int y2 = p2->y;
- int dx = x2 - x1; int dy = y2 - y1;
- return sqrt(dx * dx + dy * dy);
+DCELL dist(Coords * p1, Coords * p2)
+{
+ int x1 = p1->x;
+
+ int y1 = p1->y;
+
+ int x2 = p2->x;
+
+ int y2 = p2->y;
+
+ int dx = x2 - x1;
+
+ int dy = y2 - y1;
+
+ return sqrt(dx * dx + dy * dy);
}
-DCELL min_dist(Coords **frags, int n1, int n2) {
- Coords *p1, *p2;
- DCELL min = 1000000.0;
- // for all cells in the first patch
- for (p1 = frags[n1]; p1 < frags[n1+1]; p1++) {
+DCELL min_dist(Coords ** frags, int n1, int n2)
+{
+ Coords *p1, *p2;
+
+ DCELL min = 1000000.0;
+
+ // for all cells in the first patch
+ for (p1 = frags[n1]; p1 < frags[n1 + 1]; p1++) {
+ // if cell at the border
+ if (p1->neighbors < 4) {
+ // for all cells in the second patch
+ for (p2 = frags[n2]; p2 < frags[n2 + 1]; p2++) {
// if cell at the border
- if (p1->neighbors < 4) {
- // for all cells in the second patch
- for (p2 = frags[n2]; p2 < frags[n2+1]; p2++) {
- // if cell at the border
- if (p2->neighbors < 4) {
- DCELL d = dist(p1, p2);
- if (d < min) {
- min = d;
- }
- }
- }
+ if (p2->neighbors < 4) {
+ DCELL d = dist(p1, p2);
+
+ if (d < min) {
+ min = d;
+ }
}
+ }
}
- return min;
+ }
+ return min;
}
-int f_nearest_dist(DCELL *vals, Coords **frags, int count) {
- int i, j;
- /* for all patches */
- for (i = 0; i < count; i++) {
- // display progress
- if (verbose)
- G_percent (i, count, 2);
-
- DCELL min = 1000000.0;
- for (j = 0; j < count; j++) {
- if (i != j) {
- DCELL d = min_dist(frags, i, j);
- if (d < min) {
- min = d;
- }
- }
+int f_nearest_dist(DCELL * vals, Coords ** frags, int count)
+{
+ int i, j;
+
+ /* for all patches */
+ for (i = 0; i < count; i++) {
+ // display progress
+ if (verbose)
+ G_percent(i, count, 2);
+
+ DCELL min = 1000000.0;
+
+ for (j = 0; j < count; j++) {
+ if (i != j) {
+ DCELL d = min_dist(frags, i, j);
+
+ if (d < min) {
+ min = d;
}
- vals[i] = min;
+ }
}
+ vals[i] = min;
+ }
- return 0;
+ return 0;
}
-int f_proximity(DCELL *vals, Coords **frags, int count, DCELL param) {
- int i, j;
- for (i = 0; i < count; i++) {
- for (j = 0; j < count; j++) {
- if (i != j) {
- }
- }
+int f_proximity(DCELL * vals, Coords ** frags, int count, DCELL param)
+{
+ int i, j;
+
+ for (i = 0; i < count; i++) {
+ for (j = 0; j < count; j++) {
+ if (i != j) {
+ }
}
+ }
- return 0;
+ return 0;
}
Modified: grass-addons/raster/r.pi/r.pi.index/local_proto.h
===================================================================
--- grass-addons/raster/r.pi/r.pi.index/local_proto.h 2010-07-29 19:46:31 UTC (rev 42953)
+++ grass-addons/raster/r.pi/r.pi.index/local_proto.h 2010-07-30 08:30:00 UTC (rev 42954)
@@ -3,28 +3,41 @@
#define MAX_DOUBLE 1000000.0
-typedef struct {
- int x, y;
- int neighbors;
- } Coords;
+typedef struct
+{
+ int x, y;
+ int neighbors;
+} Coords;
extern void writeFrag(int row, int col, int nbr_cnt);
-extern int f_area(DCELL *vals, Coords **frags, int);
-extern int f_perim(DCELL *vals, Coords **frags, int);
-extern int f_shapeindex(DCELL*, Coords**, int);
-extern int f_borderindex(DCELL*, Coords**, int);
-extern int f_compactness(DCELL*, Coords**, int);
-extern int f_asymmetry(DCELL*, Coords**, int);
-extern int f_area_perim_ratio(DCELL*, Coords**, int);
-extern int f_frac_dim(DCELL*, Coords**, int);
-extern int f_nearest_dist(DCELL*, Coords**, int);
+extern int f_area(DCELL * vals, Coords ** frags, int);
+extern int f_perim(DCELL * vals, Coords ** frags, int);
+
+extern int f_shapeindex(DCELL *, Coords **, int);
+
+extern int f_borderindex(DCELL *, Coords **, int);
+
+extern int f_compactness(DCELL *, Coords **, int);
+
+extern int f_asymmetry(DCELL *, Coords **, int);
+
+extern int f_area_perim_ratio(DCELL *, Coords **, int);
+
+extern int f_frac_dim(DCELL *, Coords **, int);
+
+extern int f_nearest_dist(DCELL *, Coords **, int);
+
/* global variables */
int nrows, ncols;
-Coords ** fragments;
+
+Coords **fragments;
+
int *flagbuf;
-Coords * actpos;
+
+Coords *actpos;
+
int verbose;
#endif /* LOCAL_PROTO_H */
Modified: grass-addons/raster/r.pi/r.pi.index/main.c
===================================================================
--- grass-addons/raster/r.pi/r.pi.index/main.c 2010-07-29 19:46:31 UTC (rev 42953)
+++ grass-addons/raster/r.pi/r.pi.index/main.c 2010-07-30 08:30:00 UTC (rev 42954)
@@ -8,299 +8,312 @@
#include "local_proto.h"
/*
- Fragmentation analysis - basic indices in r.pi.index
-
- by Elshad Shirinov.
-*/
+ Fragmentation analysis - basic indices in r.pi.index
-typedef int (*f_func)(DCELL*, Coords**, int);
+ by Elshad Shirinov.
+ */
+typedef int (*f_func) (DCELL *, Coords **, int);
+
struct menu
{
- f_func method; /* routine to compute new value */
- char *name; /* method name */
- char *text; /* menu display - full description */
+ f_func method; /* routine to compute new value */
+ char *name; /* method name */
+ char *text; /* menu display - full description */
};
-static struct menu menu[] =
-{
- {f_area, "area", "area of specified patches"},
- {f_perim, "perimeter", "perimeter of specified patches"},
- {f_shapeindex, "shape", "shape index of specified patches"},
- {f_borderindex, "border", "border index of specified patches"},
- {f_compactness, "compactness", "compactness of specified patches"},
- {f_asymmetry, "asymmetry", "asymmetry of specified patches"},
- {f_area_perim_ratio, "area-perimeter", "area-perimeter ratio of specified patches"},
- {f_frac_dim, "fractal", "fractal dimension of specified patches"},
- {f_nearest_dist, "ENN", "euclidean distance to nearest patch"},
- {0,0,0}
+static struct menu menu[] = {
+ {f_area, "area", "area of specified patches"},
+ {f_perim, "perimeter", "perimeter of specified patches"},
+ {f_shapeindex, "shape", "shape index of specified patches"},
+ {f_borderindex, "border", "border index of specified patches"},
+ {f_compactness, "compactness", "compactness of specified patches"},
+ {f_asymmetry, "asymmetry", "asymmetry of specified patches"},
+ {f_area_perim_ratio, "area-perimeter",
+ "area-perimeter ratio of specified patches"},
+ {f_frac_dim, "fractal", "fractal dimension of specified patches"},
+ {f_nearest_dist, "ENN", "euclidean distance to nearest patch"},
+ {0, 0, 0}
};
-int main (int argc, char *argv[])
+int main(int argc, char *argv[])
{
- /* input */
- char *newname, *oldname, *newmapset, *oldmapset;
- char title[1024];
+ /* input */
+ char *newname, *oldname, *newmapset, *oldmapset;
- /* in and out file pointers */
- int in_fd;
- int out_fd;
- DCELL *result, res[30];
-
- /* map_type and categories */
- RASTER_MAP_TYPE map_type;
- struct Categories cats;
-
- int method;
- f_func compute_values;
-
- /* neighbors count */
- int neighb_count;
+ char title[1024];
- char *p;
+ /* in and out file pointers */
+ int in_fd;
- int row, col, i, j;
- int readrow;
- int keyval;
- DCELL range = MAX_DOUBLE;
-
- int n;
- int copycolr;
- struct Colors colr;
- struct GModule *module;
- struct
- {
- struct Option *input, *output;
- struct Option *keyval, *method;
- struct Option *title;
- } parm;
- struct
- {
- struct Flag *adjacent, *quiet;
- } flag;
+ int out_fd;
- DCELL *values;
- Coords *cells;
- int fragcount = 0;
-
- struct Cell_head ch, window;
+ DCELL *result, res[30];
- G_gisinit (argv[0]);
+ /* map_type and categories */
+ RASTER_MAP_TYPE map_type;
- module = G_define_module();
- module->keywords = _("raster");
- module->description =
- _("Provides basic patch based indices, like area, SHAPE or Nearest Neighbour distance.");
+ struct Categories cats;
- parm.input = G_define_option() ;
- parm.input->key = "input" ;
- parm.input->type = TYPE_STRING ;
- parm.input->required = YES ;
- parm.input->gisprompt = "old,cell,raster" ;
- parm.input->description= _("Raster map containing categories") ;
+ int method;
- parm.output = G_define_option() ;
- parm.output->key = "output" ;
- parm.output->type = TYPE_STRING ;
- parm.output->required = YES ;
- parm.output->gisprompt = "new,cell,raster" ;
- parm.output->description= _("Output patch-based result as raster map") ;
+ f_func compute_values;
- parm.keyval = G_define_option() ;
- parm.keyval->key = "keyval" ;
- parm.keyval->type = TYPE_INTEGER ;
- parm.keyval->required = YES ;
- parm.keyval->description= _("The value of the class to be analysed") ;
+ /* neighbors count */
+ int neighb_count;
- parm.method = G_define_option() ;
- parm.method->key = "method" ;
- parm.method->type = TYPE_STRING ;
- parm.method->required = YES ;
- p = parm.method->options = G_malloc(1024);
- for (n = 0; menu[n].name; n++)
- {
- if (n)
- strcat (p, ",");
- else
- *p = 0;
- strcat (p, menu[n].name);
- }
- parm.method->description= _("Operation to perform on fragments") ;
+ char *p;
- parm.title = G_define_option() ;
- parm.title->key = "title" ;
- parm.title->key_desc = "\"phrase\"" ;
- parm.title->type = TYPE_STRING ;
- parm.title->required = NO ;
- parm.title->description= _("Title of the output raster file") ;
+ int row, col, i, j;
- flag.adjacent = G_define_flag() ;
- flag.adjacent->key = 'a' ;
- flag.adjacent->description= _("Set for 8 cell-neighbors. 4 cell-neighbors are default.") ;
-
- flag.quiet = G_define_flag();
- flag.quiet->key = 'q';
- flag.quiet->description = _("Run quietly");
+ int readrow;
- if (G_parser(argc,argv))
- exit(1);
+ int keyval;
- /* get names of input files */
- oldname = parm.input->answer;
+ DCELL range = MAX_DOUBLE;
- /* test input files existance */
- if(NULL == (oldmapset = G_find_cell2(oldname,"")))
- {
- fprintf (stderr, "%s: <%s> raster file not found\n",
- G_program_name(), oldname);
- exit(1);
- }
+ int n;
- /* check if the new file name is correct */
- newname = parm.output->answer;
- if (G_legal_filename(newname) < 0)
- {
- fprintf (stderr, "%s: <%s> illegal file name\n",
- G_program_name(), newname);
- exit(1);
- }
- newmapset = G_mapset();
+ int copycolr;
- /* get size */
- nrows = G_window_rows();
- ncols = G_window_cols();
-
- /* open cell files */
- if ((in_fd = G_open_cell_old (oldname, oldmapset)) < 0)
- {
- char msg[200];
- sprintf(msg,"can't open cell file <%s> in mapset %s\n",
- oldname, oldmapset);
- G_fatal_error (msg);
- exit(-1);
- }
+ struct Colors colr;
- /* get map type */
- map_type = DCELL_TYPE;//G_raster_map_type(oldname, oldmapset);
+ struct GModule *module;
- /* copy color table */
- copycolr = (G_read_colors (oldname, oldmapset, &colr) > 0);
-
- /* get key value */
- sscanf(parm.keyval->answer, "%d", &keyval);
+ struct
+ {
+ struct Option *input, *output;
+ struct Option *keyval, *method;
+ struct Option *title;
+ } parm;
- /* get the method */
- for (method = 0; (p = menu[method].name); method++)
- if ((strcmp(p, parm.method->answer) == 0))
- break;
- if (!p)
- {
- G_warning (_("<%s=%s> unknown %s"),
- parm.method->key, parm.method->answer, parm.method->key);
- G_usage();
- exit(EXIT_FAILURE);
- }
+ struct
+ {
+ struct Flag *adjacent, *quiet;
+ } flag;
- /* establish the newvalue routine */
- compute_values = menu[method].method;
-
- /* get number of cell-neighbors */
- neighb_count = flag.adjacent->answer ? 8 : 4;
-
- /* allocate the cell buffers */
- cells = (Coords *) G_malloc ( nrows * ncols * sizeof(Coords));
- actpos = cells;
- fragments = (Coords **) G_malloc (nrows * ncols * sizeof (Coords*));
- fragments[0] = cells;
- flagbuf = (int *) G_malloc (nrows * ncols * sizeof (int));
- result = G_allocate_d_raster_buf();
+ DCELL *values;
- /* get title, initialize the category and stat info */
- if (parm.title->answer)
- strcpy (title, parm.title->answer);
+ Coords *cells;
+
+ int fragcount = 0;
+
+ struct Cell_head ch, window;
+
+ G_gisinit(argv[0]);
+
+ module = G_define_module();
+ module->keywords = _("raster");
+ module->description =
+ _("Provides basic patch based indices, like area, SHAPE or Nearest Neighbour distance.");
+
+ parm.input = G_define_option();
+ parm.input->key = "input";
+ parm.input->type = TYPE_STRING;
+ parm.input->required = YES;
+ parm.input->gisprompt = "old,cell,raster";
+ parm.input->description = _("Raster map containing categories");
+
+ parm.output = G_define_option();
+ parm.output->key = "output";
+ parm.output->type = TYPE_STRING;
+ parm.output->required = YES;
+ parm.output->gisprompt = "new,cell,raster";
+ parm.output->description = _("Output patch-based result as raster map");
+
+ parm.keyval = G_define_option();
+ parm.keyval->key = "keyval";
+ parm.keyval->type = TYPE_INTEGER;
+ parm.keyval->required = YES;
+ parm.keyval->description = _("The value of the class to be analysed");
+
+ parm.method = G_define_option();
+ parm.method->key = "method";
+ parm.method->type = TYPE_STRING;
+ parm.method->required = YES;
+ p = parm.method->options = G_malloc(1024);
+ for (n = 0; menu[n].name; n++) {
+ if (n)
+ strcat(p, ",");
else
- sprintf (title,"Fragmentation of file: %s", oldname);
+ *p = 0;
+ strcat(p, menu[n].name);
+ }
+ parm.method->description = _("Operation to perform on fragments");
- /* open the new cellfile */
- out_fd = G_open_raster_new (newname, map_type);
- if (out_fd < 0) {
- char msg[200];
- sprintf(msg,"can't create new cell file <%s> in mapset %s\n",
- newname, newmapset);
- G_fatal_error (msg);
- exit(1);
+ parm.title = G_define_option();
+ parm.title->key = "title";
+ parm.title->key_desc = "\"phrase\"";
+ parm.title->type = TYPE_STRING;
+ parm.title->required = NO;
+ parm.title->description = _("Title of the output raster file");
+
+ flag.adjacent = G_define_flag();
+ flag.adjacent->key = 'a';
+ flag.adjacent->description =
+ _("Set for 8 cell-neighbors. 4 cell-neighbors are default.");
+
+ flag.quiet = G_define_flag();
+ flag.quiet->key = 'q';
+ flag.quiet->description = _("Run quietly");
+
+ if (G_parser(argc, argv))
+ exit(1);
+
+ /* get names of input files */
+ oldname = parm.input->answer;
+
+ /* test input files existance */
+ if (NULL == (oldmapset = G_find_cell2(oldname, ""))) {
+ fprintf(stderr, "%s: <%s> raster file not found\n",
+ G_program_name(), oldname);
+ exit(1);
+ }
+
+ /* check if the new file name is correct */
+ newname = parm.output->answer;
+ if (G_legal_filename(newname) < 0) {
+ fprintf(stderr, "%s: <%s> illegal file name\n",
+ G_program_name(), newname);
+ exit(1);
+ }
+ newmapset = G_mapset();
+
+ /* get size */
+ nrows = G_window_rows();
+ ncols = G_window_cols();
+
+ /* open cell files */
+ if ((in_fd = G_open_cell_old(oldname, oldmapset)) < 0) {
+ char msg[200];
+
+ sprintf(msg, "can't open cell file <%s> in mapset %s\n",
+ oldname, oldmapset);
+ G_fatal_error(msg);
+ exit(-1);
+ }
+
+ /* get map type */
+ map_type = DCELL_TYPE; //G_raster_map_type(oldname, oldmapset);
+
+ /* copy color table */
+ copycolr = (G_read_colors(oldname, oldmapset, &colr) > 0);
+
+ /* get key value */
+ sscanf(parm.keyval->answer, "%d", &keyval);
+
+ /* get the method */
+ for (method = 0; (p = menu[method].name); method++)
+ if ((strcmp(p, parm.method->answer) == 0))
+ break;
+ if (!p) {
+ G_warning(_("<%s=%s> unknown %s"),
+ parm.method->key, parm.method->answer, parm.method->key);
+ G_usage();
+ exit(EXIT_FAILURE);
+ }
+
+ /* establish the newvalue routine */
+ compute_values = menu[method].method;
+
+ /* get number of cell-neighbors */
+ neighb_count = flag.adjacent->answer ? 8 : 4;
+
+ /* allocate the cell buffers */
+ cells = (Coords *) G_malloc(nrows * ncols * sizeof(Coords));
+ actpos = cells;
+ fragments = (Coords **) G_malloc(nrows * ncols * sizeof(Coords *));
+ fragments[0] = cells;
+ flagbuf = (int *)G_malloc(nrows * ncols * sizeof(int));
+ result = G_allocate_d_raster_buf();
+
+ /* get title, initialize the category and stat info */
+ if (parm.title->answer)
+ strcpy(title, parm.title->answer);
+ else
+ sprintf(title, "Fragmentation of file: %s", oldname);
+
+ /* open the new cellfile */
+ out_fd = G_open_raster_new(newname, map_type);
+ if (out_fd < 0) {
+ char msg[200];
+
+ sprintf(msg, "can't create new cell file <%s> in mapset %s\n",
+ newname, newmapset);
+ G_fatal_error(msg);
+ exit(1);
+ }
+
+ if (verbose = !flag.quiet->answer)
+ fprintf(stderr, "Loading Patches ... ");
+
+ /* find fragments */
+ for (row = 0; row < nrows; row++) {
+ G_get_d_raster_row(in_fd, result, row);
+ for (col = 0; col < ncols; col++) {
+ if (result[col] == keyval)
+ flagbuf[row * ncols + col] = 1;
}
- if (verbose = !flag.quiet->answer)
- fprintf (stderr, "Loading Patches ... ");
+ if (verbose)
+ G_percent(row, nrows, 2);
+ }
- /* find fragments */
- for (row = 0; row < nrows; row++) {
- G_get_d_raster_row (in_fd, result, row);
- for (col = 0; col < ncols; col++) {
- if(result[col] == keyval)
- flagbuf[row * ncols + col] = 1;
- }
-
- if (verbose)
- G_percent (row, nrows, 2);
+ for (row = 0; row < nrows; row++) {
+ for (col = 0; col < ncols; col++) {
+ if (flagbuf[row * ncols + col] == 1) {
+ fragcount++;
+ writeFrag(row, col, neighb_count);
+ fragments[fragcount] = actpos;
+ }
}
+ }
+ if (verbose)
+ G_percent(nrows, nrows, 2);
- for(row = 0; row < nrows; row++) {
- for(col = 0; col < ncols; col++) {
- if(flagbuf[row * ncols + col] == 1) {
- fragcount++;
- writeFrag(row, col, neighb_count);
- fragments[fragcount] = actpos;
- }
+ /* perform actual function on the patches */
+ if (verbose = !flag.quiet->answer)
+ fprintf(stderr, "Performing operation ... ");
+ values = (DCELL *) G_malloc(fragcount * sizeof(DCELL));
+ compute_values(values, fragments, fragcount);
+ if (verbose)
+ G_percent(fragcount, fragcount, 2);
+
+ /* write the output file */
+ if (verbose = !flag.quiet->answer)
+ fprintf(stderr, "Writing output ... ");
+ for (row = 0; row < nrows; row++) {
+ G_set_d_null_value(result, ncols);
+
+ for (i = 0; i < fragcount; i++) {
+ for (actpos = fragments[i]; actpos < fragments[i + 1]; actpos++) {
+ if (actpos->y == row) {
+ result[actpos->x] = values[i];
}
+ }
}
- if (verbose)
- G_percent (nrows, nrows, 2);
- /* perform actual function on the patches */
- if (verbose = !flag.quiet->answer)
- fprintf (stderr, "Performing operation ... ");
- values = (DCELL *) G_malloc ( fragcount * sizeof(DCELL));
- compute_values(values, fragments, fragcount);
- if (verbose)
- G_percent (fragcount, fragcount, 2);
+ G_put_d_raster_row(out_fd, result);
- /* write the output file */
- if (verbose = !flag.quiet->answer)
- fprintf (stderr, "Writing output ... ");
- for(row = 0; row < nrows; row++) {
- G_set_d_null_value(result, ncols);
-
- for(i = 0; i < fragcount; i++) {
- for(actpos = fragments[i]; actpos < fragments[i+1]; actpos++) {
- if(actpos->y == row) {
- result[actpos->x] = values[i];
- }
- }
- }
-
- G_put_d_raster_row(out_fd, result);
-
- if (verbose)
- G_percent (row, nrows, 2);
- }
-
if (verbose)
- G_percent (nrows, nrows, 2);
-
- G_close_cell (out_fd);
- G_close_cell (in_fd);
+ G_percent(row, nrows, 2);
+ }
- G_free(cells);
- G_free(fragments);
- G_free(flagbuf);
+ if (verbose)
+ G_percent(nrows, nrows, 2);
- G_init_cats (0, title, &cats);
- G_write_cats (newname, &cats);
+ G_close_cell(out_fd);
+ G_close_cell(in_fd);
- if(copycolr)
- G_write_colors (newname, newmapset, &colr);
+ G_free(cells);
+ G_free(fragments);
+ G_free(flagbuf);
- exit(0);
+ G_init_cats(0, title, &cats);
+ G_write_cats(newname, &cats);
+
+ if (copycolr)
+ G_write_colors(newname, newmapset, &colr);
+
+ exit(0);
}
Modified: grass-addons/raster/r.pi/r.pi.rectangle/buffer.c
===================================================================
--- grass-addons/raster/r.pi/r.pi.rectangle/buffer.c 2010-07-29 19:46:31 UTC (rev 42953)
+++ grass-addons/raster/r.pi/r.pi.rectangle/buffer.c 2010-07-30 08:30:00 UTC (rev 42954)
@@ -1,59 +1,62 @@
#include "local_proto.h"
-
-void set_buffer(CELL* buffer, int x, int y, int width, int height, int sx, int sy, int align) {
- int l, r, t, b;
- int i, j;
- int dx, dy;
-
- switch(align) {
- case 0: /* center */
- dx = width / 2;
- dy = height / 2;
- l = x - dx;
- r = x + dx;
- t = y - dy;
- b = y + dy;
- break;
- case 1: /* top-left */
- l = x;
- r = x + width - 1;
- t = y;
- b = y + height - 1;
- break;
- case 2: /* top-right */
- l = x - width + 1;
- r = x;
- t = y;
- b = y + height - 1;
- break;
- case 3: /* bottom-left */
- l = x;
- r = x + width - 1;
- t = y - height + 1;
- b = y;
- break;
- case 4: /* bottom-right */
- l = x - width + 1;
- r = x;
- t = y - height + 1;
- b = y;
- break;
- default:
- l = t = 0;
- r = b = 1;
- break;
+
+void set_buffer(CELL * buffer, int x, int y, int width, int height, int sx,
+ int sy, int align)
+{
+ int l, r, t, b;
+
+ int i, j;
+
+ int dx, dy;
+
+ switch (align) {
+ case 0: /* center */
+ dx = width / 2;
+ dy = height / 2;
+ l = x - dx;
+ r = x + dx;
+ t = y - dy;
+ b = y + dy;
+ break;
+ case 1: /* top-left */
+ l = x;
+ r = x + width - 1;
+ t = y;
+ b = y + height - 1;
+ break;
+ case 2: /* top-right */
+ l = x - width + 1;
+ r = x;
+ t = y;
+ b = y + height - 1;
+ break;
+ case 3: /* bottom-left */
+ l = x;
+ r = x + width - 1;
+ t = y - height + 1;
+ b = y;
+ break;
+ case 4: /* bottom-right */
+ l = x - width + 1;
+ r = x;
+ t = y - height + 1;
+ b = y;
+ break;
+ default:
+ l = t = 0;
+ r = b = 1;
+ break;
+ }
+
+ l = l < 0 ? 0 : l;
+ r = r >= sx ? sx - 1 : r;
+ t = t < 0 ? 0 : t;
+ b = b >= sy ? sy - 1 : b;
+
+ /* fill buffer */
+ for (j = t; j <= b; j++) {
+ for (i = l; i <= r; i++) {
+ buffer[j * sx + i] = 1;
}
-
- l = l < 0 ? 0 : l;
- r = r >= sx ? sx - 1 : r;
- t = t < 0 ? 0 : t;
- b = b >= sy ? sy - 1 : b;
-
- /* fill buffer */
- for(j = t; j <= b; j++) {
- for(i = l; i <= r; i++) {
- buffer[j * sx + i] = 1;
- }
- }
+ }
}
-
Modified: grass-addons/raster/r.pi/r.pi.rectangle/local_proto.h
===================================================================
--- grass-addons/raster/r.pi/r.pi.rectangle/local_proto.h 2010-07-29 19:46:31 UTC (rev 42953)
+++ grass-addons/raster/r.pi/r.pi.rectangle/local_proto.h 2010-07-30 08:30:00 UTC (rev 42954)
@@ -11,6 +11,7 @@
#include <time.h>
/* buffer.c */
-void set_buffer(CELL* buffer, int x, int y, int width, int height, int sx, int sy, int align);
+void set_buffer(CELL * buffer, int x, int y, int width, int height, int sx,
+ int sy, int align);
#endif /* LOCAL_PROTO_H */
Modified: grass-addons/raster/r.pi/r.pi.rectangle/main.c
===================================================================
--- grass-addons/raster/r.pi/r.pi.rectangle/main.c 2010-07-29 19:46:31 UTC (rev 42953)
+++ grass-addons/raster/r.pi/r.pi.rectangle/main.c 2010-07-30 08:30:00 UTC (rev 42954)
@@ -1,250 +1,263 @@
#include "local_proto.h"
/*
- r.pi.rectangle programming.
+ r.pi.rectangle programming.
- by Elshad Shirinov.
-*/
+ by Elshad Shirinov.
+ */
struct alignment
{
- char *name; /* method name */
- char *text; /* menu display - full description */
- int index;
+ char *name; /* method name */
+ char *text; /* menu display - full description */
+ int index;
};
-static struct alignment alignments[] =
-{
- {"center", "Key pixel will be the center of the buffer.", 0},
- {"top-left", "Key pixel will be top-left of the buffer.", 1},
- {"top-right", "Key pixel will be the top-right of the buffer.", 2},
- {"bottom-left", "Key pixel will be the bottom-left of the buffer.", 3},
- {"bottom-right", "Key pixel will be the bottom-right of the buffer.", 4},
- {0,0,0}
+static struct alignment alignments[] = {
+ {"center", "Key pixel will be the center of the buffer.", 0},
+ {"top-left", "Key pixel will be top-left of the buffer.", 1},
+ {"top-right", "Key pixel will be the top-right of the buffer.", 2},
+ {"bottom-left", "Key pixel will be the bottom-left of the buffer.", 3},
+ {"bottom-right", "Key pixel will be the bottom-right of the buffer.", 4},
+ {0, 0, 0}
};
-int main (int argc, char *argv[])
+int main(int argc, char *argv[])
{
- /* input */
- char *oldname, *oldmapset;
-
- /* input */
- char *newname, *newmapset;
-
- /* in and out file pointers */
- int in_fd;
- int out_fd;
-
- /* parameters */
- int keyval;
- int x, y;
- int align;
- int sx, sy;
-
- /* maps */
- CELL *map, *newmap;
-
- /* other parameters */
- char *title;
-
- /* helper variables */
- int row, col;
- CELL *result;
- char* str;
- int n, i;
-
- RASTER_MAP_TYPE map_type;
- struct Cell_head ch, window;
-
- struct GModule *module;
- struct
- {
- struct Option *input, *output;
- struct Option *keyval, *x, *y;
- struct Option *alignment;
- struct Option *title;
- } parm;
+ /* input */
+ char *oldname, *oldmapset;
- G_gisinit (argv[0]);
+ /* input */
+ char *newname, *newmapset;
- module = G_define_module();
- module->keywords = _("raster");
- module->description =
- _("Generates a rectangle based on a corner coordinate.");
+ /* in and out file pointers */
+ int in_fd;
- parm.input = G_define_option() ;
- parm.input->key = "input" ;
- parm.input->type = TYPE_STRING ;
- parm.input->required = YES ;
- parm.input->gisprompt = "old,cell,raster" ;
- parm.input->description= _("raster file with single pixels representing sampling points") ;
+ int out_fd;
- parm.output = G_define_option() ;
- parm.output->key = "output" ;
- parm.output->type = TYPE_STRING ;
- parm.output->required = YES ;
- parm.output->gisprompt = "new,cell,raster,output" ;
- parm.output->description= _("Name for the output raster file") ;
+ /* parameters */
+ int keyval;
- parm.keyval = G_define_option() ;
- parm.keyval->key = "keyval" ;
- parm.keyval->type = TYPE_INTEGER ;
- parm.keyval->required = YES ;
- parm.keyval->description= _("Pixel value in the input raster to be used") ;
-
- parm.x = G_define_option() ;
- parm.x->key = "x" ;
- parm.x->type = TYPE_INTEGER ;
- parm.x->required = YES ;
- parm.x->description= _("extent of generated area on the x axis (width) in pixel") ;
+ int x, y;
- parm.y = G_define_option() ;
- parm.y->key = "y" ;
- parm.y->type = TYPE_INTEGER ;
- parm.y->required = YES ;
- parm.y->description= _("extent of generated area on the y axis (height) in pixel") ;
-
- parm.alignment = G_define_option() ;
- parm.alignment->key = "alignment" ;
- parm.alignment->type = TYPE_STRING ;
- parm.alignment->required = YES ;
- str = parm.alignment->options = G_malloc(1024);
- for (n = 0; alignments[n].name; n++)
- {
- if (n)
- strcat (str, ",");
- else
- *str = 0;
- strcat (str, alignments[n].name);
- }
- parm.alignment->description= _("alignment of the rectangle relative to the input pixel. options: center, top-left, top-right, bottom") ;
+ int align;
- parm.title = G_define_option() ;
- parm.title->key = "title" ;
- parm.title->key_desc = "\"phrase\"" ;
- parm.title->type = TYPE_STRING ;
- parm.title->required = NO ;
- parm.title->description= _("Title of the output raster file") ;
+ int sx, sy;
- if (G_parser(argc,argv))
- exit(EXIT_FAILURE);
+ /* maps */
+ CELL *map, *newmap;
- /* get name of input file */
- oldname = parm.input->answer;
+ /* other parameters */
+ char *title;
- /* test input files existence */
- if( (oldmapset = G_find_cell2(oldname,"")) == NULL )
- {
- G_warning ( _("%s: <%s> raster file not found\n"), G_program_name(), oldname);
- G_usage();
- exit(EXIT_FAILURE);
- }
-
- /* check if the new file name is correct */
- newname = parm.output->answer;
- if (G_legal_filename(newname) < 0)
- {
- G_warning("%s: <%s> illegal file name\n", G_program_name(), newname);
- exit(EXIT_FAILURE);
- }
- newmapset = G_mapset();
-
- /* read keyval */
- sscanf(parm.keyval->answer, "%d", &keyval);
-
- /* read x */
- sscanf(parm.x->answer, "%d", &x);
-
- /* read y */
- sscanf(parm.y->answer, "%d", &y);
-
- /* get size */
- sx = G_window_cols();
- sy = G_window_rows();
-
- /* find alignment */
- for (n = 0; (str = alignments[n].name); n++)
- if (strcmp(str, parm.alignment->answer) == 0)
- break;
- if (str) {
- align = alignments[n].index;
- } else {
- G_warning (_("<%s=%s> unknown %s"),
- parm.alignment->key, parm.alignment->answer, parm.alignment->key);
- G_usage();
- exit(EXIT_FAILURE);
- }
+ /* helper variables */
+ int row, col;
- /* allocate map buffers */
- map = (CELL*) G_malloc (sx * sy * sizeof(CELL));
- newmap = (CELL*) G_malloc(sx * sy * sizeof(CELL));
- result = G_allocate_c_raster_buf();
-
- /* fill newmap with null */
- G_set_c_null_value(newmap, sx * sy);
-
- /* open map */
- if ((in_fd = G_open_cell_old (oldname, oldmapset)) < 0)
- {
- G_fatal_error ( _("can't open cell file <%s> in mapset %s\n"), oldname, oldmapset);
- G_usage();
- exit(EXIT_FAILURE);
+ CELL *result;
+
+ char *str;
+
+ int n, i;
+
+ RASTER_MAP_TYPE map_type;
+
+ struct Cell_head ch, window;
+
+ struct GModule *module;
+
+ struct
+ {
+ struct Option *input, *output;
+ struct Option *keyval, *x, *y;
+ struct Option *alignment;
+ struct Option *title;
+ } parm;
+
+ G_gisinit(argv[0]);
+
+ module = G_define_module();
+ module->keywords = _("raster");
+ module->description =
+ _("Generates a rectangle based on a corner coordinate.");
+
+ parm.input = G_define_option();
+ parm.input->key = "input";
+ parm.input->type = TYPE_STRING;
+ parm.input->required = YES;
+ parm.input->gisprompt = "old,cell,raster";
+ parm.input->description =
+ _("raster file with single pixels representing sampling points");
+
+ parm.output = G_define_option();
+ parm.output->key = "output";
+ parm.output->type = TYPE_STRING;
+ parm.output->required = YES;
+ parm.output->gisprompt = "new,cell,raster,output";
+ parm.output->description = _("Name for the output raster file");
+
+ parm.keyval = G_define_option();
+ parm.keyval->key = "keyval";
+ parm.keyval->type = TYPE_INTEGER;
+ parm.keyval->required = YES;
+ parm.keyval->description =
+ _("Pixel value in the input raster to be used");
+
+ parm.x = G_define_option();
+ parm.x->key = "x";
+ parm.x->type = TYPE_INTEGER;
+ parm.x->required = YES;
+ parm.x->description =
+ _("extent of generated area on the x axis (width) in pixel");
+
+ parm.y = G_define_option();
+ parm.y->key = "y";
+ parm.y->type = TYPE_INTEGER;
+ parm.y->required = YES;
+ parm.y->description =
+ _("extent of generated area on the y axis (height) in pixel");
+
+ parm.alignment = G_define_option();
+ parm.alignment->key = "alignment";
+ parm.alignment->type = TYPE_STRING;
+ parm.alignment->required = YES;
+ str = parm.alignment->options = G_malloc(1024);
+ for (n = 0; alignments[n].name; n++) {
+ if (n)
+ strcat(str, ",");
+ else
+ *str = 0;
+ strcat(str, alignments[n].name);
+ }
+ parm.alignment->description =
+ _("alignment of the rectangle relative to the input pixel. options: center, top-left, top-right, bottom");
+
+ parm.title = G_define_option();
+ parm.title->key = "title";
+ parm.title->key_desc = "\"phrase\"";
+ parm.title->type = TYPE_STRING;
+ parm.title->required = NO;
+ parm.title->description = _("Title of the output raster file");
+
+ if (G_parser(argc, argv))
+ exit(EXIT_FAILURE);
+
+ /* get name of input file */
+ oldname = parm.input->answer;
+
+ /* test input files existence */
+ if ((oldmapset = G_find_cell2(oldname, "")) == NULL) {
+ G_warning(_("%s: <%s> raster file not found\n"), G_program_name(),
+ oldname);
+ G_usage();
+ exit(EXIT_FAILURE);
+ }
+
+ /* check if the new file name is correct */
+ newname = parm.output->answer;
+ if (G_legal_filename(newname) < 0) {
+ G_warning("%s: <%s> illegal file name\n", G_program_name(), newname);
+ exit(EXIT_FAILURE);
+ }
+ newmapset = G_mapset();
+
+ /* read keyval */
+ sscanf(parm.keyval->answer, "%d", &keyval);
+
+ /* read x */
+ sscanf(parm.x->answer, "%d", &x);
+
+ /* read y */
+ sscanf(parm.y->answer, "%d", &y);
+
+ /* get size */
+ sx = G_window_cols();
+ sy = G_window_rows();
+
+ /* find alignment */
+ for (n = 0; (str = alignments[n].name); n++)
+ if (strcmp(str, parm.alignment->answer) == 0)
+ break;
+ if (str) {
+ align = alignments[n].index;
+ }
+ else {
+ G_warning(_("<%s=%s> unknown %s"),
+ parm.alignment->key, parm.alignment->answer,
+ parm.alignment->key);
+ G_usage();
+ exit(EXIT_FAILURE);
+ }
+
+ /* allocate map buffers */
+ map = (CELL *) G_malloc(sx * sy * sizeof(CELL));
+ newmap = (CELL *) G_malloc(sx * sy * sizeof(CELL));
+ result = G_allocate_c_raster_buf();
+
+ /* fill newmap with null */
+ G_set_c_null_value(newmap, sx * sy);
+
+ /* open map */
+ if ((in_fd = G_open_cell_old(oldname, oldmapset)) < 0) {
+ G_fatal_error(_("can't open cell file <%s> in mapset %s\n"), oldname,
+ oldmapset);
+ G_usage();
+ exit(EXIT_FAILURE);
+ }
+
+ /* read map */
+ G_message("Reading map file:\n");
+ for (row = 0; row < sy; row++) {
+ G_get_c_raster_row(in_fd, map + row * sx, row);
+
+ G_percent(row + 1, sy, 1);
+ }
+
+ /* create buffers */
+ for (row = 0; row < sy; row++) {
+ for (col = 0; col < sx; col++) {
+ if (map[row * sx + col] == keyval) {
+ set_buffer(newmap, col, row, x, y, sx, sy, align);
+ }
}
-
- /* read map */
- G_message("Reading map file:\n");
- for (row = 0; row < sy; row++) {
- G_get_c_raster_row (in_fd, map + row * sx, row);
+ }
- G_percent (row + 1, sy, 1);
+ /* close map */
+ G_close_cell(in_fd);
+
+ /* write the output file */
+ G_message("Writing output ... ");
+
+ /* open cell file */
+ if ((in_fd = G_open_cell_old(oldname, oldmapset)) < 0) {
+ }
+ /* open new cell file */
+ out_fd = G_open_raster_new(newname, CELL_TYPE);
+ if (out_fd < 0) {
+ G_fatal_error("Can't create new cell file <%s> in mapset %s", newname,
+ newmapset);
+ exit(EXIT_FAILURE);
+ }
+
+ /* write output */
+ for (row = 0; row < sy; row++) {
+ G_set_c_null_value(result, sx);
+
+ for (col = 0; col < sx; col++) {
+ result[col] = newmap[row * sx + col];
}
-
- /* create buffers */
- for(row = 0; row < sy; row++) {
- for(col = 0; col < sx; col++) {
- if(map[row * sx + col] == keyval) {
- set_buffer(newmap, col, row, x, y, sx, sy, align);
- }
- }
- }
-
- /* close map */
- G_close_cell(in_fd);
- /* write the output file */
- G_message("Writing output ... ");
+ G_put_c_raster_row(out_fd, result);
- /* open cell file */
- if ((in_fd = G_open_cell_old (oldname, oldmapset)) < 0)
- {
- }
- /* open new cell file */
- out_fd = G_open_raster_new (newname, CELL_TYPE);
- if (out_fd < 0) {
- G_fatal_error("Can't create new cell file <%s> in mapset %s", newname, newmapset);
- exit(EXIT_FAILURE);
- }
-
- /* write output */
- for(row = 0; row < sy; row++) {
- G_set_c_null_value(result, sx);
-
- for(col = 0; col < sx; col++) {
- result[col] = newmap[row * sx + col];
- }
-
- G_put_c_raster_row(out_fd, result);
-
- G_percent (row + 1, sy, 1);
- }
-
- /* close new file */
- G_close_cell(out_fd);
-
- /* free allocated resources */
- G_free(map);
- G_free(newmap);
- G_free(result);
-
- exit(EXIT_SUCCESS);
+ G_percent(row + 1, sy, 1);
+ }
+
+ /* close new file */
+ G_close_cell(out_fd);
+
+ /* free allocated resources */
+ G_free(map);
+ G_free(newmap);
+ G_free(result);
+
+ exit(EXIT_SUCCESS);
}
More information about the grass-commit
mailing list