[GRASS-SVN] r67107 - grass-addons/grass7/raster/r.out.legend

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Dec 13 13:30:40 PST 2015


Author: pvanbosgeo
Date: 2015-12-13 13:30:40 -0800 (Sun, 13 Dec 2015)
New Revision: 67107

Added:
   grass-addons/grass7/raster/r.out.legend/r_out_legend_1.png
   grass-addons/grass7/raster/r.out.legend/r_out_legend_2.png
   grass-addons/grass7/raster/r.out.legend/r_out_legend_3.png
Modified:
   grass-addons/grass7/raster/r.out.legend/r.out.legend.html
   grass-addons/grass7/raster/r.out.legend/r.out.legend.py
Log:
Added examples to manual page and now function provides size of output image

Modified: grass-addons/grass7/raster/r.out.legend/r.out.legend.html
===================================================================
--- grass-addons/grass7/raster/r.out.legend/r.out.legend.html	2015-12-13 19:56:58 UTC (rev 67106)
+++ grass-addons/grass7/raster/r.out.legend/r.out.legend.html	2015-12-13 21:30:40 UTC (rev 67107)
@@ -15,8 +15,9 @@
 <h2>NOTES</h2>
 
 The user is required to set the dimensions of the legend bar. This 
-does not include the category values, which will thus add to the size of 
-the output image. How much will depend on the size of the raster values.
+does not include the category values, which will thus add to the 
+size of the output image. The size of the output image in pixels and the
+unit set by the user is given in the console (see example 1).
 
 <p>
 Note that one can use category maps as input, but <em>
@@ -24,10 +25,9 @@
 -f flag, see <em>d.legend</em>). 
 
 <p>
-The script is a wrapper of the functions 
-<a href="https://grass.osgeo.org/grass71/manuals/d.mon.html">d.mon</a> 
-and <a href="https://grass.osgeo.org/grass71/manuals/d.legend.html"> 
-d.legend</a>. For detailed explanations of the different options, 
+The script is a wrapper of <a href="https://grass.osgeo.org/grass71/manuals/d.mon.html">d.mon</a> 
+and <a href="https://grass.osgeo.org/grass71/manuals/d.legend.html"> d.legend</a>. 
+For detailed explanations of the different options, 
 see the manual pages of these two functions. To find out the fonts 
 available on your system, you can run <a href="d.font">d.font -l</a>. For 
 a more interactive way to generate legend images, you can also try 
@@ -37,9 +37,22 @@
 The user may create a horizontal legend by making the box wider 
 than it is tall. Note that for vertical legends labels are placed to 
 the right of the legend box. For horizontal legends the text will 
-place text below. 
+be places below the legend.
 
+<p> The default resolution is 300 px/inch (ppi), except when the unit is 
+set to <em>px</em>, in which case the default resolution is set to 
+96 ppi. 
+
 <p>
+It is important to realize that setting the resolution will 
+determine the height and width of your image in pixels. It does not, 
+however, set the image print resolution. Other programs, like 
+Libreoffice, Word or the QGIS print composer will therefore not know 
+the intended image print resolution. This means you will have to set the
+image size manually in the document. The exact dimention of the output
+file is given in the console.
+
+<p>
 The <em>range</em> option lets the user define the limits of the 
 legend. Note however that the color scale will remain faithful to 
 the values as defined with r.colors.
@@ -52,14 +65,62 @@
 to be configured with CAIRO support (if you compile GRASS yourself, 
 use --with-cairo when configuring GRASS). 
 
+<h2>EXAMPLES</h2>
+
+The raster layers in the examples below are from the 
+<a href="https://grass.osgeo.org/download/sample-data/">North Carolina 
+sample data set</a>. 
+
+<h3>Example 1</h3>
+
+Note that because width > height, the legend is 
+printed horizontal, with the labels below the legend bar.
+
+<div class="code"><pre>
+r.out.legend raster=elevation file=r_out_legend_1.png filetype=png \
+    dimensions=4,0.4 labelnum=3 fontsize=7 unit="cm" resolution=150
+</pre></div
+
+<p><img src="r_out_legend_1.png">
+
 <p>
-This script was originally written to create smooth legend for 
-use in the map composer of QGIS, which does has the option (yet) to 
-include smooth (continuous) legend. For some other nice solutions, 
-see this 
-<a href="http://gis.stackexchange.com/questions/91179/continuous-color-band-for-a-raster-legend-in-qgis-composer-legend">Q & A</a> 
-at gis.stackexchange. 
+The dimensions (width and height) of the image are printed to the console.
 
