[GRASS-SVN] r59073 - grass/trunk/raster/r.in.gdal

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Feb 17 05:47:35 PST 2014


Author: mmetz
Date: 2014-02-17 05:47:34 -0800 (Mon, 17 Feb 2014)
New Revision: 59073

Modified:
   grass/trunk/raster/r.in.gdal/main.c
Log:
r.in.gdal: read category labels from raster attribute table

Modified: grass/trunk/raster/r.in.gdal/main.c
===================================================================
--- grass/trunk/raster/r.in.gdal/main.c	2014-02-17 10:05:22 UTC (rev 59072)
+++ grass/trunk/raster/r.in.gdal/main.c	2014-02-17 13:47:34 UTC (rev 59073)
@@ -1111,8 +1111,8 @@
     }
 
     /* colors in raster attribute table? */
-    
-    if (!have_colors && (gdal_rat = GDALGetDefaultRAT(hBand)) != NULL) {
+    gdal_rat = GDALGetDefaultRAT(hBand);
+    if (!have_colors && gdal_rat != NULL) {
 	nrows = GDALRATGetRowCount(gdal_rat);
 	ncols = GDALRATGetColumnCount(gdal_rat);
 	
@@ -1263,7 +1263,69 @@
 	    Rast_free_colors(&colors);
 	}
     }
+    
+    /* categories in raster attribute table? */
+    
+    if (gdal_rat != NULL) {
+	nrows = GDALRATGetRowCount(gdal_rat);
+	ncols = GDALRATGetColumnCount(gdal_rat);
+	
+	if (nrows > 0 && ncols > 0) {
+	    int minc, maxc, minmaxc, namec;
+	    GDALRATFieldUsage field_use;
+	    DCELL val1, val2;
+	    struct Categories cats;
+	    const char *label;
 
+	    minc = maxc = minmaxc = namec = -1;
+	    for (indx = 0; indx < ncols; indx++) {
+		 field_use = GDALRATGetUsageOfCol(gdal_rat, indx);
+		 
+		 if (field_use == GFU_Min)
+		    minc = indx;
+		 else if (field_use == GFU_Max)
+		    maxc = indx;
+		 else if (field_use == GFU_MinMax)
+		    minmaxc = indx;
+		 else if (field_use == GFU_Name)
+		    namec = indx;
+	    }
+
+	    if (namec >= 0 && minmaxc >= 0) {
+		Rast_init_cats("", &cats);
+
+		/* fetch labels */
+		for (indx = 0; indx < nrows; indx++) {
+		    val1 = GDALRATGetValueAsDouble(gdal_rat, indx, minmaxc);
+		    val2 = val1;
+		    label = GDALRATGetValueAsString(gdal_rat, indx, namec);
+		    
+		    if (label)
+			Rast_set_d_cat(&val1, &val2, label, &cats);
+		}
+		Rast_write_cats(output, &cats);
+
+		Rast_free_cats(&cats);
+	    }
+	    else if (namec >= 0 && minc >= 0 && maxc >= 0) {
+		Rast_init_cats("", &cats);
+
+		/* fetch labels */
+		for (indx = 0; indx < nrows; indx++) {
+		    val1 = GDALRATGetValueAsDouble(gdal_rat, indx, minc);
+		    val2 = GDALRATGetValueAsDouble(gdal_rat, indx, maxc);
+		    label = GDALRATGetValueAsString(gdal_rat, indx, namec);
+		    
+		    if (label)
+			Rast_set_d_cat(&val1, &val2, label, &cats);
+		}
+		Rast_write_cats(output, &cats);
+
+		Rast_free_cats(&cats);
+	    }
+	}
+    }
+
     G_message(_("Raster map <%s> created."), output);
 
     return;



More information about the grass-commit mailing list