[GRASS-SVN] r37981 - in grass/trunk: general/g.region lib/gis raster/r.distance raster/r.out.ascii raster/r.stats sites/s.out.ascii

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Jun 19 21:28:17 EDT 2009


Author: hamish
Date: 2009-06-19 21:28:16 -0400 (Fri, 19 Jun 2009)
New Revision: 37981

Modified:
   grass/trunk/general/g.region/printwindow.c
   grass/trunk/lib/gis/wind_format.c
   grass/trunk/raster/r.distance/report.c
   grass/trunk/raster/r.out.ascii/formspecific.c
   grass/trunk/raster/r.stats/raw_stats.c
   grass/trunk/sites/s.out.ascii/main.c
Log:
output correct precision for different projection types (bugs #654 and #335; merge from devbr6)

Modified: grass/trunk/general/g.region/printwindow.c
===================================================================
--- grass/trunk/general/g.region/printwindow.c	2009-06-20 01:21:21 UTC (rev 37980)
+++ grass/trunk/general/g.region/printwindow.c	2009-06-20 01:28:16 UTC (rev 37981)
@@ -23,7 +23,7 @@
     double longitude, latitude;
 
     if (print_flag & PRINT_SH)
-	x = -1;
+	x = G_projection() == PROJECTION_LL ? -1 : 0;
     else
 	x = window->proj;
 

Modified: grass/trunk/lib/gis/wind_format.c
===================================================================
--- grass/trunk/lib/gis/wind_format.c	2009-06-20 01:21:21 UTC (rev 37980)
+++ grass/trunk/lib/gis/wind_format.c	2009-06-20 01:28:16 UTC (rev 37981)
@@ -14,7 +14,7 @@
 #include <stdio.h>
 #include <grass/gis.h>
 
-static void format_double(double, char *);
+static void format_double(double, char *, int);
 
 /*!
  * \brief Northing to ASCII.
@@ -24,14 +24,16 @@
  *
  * \param north northing
  * \param[out] buf buffer to hold formatted string
- * \param projection projection code
+ * \param projection projection code, or -1 to force full precision FP
  */
 void G_format_northing(double north, char *buf, int projection)
 {
     if (projection == PROJECTION_LL)
 	G_lat_format(north, buf);
+    else if (projection == -1)
+	format_double(north, buf, TRUE);
     else
-	format_double(north, buf);
+	format_double(north, buf, FALSE);
 }
 
 /*!
@@ -42,14 +44,16 @@
  *
  * \param east easting
  * \param[out] buf buffer to hold formatted string
- * \param projection projection code
+ * \param projection projection code, or -1 to force full precision FP
  */
 void G_format_easting(double east, char *buf, int projection)
 {
     if (projection == PROJECTION_LL)
 	G_lon_format(east, buf);
+    else if (projection == -1)
+	format_double(east, buf, TRUE);
     else
-	format_double(east, buf);
+	format_double(east, buf, FALSE);
 }
 
 /*!
@@ -60,25 +64,30 @@
  *
  * \param resolution resolution value
  * \param[out] buf buffer to hold formatted string
- * \param projection projection code
+ * \param projection projection code, or -1 to force full precision FP
  */
 void G_format_resolution(double res, char *buf, int projection)
 {
     if (projection == PROJECTION_LL)
 	G_llres_format(res, buf);
+    else if (projection == -1)
+	format_double(res, buf, TRUE);
     else
-	format_double(res, buf);
+	format_double(res, buf, FALSE);
 }
 
-static void format_double(double value, char *buf)
+/*
+ * 'full_prec' is boolean, FALSE uses %.8f,  TRUE uses %.15g
+ * The reason to have this is that for lat/lon "%.8f" is not
+ * enough to preserve fidelity once converted back into D:M:S,
+ * which leads to rounding errors, especially for resolution.
+ */
+static void format_double(double value, char *buf, int full_prec)
 {
-    /* if the programmer has lied to G_format_resolution() about the
-        projection type in order to get FP values for lat/lon coords,
-        "%.8f" is not enough to preserve fidelity once converted
-        back into D:M:S, which leads to rounding errors. */
-    if (G_projection() == PROJECTION_LL)
+    if (full_prec)
 	sprintf(buf, "%.15g", value);
     else
 	sprintf(buf, "%.8f", value);
+
     G_trim_decimal(buf);
 }

Modified: grass/trunk/raster/r.distance/report.c
===================================================================
--- grass/trunk/raster/r.distance/report.c	2009-06-20 01:21:21 UTC (rev 37980)
+++ grass/trunk/raster/r.distance/report.c	2009-06-20 01:28:16 UTC (rev 37981)
@@ -59,13 +59,17 @@
 	    fprintf(stdout, "%s%s", fs, temp);
 
 	    /* print coordinates of the closest pair */
-	    G_format_easting(east1, temp, -1);
+	    G_format_easting(east1, temp,
+			     G_projection() == PROJECTION_LL ? -1 : 0);
 	    fprintf(stdout, "%s%s", fs, temp);
-	    G_format_northing(north1, temp, -1);
+	    G_format_northing(north1, temp,
+			      G_projection() == PROJECTION_LL ? -1 : 0);
 	    fprintf(stdout, "%s%s", fs, temp);
-	    G_format_easting(east2, temp, -1);
+	    G_format_easting(east2, temp,
+			     G_projection() == PROJECTION_LL ? -1 : 0);
 	    fprintf(stdout, "%s%s", fs, temp);
-	    G_format_northing(north2, temp, -1);
+	    G_format_northing(north2, temp,
+			      G_projection() == PROJECTION_LL ? -1 : 0);
 	    fprintf(stdout, "%s%s", fs, temp);
 
 	    /* print category labels */

Modified: grass/trunk/raster/r.out.ascii/formspecific.c
===================================================================
--- grass/trunk/raster/r.out.ascii/formspecific.c	2009-06-20 01:21:21 UTC (rev 37980)
+++ grass/trunk/raster/r.out.ascii/formspecific.c	2009-06-20 01:28:16 UTC (rev 37981)
@@ -142,12 +142,16 @@
     fprintf(fp, "%d %d\n", region.cols, region.rows);
 
     /* Projection set to -1 to force floating point output */
-    G_format_easting(region.west + region.ew_res / 2., fromc, -1);
-    G_format_easting(region.east - region.ew_res / 2., toc, -1);
+    G_format_easting(region.west + region.ew_res / 2., fromc,
+		     G_projection() == PROJECTION_LL ? -1 : 0);
+    G_format_easting(region.east - region.ew_res / 2., toc,
+		     G_projection() == PROJECTION_LL ? -1 : 0);
     fprintf(fp, "%s %s\n", fromc, toc);
 
-    G_format_northing(region.south + region.ns_res / 2., fromc, -1);
-    G_format_northing(region.north - region.ns_res / 2., toc, -1);
+    G_format_northing(region.south + region.ns_res / 2., fromc,
+		      G_projection() == PROJECTION_LL ? -1 : 0);
+    G_format_northing(region.north - region.ns_res / 2., toc,
+		      G_projection() == PROJECTION_LL ? -1 : 0);
     fprintf(fp, "%s %s\n", fromc, toc);
 
     G_get_fp_range_min_max(&range, &Z_MIN, &Z_MAX);

Modified: grass/trunk/raster/r.stats/raw_stats.c
===================================================================
--- grass/trunk/raster/r.stats/raw_stats.c	2009-06-20 01:21:21 UTC (rev 37980)
+++ grass/trunk/raster/r.stats/raw_stats.c	2009-06-20 01:28:16 UTC (rev 37981)
@@ -44,7 +44,8 @@
 	}
 
 	if (with_coordinates)