+<div class="code"><pre>
+----------------------------
+File saved as r_out_legend_1.png
+The image dimensions are:
+282px wide and 46px heigh
+at a resolution of 150 ppi this is:
+4.7752 cm x 0.7874 cm
+----------------------------
+</pre></div>
+
+<h3>Example 2</h3>
+
+Same as above, but the font is set to comic and the
+background color us set to grey.
+
+<div class="code"><pre>
+r.out.legend raster=elevation file=r_out_legend_2.png filetype=png \
+    dimensions=300,20 labelnum=3 fontsize=10 unit="px" \
+    font=comic bgcolor=grey
+</pre></div>
+
+<p><img src="r_out_legend_2.png">
+
+<h3>Example 3</h3>
+
+Like example 1, but without the raster values 
+printed (done by setting fontsize to 0).
+
+<div class="code"><pre>
+r.out.legend raster=elevation file=r_out_legend_3.png filetype=png \
+    dimensions=300,20 labelnum=3 fontsize=0 unit="px"
+</pre></div>
+
+<p><img src="r_out_legend_3.png">
+
 <h2>SEE ALSO</h2>
 
 <em><a href="d.mon.html">d.mon</a></em>, 

Modified: grass-addons/grass7/raster/r.out.legend/r.out.legend.py
===================================================================
--- grass-addons/grass7/raster/r.out.legend/r.out.legend.py	2015-12-13 19:56:58 UTC (rev 67106)
+++ grass-addons/grass7/raster/r.out.legend/r.out.legend.py	2015-12-13 21:30:40 UTC (rev 67107)
@@ -3,7 +3,7 @@
 #
 ########################################################################
 #
-# MODULE:       r.legend.out
+# MODULE:       r.out.legend
 # AUTHOR(S):    Paulo van Breugel
 # DESCRIPTION:  Export the legend of a raster as image, which can be used
 #               in e.g., the map composer in QGIS.
@@ -63,7 +63,7 @@
 #% description: unit of the image dimensions
 #% key_desc: unit
 #% required: no
-#% options: cm,mm,inch
+#% options: cm,mm,inch,px
 #% answer: cm
 #% guisection: Image settings
 #%end
@@ -73,8 +73,7 @@
 #% type: integer
 #% description: resolution (dots/inch)
 #% key_desc: value
-#% required: yes
-#% answer:300
+#% required: no
 #% guisection: Image settings
 #%end
 
@@ -138,9 +137,9 @@
 
 #%option
 #% key: fontsize
-#% type: string
+#% type: integer
 #% description: Font size
-#% key_desc: float
+#% key_desc: integer
 #% required: no
 #% answer: 10
 #% guisection: Font settings
@@ -173,23 +172,30 @@
     # Variables / parameters
     outputfile    = options['file']
     filetype      = options['filetype']
+    unit          = options['unit']
+    resol         = options['resolution']
+    if resol == '':
+        if unit=='px':
+            resol=96
+        else:
+            resol=300
+    else:
+        resol = int(resol)
     dimensions    = options['dimensions']
     width, height = dimensions.split(",")
-    resol         = options['resolution']
-    unit          = options['unit']
     bgcolor       = options['bgcolor']
     inmap         = options['raster']
     labelnum      = options['labelnum']
     val_range     = options['range']
     font          = options['font']
-    fontsize      = options['fontsize']
-    digits          = int(options['digits'])
+    fontsize      = int(options['fontsize'])
+    digits        = int(options['digits'])
     flag_f        = flags['f']
 
     # Check if input layer exists
     CheckLayer(inmap)
 
-    # Compute output size
+    # Compute output size of legend bar in pixels
     if unit=='cm':
         w = math.ceil(float(width)/2.54*float(resol))
         h = math.ceil(float(height)/2.54*float(resol))
@@ -197,42 +203,56 @@
         w = math.ceil(float(width)/25.4*float(resol))
         h = math.ceil(float(height)/25.4*float(resol))
     elif unit=='inch':
-        w = math.ceil(height*resol)
+        w = math.ceil(width*resol)
         h = math.ceil(height*resol)
+    elif unit=="px":
+        w=float(width)
+        h=float(height)
     else:
-        grass.error('Unit must be inch, cm or mm')
+        grass.error('Unit must be inch, cm, mm or px')
 
