[GRASS-SVN] r42990 - in grass-addons/raster/r.pi: . r.pi.import

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Aug 4 06:53:24 EDT 2010


Author: wegmann
Date: 2010-08-04 10:53:24 +0000 (Wed, 04 Aug 2010)
New Revision: 42990

Added:
   grass-addons/raster/r.pi/r.pi.import/
   grass-addons/raster/r.pi/r.pi.import/Makefile
   grass-addons/raster/r.pi/r.pi.import/description.html
   grass-addons/raster/r.pi/r.pi.import/frag.c
   grass-addons/raster/r.pi/r.pi.import/helpers.c
   grass-addons/raster/r.pi/r.pi.import/local_proto.h
   grass-addons/raster/r.pi/r.pi.import/main.c
   grass-addons/raster/r.pi/r.pi.import/parse.c
Log:
new module for exporting raster values based on patches

Added: grass-addons/raster/r.pi/r.pi.import/Makefile
===================================================================
--- grass-addons/raster/r.pi/r.pi.import/Makefile	                        (rev 0)
+++ grass-addons/raster/r.pi/r.pi.import/Makefile	2010-08-04 10:53:24 UTC (rev 42990)
@@ -0,0 +1,5 @@
+MODULE_TOPDIR =.. /..PGM = r.pi.import LIBES = $(STATSLIB) $(GISLIB)
+    DEPENDENCIES = $(STATSDEP) $(GISDEP)
+
+include $(MODULE_TOPDIR) / include / Make / Module.make default:
+cmd


Property changes on: grass-addons/raster/r.pi/r.pi.import/Makefile
___________________________________________________________________
Added: svn:executable
   + *

Added: grass-addons/raster/r.pi/r.pi.import/description.html
===================================================================
--- grass-addons/raster/r.pi/r.pi.import/description.html	                        (rev 0)
+++ grass-addons/raster/r.pi/r.pi.import/description.html	2010-08-04 10:53:24 UTC (rev 42990)
@@ -0,0 +1,27 @@
+<H2>DESCRIPTION</H2>
+
+The <EM>r.pi.import</EM> module imports values and assigns them to corresponding patches using a previously exported raster by <EM>r.pi.export</EM>. The raster must have the 
+same extent, spatial resolution etc. in order to detect the same patches and assign the same patch IDs for all patches.
+
+<P>
+
+The program will be run non-interactively if the user specifies program arguments (see OPTIONS) on the command
+line.  Alternately, the user can simply type <B>r.pi.import</B> on the command line, without program
+arguments.  In this case, the user will be prompted for flag settings and parameter values.
+
+
+<H2>NOTES</H2>
+
+For export and import of patch values after e.g. R calculation <EM>r.pi.export</EM> and <EM>r.pi.import</EM> can be applied using the same raster.
+
+
+
+<H2>SEE ALSO</H2>
+
+<EM><A HREF="r.pi.export/description.html">r.pi.export</A></EM><br>
+
+<H2>AUTHOR</H2>
+Programming: Elshad Shirinov<br>
+Scientific concept: Dr. Martin Wegmann <br>
+Department of Remote Sensing <br>Remote Sensing and Biodiversity Unit<br> University of Wuerzburg, Germany
+


Property changes on: grass-addons/raster/r.pi/r.pi.import/description.html
___________________________________________________________________
Added: svn:executable
   + *

Added: grass-addons/raster/r.pi/r.pi.import/frag.c
===================================================================
--- grass-addons/raster/r.pi/r.pi.import/frag.c	                        (rev 0)
+++ grass-addons/raster/r.pi/r.pi.import/frag.c	2010-08-04 10:53:24 UTC (rev 42990)
@@ -0,0 +1,179 @@
+#include "local_proto.h"
+
+typedef struct
+{
+    int x, y;
+} Position;
+
+Coords *writeFrag(int *flagbuf, Coords * actpos, int row, int col, int nrows,
+		  int ncols, int nbr_cnt);
+int getNeighbors(Position * res, int *flagbuf, int x, int y, int nx, int ny,
+		 int nbr_cnt);
+
+int getNeighbors(Position * res, int *flagbuf, 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;
+}
+
+Coords *writeFrag(int *flagbuf, Coords * actpos, int row, int col, int nrows,
+		  int ncols, 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, flagbuf, 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);
+    return actpos;
+}
+
+void writeFragments(int *flagbuf, int nrows, int ncols, int nbr_cnt)
+{
+    int row, col, i;
+
+    Coords *p;
+
+    fragcount = 0;
+    Coords *actpos = fragments[0];
+
+    /* find fragments */
+    for (row = 0; row < nrows; row++) {
+	for (col = 0; col < ncols; col++) {
+	    if (flagbuf[row * ncols + col] == 1) {
+		fragcount++;
+
+		fragments[fragcount] =
+		    writeFrag(flagbuf, fragments[fragcount - 1], row, col,
+			      nrows, ncols, nbr_cnt);
+	    }
+	}
+    }
+
+    return;
+}

