[GRASS-SVN] r45421 - in grass-addons/grass7/raster: . r.area

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Feb 17 14:41:56 EST 2011


Author: jarekj71
Date: 2011-02-17 11:41:56 -0800 (Thu, 17 Feb 2011)
New Revision: 45421

Added:
   grass-addons/grass7/raster/r.area/
   grass-addons/grass7/raster/r.area/Makefile
   grass-addons/grass7/raster/r.area/main.c
   grass-addons/grass7/raster/r.area/r.area.html
Log:
Initial import

Added: grass-addons/grass7/raster/r.area/Makefile
===================================================================
--- grass-addons/grass7/raster/r.area/Makefile	                        (rev 0)
+++ grass-addons/grass7/raster/r.area/Makefile	2011-02-17 19:41:56 UTC (rev 45421)
@@ -0,0 +1,12 @@
+# fix this relative to include/
+# or use absolute path to the GRASS source code
+MODULE_TOPDIR = ../..
+
+PGM = r.area
+
+LIBES = $(RASTERLIB) $(GISLIB) $(MATHLIB)
+DEPENDENCIES = $(RASTERDEP) $(GISDEP)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd

Added: grass-addons/grass7/raster/r.area/main.c
===================================================================
--- grass-addons/grass7/raster/r.area/main.c	                        (rev 0)
+++ grass-addons/grass7/raster/r.area/main.c	2011-02-17 19:41:56 UTC (rev 45421)
@@ -0,0 +1,154 @@
+/* ***************************************************************************
+ *
+ * MODULE:       r.area
+ * AUTHOR(S):    Jarek Jasiewicz <jarekj amu.edu.pl>
+ * PURPOSE:      Calculate area of clumped areas. Remove areas smaller than
+ * 							 given treshold.
+ * 
+ * COPYRIGHT:    (C) 1999-2010 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *               License (>=v2). Read the file COPYING that comes with GRASS
+ *               for details.
+ *
+ ****************************************************************************
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <grass/gis.h>
+#include <grass/raster.h>
+#include <grass/glocale.h>
+
+int main(int argc, char *argv[])
+{
+
+	struct GModule *module;
+	struct Option *input,
+								*output,
+								*par_treshold;
+	struct Flag *flag_binary;							
+									
+	struct Cell_head cellhd;
+	struct Range range;
+  struct History history;
+
+	char *mapset;
+	int nrows, ncols;
+	int binary, treshold;
+	int row, col;
+	int infd, outfd;
+	CELL *in_buf;
+	CELL *out_buf;
+	CELL c_min, c_max;
+	int *ncells;
+	int i;
+	
+	G_gisinit(argv[0]);
+	
+	input = G_define_standard_option(G_OPT_R_INPUT);
+  input->description = _("Map created with r.clump");
+  
+  output = G_define_standard_option(G_OPT_R_OUTPUT);
+	output->description = _("Map with area size (in cells)");
+	
+	par_treshold = G_define_option();	/* input stream mask file - optional */
+  par_treshold->key = "treshold";
+  par_treshold->type = TYPE_INTEGER;
+  par_treshold->answer = "0";
+  par_treshold->description = _("Remove areas lower than (0 for none):");
+	
+	flag_binary=G_define_flag();
+	flag_binary->key = 'b';
+  flag_binary->description = _("Binary output");
+	
+
+	 	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+	
+	
+	treshold = atof(par_treshold->answer);
+	binary = (flag_binary->answer != 0);
+	mapset = (char*)G_find_raster2(input->answer, "");
+	
+	  if (mapset == NULL)
+	G_fatal_error(_("Raster map <%s> not found"), input->answer);
+	
+	infd = Rast_open_old(input->answer, mapset);
+	Rast_get_cellhd(input->answer, mapset, &cellhd);
+
+	
+		if (Rast_map_type(input->answer, mapset) != CELL_TYPE)
+	 G_fatal_error(_("<%s> is not of type CELL, probably not crated with r.clump"), input->answer);
+	 
+	Rast_init_range(&range);
+	Rast_read_range(input->answer,mapset,&range);
+	Rast_get_range_min_max(&range, &c_min, &c_max);
+	
+	in_buf = Rast_allocate_c_buf();
+	
+	nrows = Rast_window_rows();
+	ncols = Rast_window_cols();
+
+	ncells=G_calloc(c_max+1,sizeof(int));
+
+	G_message(_("Reading..."));
+	for (row = 0; row < nrows; row++) {
+		G_percent(row, nrows, 2);
+		Rast_get_row(infd, in_buf, row, CELL_TYPE);
+
+		for (col = 0; col < ncols; col++) {
+				if(!Rast_is_c_null_value(&in_buf[col])) {
+						if(in_buf[col]<c_min || in_buf[col] > c_max)
+					G_fatal_error(_("Value at row %d, col %d out of range: %d"),row,col,in_buf[col]);
+					ncells[in_buf[col]]++;
+				}
+		}
+	} /* end for row */
+	
+		if(treshold) {
+				for (i=1;i<c_max;++i)
+			if(ncells[i]<treshold)
+    ncells[i]=-1;
+		}
+		
+		if(binary) {
+				for (i=1;i<c_max;++i)
+			ncells[i]= ncells[i]<treshold ? -1 : 1;
+		}
+    
+	outfd = Rast_open_new(output->answer, CELL_TYPE);
+	out_buf = Rast_allocate_c_buf();
+
+  G_message(_("Writing..."));
+	for (row = 0; row < nrows; row++) {
+		G_percent(row, nrows, 2);
+				
+	Rast_get_row(infd, in_buf, row, CELL_TYPE);
+		
+		for (col = 0; col < ncols; col++) {
+				if(Rast_is_c_null_value(&in_buf[col]) || ncells[in_buf[col]]==-1)
+						if(binary)
+					out_buf[col]=0;
+						else
+					Rast_set_c_null_value(&out_buf[col],1);
+				else
+			out_buf[col] = ncells[in_buf[col]];
+		}
+		Rast_put_row(outfd, out_buf, CELL_TYPE);
+	} /* end for row */
+  
+  G_free(ncells);
+  G_free(in_buf);
+	Rast_close(infd);
+	G_free(out_buf);
+	Rast_close(outfd);
+
+	Rast_short_history(output->answer, "raster", &history);
+	Rast_command_history(&history);
+	Rast_write_history(output->answer, &history);
+	
+	G_message(_("Done!"));
+	exit(EXIT_SUCCESS);
+}

