[GRASS-SVN] r36065 - grass-addons/raster/r.univar2.zonal

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Feb 23 07:00:15 EST 2009


Author: mmetz
Date: 2009-02-23 07:00:15 -0500 (Mon, 23 Feb 2009)
New Revision: 36065

Modified:
   grass-addons/raster/r.univar2.zonal/globals.h
   grass-addons/raster/r.univar2.zonal/r.univar_main.c
   grass-addons/raster/r.univar2.zonal/stats.c
Log:
memory corruption bugfix, proper label reporting

Modified: grass-addons/raster/r.univar2.zonal/globals.h
===================================================================
--- grass-addons/raster/r.univar2.zonal/globals.h	2009-02-23 08:05:45 UTC (rev 36064)
+++ grass-addons/raster/r.univar2.zonal/globals.h	2009-02-23 12:00:15 UTC (rev 36065)
@@ -40,6 +40,7 @@
     int map_type;
     void *nextp;
     int n_alloc;
+    int first;
 } univar_stat;
 
 typedef struct

Modified: grass-addons/raster/r.univar2.zonal/r.univar_main.c
===================================================================
--- grass-addons/raster/r.univar2.zonal/r.univar_main.c	2009-02-23 08:05:45 UTC (rev 36064)
+++ grass-addons/raster/r.univar2.zonal/r.univar_main.c	2009-02-23 12:00:15 UTC (rev 36065)
@@ -109,7 +109,7 @@
     }
 
     /* TODO: make it an option */
-    zone_info.sep = ";";
+    zone_info.sep = "|";
 
     G_get_window(&region);
     rows = region.rows;
@@ -168,10 +168,6 @@
 
     process_raster(stats, fd, fdz, &region);
 
-    /* closing raster maps */
-    G_close_cell(fd);
-    G_close_cell(fdz);
-
     /* create the output */
     if (param.table->answer)
 	print_stats2(stats);
@@ -181,6 +177,10 @@
     /* release memory */
     free_univar_stat_struct(stats);
 
+    /* closing raster maps */
+    G_close_cell(fd);
+    G_close_cell(fdz);
+
     exit(EXIT_SUCCESS);
 }
 
@@ -213,7 +213,7 @@
     stats = create_univar_stat_struct(map_type, size, i);
     for (i = 0; i < zone_info.n_zones; i++) {
 	for (j = 0; j < stats[i].n_perc; j++) {
-	    sscanf(param.percentile->answers[j], "%i", &stats[i].perc[j]);
+	    sscanf(param.percentile->answers[j], "%i", &(stats[i].perc[j]));
 	}
     }
 
@@ -227,8 +227,6 @@
     /* use G_window_rows(), G_window_cols() here? */
     const int rows = region->rows;
     const int cols = region->cols;
-    const int n_zones = zone_info.n_zones;
-    int *first, i;
 
     const RASTER_MAP_TYPE map_type = G_get_raster_map_type(fd);
     const size_t value_sz = G_raster_size(map_type);
@@ -236,42 +234,7 @@
     void *raster_row;
     CELL *zoneraster_row;
 
