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

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Feb 8 20:59:56 PST 2014


Author: hamish
Date: 2014-02-08 20:59:56 -0800 (Sat, 08 Feb 2014)
New Revision: 58961

Modified:
   grass/trunk/raster/r.li/r.li.cwed/cwed.c
   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/r.li.setup.main
   grass/trunk/raster/r.li/r.li.setup/sample_area_vector.sh
Log:
remerge Rashad's patch from #2024 with some adjustments; note bugifx in daemon.c: m.f.f_ma.x and m.f.f_ma.y were reversed in older versions of the code.

Modified: grass/trunk/raster/r.li/r.li.cwed/cwed.c
===================================================================
--- grass/trunk/raster/r.li/r.li.cwed/cwed.c	2014-02-09 04:49:43 UTC (rev 58960)
+++ grass/trunk/raster/r.li/r.li.cwed/cwed.c	2014-02-09 04:59:56 UTC (rev 58961)
@@ -243,16 +243,14 @@
 	    }
 
 	}
-	else;
-	/* num = 1  ---> in the line there is only 1 token 
+	/*else
+	 *    num = 1  ---> in the line there is only 1 token 
 	 * I ignore this line
 	 */
-
     }
 
 
 
-
     switch (ad->data_type) {
     case CELL_TYPE:
 	{

Modified: grass/trunk/raster/r.li/r.li.daemon/daemon.c
===================================================================
--- grass/trunk/raster/r.li/r.li.daemon/daemon.c	2014-02-09 04:49:43 UTC (rev 58960)
+++ grass/trunk/raster/r.li/r.li.daemon/daemon.c	2014-02-09 04:59:56 UTC (rev 58961)
@@ -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));
@@ -67,7 +65,7 @@
 
     worker_init(raster, f, parameters);
 
-    /*########################################################      
+    /*#########################################################
        -----------------create area queue----------------------
        ######################################################### */
 
@@ -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, "|"));
@@ -245,7 +243,6 @@
 
 	    if (rel_sa_x == -1.0 && rel_sa_y == -1.0) {
 		/* runtime disposition */
-
 		int sa_rl, sa_cl;
 
 		sa_rl = (int)rint(cellhd.rows * rel_sa_rl);
@@ -282,13 +279,12 @@
 		 strcmp(token, "SAMPLEAREA") == 0);
 
 	close(setup);
-
 	return toReturn;
     }
     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, "|"));
@@ -328,17 +324,24 @@
 		insertNode(l, m);
 	    }
 	}
+
 	while ((token = strtok(NULL, " ")) != NULL &&
 	       strcmp(token, "MASKEDSAMPLEAREA") == 0);
+
 	close(setup);
 	return NORMAL;
     }
     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'};
+	struct Cell_head window;
 	msg m;
 
+	/* Get the window setting. g.region rast=<input raster> */
+	/*   ? same as cellhd above ? */
+	G_get_window(&window);
+
 	do {
 	    strcpy(maskname, strtok(NULL, "|"));
 	    sa_n = atof(strtok(NULL, "|"));
@@ -347,10 +350,21 @@
 	    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);
+
+	    /* 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 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)Rast_easting_to_col(sa_e, &window);
+	    m.f.f_ma.y = (int)Rast_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 +372,20 @@
 	}
 	while ((token = strtok(NULL, " ")) != NULL &&
 	       (strcmp(token, "MASKEDOVERLAYAREA") == 0));
+
 	if (strcmp(token, "RASTERMAP") != 0)
-	    G_fatal_error(_("Irregular maskedoverlay areas definition"));
+	    G_fatal_error(_("Irregular MASKEDOVERLAY areas definition"));
+
 	token = strtok(NULL, "\n");
 	if (strcmp(token, raster) != 0)
-	    G_fatal_error(_("The configuration file can be used only with \
-			%s rasterfile"), token);
+	    G_fatal_error(_("The configuration file can only be used "
+			    "with the <%s> raster map"), token);
 	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 +488,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-09 04:49:43 UTC (rev 58960)
+++ grass/trunk/raster/r.li/r.li.daemon/daemon.h	2014-02-09 04:59:56 UTC (rev 58961)
@@ -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-09 04:49:43 UTC (rev 58960)
+++ grass/trunk/raster/r.li/r.li.edgedensity/edgedensity.c	2014-02-09 04:59:56 UTC (rev 58961)
@@ -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"
@@ -72,7 +71,6 @@
     return calculateIndex(conf->answer, edgedensity, par, raster->answer,
 			  output->answer);
 
-
 }
 
 int edgedensity(int fd, char **valore, struct area_entry *ad, double *result)
@@ -115,7 +113,6 @@
 }
 
 
-
 int calculate(int fd, struct area_entry *ad, char **valore, double *result)
 {
     double indice = 0;
@@ -142,11 +139,13 @@
 
     generic_cell c1;
 
+    buf_sup = NULL;
     c1.t = CELL_TYPE;
+
     /* 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;
 	}
 
@@ -171,9 +170,9 @@
 	return RLI_ERRORE;
     }
 
-    Rast_set_c_null_value(buf_sup + ad->x, ad->cl);	/*the first time buf_sup is all null */
+    /* the first time buf_sup is all null */
+    Rast_set_c_null_value(buf_sup + ad->x, ad->cl);
 
-
     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 */