Added: grass-addons/raster/r.pi/r.pi.import/helpers.c
===================================================================
--- grass-addons/raster/r.pi/r.pi.import/helpers.c	                        (rev 0)
+++ grass-addons/raster/r.pi/r.pi.import/helpers.c	2010-08-04 10:53:24 UTC (rev 42990)
@@ -0,0 +1,94 @@
+#include "local_proto.h"
+
+int Round(double d)
+{
+    return d < 0 ? d - 0.5 : d + 0.5;
+}
+
+int Random(int max)
+{
+    return max <=
+	RAND_MAX ? rand() % max : floor((double)rand() /
+					(double)(RAND_MAX + 1) * max);
+}
+
+double Randomf()
+{
+    return ((double)rand()) / ((double)RAND_MAX);
+}
+
+void print_buffer(int *buffer, int sx, int sy)
+{
+    int x, y;
+
+    fprintf(stderr, "buffer:\n");
+    for (y = 0; y < sy; y++) {
+	for (x = 0; x < sx; x++) {
+	    switch (buffer[x + y * sx]) {
+	    case NOTHING:
+		fprintf(stderr, ".");
+		break;
+	    default:
+		if (buffer[x + y * sx] < 0) {
+		    fprintf(stderr, "%d", buffer[x + y * sx]);
+		}
+		else {
+		    fprintf(stderr, "%d", buffer[x + y * sx]);
+		}
+		break;
+	    }
+	}
+	fprintf(stderr, "\n");
+    }
+}
+
+void print_d_buffer(DCELL * buffer, int sx, int sy)
+{
+    int x, y;
+
+    fprintf(stderr, "buffer:\n");
+    for (y = 0; y < sy; y++) {
+	for (x = 0; x < sx; x++) {
+	    fprintf(stderr, "%0.2f ", buffer[y * sx + x]);
+	}
+	fprintf(stderr, "\n");
+    }
+}
+
+void print_array(DCELL * buffer, int size)
+{
+    int i;
+
+    for (i = 0; i < size; i++) {
+	fprintf(stderr, "%0.2f ", buffer[i]);
+    }
+    fprintf(stderr, "\n");
+}
+
+void print_fragments()
+{
+    int f;
+
+    Coords *p;
+
+    for (f = 0; f < fragcount; f++) {
+	fprintf(stderr, "frag%d: ", f);
+	for (p = fragments[f]; p < fragments[f + 1]; p++) {
+	    fprintf(stderr, "(%d,%d), n=%d ", p->x, p->y, p->neighbors);
+	}
+	fprintf(stderr, "\n");
+    }
+}
+
+void print_map(double *map, int size)
+{
+    int x, y;
+
+    fprintf(stderr, "map:\n");
+    for (y = 0; y < size; y++) {
+	for (x = 0; x < size; x++) {
+	    fprintf(stderr, "%0.0f ", map[x + y * size]);
+	}
+	fprintf(stderr, "\n");
+    }
+}

Added: grass-addons/raster/r.pi/r.pi.import/local_proto.h
===================================================================
--- grass-addons/raster/r.pi/r.pi.import/local_proto.h	                        (rev 0)
+++ grass-addons/raster/r.pi/r.pi.import/local_proto.h	2010-08-04 10:53:24 UTC (rev 42990)
@@ -0,0 +1,64 @@
+#ifndef LOCAL_PROTO_H
+#define LOCAL_PROTO_H
+
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include <grass/stats.h>
+#include <math.h>
+#include <time.h>
+
+#define NOTHING 0
+
+#define RESOLUTION 10000
+
+#define MIN_DOUBLE -1000000
+#define MAX_DOUBLE 1000000
+
+typedef struct
+{
+    int x, y;
+    int neighbors;
+} Coords;
+
+typedef DCELL(*f_statmethod) (DCELL *, int);
+
+/* helpers.c */
+int Round(double d);
+
+int Random(int max);
+
+double Randomf();
+
+void print_buffer(int *buffer, int sx, int sy);
+
+void print_d_buffer(DCELL * buffer, int sx, int sy);
+
+void print_map(double *map, int size);
+
+void print_array(DCELL * buffer, int size);
+
+void print_fragments();
+
+/* frag.c */
+void writeFragments(int *flagbuf, int nrows, int ncols, int nbr_cnt);
+
+/* parse.c */
+void parse(DCELL * values, char *file_name, int id_col, int val_col);
+
+/* global variables */
+int verbose;
+
+Coords **fragments;
+
+Coords *cells;
+
+int fragcount;
+
+int sx, sy;
+
+int *adj_matrix;
+
+#endif /* LOCAL_PROTO_H */

