[GRASS-SVN] r65870 - in grass-addons/grass7/raster: . r.soils.texture

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Aug 9 12:59:21 PDT 2015


Author: gianluca
Date: 2015-08-09 12:59:21 -0700 (Sun, 09 Aug 2015)
New Revision: 65870

Added:
   grass-addons/grass7/raster/r.soils.texture/
   grass-addons/grass7/raster/r.soils.texture/Makefile
   grass-addons/grass7/raster/r.soils.texture/local_include.h
   grass-addons/grass7/raster/r.soils.texture/main.c
   grass-addons/grass7/raster/r.soils.texture/r.soils.texture.html
   grass-addons/grass7/raster/r.soils.texture/scheme/
Log:
r.soils.texture - grass7 porting


Property changes on: grass-addons/grass7/raster/r.soils.texture
___________________________________________________________________
Added: svn:ignore
   + OBJ.x86_64-unknown-linux-gnu


Added: grass-addons/grass7/raster/r.soils.texture/Makefile
===================================================================
--- grass-addons/grass7/raster/r.soils.texture/Makefile	                        (rev 0)
+++ grass-addons/grass7/raster/r.soils.texture/Makefile	2015-08-09 19:59:21 UTC (rev 65870)
@@ -0,0 +1,10 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.soils.texture
+
+LIBES = $(RASTERLIB) $(GISLIB)
+DEPENDENCIES = $(RASTERDEP) $(GISDEP)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd

Added: grass-addons/grass7/raster/r.soils.texture/local_include.h
===================================================================
--- grass-addons/grass7/raster/r.soils.texture/local_include.h	                        (rev 0)
+++ grass-addons/grass7/raster/r.soils.texture/local_include.h	2015-08-09 19:59:21 UTC (rev 65870)
@@ -0,0 +1,42 @@
+
+/****************************************************************************
+ *
+ * MODULE:       r.soils.texture
+ * AUTHOR(S):    Gianluca Massei (g_massa at libero.it)
+ * PURPOSE:      Intended to define soil texture from sand and clay grid.
+ *				 Require texture scheme supplied in scheme directory
+ *
+ * COPYRIGHT:    (C) 2008 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.
+ *
+ *****************************************************************************/
+/* for gettext macros - i18N localization */
+#define MAX_POINT 15
+#define MAX_LABEL 50
+
+/*///////List element prototipe/////////////////////////////// */
+struct TextureTriangleCoord
+{
+    int numVertex;		// polygons vertex of texture triangle 
+    float xSand[MAX_POINT], yClay[MAX_POINT];	// coordinate vector polygons in triangle 
+    int codeTexture;		// code for identify textural type
+    struct TextureTriangleCoord *PointToNext;	//pointer to next element
+};
+
+struct label_struct
+{
+    int id;
+    char etichetta[1024];
+};
+
+/*
+ * function prototype 
+ */
+
+struct TextureTriangleCoord *ReadTriangle(int *, char *);	//read texture file criteria (es. USDA.DAT)
+int ReadList(struct TextureTriangleCoord *, int *, float, float);	//read all list of texture criteria
+int DefineTexture(int numVert, float *xSand, float *yClay, float SandVal, float ClayVal, int codeTxt);	/*define texture for each point */
+void reclassTexture(char *SchemeName, char *result); //reclass texture file with label

