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

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Dec 13 06:59:36 PST 2015


Author: pvanbosgeo
Date: 2015-12-13 06:59:36 -0800 (Sun, 13 Dec 2015)
New Revision: 67102

Added:
   grass-addons/grass7/raster/r.out.legend/Makefile
   grass-addons/grass7/raster/r.out.legend/r.out.legend.html
   grass-addons/grass7/raster/r.out.legend/r.out.legend.py
Log:
New addon to export/create an image with the legend of a raster layer (wrapper of d.mon and d.legend)

Added: grass-addons/grass7/raster/r.out.legend/Makefile
===================================================================
--- grass-addons/grass7/raster/r.out.legend/Makefile	                        (rev 0)
+++ grass-addons/grass7/raster/r.out.legend/Makefile	2015-12-13 14:59:36 UTC (rev 67102)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.out.legend
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script


Property changes on: grass-addons/grass7/raster/r.out.legend/Makefile
___________________________________________________________________
Added: svn:mime-type
   + text/x-makefile
Added: svn:eol-style
   + native

Added: grass-addons/grass7/raster/r.out.legend/r.out.legend.html
===================================================================
--- grass-addons/grass7/raster/r.out.legend/r.out.legend.html	                        (rev 0)
+++ grass-addons/grass7/raster/r.out.legend/r.out.legend.html	2015-12-13 14:59:36 UTC (rev 67102)
@@ -0,0 +1,76 @@
+<h2>DESCRIPTION</h2>
+
+The <em>r.out.legend</em> script provides a convenient way 
+to quickly create a smoothed legend image for floating point raster maps 
+(continous values as opposed to categories), with the dimensions and 
+resolution required. It is furthermore possible to set the font (default 
+is 'Arial') and font size (default=10). The legend can be saved as 
+<a href="https://grass.osgeo.org/grass64/manuals/pngdriver.html">PNG</a>, 
+<a href="https://grass.osgeo.org/grass64/manuals/psdriver.html">PS</a> 
+(postscript), BMP, PPM, PDF AND SVG. The latter four are supported through the 
+Cairo driver (see Notes). The images can be used to add a continuous 
+legends in maps created in e.g., QGIS composer or as part of a 
+Google Earth map.
+
+<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.
+
+<p>
+Note that one can use category maps as input, but <em>
+r.out.legend</em> will still create a smooth legend (by setting the 
+-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, 
+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 
+<a href="https://grass.osgeo.org/grass70/manuals/ps.map.html">ps.map</a>.
+
+<p>
+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. 
+
+<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.
+
+<p>
+The <a href="https://grass.osgeo.org/grass64/manuals/cairodriver.html">Cairo driver</a> 
+generates PNG, BMP, PPM, PS, PDF or SVG images 
+using the Cairo graphics library. The image format is selected from 
+the extension of the output file. For this option to work, GRASS has 
+to be configured with CAIRO support (if you compile GRASS yourself, 
+use --with-cairo when configuring GRASS). 
+
+<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. 
+
+<h2>SEE ALSO</h2>
+
+<em><a href="d.mon.html">d.mon</a></em>, 
+<em><a href="d.legend.html">d.legend</a></em>, 
+<em><a href="d.font.html">d.font</a></em>,
+<em><a href="d.out.file.html">d.out.file</a></em>,
+<em><a href="ps.map.html">ps.map</a></em>,
+<em><a href="r.category.trim">r.category.trim</a></em>
+
+<h2>AUTHOR</h2>
+
+Paulo van Breugel, paulo at ecodiv.org
+
+<p><em>Last changed: $Date$</em>


Property changes on: grass-addons/grass7/raster/r.out.legend/r.out.legend.html
___________________________________________________________________
Added: svn:mime-type
   + text/html
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Added: grass-addons/grass7/raster/r.out.legend/r.out.legend.py
===================================================================
--- grass-addons/grass7/raster/r.out.legend/r.out.legend.py	                        (rev 0)
+++ grass-addons/grass7/raster/r.out.legend/r.out.legend.py	2015-12-13 14:59:36 UTC (rev 67102)
@@ -0,0 +1,258 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+########################################################################
+#
+# MODULE:       r.legend.out
+# 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.
+#
+# COPYRIGHT: (C) 2015 Paulo van Breugel
+#            http://ecodiv.org
+#            http://pvanb.wordpress.com/
+#
+#            This program is free software under the GNU General Public
+#            License (>=v2). Read the file COPYING that comes with GRASS
+#            for details.
+#
+########################################################################
+#
+#%Module
+#% description: Create and export image file with legend of a raster map
+#% keyword: raster
+#% keyword: color
+#% keyword: color table
+#% keyword: image
+#%End
+
+#%option G_OPT_R_MAP
+#% key: raster
+#%end
+
+#%option G_OPT_F_OUTPUT
+#% key:file
+#% description: Name of the output file
+#% key_desc: name
+#%end
+
+#%option
+#% key: filetype
+#% type: string
+#% description: File type
+#% key_desc: extension
+#% options: png,ps,cairo
+#% required: yes
+#% multiple: no
+#%end
+
+#------------------------------------------------------------------------------
+
+#%option
+#% key: dimensions
+#% type: string
+#% description: width and height of the color legend
+#% key_desc: width,height
+#% required: yes
+#% guisection: Image settings
+#%end
+
+#%option
+#% key: unit
+#% type: string
+#% description: unit of the image dimensions
+#% key_desc: unit
+#% required: no
+#% options: cm,mm,inch
+#% answer: cm
+#% guisection: Image settings
+#%end
+
+#%option
+#% key: resolution
+#% type: integer
+#% description: resolution (dots/inch)
+#% key_desc: value
+#% required: yes
+#% answer:300
+#% guisection: Image settings
+#%end
+
+#%option G_OPT_CN
+#% key: bgcolor
+#% description: background colour
+#% key_desc: name
+#% required: no
+#% answer: none
+#% guisection: Image settings
+#%end
+
+#------------------------------------------------------------------------------
+
+#%option
+#% key: labelnum
+#% type: integer
+#% description: Number of text labels
+#% key_desc: integer
+#% required: no
+#% answer: 5
+#% guisection: Extra options
+#%end
+
+#%option
+#% key: range
+#% type: string
+#% description: Use a subset of the map range for the legend
+#% key_desc: min,max
+#% required: no
+#% guisection: Extra options
+#%end
+
+#%option
+#% key: digits
+#% type: integer
+#% description: Maximum number of digits for raster value display
+#% key_desc: integer
+#% required: no
+#% answer: 1
+#%end
+
+#%flag:
+#% key: f
+#% description: Flip legend
+#% guisection: Extra options
+#%end
+
+
+#------------------------------------------------------------------------------
+
+#%option
+#% key: font
+#% type: string
+#% description: Font name
+#% key_desc: string
+#% required: no
+#% answer: Arial
+#% guisection: Font settings
+#%end
+
+#%option
+#% key: fontsize
+#% type: string
+#% description: Font size
+#% key_desc: float
+#% required: no
+#% answer: 10
+#% guisection: Font settings
+#%end
+
+#=======================================================================
+## General
+#=======================================================================
+
+# import libraries
+import os
+import sys
+import math
+import grass.script as grass
+
+# Check if running in GRASS
+if not os.environ.has_key("GISBASE"):
+    grass.message("You must be in GRASS GIS to run this program.")
+    sys.exit(1)
+
+# Check if layers exist
+def CheckLayer(raster):
+    ffile = grass.find_file(raster, element = 'cell')
+    if ffile['fullname'] == '':
+        grass.fatal("The layer " + raster + " does not exist.")
+
+# main function
+def main():
+
+    # Variables / parameters
+    outputfile    = options['file']
+    filetype      = options['filetype']
+    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'])
+    flag_f        = flags['f']
+
+    # Check if input layer exists
+    CheckLayer(inmap)
+
+    # Compute output size
+    if unit=='cm':
+        w = math.ceil(float(width)/2.54*float(resol))
+        h = math.ceil(float(height)/2.54*float(resol))
+    elif unit=='mm':
+        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)
+        h = math.ceil(height*resol)
+    else:
+        grass.error('Unit must be inch, cm or mm')
+
+    # 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
+        ih = h
+        at = "1,99,1," + str((100*w/iw)-1)
+    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))
+
+    # 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'
+    else:
+        flag="vs"
+    if val_range=='':
+        grass.run_command("d.legend", flags=flag, raster=inmap, font=font,
+                      at=at, fontsize=fz, labelnum=labelnum)
+    else:
+        grass.run_command("d.legend", flags=flag, raster=inmap, font=font,
+                      at=at, fontsize=fz, labelnum=labelnum, range=val_range)
+
+    grass.run_command("d.mon", stop=filetype)
+    grass.info("File saved as " + outputfile)
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    sys.exit(main())
+
+
+
+
+
+
+
+
+
+


Property changes on: grass-addons/grass7/raster/r.out.legend/r.out.legend.py
___________________________________________________________________
Added: svn:executable
   + *
Added: svn:mime-type
   + text/x-python
Added: svn:eol-style
   + native



More information about the grass-commit mailing list