[GRASS-SVN] r68536 - in grass-addons/grass7/raster: . r.colors.matplotlib

svn_grass at osgeo.org svn_grass at osgeo.org
Mon May 30 10:38:27 PDT 2016


Author: wenzeslaus
Date: 2016-05-30 10:38:27 -0700 (Mon, 30 May 2016)
New Revision: 68536

Added:
   grass-addons/grass7/raster/r.colors.matplotlib/
   grass-addons/grass7/raster/r.colors.matplotlib/Makefile
   grass-addons/grass7/raster/r.colors.matplotlib/r.colors.matplotlib.html
   grass-addons/grass7/raster/r.colors.matplotlib/r.colors.matplotlib.py
   grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib.png
   grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_autumn.png
   grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_cubehelix_discrete.png
   grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_gist_earth.png
   grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_gist_earth_discrete.png
   grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_terrain.png
   grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_winter_discrete.png
Modified:
   grass-addons/grass7/raster/Makefile
Log:
r.colors.matplotlib: use Matplotlib color tables in GRASS

Modified: grass-addons/grass7/raster/Makefile
===================================================================
--- grass-addons/grass7/raster/Makefile	2016-05-30 14:24:11 UTC (rev 68535)
+++ grass-addons/grass7/raster/Makefile	2016-05-30 17:38:27 UTC (rev 68536)
@@ -21,6 +21,7 @@
 	r.bitpattern \
 	r.catchment \
 	r.category.trim \
+	r.colors.matplotlib \
 	r.convergence \
 	r.cpt2grass \
 	r.crater \

Added: grass-addons/grass7/raster/r.colors.matplotlib/Makefile
===================================================================
--- grass-addons/grass7/raster/r.colors.matplotlib/Makefile	                        (rev 0)
+++ grass-addons/grass7/raster/r.colors.matplotlib/Makefile	2016-05-30 17:38:27 UTC (rev 68536)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.colors.matplotlib
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script


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