-	    G_format_northing(G_row_to_northing(row + .5, &window), nbuf, -1);
+	    G_format_northing(G_row_to_northing(row + .5, &window), nbuf, 
+			      G_projection() == PROJECTION_LL ? -1 : 0);
 
 	for (col = 0; col < ncols; col++) {
 	    if (no_nulls || no_nulls_all) {
@@ -69,7 +70,7 @@
 	    }
 	    if (with_coordinates) {
 		G_format_easting(G_col_to_easting(col + .5, &window), ebuf,
-				 -1);
+				 G_projection() == PROJECTION_LL ? -1 : 0);
 		fprintf(stdout, "%s%s%s%s", ebuf, fs, nbuf, fs);
 	    }
 	    if (with_xy)

Modified: grass/trunk/sites/s.out.ascii/main.c
===================================================================
--- grass/trunk/sites/s.out.ascii/main.c	2009-06-20 01:21:21 UTC (rev 37980)
+++ grass/trunk/sites/s.out.ascii/main.c	2009-06-20 01:28:16 UTC (rev 37981)
@@ -115,9 +115,12 @@
     while (G_site_get(fd, site) == 0) {
 	if (all || G_site_in_region(site, &window)) {
 	    if (!full) {
-		G_format_easting(site->east, ebuf, -1);
-		G_format_northing(site->north, nbuf, -1);
+		G_format_easting(site->east, ebuf,
+				 G_projection() == PROJECTION_LL ? -1 : 0);
+		G_format_northing(site->north, nbuf,
+				  G_projection() == PROJECTION_LL ? -1 : 0);
 		fprintf(stdout, "%s%s%s", ebuf, fs, nbuf);
+
 		for (n = 0; n < site->dim_alloc; ++n) {
 		    sprintf(nbuf, "%.8f", site->dim[n]);
 		    G_trim_decimal(nbuf);



More information about the grass-commit mailing list