Added: grass-addons/raster/r.pi/r.pi.import/main.c
===================================================================
--- grass-addons/raster/r.pi/r.pi.import/main.c	                        (rev 0)
+++ grass-addons/raster/r.pi/r.pi.import/main.c	2010-08-04 10:53:24 UTC (rev 42990)
@@ -0,0 +1,253 @@
+#include "local_proto.h"
+
+/*
+   Import of patch values and generating a corresponding raster
+
+   by Elshad Shirinov.
+ */
+
+int main(int argc, char *argv[])
+{
+    /* input */
+    char *oldname, *oldmapset;
+
+    /* output */
+    char *newname, *newmapset;
+
+    /* in and out file pointers */
+    int in_fd;
+
+    int out_fd;
+
+    /* parameters */
+    int keyval;
+
+    int id_col;
+
+    int val_col;
+
+    int neighb_count;
+
+    /* maps */
+    int *map;
+
+    /* other parameters */
+    char *title;
+
+    /* helper variables */
+    int row, col;
+
+    DCELL *d_res;
+
+    DCELL *values;
+
+    int *result;
+
+    int i, n;
+
+    int x, y;
+
+    Coords *p;
+
+    char output_name[256];
+
+    char *str;
+
+    DCELL val;
+
+    RASTER_MAP_TYPE map_type;
+
+    struct Cell_head ch, window;
+
+    struct GModule *module;
+
+    struct
+    {
+	struct Option *input, *raster, *output;
+	struct Option *keyval, *id_col, *val_col;
+	struct Option *title;
+    } parm;
+
+    struct
+    {
+	struct Flag *adjacent, *quiet;
+    } flag;
+
+    G_gisinit(argv[0]);
+
+    module = G_define_module();
+    module->keywords = _("raster");
+    module->description =
+	_("Reads a text-file with Patch IDs and values and creates a raster file with these values for patches.");
+
+    parm.input = G_define_option();
+    parm.input->key = "input";
+    parm.input->type = TYPE_STRING;
+    parm.input->required = YES;
+    parm.input->gisprompt = "old_file,file,input";
+    parm.input->description = _("Name of the input ASCII-file");
+
+    parm.raster = G_define_option();
+    parm.raster->key = "raster";
+    parm.raster->type = TYPE_STRING;
+    parm.raster->required = YES;
+    parm.raster->gisprompt = "old,cell,raster";
+    parm.raster->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.keyval = G_define_option();
+    parm.keyval->key = "keyval";
+    parm.keyval->type = TYPE_INTEGER;
+    parm.keyval->required = YES;
+    parm.keyval->description =
+	_("Category value of the patches in the existing raster file");
+
+    parm.id_col = G_define_option();
+    parm.id_col->key = "id_col";
+    parm.id_col->type = TYPE_INTEGER;
+    parm.id_col->required = YES;
+    parm.id_col->description = _("Number of the column with patch IDs");
+
+    parm.val_col = G_define_option();
+    parm.val_col->key = "val_col";
+    parm.val_col->type = TYPE_INTEGER;
+    parm.val_col->required = YES;
+    parm.val_col->description = _("Number of the column with patch values");
+
+    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.");
+
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    /* get name of raster file */
+    oldname = parm.raster->answer;
+
+    /* test raster files existance */
+    if ((oldmapset = G_find_cell2(oldname, "")) == NULL) {
+	G_fatal_error(_("%s: <%s> raster file not found\n"), G_program_name(),
+		      oldname);
+	G_usage();
+	exit(EXIT_FAILURE);
+    }
+
+    /* get number of cell-neighbors */
+    neighb_count = flag.adjacent->answer ? 8 : 4;
+
+    /* get keyval */
+    sscanf(parm.keyval->answer, "%d", &keyval);
+
+    /* get id_col */
+    sscanf(parm.id_col->answer, "%d", &id_col);
+
+    /* get val_col */
+    sscanf(parm.val_col->answer, "%d", &val_col);
+
+    /* check if the new file name is correct */
+    newname = parm.output->answer;
+    if (G_legal_filename(newname) < 0) {
+	G_fatal_error(_("%s: <%s> illegal file name\n"), G_program_name(),
+		      newname);
+	G_usage();
+	exit(EXIT_FAILURE);
+    }
+    newmapset = G_mapset();
+
+    /* get size */
+    sx = G_window_cols();
+    sy = G_window_rows();
+
+    /* allocate map buffers */
+    map = (int *)G_malloc(sx * sy * sizeof(int));
+    values = (DCELL *) G_malloc(sx * sy * sizeof(DCELL));
+    d_res = G_allocate_d_raster_buf();
+    result = G_allocate_c_raster_buf();
+    cells = (Coords *) G_malloc(sx * sy * sizeof(Coords));
+    fragments = (Coords **) G_malloc(sx * sy * sizeof(Coords *));
+    fragments[0] = cells;
+
+    memset(map, 0, sx * sy * sizeof(int));
+
+    /* 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... ");
+    for (row = 0; row < sy; row++) {
+	G_get_c_raster_row(in_fd, result, row);
+	for (col = 0; col < sx; col++) {
+	    if (result[col] == keyval)
+		map[row * sx + col] = 1;
+	}
+
+	G_percent(row, sy, 2);
+    }
+    G_percent(1, 1, 2);
+
+    /* close map */
+    G_close_cell(in_fd);
+
+    /* find fragment values */
+    writeFragments(map, sy, sx, neighb_count);
+
+    /* parse input */
+    parse(values, parm.input->answer, id_col, val_col);
+
+    G_message("Writing output ... ");
+
+    /* open new cellfile  */
+    out_fd = G_open_raster_new(newname, DCELL_TYPE);
+    if (out_fd < 0) {
+	G_fatal_error(_("can't create new cell file <%s> in mapset %s\n"),
+		      newname, newmapset);
+	exit(EXIT_FAILURE);
+    }
+
+    /* write the output file */
+    for (row = 0; row < sy; row++) {
+	G_set_d_null_value(d_res, sx);
+
+	for (i = 0; i < fragcount; i++) {
+	    for (p = fragments[i]; p < fragments[i + 1]; p++) {
+		if (p->y == row) {
+		    d_res[p->x] = values[i];
+		}
+	    }
+	}
+
+	G_put_d_raster_row(out_fd, d_res);
+
+	G_percent(row + 1, sy, 1);
+    }
+
+    /* close output */
+    G_close_cell(out_fd);
+
+    /* free allocated resources */
+    G_free(map);
+    G_free(values);
+    G_free(cells);
+    G_free(fragments);
+
+    exit(EXIT_SUCCESS);
+}

