[GRASS-SVN] r66668 - grass-addons/grass7/raster/r.category.trim

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Oct 30 01:29:48 PDT 2015


Author: pvanbosgeo
Date: 2015-10-30 01:29:48 -0700 (Fri, 30 Oct 2015)
New Revision: 66668

Modified:
   grass-addons/grass7/raster/r.category.trim/r.category.trim.html
   grass-addons/grass7/raster/r.category.trim/r.category.trim.py
Log:
Some small corrections and code clean up

Modified: grass-addons/grass7/raster/r.category.trim/r.category.trim.html
===================================================================
--- grass-addons/grass7/raster/r.category.trim/r.category.trim.html	2015-10-29 17:08:15 UTC (rev 66667)
+++ grass-addons/grass7/raster/r.category.trim/r.category.trim.html	2015-10-30 08:29:48 UTC (rev 66668)
@@ -1,5 +1,6 @@
 <h2>DESCRIPTION</h2>
 
+<p><b>Trim redundant category labels and colors</b>
 <p>In GRASS you can 'cut out' a subset of a larger raster layer by 
 setting the extent to something smaller then the extent of the 
 original map using g.region and/or by setting a mask using r.mask, 
@@ -18,6 +19,7 @@
 present in the new map. You can do this on the input map, or do this 
 on a copy of the map.
 
+<p><b>Recode to consecutive category values</b>
 <p>If you prefer the map to have consecutive values (i.e., without 
 gaps), there is the option to change the category values to a 
 consecutive series by setting the n-flag. For example, if the map 
@@ -36,28 +38,38 @@
 3 label9
 </pre></div>
 
-<p>Although it is possible to <a href= 
-"https://pvanb.wordpress.com/2015/10/18/the-qgis-grass-plugin-is-back/
-">open GRASS GIS raster layers directly in QGIS</a>, the legend in 
-QGIS will show the right colours, but not the category labels. The 
-addon let's you export the categories, category labels and colours 
-as QGIS colour map file. This is just a simple text file with the 
-raster categories and corresponding colour definitions and category 
-labels. QGIS can use this to set the colour and labels for a raster 
-layer. See <a href="https://pvanb.wordpress.com/?p=3650">this 
-blogpost</a> for more details how to use the colour map file in 
-QGIS. Alternatively, you can also export the categories and category 
-labels in a normal csv file.
+<p><b>Export QGIS color map file</b> <p>The addon let's you export 
+the categories, category labels and colours as QGIS colour map file. 
+This is just a simple text file with the raster categories and 
+corresponding colour definitions and category labels. QGIS can use 
+this to set the colour and labels for a raster layer. See <a href= 
+"https://pvanb.wordpress.com/grass-gis-categorical-raster-layers-in-qg
+is">this blogpost</a> for more details how to use the colour map 
+file in QGIS. Alternatively, you can also export the categories and 
+category labels as a normal comma separated calues (CSV) file file, 
+which can be easily viewed in a spreadsheet program such as 
+Libre/Open Office Calc, Microsoft Excel, or Google Docs and can be 
+easily shared together with an exported raster file for users that 
+use other GIS programs
 
 
-<h2>NOTE</h2>
-The file is only useful for categorical maps. Therefore only integer maps are accepted as input.
-    
-<h2>SEE ALSO</h2>
+<h2>NOTE</h2> The file is only useful for categorical maps. 
+Therefore only integer maps are accepted as input.
 
-<a href="https://pvanb.wordpress.com/?p=3650">GRASS GIS categorical raster layers in QGIS </a>
-<a href="https://pvanb.wordpress.com/2015/09/22/categorical-maps-and-legends/">GRASS GIS categorical maps and legends</a>
+<p>When you use the option to recode the map, you need to set an 
+output map as well. It uses the <a href="r.recode">r.recode </a> 
+function, with the 'a' flag set, i.e., the region is 
+aligned to the input raster map.
+   
+<h2>Examples</h2>
+See <a href="https://pvanb.wordpress.com/grass-gis-categorical-raster-layers-in-qgis">here</a> for examples
 
+<h2>See also</h2>
+<a href="r.recode.html">r.recode</a>,
+<a href="r.category.html">r.category</a>,
+<a href="r.colors.html">r.colors</a>,
+<a href="r.describe.html">r.describe</a>
+
 <h2>AUTHOR</h2>
 
 Paulo van Breugel, paulo at ecodiv.org