Added: grass-addons/grass7/raster/r.colors.matplotlib/r.colors.matplotlib.html
===================================================================
--- grass-addons/grass7/raster/r.colors.matplotlib/r.colors.matplotlib.html	                        (rev 0)
+++ grass-addons/grass7/raster/r.colors.matplotlib/r.colors.matplotlib.html	2016-05-30 17:38:27 UTC (rev 68536)
@@ -0,0 +1,157 @@
+<h2>DESCRIPTION</h2>
+
+The <em>r.colors.matplotlib</em> module converts
+<a href="http://matplotlib.org/">Matplotlib</a> color maps
+to GRASS color table format (rules) and assigns it to given raster map.
+
+
+The created color table is always relative (color rules with
+percent) 
+When option <b>map</b> is specified <em>r.colors.matplotlib</em>
+assigns the color rules to the given raster map.
+The color tables is always stratched based on the range of values of the map
+
+<p>
+Depending on the use case,
+it may be advantageous to use the <b>-d</b> to discretize
+the color table into intervals.
+
+<!--
+r.colors.matplotlib color=gist_earth ncolors=8 map=elevation
+r.colors.matplotlib color=gist_earth ncolors=8 map=elevation -d
+-->
+
+<center>
+<img src="r_colors_matplotlib_gist_earth.png" alt="Matplotlib gist_earth continuous">
+<img src="r_colors_matplotlib_gist_earth_discrete.png" alt="Matplotlib gist_earth discrete">
+<p><em>Continuous (default) and discrete (<b>-d</b>) color table</em>
+</center>
+
+
+<h2>NOTES</h2>
+
+This module depends on
+<em><a href="http://matplotlib.org/">Matplotlib</a></em>
+which needs to be installed on your computer.
+Use your Python package manager (e.g. <em>pip</em>) or distribution package
+manager to install it.
+
+<p>
+The selection of color tables depends on the Matplotlib version. Note
+that the perceptually uniform sequential color tables, namely
+<em>viridis</em>, <em>inferno</em>, <em>plasma</em>, and <em>magma</em>,
+are available in Matplotlib 1.5 and above.
+Color tables are called <em>color maps</em> in Matplotlib
+and the best overview of available color maps in the
+<a href="http://matplotlib.org/examples/color/colormaps_reference.html">colormaps_reference</a>
+example in Matplotlib documentation.
+
+
+<h2>EXAMPLES</h2>
+
+<h3>Creating a color table as GRASS color rules</h3>
+
+Convert <em>summer</em> color table to GRASS color table rules format.
+If we don't specify output file, it is printed to standard output.
+We set number of colors to 2 because that's enough for this given color
+table (it has one color at the beginning and one at the end and linear
+interpolation can be used for the values in between).
+
+<div class="code"><pre>
+r.colors.matplotlib color=summer ncolors=2
+</pre></div>
+<pre>
+0.000% 0:127:102
+100.000% 255:255:102
+</pre>
+
+In case we want to use discrete color table with intervals with given
+constant color, we use the <b>-d</b> flag and the number of colors
+is now the number of intervals, so we want to make it higher, 5 in this
+case.
+
+<div class="code"><pre>
+r.colors.matplotlib color=summer ncolors=5 -d
+</pre></div>
+<pre>
+0.000% 0:127:102
+20.000% 0:127:102
+20.000% 63:159:102
+40.000% 63:159:102
+40.000% 127:191:102
+60.000% 127:191:102
+60.000% 191:223:102
+80.000% 191:223:102
+80.000% 255:255:102
+100.000% 255:255:102
+</pre>
+
+<h3>Setting color table for a raster map</h3>
+
+Now we set several different color tables for the elevation raster map
+from the North Carolina sample dataset.
+We use continuous and discrete color tables (gradients).
+The color tables ae stretched to fit the raster map range.
+
+<div class="code"><pre>
+r.colors.matplotlib color=summer map=elevation
+r.colors.matplotlib color=winter ncolors=8 map=elevation -d
+r.colors.matplotlib color=autumn map=elevation
+r.colors.matplotlib color=cubehelix ncolors=8 map=elevation -d
+r.colors.matplotlib color=terrain map=elevation
+</pre></div>
+
+We can display legend:
+<div class="code"><pre>
+d.legend raster=elevation labelnum=10 at=5,50,7,10
+</pre></div>
+
+<center>
+<img src="r_colors_matplotlib_summer.png" alt="Matplotlib summer">
+<img src="r_colors_matplotlib_winter_discrete.png" alt="Matplotlib winter discrete">
+<img src="r_colors_matplotlib_autumn.png" alt="Matplotlib autumn">
+<img src="r_colors_matplotlib_cubehelix_discrete.png" alt="Matplotlib cubehelix discrete">
+<img src="r_colors_matplotlib_terrain.png" alt="Matplotlib terrain">
+<p><em>
+summer, winter, autumn, cubehelix, and terrain color tables applied
+to elevation raster from North Carolina sample dataset. winter and
+cubehelix are set to be discrete instead of continuous.
+</em>
+</center>
+
+<h3>Setting color table for a vector map</h3>
+
+First we create a text file with color rules:
+
+<div class="code"><pre>
+r.colors.matplotlib color=summer output=mpl_summer.txt
+</pre></div>
+
+Then we set color table for the vector to the rules stored in a file:
+
+<div class="code"><pre>
+v.colors map=points rules=mpl_summer.txt
+</pre></div>
+
+Color table for 3D raster map can be set in the same way.
+
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="r.colors.html">r.colors</a>,
+<a href="v.colors.html">v.colors</a>,
+<a href="r3.colors.html">r3.colors</a>,
+<a href="r.cpt2grass.html">r.cpt2grass</a>,
+<a href="r.colors.cubehelix.html">r.colors.cubehelix</a>
+</em>
+
+<p>
+<a href="http://matplotlib.org/examples/color/colormaps_reference.html">colormaps_reference</a>
+example in Matplotlib documentation
+
+<h2>AUTHORS</h2>
+Vaclav Petras, <a href="http://geospatial.ncsu.edu/osgeorel/">NCSU OSGeoREL</a><br>
+
+<p>
+<i>Last changed: $Date$</i>


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