Added: grass-addons/grass7/raster/r.soils.texture/main.c
===================================================================
--- grass-addons/grass7/raster/r.soils.texture/main.c	                        (rev 0)
+++ grass-addons/grass7/raster/r.soils.texture/main.c	2015-08-09 19:59:21 UTC (rev 65870)
@@ -0,0 +1,408 @@
+
+/****************************************************************************
+ *
+ * MODULE:       r.soils.texture
+ * AUTHOR(S):    Gianluca Massei (g_massa at libero.it)
+ * PURPOSE:      Intended to define soil texture from sand and clay grid.
+ *		 Require texture scheme supplied in scheme directory
+ *
+ * COPYRIGHT:    (C) 2008 by Gianluca MAssei and 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stddef.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include <grass/raster.h>
+#include "local_include.h"
+
+
+
+/* global function declaration */
+extern CELL f_c(CELL);
+extern FCELL f_f(FCELL);
+extern DCELL f_d(DCELL);
+
+CELL c_calc(CELL c_sand, CELL c_clay, struct TextureTriangleCoord *punt_lista,
+	    int *numPoly)
+{
+    /*                           */
+    CELL out;
+    int polygons;
+
+    polygons = *numPoly;
+    out = ReadList(punt_lista, &polygons, c_sand, c_clay);
+    return out;
+}
+
+FCELL f_calc(FCELL f_sand, FCELL f_clay,
+	     struct TextureTriangleCoord * punt_lista, int *numPoly)
+{
+    /*                           */
+    FCELL out;
+    int polygons;
+
+    polygons = *numPoly;
+    out = ReadList(punt_lista, &polygons, f_sand, f_clay);
+    return out;
+}
+
+DCELL d_calc(DCELL d_sand, DCELL d_clay,
+	     struct TextureTriangleCoord * punt_lista, int *numPoly)
+{
+    /*                          */
+    DCELL out;
+    int polygons;
+
+    polygons = *numPoly;
+    out = ReadList(punt_lista, &polygons, d_sand, d_clay);
+    return out;
+}
+
+/*
+ * main function
+ * it copies raster inputSand raster file, calling the appropriate function for each
+ * data type (CELL, DCELL, FCELL)
+ */
+int main(int argc, char *argv[])
+{
+    struct Cell_head cellhd;	/* it stores region information, and header information of rasters */
+    char *SandName, *ClayName, *SchemeName;	/* inputSand raster name */
+    char *result;		/* output raster name */
+    char *mapsetSand, *mapsetClay;	/* mapset name */
+    void *inrastSand, *inrastClay;	/* input buffer */
+    unsigned char *outrast;	/* output buffer */
+    int nrows, ncols;
+    int row, col;
+    int infdSand, infdClay, outfd;	/* file descriptor */
+    int verbose;
+    int polygons;
+    RASTER_MAP_TYPE data_type_Sand, data_type_Clay, data_type;	/* type of the map (CELL/DCELL/...) */
+	struct History history;	/* holds meta-data (title, comments,..)*/
+    struct GModule *module;	/* GRASS module for parsing arguments */
+
+    struct Option *inputSand, *inputClay, *inputTextureScheme, *output;	/* options */
+
+    struct TextureTriangleCoord *punt_lista;
+
+
+    /* initialize GIS environment */
+    G_gisinit(argv[0]);		/* reads grass env, stores program name to G_program_name() */
+
+    /* initialize module */
+    module = G_define_module();
+    G_add_keyword(_("raster"));
+    G_add_keyword(_("soil"));
+    G_add_keyword(_("texture"));
+    module->description = _("Define soil texture from sand and clay grid.");
+
+    /* Define the different options for SAND file */
+    inputSand = G_define_standard_option(G_OPT_R_INPUT);
+    inputSand->key = "sand";
+    inputSand->description = _("Name of input sand map [0-100]");
+
+    /* Define the different options for CLAY file */
+    inputClay = G_define_standard_option(G_OPT_R_INPUT);
+    inputClay->key = "clay";
+    inputClay->description = _("Name of input clay map [0-100]");
+
+    /*Define the texture file scheme: USDA, FAO, International or other scheme */
+    inputTextureScheme = G_define_standard_option(G_OPT_F_INPUT);
+    inputTextureScheme->key = "scheme";
+    inputTextureScheme->description = _("Text file with texture scheme");
+
+    output = G_define_standard_option(G_OPT_R_OUTPUT);
+
+    /* options and flags pareser */
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+
+    /* stores options and flags to variables */
+    SandName = inputSand->answer;
+    ClayName = inputClay->answer;
+    SchemeName = inputTextureScheme->answer;
+    result = output->answer;
+
+/*************************************control sand file*****************************/
+    /* returns NULL if the map was not found in any mapset, 
+     * mapset name otherwise */
+    mapsetSand = (char *) G_find_raster2(SandName, "");
+    if (mapsetSand == NULL)
+	G_fatal_error(_("cell file [%s] not found"), SandName);
+
+   /* if (G_legal_filename(result) < 0)
+	G_fatal_error(_("[%s] is an illegal name"), result);*/
+
+    /* determine the input map type (CELL/FCELL/DCELL) */
+    data_type_Sand = Rast_map_type(SandName, mapsetSand);
+
+    /* G_open_cell_old - returns file destriptor (>0) */
+    infdSand = Rast_open_old(SandName, mapsetSand);
+
+    /* controlling, if we can open inputSand raster */
+    Rast_get_cellhd(SandName, mapsetSand, &cellhd);
+	G_debug(3, "number of rows %d", cellhd.rows);
+/*********************************control clay file **********************************/
+
+    /* returns NULL if the map was not found in any mapset, 
+     * mapset name otherwise */
+    mapsetClay = (char *) G_find_raster2(ClayName, "");
+  
+    if (mapsetClay == NULL)
+	G_fatal_error(_("cell file [%s] not found"), ClayName);
+
+    /*if (G_legal_filename(result) < 0)
+	G_fatal_error(_("[%s] is an illegal name"), result);*/
+
+    /* determine the input map type (CELL/FCELL/DCELL) */
+    data_type_Clay = Rast_map_type(ClayName, mapsetClay);
+
+    /* G_open_cell_old - returns file destriptor (>0) */
+    infdClay = Rast_open_old(ClayName, mapsetClay);
+
+    /* controlling, if we can open inputClay raster */
+    Rast_get_cellhd(ClayName, mapsetClay, &cellhd);
+	
+	G_debug(3, "number of rows %d", cellhd.rows);
+/**************************************************************************************/
+    /* Allocate input buffer */
+    inrastSand = Rast_allocate_buf(data_type_Sand);
+    inrastClay = Rast_allocate_buf(data_type_Clay);
+
+    /* Allocate output buffer, use input Sand map data_type */
+    nrows = Rast_window_rows();
+    ncols = Rast_window_rows();
+    outrast = Rast_allocate_buf(data_type_Sand);
+    data_type = data_type_Sand;
+
+    /* controlling, if we can write the raster */
+    outfd = Rast_open_new(result, data_type);
+
+    punt_lista = ReadTriangle(&polygons, SchemeName);	//read texture criteria file (es USDA.DAT) and buil a list
+    CELL c_sand, c_clay, c;
+    FCELL f_sand, f_clay, f;
+    DCELL d_sand, d_clay, d;
+
+    /* for each row */
+    for (row = 0; row < nrows; row++) {
+	if (verbose)
+	    G_percent(row, nrows, 2);
+
+	/* read input Sand map */
+	Rast_get_row(infdSand, inrastSand, row, data_type_Sand);
+	/* read input Sand map */
+	Rast_get_row(infdClay, inrastClay, row, data_type_Sand);
+
+
+	/* process the data */
+	for (col = 0; col < ncols; col++) {
+	    /* use different function for each data type */
+	    switch (data_type) {
+	    case CELL_TYPE:
+		c_sand = ((CELL *) inrastSand)[col];
+		c_clay = ((CELL *) inrastClay)[col];
+		c = c_calc(c_sand, c_clay, punt_lista, &polygons);	/* calculate */
+		((CELL *) outrast)[col] = c;
+		break;
+	    case FCELL_TYPE:
+		f_sand = ((FCELL *) inrastSand)[col];
+		f_clay = ((FCELL *) inrastClay)[col];
+		f = f_calc(f_sand, f_clay, punt_lista, &polygons);	/* calculate */
+		((FCELL *) outrast)[col] = f;
+		break;
+	    case DCELL_TYPE:
+		d_sand = ((DCELL *) inrastSand)[col];
+		d_clay = ((DCELL *) inrastClay)[col];
+		d = d_calc(d_sand, d_clay, punt_lista, &polygons);	/* calculate */
+		((DCELL *) outrast)[col] = d;
+		break;
+	    }
+	}
+
+	/* write raster row to output raster file */
+	Rast_put_row(outfd, outrast, data_type);
+    }
+
+    /* memory cleanup */
+    G_free(inrastSand);
+    G_free(inrastClay);
+    G_free(outrast);
+
+    /* closing raster files */
+    Rast_close(infdSand);
+    Rast_close(infdClay);
+    Rast_close(outfd);
+
+    /* add command line incantation to history file */
+    Rast_short_history(result, "raster", &history);
+    Rast_command_history(&history);
+    Rast_write_history(result, &history);
+
+    reclassTexture(SchemeName, result);
+
+    G_done_msg(_("Soil texture file generated with name: <%s>"), result);
+    exit(EXIT_SUCCESS);
+}
+
+
+
+
+
+struct TextureTriangleCoord *ReadTriangle(int *numPoly, char *SchemeName)
+{
+    /*//////////// Declare Read Poly texture structure //////////////////////////// */
+    struct TextureTriangleCoord Triangle, *pTxt, *paus;
+
+    pTxt = &Triangle;		//pointer to struct
+    /*////////////////////////////////////////////////////////////////////////////// */
+    int i, j, polygons;
+    FILE *fp_dat;
+
+
+    fp_dat = fopen(SchemeName, "r");
+    if (fp_dat == NULL) {
+	printf("Could not read from <%s>", SchemeName);
+    }
+
+    fscanf(fp_dat, "%d", numPoly);
+    polygons = *numPoly;
+
+    if (polygons == 0) {
+	pTxt = NULL;		/*break if no polygons */
+    }
+    else {
+	pTxt = (struct TextureTriangleCoord *)malloc(sizeof(struct TextureTriangleCoord));	/*first struct PolygonCoord */
+	fscanf(fp_dat, "%d", &pTxt->codeTexture);
+	fscanf(fp_dat, "%d", &pTxt->numVertex);
+
+	for (j = 0; j < (pTxt->numVertex); j++) {	//fill sand array with coord. polygons array        
+	    fscanf(fp_dat, "%f", &pTxt->xSand[j]);
+	}
+	for (j = 0; j < (pTxt->numVertex); j++) {	//fill clay array with coord. polygons array          
+	    fscanf(fp_dat, "%f", &pTxt->yClay[j]);
+	}
+	paus = pTxt;
+
+
+	for (i = 2; i <= polygons; i++) {
+	    paus->PointToNext =
+		(struct TextureTriangleCoord *)
+		malloc(sizeof(struct TextureTriangleCoord));
+	    paus = paus->PointToNext;
+	    fscanf(fp_dat, "%d", &paus->codeTexture);
+	    fscanf(fp_dat, "%d", &paus->numVertex);
+	    for (j = 0; j < (paus->numVertex); j++) {	//fill sand array with coord. polygons array        
+		fscanf(fp_dat, "%f", &paus->xSand[j]);
+	    }
+	    for (j = 0; j < (paus->numVertex); j++) {	//fill clay array with coord. polygons array          
+		fscanf(fp_dat, "%f", &paus->yClay[j]);
+	    }
+	}
+
+	paus->PointToNext = NULL;
+    }
+    fclose(fp_dat);
+    return (pTxt);
+}
+
+
+int ReadList(struct TextureTriangleCoord *pTxt, int *numPoly, float SandVal,
+	     float ClayVal)
+{
+    int polygons, textureOUT;
+
+    textureOUT = 0;
+    polygons = *numPoly;
+    while (pTxt != NULL && textureOUT == 0) {
+	textureOUT =
+	    DefineTexture(pTxt->numVertex, pTxt->xSand, pTxt->yClay, SandVal,
+			  ClayVal, pTxt->codeTexture);
+	pTxt = pTxt->PointToNext;
+    }
+
+    return textureOUT;
+}
+
+
+int DefineTexture(int numVert, float *xSand, float *yClay, float SandVal,
+		  float ClayVal, int codeTxt)
+{
+    int i, j, textureVER = 0;
+
+    for (i = 0, j = numVert - 1; i < numVert; j = i++) {
+	if ((((yClay[i] <= ClayVal) && (ClayVal < yClay[j])) ||
+	     ((yClay[j] <= ClayVal) && (ClayVal < yClay[i]))) &&
+	    (SandVal <
+	     (xSand[j] - xSand[i]) * (ClayVal - yClay[i]) / (yClay[j] -
+							     yClay[i]) +
+	     xSand[i]))
+	    textureVER = !textureVER;
+    }
+
+    if (textureVER == 1) {
+	textureVER = codeTxt;
+    }
+    else {
+	textureVER = 0;
+    }
+    return textureVER;
+
+
+}
+
+
+
+void reclassTexture(char *SchemeName, char *result)
+{				/*reclass texture file with label in scheme file */
+
+    int control, i;
+    char c, tag[1024];
+
+    struct label_struct label, *plabel;
+    struct Categories pcats;
+
+    FILE *fp_dat;
+	DCELL dcat;
+	
+    fp_dat = fopen(SchemeName, "r");
+    plabel = &label;
+
+	Rast_init_cats ("", &pcats); /*Initialize category structure. */
+    if ( Rast_read_cats (result, G_mapset(), &pcats) < 0)	/*read raster category file */
+		G_fatal_error(("%s: %s in %s - can't read category file"),
+		      G_program_name(), result, G_mapset());
+
+    if (fp_dat == NULL) {
+		G_fatal_error(_("Could not open <%s>"), SchemeName);
+    }
+
+    fscanf(fp_dat, "%d", &control);
+
+    while (c != '#') {
+		fscanf(fp_dat, "%c", &c);
+		}
+
+    sprintf(tag, "NULL");
+    /*set a category label. The label is copied into the cats structure for category n. */
+	Rast_set_d_cat(&dcat, &dcat, tag, &pcats);
+    for (i = 1; i <= control; i++) {
+		dcat=(double) i;
+		fscanf(fp_dat, "%s", &plabel->etichetta);
+		sprintf(tag, "%s", plabel->etichetta);
+		Rast_set_d_cat(&dcat, &dcat, tag, &pcats);
+    }
+
+    Rast_write_cats (result, &pcats);
+
+    fclose(fp_dat);
+    /*return (0); */
+}

