[GRASS-SVN] r58850 - in grass/trunk/raster/r.li: r.li.daemon r.li.edgedensity r.li.patchdensity r.li.setup

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Feb 3 07:01:44 PST 2014


Author: neteler
Date: 2014-02-03 07:01:43 -0800 (Mon, 03 Feb 2014)
New Revision: 58850

Modified:
   grass/trunk/raster/r.li/r.li.daemon/daemon.c
   grass/trunk/raster/r.li/r.li.daemon/daemon.h
   grass/trunk/raster/r.li/r.li.edgedensity/edgedensity.c
   grass/trunk/raster/r.li/r.li.patchdensity/main.c
   grass/trunk/raster/r.li/r.li.setup/sample_area_vector.sh
Log:
r.li.*: fixes for trac #2024 (author: rashadkm, forward port of r58848)

Modified: grass/trunk/raster/r.li/r.li.daemon/daemon.c
===================================================================
--- grass/trunk/raster/r.li/r.li.daemon/daemon.c	2014-02-03 14:50:19 UTC (rev 58849)
+++ grass/trunk/raster/r.li/r.li.daemon/daemon.c	2014-02-03 15:01:43 UTC (rev 58850)
@@ -56,8 +56,6 @@
     struct list *l;
     msg m, doneJob;
 
-    /* int perc=0; */
-
     g = (struct g_area *) G_malloc(sizeof(struct g_area));
     g->maskname = NULL;
     l = (struct list*) G_malloc(sizeof(struct list));
@@ -208,7 +206,7 @@
 
     token = strtok(buf, " ");
     if (strcmp("SAMPLINGFRAME", token) != 0)
-	G_fatal_error(_("Unable to parse configuration file"));
+	G_fatal_error(_("Unable to parse configuration file (sampling frame)"));
 
     rel_x = atof(strtok(NULL, "|"));
     rel_y = atof(strtok(NULL, "|"));
