[GRASS-SVN] r69131 - grass/trunk/raster/r.out.gdal

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Aug 11 12:08:46 PDT 2016


Author: mmetz
Date: 2016-08-11 12:08:46 -0700 (Thu, 11 Aug 2016)
New Revision: 69131

Modified:
   grass/trunk/raster/r.out.gdal/attr.c
Log:
r.out.gdal: try to keep the raster attribute table as simple as possible

Modified: grass/trunk/raster/r.out.gdal/attr.c
===================================================================
--- grass/trunk/raster/r.out.gdal/attr.c	2016-08-11 19:07:53 UTC (rev 69130)
+++ grass/trunk/raster/r.out.gdal/attr.c	2016-08-11 19:08:46 UTC (rev 69131)
@@ -54,38 +54,88 @@
      * what to do for a cell value that has a category but no color rule ?
      * what to do for a cell value that has a color rule but no category ?
      */
-    if (cats.ncats > 0) { /*  && rcount == 0 */
-	/* create new raster attribute table */
-	hrat = GDALCreateRasterAttributeTable();
-	
-	if (maptype == CELL_TYPE) {
-	    GDALRATCreateColumn(hrat, "min", GFT_Integer, GFU_Min);
-	    GDALRATCreateColumn(hrat, "max", GFT_Integer, GFU_Max);
-	}
-	else {
-	    GDALRATCreateColumn(hrat, "min", GFT_Real, GFU_Min);
-	    GDALRATCreateColumn(hrat, "max", GFT_Real, GFU_Max);
-	}
-	GDALRATCreateColumn(hrat, "label", GFT_String, GFU_Name);
+    if (cats.ncats > 0) {
+	int use_minmax = 0;
 
-	GDALRATSetRowCount(hrat, cats.ncats);
-
 	if (maptype == CELL_TYPE) {
 	    for (i = 0; i < cats.ncats; i++) {
 		label = Rast_get_ith_c_cat(&cats, i, &CellMin, &CellMax);
-		GDALRATSetValueAsInt(hrat, i, 0, CellMin);
-		GDALRATSetValueAsInt(hrat, i, 1, CellMax);
-		GDALRATSetValueAsString(hrat, i, 2, label);
+		if (CellMin != CellMax) {
+		    use_minmax = 1;
+		    break;
+		}
 	    }
 	}
 	else {
 	    for (i = 0; i < cats.ncats; i++) {
 		label = Rast_get_ith_d_cat(&cats, i, &dfCellMin, &dfCellMax);
-		GDALRATSetValueAsDouble(hrat, i, 0, dfCellMin);
-		GDALRATSetValueAsDouble(hrat, i, 1, dfCellMax);
-		GDALRATSetValueAsString(hrat, i, 2, label);
+		if (dfCellMin != dfCellMax) {
+		    use_minmax = 1;
+		    break;
+		}
 	    }
 	}
+
+	/* create new raster attribute table */
+	hrat = GDALCreateRasterAttributeTable();
+	
+	if (use_minmax) {
+	    if (maptype == CELL_TYPE) {
+		GDALRATCreateColumn(hrat, "min", GFT_Integer, GFU_Min);
+		GDALRATCreateColumn(hrat, "max", GFT_Integer, GFU_Max);
+	    }
+	    else {
+		GDALRATCreateColumn(hrat, "min", GFT_Real, GFU_Min);
+		GDALRATCreateColumn(hrat, "max", GFT_Real, GFU_Max);
+	    }
+	    GDALRATCreateColumn(hrat, "label", GFT_String, GFU_Name);
+
+	    GDALRATSetRowCount(hrat, cats.ncats);
+
+	    if (maptype == CELL_TYPE) {
+		for (i = 0; i < cats.ncats; i++) {
+		    label = Rast_get_ith_c_cat(&cats, i, &CellMin, &CellMax);
+		    GDALRATSetValueAsInt(hrat, i, 0, CellMin);
+		    GDALRATSetValueAsInt(hrat, i, 1, CellMax);
+		    GDALRATSetValueAsString(hrat, i, 2, label);
+		}
+	    }
+	    else {
+		for (i = 0; i < cats.ncats; i++) {
+		    label = Rast_get_ith_d_cat(&cats, i, &dfCellMin, &dfCellMax);
+		    GDALRATSetValueAsDouble(hrat, i, 0, dfCellMin);
+		    GDALRATSetValueAsDouble(hrat, i, 1, dfCellMax);
+		    GDALRATSetValueAsString(hrat, i, 2, label);
+		}
+	    }
+	}
+	else {
+	    if (maptype == CELL_TYPE) {
+		GDALRATCreateColumn(hrat, "value", GFT_Integer, GFU_MinMax);
+	    }
+	    else {
+		GDALRATCreateColumn(hrat, "value", GFT_Real, GFU_MinMax);
+	    }
+	    GDALRATCreateColumn(hrat, "label", GFT_String, GFU_Name);
+
+	    GDALRATSetRowCount(hrat, cats.ncats);
+
+	    if (maptype == CELL_TYPE) {
+		for (i = 0; i < cats.ncats; i++) {
+		    label = Rast_get_ith_c_cat(&cats, i, &CellMin, &CellMax);
+		    GDALRATSetValueAsInt(hrat, i, 0, CellMin);
+		    GDALRATSetValueAsString(hrat, i, 1, label);
+		}
+	    }
+	    else {
+		for (i = 0; i < cats.ncats; i++) {
+		    label = Rast_get_ith_d_cat(&cats, i, &dfCellMin, &dfCellMax);
+		    GDALRATSetValueAsDouble(hrat, i, 0, dfCellMin);
+		    GDALRATSetValueAsString(hrat, i, 1, label);
+		}
+	    }
+	}
+
 	if (GDALSetDefaultRAT(hBand, hrat) != CE_None) {
 	    G_warning(_("Failed to set raster attribute table"));
 	    ret = -1;



More information about the grass-commit mailing list