[GRASS-SVN] r58848 - in grass/branches/releasebranch_6_4/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 06:43:16 PST 2014


Author: neteler
Date: 2014-02-03 06:43:16 -0800 (Mon, 03 Feb 2014)
New Revision: 58848

Modified:
   grass/branches/releasebranch_6_4/raster/r.li/r.li.daemon/daemon.c
   grass/branches/releasebranch_6_4/raster/r.li/r.li.daemon/daemon.h
   grass/branches/releasebranch_6_4/raster/r.li/r.li.daemon/worker.c
   grass/branches/releasebranch_6_4/raster/r.li/r.li.edgedensity/edgedensity.c
   grass/branches/releasebranch_6_4/raster/r.li/r.li.patchdensity/main.c
   grass/branches/releasebranch_6_4/raster/r.li/r.li.setup/r.li.setup.main
   grass/branches/releasebranch_6_4/raster/r.li/r.li.setup/sample_area_vector.sh
Log:
r.li.*: fixes for trac #2024 (author: rashadkm)

Modified: grass/branches/releasebranch_6_4/raster/r.li/r.li.daemon/daemon.c
===================================================================
--- grass/branches/releasebranch_6_4/raster/r.li/r.li.daemon/daemon.c	2014-02-03 11:24:32 UTC (rev 58847)
+++ grass/branches/releasebranch_6_4/raster/r.li/r.li.daemon/daemon.c	2014-02-03 14:43:16 UTC (rev 58848)
@@ -52,7 +52,7 @@
     list l;
     msg m, doneJob;
 
-    /* int perc=0; */
+    int perc=0;
 
     g = (g_areas) G_malloc(sizeof(struct generatore));
     g->maskname = NULL;
@@ -168,8 +168,7 @@
 	int j = 0, donePid;
 
 	receive(receiveChannel, &doneJob);
-	/*perc++; */
-	/*G_percent (perc, WORKERS, 1); */
+
 	if (doneJob.type == DONE) {
 	    double result;
 
@@ -246,10 +245,12 @@
 	if (!(WIFEXITED(status)))
 	    G_warning(_("r.li.worker (pid %i) exited with abnormal status: %i"),
 		      donePid, status);
-	else
+	else {
 	    G_verbose_message(_("r.li.worker (pid %i) terminated successfully"),
 			      donePid);
-
+	    perc++;
+	    G_percent (perc, WORKERS, 1);
+        }
 	/* remove pipe */
 	if (close(child[j].channel) != 0)
 	    G_message(_("Cannot close %s file (PIPE)"), child[j].pipe);
@@ -333,7 +334,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, "|"));
@@ -342,6 +343,8 @@
 
     /* find raster map */
     mapset = G_find_cell(raster, "");
+    char * raster_ = G_fully_qualified_name(raster, mapset);
+   
     if (G_get_cellhd(raster, mapset, &cellhd) == -1)
 	G_fatal_error(_("Cannot read raster header file"));
 
@@ -415,7 +418,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, "|"));
@@ -423,7 +426,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;
@@ -463,7 +465,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 {
@@ -474,10 +476,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++;
@@ -485,18 +501,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;
 }