Added: grass-addons/grass7/raster/r.area/r.area.html
===================================================================
--- grass-addons/grass7/raster/r.area/r.area.html	                        (rev 0)
+++ grass-addons/grass7/raster/r.area/r.area.html	2011-02-17 19:41:56 UTC (rev 45421)
@@ -0,0 +1,26 @@
+<h2>OPTIONS</h2>
+<DL>
+<DT><b>input</b></DT>
+<DD>Name of input created with r.clump or another cell category map.</DD>
+<DT><b>treshold</b></DT>
+<DD>Remove areas smaller than treshold, set null for removing areas</DD>
+<DT><b>binary</b></DT>
+<DD>Output map has only 0 and 1 values. If treshold is applied small areas are emoved and replaced by 0</DD>
+<DT><b>output</b></DT>
+<DD>Original categories replaced with number of cells for each category. If areas belonging to category are not continuous every area belonging to the same category has the same area value.</DD>
+</DL>
+<h2>DESCRIPTION</h2>
+<p>module can be used to remove, areas smaller than treshold, reclass according areas (similar to r.reclass area, but work in cells, not hectares and allow create more classes)</p>
+
+<h2>SEE ALSO</h2>
+<em>
+<a href="r.clump.html">r.clump</a>,
+<a href="r.mapcalc.html">r.mapcalc</a>,
+<a href="r.reclass.area.html">r.reclass.area</a>
+</em>
+
+
+<h2>AUTHOR</h2>
+Jarek  Jasiewicz
+
+



More information about the grass-commit mailing list