@@ -288,7 +286,7 @@
     else if (strcmp("MASKEDSAMPLEAREA", token) == 0) {
 	double rel_sa_x, rel_sa_y, rel_sa_rl, rel_sa_cl;
 	int aid = 1;
-	char maskname[GNAME_MAX];
+	char maskname[GNAME_MAX] = {'\0'};
 
 	do {
 	    rel_sa_x = atof(strtok(NULL, "|"));
@@ -296,7 +294,6 @@
 	    rel_sa_rl = atof(strtok(NULL, "|"));
 	    rel_sa_cl = atof(strtok(NULL, "|"));
 	    strcpy(maskname, strtok(NULL, "\n"));
-
 	    if (rel_sa_x == -1 && rel_sa_y == -1) {
 		/* runtime disposition */
 		int sa_rl, sa_cl;
@@ -336,7 +333,7 @@
     else if (strcmp("MASKEDOVERLAYAREA", token) == 0) {
 	double sa_n, sa_s, sa_w, sa_e;
 	int aid = 1;
-	char maskname[GNAME_MAX];
+	char maskname[GNAME_MAX] = { '\0' };
 	msg m;
 
 	do {
@@ -347,10 +344,24 @@
 	    sa_w = atof(strtok(NULL, "\n"));
 
 	    m.type = MASKEDAREA;
-	    m.f.f_ma.x = (int)rint((cellhd.north - sa_n) * cellhd.ns_res);
-	    m.f.f_ma.y = (int)rint((cellhd.west + sa_w) * cellhd.ew_res);
-	    m.f.f_ma.rl = (int)rint((sa_n - sa_s) * cellhd.ns_res);
-	    m.f.f_ma.cl = (int)rint((sa_e - sa_w) * cellhd.ew_res);
+
+	    struct Cell_head window;
+	    /* Get the window setting. g.region rast=<input raster> */
+	    G_get_window(&window);
+	    /* Each input overlay area from input vector are converted to raster
+	    via v.to.rast. See r.li.setup/sample_area_vector.sh. This is to used 
+	    only for reading the region (NS, EW). */
+
+	    /* Get start x and y position of masked overlay raster with respect 
+	    to input raster region from window. 
+	    sa_n, sa_e are read from configuration file. */
+	    m.f.f_ma.x = (int)G_easting_to_col(sa_e, &window);
+	    m.f.f_ma.y = (int)G_northing_to_row(sa_n, &window);
+
+	    /* Get row count and column count of overlay raster */
+	    m.f.f_ma.rl = (int)rint((sa_n - sa_s) / cellhd.ns_res);
+	    m.f.f_ma.cl = (int)rint((sa_e - sa_w) / cellhd.ew_res);
+
 	    m.f.f_ma.aid = aid;
 	    strcpy(m.f.f_ma.mask, maskname);
 	    aid++;
@@ -358,18 +369,20 @@
 	}
 	while ((token = strtok(NULL, " ")) != NULL &&
 	       (strcmp(token, "MASKEDOVERLAYAREA") == 0));
+
 	if (strcmp(token, "RASTERMAP") != 0)
 	    G_fatal_error(_("Irregular maskedoverlay areas definition"));
+
 	token = strtok(NULL, "\n");
-	if (strcmp(token, raster) != 0)
+	if (strcmp(token, raster_) != 0)
 	    G_fatal_error(_("The configuration file can be used only with \
-			%s rasterfile"), token);
+			%s rasterfile and not with %s "), token, raster_);
 	close(setup);
 	return NORMAL;
-
     }
     else
-	G_fatal_error(_("Illegal configuration file (sample area)"));
+	G_fatal_error(_("Unable to parse configuration file (sample area)"));
+
     close(setup);
     return ERROR;
 }
@@ -472,7 +485,7 @@
 	r_strat_len = (int)rint(g->rows / r_strat);
 	c_strat_len = (int)rint(g->cols / c_strat);
 	if (r_strat_len < g->rl || c_strat_len < g->cl)
-	    G_fatal_error(_("Too many strats for raster map"));
+	    G_fatal_error(_("Too many stratified random sample for raster map"));
 	loop = r_strat * c_strat;
 	srandom(0);
 	for (i = 0; i < loop; i++) {

Modified: grass/trunk/raster/r.li/r.li.daemon/daemon.h
===================================================================
--- grass/trunk/raster/r.li/r.li.daemon/daemon.h	2014-02-03 14:50:19 UTC (rev 58849)
+++ grass/trunk/raster/r.li/r.li.daemon/daemon.h	2014-02-03 15:01:43 UTC (rev 58850)
@@ -20,6 +20,7 @@
 #include <grass/gis.h>
 #include <grass/raster.h>
 #include "list.h"
+#include "defs.h"
 
 
 /**

Modified: grass/trunk/raster/r.li/r.li.edgedensity/edgedensity.c
===================================================================
--- grass/trunk/raster/r.li/r.li.edgedensity/edgedensity.c	2014-02-03 14:50:19 UTC (rev 58849)
+++ grass/trunk/raster/r.li/r.li.edgedensity/edgedensity.c	2014-02-03 15:01:43 UTC (rev 58850)
@@ -17,7 +17,6 @@
 #include <fcntl.h>		/* for O_RDONLY usage */
 #include <math.h>
 
-#include "../r.li.daemon/defs.h"
 #include "../r.li.daemon/avlDefs.h"
 #include "../r.li.daemon/avl.h"
 #include "../r.li.daemon/daemon.h"
@@ -128,6 +127,8 @@
 
     AVL_table *array;
 
+    buf_sup = NULL;
+
     long tot = 0;
     long zero = 0;
     long m = 0;
@@ -146,7 +147,7 @@
     /* open mask if needed */
     if (ad->mask == 1) {
 	if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0) {
-	    G_fatal_error("can't  open mask");
+	    G_fatal_error("can't  open mask %s", ad->mask_name);
 	    return RLI_ERRORE;
 	}
 
@@ -173,7 +174,6 @@
 
     Rast_set_c_null_value(buf_sup + ad->x, ad->cl);	/*the first time buf_sup is all null */
 
-
     for (j = 0; j < ad->rl; j++) {	/* for each raster row */
 
 	buf_corr = RLI_get_cell_raster_row(fd, j + ad->y, ad);	/* read row of raster */
@@ -309,23 +309,15 @@
 		}
 
 		bordoCorr = 0;
-
 	    }
 
-
 	    prevCell = buf_corr[i + ad->x];
 	}
 
-
-
 	if (masked)
 	    mask_sup = mask_corr;
-
-
-
     }
 
-
     /* calculate index */
     if (area == 0)
 	indice = -1;