@@ -599,7 +617,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(getpid());
 	for (i = 0; i < loop; i++) {

Modified: grass/branches/releasebranch_6_4/raster/r.li/r.li.daemon/daemon.h
===================================================================
--- grass/branches/releasebranch_6_4/raster/r.li/r.li.daemon/daemon.h	2014-02-03 11:24:32 UTC (rev 58847)
+++ grass/branches/releasebranch_6_4/raster/r.li/r.li.daemon/daemon.h	2014-02-03 14:43:16 UTC (rev 58848)
@@ -19,6 +19,7 @@
 
 #include <grass/gis.h>
 #include "list.h"
+#include "defs.h"
 
 
 /**

Modified: grass/branches/releasebranch_6_4/raster/r.li/r.li.daemon/worker.c
===================================================================
--- grass/branches/releasebranch_6_4/raster/r.li/r.li.daemon/worker.c	2014-02-03 11:24:32 UTC (rev 58847)
+++ grass/branches/releasebranch_6_4/raster/r.li/r.li.daemon/worker.c	2014-02-03 14:43:16 UTC (rev 58848)
@@ -235,14 +235,16 @@
     G_debug(3, "daemon mask preproc: raster=[%s] mask=[%s]  rl=%d cl=%d",
 	    raster, mask, rl, cl);
 
+    /* mapset is used hold the mapset of input raster */
     mapset = G_find_cell(raster, "");
     /* open raster */
     if (G_get_cellhd(raster, mapset, &cell) == -1)
 	return NULL;
 
-    mapset = G_find_cell(mask, "");
+    /* mmapset is used hold the mapset where mask is saved */
+    char *mmapset = G_find_cell(mask, "");
     /* open raster */
-    if (G_get_cellhd(mask, mapset, &oldcell) == -1)
+    if (G_get_cellhd(mask, mmapset, &oldcell) == -1)
 	return NULL;
 
     add_row = 1.0 * oldcell.rows / rl;

Modified: grass/branches/releasebranch_6_4/raster/r.li/r.li.edgedensity/edgedensity.c
===================================================================
--- grass/branches/releasebranch_6_4/raster/r.li/r.li.edgedensity/edgedensity.c	2014-02-03 11:24:32 UTC (rev 58847)
+++ grass/branches/releasebranch_6_4/raster/r.li/r.li.edgedensity/edgedensity.c	2014-02-03 14:43:16 UTC (rev 58848)
@@ -16,7 +16,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"
@@ -71,9 +70,9 @@
     return calculateIndex(conf->answer, edgedensity, par, raster->answer,
 			  output->answer);
 
-
 }
 
+
 int edgedensity(int fd, char **valore, area_des ad, double *result)
 {
     struct Cell_head hd;
@@ -122,7 +121,6 @@
 }
 
 
-
 int calculate(int fd, area_des ad, char **valore, double *result)
 {
     double indice = 0;
@@ -135,6 +133,8 @@
 
     AVL_table *array;
 
+    buf_sup = NULL;
+
     long tot = 0;
     long zero = 0;
     long m = 0;
@@ -153,7 +153,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;
 	}
 
@@ -180,7 +180,6 @@
 
     G_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 */
@@ -223,7 +222,6 @@
 	    }
 	}
 
-
 	G_set_c_null_value(&nextCell, 1);
 	G_set_c_null_value(&prevCell, 1);
 	G_set_c_null_value(&corrCell, 1);
@@ -316,23 +314,15 @@
 		}
 
 		bordoCorr = 0;
-
 	    }
 
-
 	    prevCell = buf_corr[i + ad->x];
 	}
 
-
-
 	if (masked)
 	    mask_sup = mask_corr;
-
-
-
     }
 
-
     /* calculate index */
     if (area == 0)
 	indice = -1;
@@ -354,7 +344,6 @@
 	}
 	else {			/* all classes */
 
-
 	    array = G_malloc(m * sizeof(AVL_tableRow));
 	    if (array == NULL) {
 		G_fatal_error("malloc array failed");
@@ -380,11 +369,11 @@
 	G_free(mask_inf);
 	G_free(mask_corr);
     }
-
-    G_free(buf_sup);
+    /* G_free(buf_sup); */
     return RLI_OK;
 }
 
+
 int calculateD(int fd, area_des ad, char **valore, double *result)
 {
     double indice = 0;
@@ -489,7 +478,6 @@
 	    }
 	}
 
-
 	G_set_d_null_value(&nextCell, 1);
 	G_set_d_null_value(&prevCell, 1);
 	G_set_d_null_value(&corrCell, 1);