Modified: grass-addons/grass7/raster/r.category.trim/r.category.trim.py
===================================================================
--- grass-addons/grass7/raster/r.category.trim/r.category.trim.py	2015-10-29 17:08:15 UTC (rev 66667)
+++ grass-addons/grass7/raster/r.category.trim/r.category.trim.py	2015-10-30 08:29:48 UTC (rev 66668)
@@ -8,11 +8,9 @@
 # DESCRIPTION:  Trim a cut out raster layer by removing non-existing categories
 #               and their color defintions. Optionally, a new map can be
 #               created to ensure consecutive categories values, but retaining
-#               the category labels and colors.
-#               The user can opt to export the categories, category labels and
-#               color codes (RGB) as csv file or as a QGIS color map file. The
-#               latter can be used in QGIS to define the colors and category
-#               labels of the raster.
+#               the category labels and colors. The user als has the option to
+#               export the categories, category labels and color codes (RGB) as
+#               csv file or as a QGIS color map file.
 #
 # COPYRIGHT: (C) 2015 Paulo van Breugel
 #            http://ecodiv.org
@@ -25,7 +23,7 @@
 ########################################################################
 #
 #%Module
-#% description: Removing non-existing categories and their color defintions
+#% description: Trim redundant category labels and colors and export as QGIS colour file
 #% keyword: raster
 #% keyword: color
 #% keyword: category
@@ -79,12 +77,6 @@
 #% requires_all: -n, output
 #%end
 
-#----------------------------------------------------------------------------
-#Test
-#----------------------------------------------------------------------------
-#options = {"input":"test", "output":"test3", "csv":"test.csv", "qgis":"qgiscolor.txt"}
-#flags = {"n":True}
-
 #=======================================================================
 ## General
 #=======================================================================
@@ -116,30 +108,27 @@
     if iscell != u'CELL':
         grass.error('Input should be an integer raster layer')
 
-    # Get category values that are actually in the map, and their category
-    # labels
+    # Get map category values and their labels
     CATV = Module('r.category', map=IP, stdout_=PIPE).outputs.stdout
     RCAT = CATV.split('\n')
     RCAT = filter(None,RCAT)
     RID = [z.split('\t')[0] for z in RCAT]
     RIDI = map(int, RID)
 
-    # Get list of all colors in the color table (i.e., this includes
-    # colors defined for categories that are not in the map
+    # Get full color table
     RCOL = grass.read_command("r.colors.out", map=IP).split('\n')
     RCOL = [ x for x in RCOL if "nv" not in x and 'default' not in x]
     RCOL = filter(None, RCOL)
     CCAT = [z.split(' ')[0] for z in RCOL]
     CCAT = map(int, CCAT)
-    CR = ""
-    CV = []
 
-    # If attribute values should be recoded to consecutive numbers
+    # Set strings / list to be used in loop
+    CR = ""; RECO = ""; CL = ""; CV = []
+
+    # recode to consecutive category values
     if flags_n:
         RIDN = range(1, len(RID) + 1)
-        RECO = ""
-        CL = ""
-        RLAB = [z.split('\t')[1] for z in RCAT] # Category labels
+        RLAB = [z.split('\t')[1] for z in RCAT]
         for j in xrange(len(RID)):
             RECO = RECO + RID[j] + ":" + RID[j] + ":" + str(RIDN[j]) + "\n"
             A = map(int, [i for i, x in enumerate(CCAT) if x == RIDI[j]])
@@ -148,7 +137,7 @@
             CL = CL + str(RIDN[j]) + "|" + RLAB[j] + "\n"
 
         CR = CR + 'nv 255:255:255\ndefault 255:255:255\n'
-        Module("r.recode", input=IP, output=OP, rules="-", stdin_=RECO, quiet=True)
+        Module("r.recode", flags="a", input=IP, output=OP, rules="-", stdin_=RECO, quiet=True)
         Module("r.colors", map=OP, rules="-", stdin_=CR, quiet=True)
         Module("r.category", map=OP, rules="-", stdin_=CL, quiet=True, separator="pipe")
     else:
@@ -193,7 +182,7 @@
         else:
             RCAT = [w.replace('\t', ',') for w in RCAT]
         text_file = open(QGIS, "w")
-        text_file.write("# QGIS color map for " + OP + " raster, generated by r.category.trim\n")
+        text_file.write("# QGIS color map for " + OP + "\n")
         text_file.write("INTERPOLATION:EXACT\n")
         for k in xrange(len(RCAT)):
             RC2 = RCAT[k].split(',')



More information about the grass-commit mailing list