[GRASS-SVN] r58986 - grass/branches/develbranch_6/raster/r.stats

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Feb 10 17:22:49 PST 2014


Author: hamish
Date: 2014-02-10 17:22:49 -0800 (Mon, 10 Feb 2014)
New Revision: 58986

Modified:
   grass/branches/develbranch_6/raster/r.stats/main.c
   grass/branches/develbranch_6/raster/r.stats/raw_stats.c
Log:
export FP data values with better precision (#335, merge from trunk)

Modified: grass/branches/develbranch_6/raster/r.stats/main.c
===================================================================
--- grass/branches/develbranch_6/raster/r.stats/main.c	2014-02-11 01:20:21 UTC (rev 58985)
+++ grass/branches/develbranch_6/raster/r.stats/main.c	2014-02-11 01:22:49 UTC (rev 58986)
@@ -199,9 +199,11 @@
     flag.i->key = 'i';
     flag.i->description = _("Read fp map as integer (use map's quant rules)");
 
+
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
+
     name = option.output->answer;
     if (name != NULL && strcmp(name, "-") != 0) {
 	if (NULL == freopen(name, "w", stdout)) {
@@ -240,7 +242,7 @@
     with_coordinates = flag.g->answer;
     with_xy = flag.x->answer;
     if (with_coordinates || with_xy)
-	raw_data = 1;
+	raw_data = TRUE;
 
     /* get field separator */
     strcpy(fs, " ");
@@ -276,6 +278,7 @@
 	fd[nfiles] = G_open_cell_old(name, mapset);
 	if (fd[nfiles] < 0)
 	    exit(1);
+
 	if (!as_int)
 	    is_fp[nfiles] = G_raster_map_is_fp(name, mapset);
 	else {

Modified: grass/branches/develbranch_6/raster/r.stats/raw_stats.c
===================================================================
--- grass/branches/develbranch_6/raster/r.stats/raw_stats.c	2014-02-11 01:20:21 UTC (rev 58985)
+++ grass/branches/develbranch_6/raster/r.stats/raw_stats.c	2014-02-11 01:22:49 UTC (rev 58986)
@@ -1,5 +1,6 @@
 #include <stdlib.h>
 #include <grass/gis.h>
+#include <grass/glocale.h>
 #include "global.h"
 
 int raw_stats(int fd[], int with_coordinates, int with_xy, int with_labels)
@@ -17,13 +18,16 @@
     /* allocate i/o buffers for each raster map */
     rast = (void **)G_calloc(nfiles, sizeof(void *));
     rastp = (void **)G_calloc(nfiles, sizeof(void *));
+
     map_type = (RASTER_MAP_TYPE *) G_calloc(nfiles, sizeof(RASTER_MAP_TYPE));
+
     for (i = 0; i < nfiles; i++) {
 	/* if fp map and report real data, not cat indexes, set type to DCELL */
 	if (is_fp[i] && !raw_output && !as_int)
-	    map_type[i] = DCELL_TYPE;
+	    map_type[i] = G_get_raster_map_type(fd[i]);
 	else
 	    map_type[i] = CELL_TYPE;
+
 	rast[i] = G_allocate_raster_buf(map_type[i]);
     }
 
@@ -90,17 +94,29 @@
 			fprintf(stdout, "%s%s", fs,
 				G_get_cat(*((CELL *) rastp[i]), &labels[i]));
 		}
-		else {		/* floating point cell */
-
-		    sprintf(str1, "%.10f", *((DCELL *) rastp[i]));
+		else if (map_type[i] == FCELL_TYPE) {
+		    sprintf(str1, "%.8g", *((FCELL *) rastp[i]));
 		    G_trim_decimal(str1);
 		    G_strip(str1);
 		    fprintf(stdout, "%s%s", i ? fs : "", str1);
 		    if (with_labels)
 			fprintf(stdout, "%s%s", fs,
+				G_get_f_raster_cat((FCELL *) rastp[i],
+						   &labels[i]));
+		}
+		else if (map_type[i] == DCELL_TYPE) {
+		    sprintf(str1, "%.16g", *((DCELL *) rastp[i]));
+		    G_trim_decimal(str1);
+		    G_strip(str1);
+		    fprintf(stdout, "%s%s", i ? fs : "", str1);
+		    if (with_labels)
+			fprintf(stdout, "%s%s", fs,
 				G_get_d_raster_cat((DCELL *) rastp[i],
 						   &labels[i]));
 		}
+		else
+		    G_fatal_error(_("Invalid map type"));
+
 		rastp[i] =
 		    G_incr_void_ptr(rastp[i], G_raster_size(map_type[i]));
 	    }



More information about the grass-commit mailing list