@@ -507,8 +495,6 @@
 
 	    if (!(G_is_null_value(&corrCell, DCELL_TYPE))) {
 
-
-
 		if ((i + 1) == ad->cl)	/*last cell of the row */
 		    G_set_d_null_value(&nextCell, 1);
 		else if (masked && mask_corr[i + 1 + ad->x] == 0)
@@ -531,7 +517,6 @@
 		    bordoCorr++;
 		}
 
-
 		if ((G_is_null_value(&supCell, DCELL_TYPE)) ||
 		    (corrCell != supCell)) {
 		    bordoCorr++;
@@ -542,7 +527,6 @@
 		    bordoCorr++;
 		}
 
-
 		if ((G_is_null_value(&nextCell, DCELL_TYPE)) ||
 		    (corrCell != nextCell)) {
 		    bordoCorr++;
@@ -589,25 +573,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;
@@ -620,10 +597,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");
@@ -708,7 +683,6 @@
 
     G_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 */
@@ -716,7 +690,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);
@@ -739,7 +712,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");
@@ -754,7 +726,6 @@
 	    }
 	}
 
-
 	G_set_f_null_value(&nextCell, 1);
 	G_set_f_null_value(&prevCell, 1);
 	G_set_f_null_value(&corrCell, 1);
@@ -793,7 +764,6 @@
 		    bordoCorr++;
 		}
 
-
 		if ((G_is_null_value(&supCell, FCELL_TYPE)) ||
 		    (corrCell != supCell)) {
 		    bordoCorr++;
@@ -804,7 +774,6 @@
 		    bordoCorr++;
 		}
 
-
 		if ((G_is_null_value(&nextCell, FCELL_TYPE)) ||
 		    (corrCell != nextCell)) {
 		    bordoCorr++;
@@ -851,25 +820,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;
@@ -885,8 +847,6 @@
 
 	}
 	else {			/* all classes */
-
-
 	    array = G_malloc(m * sizeof(AVL_tableRow));
 	    if (array == NULL) {
 		G_fatal_error("malloc array failederror");

Modified: grass/branches/releasebranch_6_4/raster/r.li/r.li.patchdensity/main.c
===================================================================
--- grass/branches/releasebranch_6_4/raster/r.li/r.li.patchdensity/main.c	2014-02-03 11:24:32 UTC (rev 58847)
+++ grass/branches/releasebranch_6_4/raster/r.li/r.li.patchdensity/main.c	2014-02-03 14:43:16 UTC (rev 58848)
@@ -20,7 +20,6 @@
 #include <grass/gis.h>
 #include <grass/glocale.h>
 #include "../r.li.daemon/daemon.h"
-#include "../r.li.daemon/defs.h"
 
 int main(int argc, char *argv[])
 {
@@ -200,6 +199,6 @@
     else
 	*result = -1;
 
-    G_free(sup);
+    /* G_free(sup);  */
     return RLI_OK;
 }

Modified: grass/branches/releasebranch_6_4/raster/r.li/r.li.setup/r.li.setup.main
===================================================================
--- grass/branches/releasebranch_6_4/raster/r.li/r.li.setup/r.li.setup.main	2014-02-03 11:24:32 UTC (rev 58847)
+++ grass/branches/releasebranch_6_4/raster/r.li/r.li.setup/r.li.setup.main	2014-02-03 14:43:16 UTC (rev 58848)
@@ -378,7 +378,8 @@
 	button .removeconf.buttons.y -text "Yes" -width 8 -command { 
 	      if { [ catch { exec rm $env(HOME)/.r.li/history/$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 "~/.r.li/history"
 		destroy .removeconf}
 	}

Modified: grass/branches/releasebranch_6_4/raster/r.li/r.li.setup/sample_area_vector.sh
===================================================================
--- grass/branches/releasebranch_6_4/raster/r.li/r.li.setup/sample_area_vector.sh	2014-02-03 11:24:32 UTC (rev 58847)
+++ grass/branches/releasebranch_6_4/raster/r.li/r.li.setup/sample_area_vector.sh	2014-02-03 14:43:16 UTC (rev 58848)
@@ -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