-    switch (map_type) {
-	case CELL_TYPE:
-	    for (i = 0; i < n_zones; i++) {
-		stats[i].nextp
-		= ((!param.extended->answer) ? 0 : (void *)stats[i].cell_array);
-	    }
-	    break;
-
-	case FCELL_TYPE:
-	    for (i = 0; i < n_zones; i++) {
-		stats[i].nextp
-		= ((!param.extended->answer) ? 0 : (void *)stats[i].fcell_array);
-	    }
-	    break;
-
-	case DCELL_TYPE:
-	    for (i = 0; i < n_zones; i++) {
-		stats[i].nextp
-		= ((!param.extended->answer) ? 0 : (void *)stats[i].dcell_array);
-	    }
-	    break;
-
-	default:
-	    G_fatal_error("input raster has unknown type");
-	    break;
-    }
-
-    first = (int *) G_malloc((n_zones - 1) * sizeof(int));
-    {
-	int i;
-	for (i = 0; i < n_zones; i++)
-	    first[i] = (stats[i].n < 1);
-    }
-
-
-    raster_row = G_calloc(cols, value_sz);
+    raster_row = G_allocate_raster_buf(map_type);
     zoneraster_row = G_allocate_c_raster_buf();
 
     for (row = 0; row < rows; row++) {
@@ -289,18 +252,20 @@
 	zptr = zoneraster_row;
 
 	for (col = 0; col < cols; col++) {
+	    double val;
 
-	    /* skip NULL cells in zone map altogether */
+	    /* skip NULL cells in zone map */
 	    if (G_is_c_null_value(zptr)) {
 		ptr = G_incr_void_ptr(ptr, value_sz);
 		zptr++;
 		continue;
 	    }
 
-	    /* also count NULL cell in input map */
+	    /* count NULL cells in input map */
 	    zone = *zptr - zone_info.min;
 	    stats[zone].size++;
 	    
+	    /* can't do stats with NULL cells in input map */
 	    if (G_is_null_value(ptr, map_type)) {
 		ptr = G_incr_void_ptr(ptr, value_sz);
 		zptr++;
@@ -312,50 +277,52 @@
 		if (stats[zone].n >= stats[zone].n_alloc) {
 		    stats[zone].n_alloc += 1000;
 		    size_t msize;
-		    if (map_type == DCELL_TYPE) {
-			msize = stats[zone].n_alloc * sizeof(DCELL);
-			stats[zone].dcell_array =
-			    (DCELL *)G_realloc((void *)stats[zone].dcell_array, msize);
-			stats[zone].nextp = (void *)&(stats[zone].dcell_array[stats[zone].n]);
+		    switch (map_type) {
+			case DCELL_TYPE:
+			    msize = stats[zone].n_alloc * sizeof(DCELL);
+			    stats[zone].dcell_array =
+				(DCELL *)G_realloc((void *)stats[zone].dcell_array, msize);
+			    stats[zone].nextp = (void *)&(stats[zone].dcell_array[stats[zone].n]);
+			    break;
+			case FCELL_TYPE:
+			    msize = stats[zone].n_alloc * sizeof(FCELL);
+			    stats[zone].fcell_array =
+				(FCELL *)G_realloc((void *)stats[zone].fcell_array, msize);
+			    stats[zone].nextp = (void *)&(stats[zone].fcell_array[stats[zone].n]);
+			    break;
+			case CELL_TYPE:
+			    msize = stats[zone].n_alloc * sizeof(CELL);
+			    stats[zone].cell_array =
+				(CELL *)G_realloc((void *)stats[zone].cell_array, msize);
+			    stats[zone].nextp = (void *)&(stats[zone].cell_array[stats[zone].n]);
+			    break;
+			default:
+			    break;
 		    }
-		    else if (map_type == FCELL_TYPE) {
-			msize = stats[zone].n_alloc * sizeof(FCELL);
-			stats[zone].fcell_array =
-			    (FCELL *)G_realloc((void *)stats[zone].fcell_array, msize);
-			stats[zone].nextp = (void *)&(stats[zone].fcell_array[stats[zone].n]);
-		    }
-		    else if (map_type == CELL_TYPE) {
-			msize = stats[zone].n_alloc * sizeof(CELL);
-			stats[zone].cell_array =
-			    (CELL *)G_realloc((void *)stats[zone].cell_array, msize);
-			stats[zone].nextp = (void *)&(stats[zone].cell_array[stats[zone].n]);
-		    }
 		}
 		/* put the value into stats->XXXcell_array */
 		memcpy(stats[zone].nextp, ptr, value_sz);
 		stats[zone].nextp = G_incr_void_ptr(stats[zone].nextp, value_sz);
 	    }
 
-	    {
-		double val = ((map_type == DCELL_TYPE) ? *((DCELL *) ptr)
-			      : (map_type == FCELL_TYPE) ? *((FCELL *) ptr)
-			      : *((CELL *) ptr));
+	    val = ((map_type == DCELL_TYPE) ? *((DCELL *) ptr)
+			  : (map_type == FCELL_TYPE) ? *((FCELL *) ptr)
+			  : *((CELL *) ptr));
 
-		stats[zone].sum += val;
-		stats[zone].sumsq += val * val;
-		stats[zone].sum_abs += fabs(val);
+	    stats[zone].sum += val;
+	    stats[zone].sumsq += val * val;
+	    stats[zone].sum_abs += fabs(val);
 
-		if (first[zone]) {
+	    if (stats[zone].first) {
+		stats[zone].max = val;
+		stats[zone].min = val;
+		stats[zone].first = FALSE;
+	    }
+	    else {
+		if (val > stats[zone].max)
 		    stats[zone].max = val;
+		if (val < stats[zone].min)
 		    stats[zone].min = val;
-		    first[zone] = FALSE;
-		}
-		else {
-		    if (val > stats[zone].max)
-			stats[zone].max = val;
-		    if (val < stats[zone].min)
-			stats[zone].min = val;
-		}
 	    }
 
 	    ptr = G_incr_void_ptr(ptr, value_sz);

Modified: grass-addons/raster/r.univar2.zonal/stats.c
===================================================================
--- grass-addons/raster/r.univar2.zonal/stats.c	2009-02-23 08:05:45 UTC (rev 36064)
+++ grass-addons/raster/r.univar2.zonal/stats.c	2009-02-23 12:00:15 UTC (rev 36065)
@@ -44,10 +44,12 @@
 
 	stats[i].n_alloc = 0;
 
+	stats[i].first = TRUE;
+
 	/* allocate memory for extended computation */
 	/* changed to on-demand block allocation */
 
-	if (param.extended->answer) {
+/*	if (param.extended->answer) {
 	    if (map_type == DCELL_TYPE) {
 		stats[i].dcell_array = NULL;
 	    }
@@ -58,6 +60,7 @@
 		stats[i].cell_array = NULL;
 	    }
 	}
+*/
 
     }
 
@@ -122,7 +125,7 @@
 	sprintf(sum_str, "%.10f", stats[z].sum);
 	G_trim_decimal(sum_str);
 
-	fprintf(stdout, "\nzone %d %s\n\n", z + zone_info.min, G_get_c_raster_cat(&z + zone_info.min, &(zone_info.cats)));
+	fprintf(stdout, "\nzone %d %s\n\n", z + zone_info.min, G_get_cat(z + zone_info.min, &(zone_info.cats)));
 
 	if (!param.shell_style->answer) {
 	    fprintf(stdout, "total null and non-null cells: %d\n", stats[z].size);
@@ -289,12 +292,14 @@
     fprintf(stdout, "sum%s", zone_info.sep);
     fprintf(stdout, "sum_abs%s", zone_info.sep);
 
-    fprintf(stdout, "first_quart%s", zone_info.sep);
-    fprintf(stdout, "median%s", zone_info.sep);
-    fprintf(stdout, "third_quart%s", zone_info.sep);
-    for (i = 0; i < stats[0].n_perc; i++) {
-	fprintf(stdout, "perc_%d%s", stats[0].perc[i],
-		zone_info.sep);
+    if (param.extended->answer) {
+	fprintf(stdout, "first_quart%s", zone_info.sep);
+	fprintf(stdout, "median%s", zone_info.sep);
+	fprintf(stdout, "third_quart%s", zone_info.sep);
+	for (i = 0; i < stats[0].n_perc; i++) {
+	    fprintf(stdout, "perc_%d%s", stats[0].perc[i],
+		    zone_info.sep);
+	}
     }
     fprintf(stdout, "\n");
 
@@ -327,7 +332,7 @@
 	/* zone number */
 	fprintf(stdout, "%d%s", z + zone_info.min, zone_info.sep);
 	/* zone label */
-	fprintf(stdout,"%s%s", G_get_c_raster_cat(&z + zone_info.min, &(zone_info.cats)), zone_info.sep);
+	fprintf(stdout,"%s%s", G_get_cat(z + zone_info.min, &(zone_info.cats)), zone_info.sep);
 
 	/* total cells */
 	fprintf(stdout, "%d%s", stats[z].n, zone_info.sep);



More information about the grass-commit mailing list