@@ -347,7 +339,6 @@
 	}
 	else {			/* all classes */
 
-
 	    array = G_malloc(m * sizeof(AVL_tableRow));
 	    if (array == NULL) {
 		G_fatal_error("malloc array failed");
@@ -500,8 +491,6 @@
 
 	    if (!(Rast_is_null_value(&corrCell, DCELL_TYPE))) {
 
-
-
 		if ((i + 1) == ad->cl)	/*last cell of the row */
 		    Rast_set_d_null_value(&nextCell, 1);
 		else if (masked && mask_corr[i + 1 + ad->x] == 0)
@@ -582,25 +571,18 @@
 
 	    }
 
-
 	    prevCell = buf_corr[i + ad->x];
 	}
 
-
-
 	if (masked)
 	    mask_sup = mask_corr;
 
-
-
     }
 
-
     /* calculate index */
     if (area == 0)
 	indice = -1;
     else {
-
 	if (valore != NULL) {	/* only 1 class */
 	    char *sval;
 	    double val;
@@ -613,10 +595,8 @@
 	    c1.t = DCELL_TYPE;
 	    e = (double)howManyCell(albero, c1);
 	    somma = e;
-
 	}
 	else {			/* all classes */
-
 	    array = G_malloc(m * sizeof(AVL_tableRow));
 	    if (array == NULL) {
 		G_fatal_error("malloc array failed");
@@ -701,7 +681,6 @@
 
     Rast_set_f_null_value(buf_sup + ad->x, ad->cl);	/*the first time buf_sup is all null */
 
-
     for (j = 0; j < ad->rl; j++) {	/* for each raster row */
 
 	buf_corr = RLI_get_fcell_raster_row(fd, j + ad->y, ad);	/* read row of raster */
@@ -709,7 +688,6 @@
 	if (j > 0)		/* not first row */
 	    buf_sup = RLI_get_fcell_raster_row(fd, j - 1 + ad->y, ad);
 
-
 	if ((j + 1) < ad->rl) {	/*not last row */
 
 	    buf_inf = RLI_get_fcell_raster_row(fd, 1 + j + ad->y, ad);
@@ -732,7 +710,6 @@
 		return RLI_ERRORE;
 	    }
 
-
 	    if ((j + 1) < ad->rl) {	/*not last row */
 		if (read(mask_fd, mask_inf, (ad->cl * sizeof(int))) < 0) {
 		    G_fatal_error("reading mask_inf");
@@ -844,25 +821,18 @@
 
 	    }
 
-
 	    prevCell = buf_corr[i + ad->x];
 	}
 
-
-
 	if (masked)
 	    mask_sup = mask_corr;
 
-
-
     }
 
-
     /* calculate index */
     if (area == 0)
 	indice = -1;
     else {
-
 	if (valore != NULL) {	/* only 1 class */
 	    char *sval;
 	    float val;
@@ -878,8 +848,6 @@
 
 	}
 	else {			/* all classes */
-
-
 	    array = G_malloc(m * sizeof(AVL_tableRow));
 	    if (array == NULL) {
 		G_fatal_error("malloc array failederror");

Modified: grass/trunk/raster/r.li/r.li.patchdensity/main.c
===================================================================
--- grass/trunk/raster/r.li/r.li.patchdensity/main.c	2014-02-03 14:50:19 UTC (rev 58849)
+++ grass/trunk/raster/r.li/r.li.patchdensity/main.c	2014-02-03 15:01:43 UTC (rev 58850)
@@ -21,7 +21,6 @@
 #include <grass/raster.h>
 #include <grass/glocale.h>
 #include "../r.li.daemon/daemon.h"
-#include "../r.li.daemon/defs.h"
 
 int main(int argc, char *argv[])
 {

Modified: grass/trunk/raster/r.li/r.li.setup/sample_area_vector.sh
===================================================================
--- grass/trunk/raster/r.li/r.li.setup/sample_area_vector.sh	2014-02-03 14:50:19 UTC (rev 58849)
+++ grass/trunk/raster/r.li/r.li.setup/sample_area_vector.sh	2014-02-03 15:01:43 UTC (rev 58850)
@@ -1,4 +1,4 @@
-#!/bin/sh -x
+#!/bin/bash
 #
 # This program is free software under the GPL (>=v2)
 # Read the COPYING file that comes with GRASS for details.
@@ -50,26 +50,46 @@
 
 f_path="$GISBASE/etc/r.li.setup"
 
-# list categories
-v.build map=$GIS_OPT_vector option=cdump| grep '|' |cut -f1 -d '|' |\
-    tr ' ' -d > $TMP.cat
-# number of categories
-N="`grep -c "" < $TMP.cat`"
-((N= N -3))
+##############################################################
+#read categories from input vector, extract,
+#convert to raster and save the bounds to configuration file
+##############################################################
 
-#scanning categories
-I=1
-while(($I<=$N)); do
-    #extract vector[i]
-    v.extract input=$GIS_OPT_vector output=$GIS_OPT_vector"part"$I \
-	type=point,line,boundary,centroid,area,face new=-1 -d where="(CAT=$I)"
+#using v.category instead of v.build with cdump
+v.category input=$GIS_OPT_vector option=print > $TMP.cat
+
+#get input vector name
+GIS_OPT_input_vector=`echo $GIS_OPT_vector| cut -d'@' -f 1`
+
+#get input vector mapset
+GIS_OPT_input_mapset=`echo $GIS_OPT_vector| cut -d'@' -f 2`
+
+#read input vector categories into CAT_LIST array
+IFS=$'\r\n' CAT_LIST=($(cat $TMP.cat))
+
+TMP_REGION="reg`g.tempfile pid=$$`"
+#save the current region settings temporarily to avoid surpirses later.
+g.region save=$TMP_REGION
+
+#process each feature in the vector having category values in the CAT_LIST array
+for CAT in "${CAT_LIST[@]}" 
+do
+    #vector to store a feature fro $GIS_OPT_vector with category value $CAT.
+    #This temporary vector will be removed at the end. 
+    EXTRACT=$GIS_OPT_input_vector"_"$CAT"_part@"$GIS_OPT_input_mapset
+    
+    #extract only a part of $GIS_OPT_vector where category = $CAT and store in $EXTRACT
+    v.extract input=$GIS_OPT_vector output=$EXTRACT type=point,line,boundary,centroid,area,face new=-1 -d where='CAT='$CAT
+    
+    #TODO: anyway to check if x1 is in use?
     #opening monitor x1
     d.mon stop=x1
     d.mon start=x1
     #setting region with raster resolution
-    g.region vect=$GIS_OPT_vector"part"$I align=$GIS_OPT_raster
-    d.rast -o $GIS_OPT_raster
-    d.vect $GIS_OPT_vector"part"$I
+    g.region vect=$EXTRACT align=$GIS_OPT_raster
+    d.rast -o $GIS_OPT_raster    
+    #render extracted vector map
+    d.vect $EXTRACT
     #ask the user if he wants to analyse this vector and a name for raster
     #in graphical mode using wish
     export name=$TMP.val # where find the answer
@@ -80,10 +100,11 @@
     r_name=""
     read r_name < $name.var
     echo $r_name
+
     if [ $ok -eq 1 ] ; then
 	#area selected, create mask
-	v.to.rast input=$GIS_OPT_vector"part"$I output=$r_name use=cat value=1 rows=4096 
-	#write info in configuration file
+	v.to.rast input=$EXTRACT output=$r_name use=cat value=1 rows=4096
+	#read the region settings to save to configuration file
 	g.region -g| grep "n=" | cut -f2 -d'='> $name.var
 	read north < $name.var
 	g.region -g| grep "s=" | cut -f2 -d'='  > $name.var
@@ -92,15 +113,19 @@
 	read east < $name.var
 	g.region -g| grep "w=" | cut -f2 -d'=' > $name.var
 	read west < $name.var
-	echo "MASKEDOVERLAYAREA $r_name|n=$north|s=$south|e=$east|w=$west" >> $GIS_OPT_conf
+	#write info in configuration file
+	echo "MASKEDOVERLAYAREA $r_name|$north|$south|$east|$west" >> $GIS_OPT_conf
     fi
-    #clear vector map part
-    g.remove vect=$GIS_OPT_vector"part"$I
+    #remove temporary vector map created from v.extract
+    g.remove vect=$EXTRACT
     #rm -fR "$GISDBASE"/"$LOCATION"/"$MAPSET"/vector/$GIS_OPT_vector"part"$I
     #echo DROP TABLE $GIS_OPT_vector"part"$I | db.execute 
-    ((I++))
 done
 d.mon stop=x1
+#restore user region
+g.region region=$TMP_REGION
+#remove temporary region, $TMP_REGION
+g.remove region=$TMP_REGION
 # clean tmp files
 rm -f $TMP*
 



More information about the grass-commit mailing list