-    # Compute fontsize
-    fz = round(float(fontsize) * (float(resol)/72.272))
-
-    # Compute image position
-    maprange = grass.raster_info(inmap)
-    maxval = round(maprange['max'],digits)
-    if maxval<1:
-        maxl=len(str(maxval)) -1
-    else:
-        maxl=len(str(maxval)) - 2
-
-    if float(height)>float(width):
-        iw = w + fz * maxl
+    # Check if fontsize = 0 ( = no raster values)
+    if fontsize==0:
+        iw = w
         ih = h
-        at = "1,99,1," + str((100*w/iw)-1)
+        fz = 1
+        at = "1,99,1,99"
     else:
-        margin_left = 0.5 * (len(str(maprange['min'])) - 1)
-        margin_right = 0.5 * maxl
-        iw = w + fz * (margin_left + margin_right)
-        ih = h + fz
-        at = str(100 - (100*h/ih)+5) + ",99," + \
-        str((100 * fz * margin_left / iw)) + "," + \
-        str(100 - (100 * fz * margin_right / iw))
+        fz = round(float(fontsize) * (float(resol)/72.272))
 
+        # Compute image position and position
+        maprange = grass.raster_info(inmap)
+        maxval = round(maprange['max'],digits)
+        if maxval<1:
+            maxl=len(str(maxval)) -1
+        else:
+            maxl=len(str(maxval)) - 2
+
+        if float(height)>float(width):
+            iw = w + fz * maxl
+            ih = h
+            at = "1,99,1," + str((100*w/iw)-1)
+        else:
+            minval = round(maprange['min'],digits)
+            margin_left = 0.5 * (len(str(minval)) - 1)
+            margin_right = 0.5 * maxl
+            iw = w + fz * (margin_left + margin_right)
+            ih = h + fz * 1.5
+            at = str(100 - (100*h/ih)) + ",99," + \
+            str((100 * fz * margin_left / iw)) + "," + \
+            str(100 - (100 * fz * margin_right / iw))
+
     # Open file connection, set font, write legend and close file
     grass.run_command("d.mon", start=filetype, output=outputfile, width=iw, height=ih,
                       resolution=1, bgcolor=bgcolor)
-    if flag_f:
-        flag='vsf'
+    if flag_f and fontsize==0:
+        flag='cfsv'
+    elif flag_f:
+        flag='fsv'
+    elif fontsize==0:
+        flag='csv'
     else:
-        flag="vs"
+        flag='sv'
     if val_range=='':
         grass.run_command("d.legend", flags=flag, raster=inmap, font=font,
                       at=at, fontsize=fz, labelnum=labelnum)
@@ -241,7 +261,25 @@
                       at=at, fontsize=fz, labelnum=labelnum, range=val_range)
 
     grass.run_command("d.mon", stop=filetype)
+    grass.info("----------------------------\n")
     grass.info("File saved as " + outputfile)
+    grass.info("The image dimensions are:\n")
+    grass.info(str(int(iw)) + "px wide and " + str(int(ih)) + "px heigh\n")
+    if unit=='inch':
+        wr = iw/resol
+        hr = ih/resol
+    elif unit=='cm':
+        wr = iw/resol*2.54
+        hr = ih/resol*2.54
+    elif unit=='mm':
+        wr = iw/resol*2.54*10
+        hr = ih/resol*2.54*10
+    else:
+        wr = "same"
+    if wr != "same":
+        grass.info("at a resolution of " + str(resol) + " ppi this is:")
+        grass.info(str(wr) + " " + unit + " x " + str(hr) + " " + unit + "\n")
+    grass.info("----------------------------\n")
 
 if __name__ == "__main__":
     options, flags = grass.parser()

Added: grass-addons/grass7/raster/r.out.legend/r_out_legend_1.png
===================================================================
(Binary files differ)


Property changes on: grass-addons/grass7/raster/r.out.legend/r_out_legend_1.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: grass-addons/grass7/raster/r.out.legend/r_out_legend_2.png
===================================================================
(Binary files differ)


Property changes on: grass-addons/grass7/raster/r.out.legend/r_out_legend_2.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: grass-addons/grass7/raster/r.out.legend/r_out_legend_3.png
===================================================================
(Binary files differ)


Property changes on: grass-addons/grass7/raster/r.out.legend/r_out_legend_3.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream



More information about the grass-commit mailing list