[GRASS-SVN] r42923 - in grass-addons/raster/r.pi: . r.pi.rectangle

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jul 28 09:39:31 EDT 2010


Author: wegmann
Date: 2010-07-28 13:39:31 +0000 (Wed, 28 Jul 2010)
New Revision: 42923

Added:
   grass-addons/raster/r.pi/r.pi.rectangle/
   grass-addons/raster/r.pi/r.pi.rectangle/Makefile
   grass-addons/raster/r.pi/r.pi.rectangle/buffer.c
   grass-addons/raster/r.pi/r.pi.rectangle/description.html
   grass-addons/raster/r.pi/r.pi.rectangle/local_proto.h
   grass-addons/raster/r.pi/r.pi.rectangle/main.c
Log:
r.pi.rectangle module to create an area based on a GPS point of a sampling area

Added: grass-addons/raster/r.pi/r.pi.rectangle/Makefile
===================================================================
--- grass-addons/raster/r.pi/r.pi.rectangle/Makefile	                        (rev 0)
+++ grass-addons/raster/r.pi/r.pi.rectangle/Makefile	2010-07-28 13:39:31 UTC (rev 42923)
@@ -0,0 +1,10 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.pi.rectangle
+
+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.rectangle/Makefile
___________________________________________________________________
Added: svn:executable
   + *

Added: grass-addons/raster/r.pi/r.pi.rectangle/buffer.c
===================================================================
--- grass-addons/raster/r.pi/r.pi.rectangle/buffer.c	                        (rev 0)
+++ grass-addons/raster/r.pi/r.pi.rectangle/buffer.c	2010-07-28 13:39:31 UTC (rev 42923)
@@ -0,0 +1,59 @@
+#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;
+	}
+	
+	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;
+		}
+	}
+}
+

Added: grass-addons/raster/r.pi/r.pi.rectangle/description.html
===================================================================
--- grass-addons/raster/r.pi/r.pi.rectangle/description.html	                        (rev 0)
+++ grass-addons/raster/r.pi/r.pi.rectangle/description.html	2010-07-28 13:39:31 UTC (rev 42923)
@@ -0,0 +1,101 @@
+</head>
+<body bgcolor="white">
+
+<img src="grass_logo.png" alt="GRASS logo"><hr align=center size=6 noshade>
+
+<h2>NAME</h2>
+<em><b>r.pi.rectangle</b></em> converts sampling points (e.g. GPS coordinates)
+of the corner of a sampling site into an area by generating a defined rectangle.
+<h2>KEYWORDS</h2>
+raster, patch index, landscape ecology
+<h2>SYNOPSIS</h2>
+<b>r.pi.rectangle</b><br>
+<b>r.pi.rectangle help</b><br>
+
+
+
+<b>r.pi.rectangle</b> <b>input</b>=<em>name</em>
+<b>output</b>=<em>name</em> 
+<b>keyval</b>=<em>val</em>   <b>x</b>=<em>value</em>
+<b>y</b>=<em>value</em>
+<b>alignment</b>=<em>string</em>
+[<b>title</b>=<em>phrase</em>]  [--<b>overwrite</b>]  [--<b>verbose</b>] 
+[--<b>quiet</b>] 
+
+<h3>Flags:</h3>
+<DL>
+<DT><b>--overwrite</b></DT>
+<DD>Allow output files to overwrite existing files</DD>
+<DT><b>--verbose</b></DT>
+<DD>Verbose module output</DD>
+<DT><b>--quiet</b></DT>
+<DD>Quiet module output</DD>
+</DL>
+
+
+<h3>Parameters:</h3>
+<DL>
+<DT><b>input</b>=<em>name</em></DT>
+<DD>raster file with single pixels representing sampling points</DD>
+
+
+<DT><b>x</b>=<em>value</em></DT>
+<DD>extent of generated area on the x axis (width) in pixel</DD>
+
+<DT><b>y</b>=<em>value</em></DT>
+<DD>extent of generated area on the y axis (height) in pixel</DD>
+
+<DT><b>alignment</b>=<em>string</em></DT>
+<DD>alignment of the rectangle relative to the input pixel. options:
+center, top-left, top-right, bottom</DD>
+
+<DT><b>title</b>=<em>name</em></DT>
+<DD>Optional title of output map</DD>
+
+</DL>
+
+
+
+<H2>DESCRIPTION</H2>
+
+<P>
+This modules aims at generating sampling areas which are only known by the
+coordinate of one corner. The input are single points, while the output are
+areas representing the corresponding area for each of the single
+points/coordinates.
+
+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.rectangle</B> on the
+command line, without program arguments.  In this case, the user will be
+prompted for flag settings and parameter values.
+
+
+
+
+</DL>
+<H2>NOTES</H2>
+
+The areas can only be generated horizontally, not diagonal. This can be added
+as wish and might be implemented in the future.
+
+
+<P>
+
+<H2>BUGS</H2>
+
+
+
+<H2>SEE ALSO</H2>
+
+<EM><A HREF="r.pi.ENN.html">r.pi.ENN</A></EM><br>
+<EM><A HREF="r.pi.FNN.html">r.pi.FNN</A></EM><br>
+<EM><A HREF="r.pi.dist.html">r.pi.dist</A></EM><br>
+<EM><A HREF="r.li.setup.html">r.li.setup</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
+
+<p><i>Last changed: $Date: 2006/04/13 19:01:37 $</i>


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

Added: grass-addons/raster/r.pi/r.pi.rectangle/local_proto.h
===================================================================
--- grass-addons/raster/r.pi/r.pi.rectangle/local_proto.h	                        (rev 0)
+++ grass-addons/raster/r.pi/r.pi.rectangle/local_proto.h	2010-07-28 13:39:31 UTC (rev 42923)
@@ -0,0 +1,16 @@
+#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>
+
+/* buffer.c */
+void set_buffer(CELL* buffer, int x, int y, int width, int height, int sx, int sy, int align);
+
+#endif /* LOCAL_PROTO_H */

Added: grass-addons/raster/r.pi/r.pi.rectangle/main.c
===================================================================
--- grass-addons/raster/r.pi/r.pi.rectangle/main.c	                        (rev 0)
+++ grass-addons/raster/r.pi/r.pi.rectangle/main.c	2010-07-28 13:39:31 UTC (rev 42923)
@@ -0,0 +1,250 @@
+#include "local_proto.h"
+
+/*
+	r.pi.rectangle programming.
+
+	by Elshad Shirinov.
+*/
+
+struct alignment
+{
+	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}
+};
+
+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;
+
+	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= _("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,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= _("Value of relevant pixels in the input raster") ;
+	
+	parm.x = G_define_option() ;
+	parm.x->key        = "x" ;
+	parm.x->type       = TYPE_INTEGER ;
+	parm.x->required   = YES ;
+	parm.x->description= _("Width of the buffer") ;
+
+	parm.y = G_define_option() ;
+	parm.y->key        = "y" ;
+	parm.y->type       = TYPE_INTEGER ;
+	parm.y->required   = YES ;
+	parm.y->description= _("Height of the buffer") ;
+	
+	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 buffer relative to the key pixel") ;
+
+	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);
+			}
+		}
+	}
+	
+	/* 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];
+		}
+		
+		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);
+}



More information about the grass-commit mailing list