[GRASS-SVN] r72961 - grass/trunk/raster/r.out.gdal
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Jul 7 13:09:46 PDT 2018
Author: mmetz
Date: 2018-07-07 13:09:46 -0700 (Sat, 07 Jul 2018)
New Revision: 72961
Modified:
grass/trunk/raster/r.out.gdal/export_band.c
grass/trunk/raster/r.out.gdal/local_proto.h
grass/trunk/raster/r.out.gdal/main.c
Log:
r.out.gdal: improve range checks for float and double
Modified: grass/trunk/raster/r.out.gdal/export_band.c
===================================================================
--- grass/trunk/raster/r.out.gdal/export_band.c 2018-07-06 13:16:21 UTC (rev 72960)
+++ grass/trunk/raster/r.out.gdal/export_band.c 2018-07-07 20:09:46 UTC (rev 72961)
@@ -229,6 +229,7 @@
int rows = cellhead->rows;
int ret = 0;
char value[200];
+ char *units;
/* Open GRASS raster */
fd = Rast_open_old(name, mapset);
@@ -244,6 +245,10 @@
if (!no_metadata)
GDALSetDescription(hBand, name);
+ units = Rast_read_units(name, mapset);
+ if (units)
+ GDALSetRasterUnitType(hBand, units);
+
/* Get min/max values. */
if (Rast_read_fp_range(name, mapset, &sRange) == -1) {
bHaveMinMax = FALSE;
@@ -536,9 +541,11 @@
case GDT_Float32:
case GDT_CFloat32:
- if (min < TYPE_FLOAT32_MIN || max > TYPE_FLOAT32_MAX) {
+ /* support export of inf / -inf ? */
+ if ((float)min !=TYPE_FLOAT32_MIN && (float)max != TYPE_FLOAT32_MAX &&
+ (min < TYPE_FLOAT32_MIN || max > TYPE_FLOAT32_MAX)) {
G_warning(_("Selected GDAL datatype does not cover data range."));
- G_warning(_("GDAL datatype: %s, range: %g - %g"),
+ G_warning(_("GDAL datatype: %s, range: %.7g - %.7g"),
GDALGetDataTypeName(datatype), TYPE_FLOAT32_MIN,
TYPE_FLOAT32_MAX);
G_warning(_("Raster map <%s> range: %g - %g"), name, min, max);
@@ -550,9 +557,10 @@
case GDT_Float64:
case GDT_CFloat64:
/* not possible because DCELL is FLOAT64, not 128bit floating point, but anyway... */
+ /* support export of inf / -inf ? */
if (min < TYPE_FLOAT64_MIN || max > TYPE_FLOAT64_MAX) {
G_warning(_("Selected GDAL datatype does not cover data range."));
- G_warning(_("GDAL datatype: %s, range: %g - %g"),
+ G_warning(_("GDAL datatype: %s, range: %.16g - %.16g"),
GDALGetDataTypeName(datatype), TYPE_FLOAT64_MIN,
TYPE_FLOAT64_MAX);
G_warning(_("Raster map <%s> range: %g - %g"), name, min, max);
Modified: grass/trunk/raster/r.out.gdal/local_proto.h
===================================================================
--- grass/trunk/raster/r.out.gdal/local_proto.h 2018-07-06 13:16:21 UTC (rev 72960)
+++ grass/trunk/raster/r.out.gdal/local_proto.h 2018-07-07 20:09:46 UTC (rev 72961)
@@ -2,6 +2,8 @@
#define __LOCAL_PROTO_H__
#include <gdal.h>
+#include <math.h>
+/* is it safe to #include <float.h> ? */
/* range limits */
/*
@@ -36,8 +38,9 @@
#define TYPE_FLOAT32_MIN (-MAXFLOAT)
#define TYPE_FLOAT32_MAX MAXFLOAT
#else
-#define TYPE_FLOAT32_MIN -3.4E38f
-#define TYPE_FLOAT32_MAX 3.4E38f
+/* formula from float.h */
+#define TYPE_FLOAT32_MIN (float)(-((1 + (1 - pow(2, -23))) * pow(2, 127)))
+#define TYPE_FLOAT32_MAX (float)((1 + (1 - pow(2, -23))) * pow(2, 127))
#endif
#ifdef DBL_MAX
@@ -47,8 +50,9 @@
#define TYPE_FLOAT64_MIN (-MAXDOUBLE)
#define TYPE_FLOAT64_MAX MAXDOUBLE
#else
-#define TYPE_FLOAT64_MIN -1.79E308
-#define TYPE_FLOAT64_MAX 1.79E308
+/* formula from float.h */
+#define TYPE_FLOAT64_MIN (-((1 + (1 - pow(2, -52))) * pow(2, 1023)))
+#define TYPE_FLOAT64_MAX ((1 + (1 - pow(2, -52))) * pow(2, 1023))
#endif
#define GRASS_MAX_COLORS TYPE_UINT16_MAX /* ok? */
Modified: grass/trunk/raster/r.out.gdal/main.c
===================================================================
--- grass/trunk/raster/r.out.gdal/main.c 2018-07-06 13:16:21 UTC (rev 72960)
+++ grass/trunk/raster/r.out.gdal/main.c 2018-07-07 20:09:46 UTC (rev 72961)
@@ -775,7 +775,7 @@
if ((!CPLIsInf(max) && max < TYPE_FLOAT32_MIN) ||
(!CPLIsInf(min) && min > TYPE_FLOAT32_MAX)) {
G_warning(_("Selected GDAL datatype does not cover data range."));
- G_warning(_("GDAL datatype: %s, range: %g - %g"),
+ G_warning(_("GDAL datatype: %s, range: %.7g - %.7g"),
GDALGetDataTypeName(datatype), TYPE_FLOAT32_MIN,
TYPE_FLOAT32_MAX);
G_warning(_("Range to be exported: %g - %g"), min, max);
More information about the grass-commit
mailing list