@@ -216,7 +215,6 @@
 	    }
 	}
 
-
 	Rast_set_c_null_value(&nextCell, 1);
 	Rast_set_c_null_value(&prevCell, 1);
 	Rast_set_c_null_value(&corrCell, 1);
@@ -309,23 +307,15 @@
 		}
 
 		bordoCorr = 0;
-
 	    }
 
-
 	    prevCell = buf_corr[i + ad->x];
 	}
 
-
-
 	if (masked)
 	    mask_sup = mask_corr;
-
-
-
     }
 
-
     /* calculate index */
     if (area == 0)
 	indice = -1;
@@ -347,7 +337,6 @@
 	}
 	else {			/* all classes */
 
-
 	    array = G_malloc(m * sizeof(AVL_tableRow));
 	    if (array == NULL) {
 		G_fatal_error("malloc array failed");
@@ -434,14 +423,16 @@
 	return RLI_ERRORE;
     }
 
-    Rast_set_d_null_value(buf_sup + ad->x, ad->cl);	/*the first time buf_sup is all null */
+    /* the first time buf_sup is all null */
+    Rast_set_d_null_value(buf_sup + ad->x, ad->cl);
 
+    /* for each raster row */
+    for (j = 0; j < ad->rl; j++) {
+	/* read row of raster */
+	buf_corr = RLI_get_dcell_raster_row(fd, j + ad->y, ad);
 
-    for (j = 0; j < ad->rl; j++) {	/* for each raster row */
-
-	buf_corr = RLI_get_dcell_raster_row(fd, j + ad->y, ad);	/* read row of raster */
-
-	if (j > 0)		/* not first row */
+	/* not first row */
+	if (j > 0)
 	    buf_sup = RLI_get_dcell_raster_row(fd, j - 1 + ad->y, ad);
 
 
@@ -482,7 +473,6 @@
 	    }
 	}
 
-
 	Rast_set_d_null_value(&nextCell, 1);
 	Rast_set_d_null_value(&prevCell, 1);
 	Rast_set_d_null_value(&corrCell, 1);
@@ -500,8 +490,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)
@@ -524,7 +512,6 @@
 		    bordoCorr++;
 		}
 
-
 		if ((Rast_is_null_value(&supCell, DCELL_TYPE)) ||
 		    (corrCell != supCell)) {
 		    bordoCorr++;
@@ -535,7 +522,6 @@
 		    bordoCorr++;
 		}
 
-
 		if ((Rast_is_null_value(&nextCell, DCELL_TYPE)) ||
 		    (corrCell != nextCell)) {
 		    bordoCorr++;
@@ -582,25 +568,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 +592,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");
@@ -699,17 +676,17 @@
 	return RLI_ERRORE;
     }
 
-    Rast_set_f_null_value(buf_sup + ad->x, ad->cl);	/*the first time buf_sup is all null */
+    /* the first time buf_sup is all null */
+    Rast_set_f_null_value(buf_sup + ad->x, ad->cl);
 
+    /* for each raster row */
+    for (j = 0; j < ad->rl; j++) {
+	/* read row of raster */
+	buf_corr = RLI_get_fcell_raster_row(fd, j + ad->y, ad);
 
-    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 */
-
 	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 +709,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");
@@ -747,7 +723,6 @@
 	    }
 	}
 
-
 	Rast_set_f_null_value(&nextCell, 1);
 	Rast_set_f_null_value(&prevCell, 1);
 	Rast_set_f_null_value(&corrCell, 1);
@@ -786,7 +761,6 @@
 		    bordoCorr++;
 		}
 
-
 		if ((Rast_is_null_value(&supCell, FCELL_TYPE)) ||
 		    (corrCell != supCell)) {
 		    bordoCorr++;
@@ -797,7 +771,6 @@
 		    bordoCorr++;
 		}
 
-
 		if ((Rast_is_null_value(&nextCell, FCELL_TYPE)) ||
 		    (corrCell != nextCell)) {
 		    bordoCorr++;
@@ -844,25 +817,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 +844,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-09 04:49:43 UTC (rev 58960)
+++ grass/trunk/raster/r.li/r.li.patchdensity/main.c	2014-02-09 04:59:56 UTC (rev 58961)
@@ -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/r.li.setup.main
===================================================================
--- grass/trunk/raster/r.li/r.li.setup/r.li.setup.main	2014-02-09 04:49:43 UTC (rev 58960)
+++ grass/trunk/raster/r.li/r.li.setup/r.li.setup.main	2014-02-09 04:59:56 UTC (rev 58961)
@@ -377,7 +377,8 @@
 	button .removeconf.buttons.y -text "Yes" -width 8 -command { 
 	      if { [ catch { exec rm $env(HOME)/.grass7/r.li/$selection } ] } then { 
 		tk_messageBox -message "'$selection' Not deleted" -type ok -icon error} else {
-		tk_messageBox -message "$selection  deleted" -type ok
+		#annoying "deleted" messageBox
+		#tk_messageBox -message "$selection  deleted" -type ok
 		openDir .files "~/.grass7/r.li"
 		destroy .removeconf}
 	}

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-09 04:49:43 UTC (rev 58960)
+++ grass/trunk/raster/r.li/r.li.setup/sample_area_vector.sh	2014-02-09 04:59:56 UTC (rev 58961)
@@ -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