Added: grass-addons/raster/r.pi/r.pi.import/parse.c
===================================================================
--- grass-addons/raster/r.pi/r.pi.import/parse.c	                        (rev 0)
+++ grass-addons/raster/r.pi/r.pi.import/parse.c	2010-08-04 10:53:24 UTC (rev 42990)
@@ -0,0 +1,61 @@
+#include "local_proto.h"
+
+void parse_line(DCELL * values, char *buffer, int id_col, int val_col)
+{
+    int counter = 1;
+
+    char *p = buffer;
+
+    int id;
+
+    DCELL value;
+
+    while (*p != 0) {
+
+	/* if current column is the id column or value column, then read value */
+	if (counter == id_col) {
+	    sscanf(p, "%d", &id);
+	}
+	if (counter == val_col) {
+	    sscanf(p, "%lf", &value);
+	}
+
+	/* skip to next column */
+	while (!(*p == 0 || *p == ' ')) {
+	    p++;
+	}
+
+	counter++;
+
+	if (*p != 0) {
+	    p++;
+	}
+    }
+
+    values[id] = value;
+}
+
+void parse(DCELL * values, char *file_name, int id_col, int val_col)
+{
+    char buffer[256];
+
+    FILE *fp;
+
+    int i;
+
+    fp = fopen(file_name, "r");
+
+    /* fill values with NULL */
+    G_set_d_null_value(values, fragcount);
+
+    if (fp == NULL) {
+	G_fatal_error("Couldn't open input file!");
+    }
+
+    /* read lines */
+    while (fgets(buffer, 256, fp)) {
+	parse_line(values, buffer, id_col, val_col);
+    }
+
+    fclose(fp);
+}



More information about the grass-commit mailing list