Added: grass-addons/grass7/raster/r.colors.matplotlib/r.colors.matplotlib.py
===================================================================
--- grass-addons/grass7/raster/r.colors.matplotlib/r.colors.matplotlib.py	                        (rev 0)
+++ grass-addons/grass7/raster/r.colors.matplotlib/r.colors.matplotlib.py	2016-05-30 17:38:27 UTC (rev 68536)
@@ -0,0 +1,173 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+############################################################################
+#
+# MODULE:       r.colors.matplotlib
+# AUTHOR:       Vaclav Petras <wenzeslaus gmail com>
+# PURPOSE:      Use Matplotlib color tables to GRASS GIS
+# COPYRIGHT:    (C) 2016 by Vaclav Petras and the GRASS Development Team
+#               This program is free software under the GNU General Public
+#               License (>=v2). Read the file COPYING that comes with GRASS
+#               for details.
+#
+#############################################################################
+
+#%module
+#% description: Convert or apply a Matplotlib color table to a GRASS raster map
+#% keyword: raster
+#% keyword: color table
+#% keyword: matplotlib
+#%end
+
+#%option G_OPT_R_MAPS
+#% description: Raster map(s) to apply color table to
+#% required: no
+#% guisection: Basic
+#%end
+#%option G_OPT_F_OUTPUT
+#% description: Name for the new color table rules file
+#% required: no
+#%end
+#%option
+#% key: color
+#% type: string
+#% label: Name of color table
+#% description: Available color tables depend on the Matplotlib version
+#% required: no
+#% guisection: Basic
+#%end
+
+#%option
+#% key: ncolors
+#% type: integer
+#% label: Number of colors in the color table
+#% description: Number of color intervals in a discrete color table with -d
+#% options: 2-
+#% answer: 6
+#% required: no
+#% guisection: Rules
+#%end
+#%flag
+#% key: d
+#% label: Generate discrete color table
+#% description: Generate discrete (interval) color table instead of a continuous one
+#% guisection: Rules
+#%end
+
+#%flag
+#% key: n
+#% description: Reverse the order of colors (invert colors)
+#% guisection: Basic
+#%end
+#%flag
+#% key: g
+#% description: Logarithmic scaling
+#% guisection: Basic
+#%end
+#%flag
+#% key: a
+#% description: Logarithmic-absolute scaling
+#% guisection: Basic
+#%end
+#%flag
+#% key: e
+#% description: Histogram equalization
+#% guisection: Basic
+#%end
+#%rules
+#% requires: -g, map
+#% requires: -a, map
+#% requires: -e, map
+#%end
+
+
+import os
+import sys
+import grass.script as gscript
+import matplotlib.pyplot as plt
+
+
+def values_to_rule(value, red, green, blue, percent):
+    """Return textual representation of one color rule line"""
+    return "{v:.3f}{p} {r}:{g}:{b}".format(v=value,
+                                           p='%' if percent else '',
+                                           r=red, g=green, b=blue)
+
+
+# sync with r.colors.cubehelix
+# this can potentially go to the core as something like grass.utils
+def mpl_cmap_to_rules(cmap, n_colors=None, discrete=False, comments=None):
+    if not n_colors:
+        n_colors = cmap.N
+    # determine numbers for recomputing from absolute range to relative
+    cmin = 0
+    cmax = n_colors
+    if not discrete:
+        cmax -= 1
+    crange = cmax - cmin
+    cinterval = float(crange) / n_colors
+
+    rules = []
+    if comments:
+        for comment in comments:
+            rules.append("# {}".format(comment))
+    for v1 in range(0, n_colors, 1):
+        r1, g1, b1 = cmap(v1)[:3]
+        if discrete:
+            v2 = v1 + cinterval
+        v1 = 100 * (crange - (cmax - v1)) / float(crange)
+        if discrete:
+            v2 = 100 * (crange - (cmax - v2)) / float(crange)
+
+        r1 = int(r1 * 255)
+        g1 = int(g1 * 255)
+        b1 = int(b1 * 255)
+        rules.append(values_to_rule(value=v1, red=r1, green=g1,
+                                    blue=b1, percent=True))
+        if discrete:
+            rules.append(values_to_rule(value=v2, red=r1, green=g1,
+                                        blue=b1, percent=True))
+    return '\n'.join(rules)
+
+
+def main(options, flags):
+    name = options['color']
+    n_colors = int(options['ncolors'])
+    discrete = flags['d']
+
+    if flags['n']:
+        name += '_r'
+
+    cmap = plt.get_cmap(name, lut=n_colors)
+
+    comments = []
+    comments.append(
+        "Generated from Matplotlib color table <{}>".format(name))
+    comments.append(
+        "using:")
+    command = [sys.argv[0].split(os.path.sep)[-1]]
+    command.extend(sys.argv[1:])
+    comments.append(
+        "  {}".format(' '.join(command)))
+
+    rules = mpl_cmap_to_rules(cmap, n_colors=n_colors,
+                              discrete=discrete, comments=comments)
+
+    if options['map']:
+        rcf = ''
+        for char in 'gae':
+            if flags[char]:
+                rcf += char
+        gscript.write_command('r.colors', map=options['map'], flags=rcf,
+                              rules='-', stdin=rules,)
+    if options['output']:
+        with open(options['output'], 'w') as f:
+            f.write(rules)
+            f.write('\n')
+    elif not options['map']:
+        print rules
+
+
+if __name__ == '__main__':
+    sys.exit(main(*gscript.parser()))


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

Added: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib.png
===================================================================
(Binary files differ)


Property changes on: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib.png
___________________________________________________________________
Added: svn:mime-type
   + image/png

Added: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_autumn.png
===================================================================
(Binary files differ)


Property changes on: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_autumn.png
___________________________________________________________________
Added: svn:mime-type
   + image/png

Added: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_cubehelix_discrete.png
===================================================================
(Binary files differ)


Property changes on: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_cubehelix_discrete.png
___________________________________________________________________
Added: svn:mime-type
   + image/png

Added: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_gist_earth.png
===================================================================
(Binary files differ)


Property changes on: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_gist_earth.png
___________________________________________________________________
Added: svn:mime-type
   + image/png

Added: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_gist_earth_discrete.png
===================================================================
(Binary files differ)


Property changes on: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_gist_earth_discrete.png
___________________________________________________________________
Added: svn:mime-type
   + image/png

Added: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_terrain.png
===================================================================
(Binary files differ)


Property changes on: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_terrain.png
___________________________________________________________________
Added: svn:mime-type
   + image/png

Added: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_winter_discrete.png
===================================================================
(Binary files differ)


Property changes on: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_winter_discrete.png
___________________________________________________________________
Added: svn:mime-type
   + image/png



More information about the grass-commit mailing list