Added: grass-addons/grass7/raster/r.soils.texture/r.soils.texture.html
===================================================================
--- grass-addons/grass7/raster/r.soils.texture/r.soils.texture.html	                        (rev 0)
+++ grass-addons/grass7/raster/r.soils.texture/r.soils.texture.html	2015-08-09 19:59:21 UTC (rev 65870)
@@ -0,0 +1,39 @@
+<h2>DESCRIPTION</h2>
+
+<em>r.soils.texture</em> define soils texture from sand and clay
+distribution grid and a texture scheme definition.
+<br>
+Sand and clay grid are simple grid theme; scheme file is text
+ASCII file with *.dat extention like USDA.dat, FAO.dat and
+nternational.dat supplied with source code module in scheme
+directory.
+
+
+<h2>SEE ALSO<br></h2>
+
+<em><a href="http://grass.osgeo.org/programming6/">GRASS 7 Programmer's Manual</a></em>
+
+
+<h2>AUTHOR</h2>
+Gianluca Massei (g_massa at libero.it) 
+
+<br>
+<br>
+
+<h2>REFERENCE</h2>
+<p>Bourke P., 1987 Determining if a point lies on the interior of a polygon. Downloaded printed</p>
+<p>Cavallini P, Neteler M., 2005 I GIS Open Source: una alternativa possible? MondoGIS n° 47</p>
+<p>Cavallini P, Neteler M., 2006 L'analisi geografica Open Source: GRASS GIS parte 2  raster MondoGIS n° 52</p>
+<p>Neteler M. and H. Mitasova  2004  Open Source GIS: A GRASS GIS Approach - Kluwer Academic Publisher/Springer, Boston, -Second edition ISBN: 1-4020-8064-6</p>
+<p>Sanesi G., 2000 - Elementi di pedologia - Edagricole, Bologna</p>
+<p>Texture AutoLookup (TAL) ver 4.2, 2002 </p>
+<p>Van Rossum G. - 2005 - Manuale di riferimento di python - Ed. Italiana, Fred L. Drake, Jr., editor</p>
+<p>WATTON J. D., 1997 - Comparing C, Fortran, Lisp, java, python, perl,
+scheme (guile), and tcl using a floating point numerical test - point inside
+polygon. Applied mathematics and computer technology center alcoa technical
+center. Distribution documents.</p>
+<p>
+<br><br>
+</p>
+<p>
+<i>Last changed: $Date: 2013-07-12 00:46:52 +0200 (ven, 12 lug 2013) $</i>



More information about the grass-commit mailing list