[GRASS-SVN] r58985 - grass/trunk/raster/r.stats

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Feb 10 17:20:21 PST 2014


Author: hamish
Date: 2014-02-10 17:20:21 -0800 (Mon, 10 Feb 2014)
New Revision: 58985

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

Modified: grass/trunk/raster/r.stats/main.c
===================================================================
--- grass/trunk/raster/r.stats/main.c	2014-02-11 01:11:14 UTC (rev 58984)
+++ grass/trunk/raster/r.stats/main.c	2014-02-11 01:20:21 UTC (rev 58985)
@@ -220,9 +220,11 @@
     flag.i->description = _("Read floating-point map as integer (use map's quant rules)");
     flag.i->guisection = _("Floating point");
 
+
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
+
     name = option.output->answer;
     if (name != NULL && strcmp(name, "-") != 0) {
 	if (NULL == freopen(name, "w", stdout)) {
@@ -277,7 +279,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 */
     fs = G_option_to_separator(option.fs);
@@ -296,6 +298,7 @@
 	DMIN = (DCELL *) G_realloc(DMIN, (nfiles + 1) * sizeof(DCELL));
 
 	fd[nfiles] = Rast_open_old(name, "");
+
 	if (!as_int)
 	    is_fp[nfiles] = Rast_map_is_fp(name, "");
 	else {

Modified: grass/trunk/raster/r.stats/raw_stats.c
===================================================================
--- grass/trunk/raster/r.stats/raw_stats.c	2014-02-11 01:11:14 UTC (rev 58984)
+++ grass/trunk/raster/r.stats/raw_stats.c	2014-02-11 01:20:21 UTC (rev 58985)
@@ -1,6 +1,7 @@
 #include <stdlib.h>
 #include <grass/gis.h>
 #include <grass/raster.h>
+#include <grass/glocale.h>
 #include "global.h"
 
 int raw_stats(int fd[], int with_coordinates, int with_xy, int with_labels)
@@ -18,13 +19,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] = Rast_get_map_type(fd[i]);
 	else
 	    map_type[i] = CELL_TYPE;
+
 	rast[i] = Rast_allocate_buf(map_type[i]);
     }
 
@@ -90,17 +94,29 @@
 			fprintf(stdout, "%s%s", fs,
 				Rast_get_c_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,
+				Rast_get_f_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,
 				Rast_get_d_cat((DCELL *) rastp[i],
 						   &labels[i]));
 		}
+		else
+		    G_fatal_error(_("Invalid map type"));
+
 		rastp[i] =
 		    G_incr_void_ptr(rastp[i], Rast_cell_size(map_type[i]));
 	    }



More information about the grass-commit mailing list