[GRASS-SVN] r68537 - in grass-addons/grass7/raster: . r.colors.cubehelix
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon May 30 10:40:22 PDT 2016
Author: wenzeslaus
Date: 2016-05-30 10:40:22 -0700 (Mon, 30 May 2016)
New Revision: 68537
Added:
grass-addons/grass7/raster/r.colors.cubehelix/
grass-addons/grass7/raster/r.colors.cubehelix/Makefile
grass-addons/grass7/raster/r.colors.cubehelix/r.colors.cubehelix.html
grass-addons/grass7/raster/r.colors.cubehelix/r.colors.cubehelix.py
grass-addons/grass7/raster/r.colors.cubehelix/r_colors_cubehelix.png
grass-addons/grass7/raster/r.colors.cubehelix/r_colors_cubehelix_two_colors.png
Modified:
grass-addons/grass7/raster/Makefile
Log:
r.colors.cubehelix: generate cubehelix color tables using seaborn
Modified: grass-addons/grass7/raster/Makefile
===================================================================
--- grass-addons/grass7/raster/Makefile 2016-05-30 17:38:27 UTC (rev 68536)
+++ grass-addons/grass7/raster/Makefile 2016-05-30 17:40:22 UTC (rev 68537)
@@ -21,6 +21,7 @@
r.bitpattern \
r.catchment \
r.category.trim \
+ r.colors.cubehelix \
r.colors.matplotlib \
r.convergence \
r.cpt2grass \
Added: grass-addons/grass7/raster/r.colors.cubehelix/Makefile
===================================================================
--- grass-addons/grass7/raster/r.colors.cubehelix/Makefile (rev 0)
+++ grass-addons/grass7/raster/r.colors.cubehelix/Makefile 2016-05-30 17:40:22 UTC (rev 68537)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.colors.cubehelix
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script
Property changes on: grass-addons/grass7/raster/r.colors.cubehelix/Makefile
___________________________________________________________________
Added: svn:mime-type
+ text/x-makefile
Added: svn:eol-style
+ native
Added: grass-addons/grass7/raster/r.colors.cubehelix/r.colors.cubehelix.html
===================================================================
--- grass-addons/grass7/raster/r.colors.cubehelix/r.colors.cubehelix.html (rev 0)
+++ grass-addons/grass7/raster/r.colors.cubehelix/r.colors.cubehelix.html 2016-05-30 17:40:22 UTC (rev 68537)
@@ -0,0 +1,126 @@
+<h2>DESCRIPTION</h2>
+
+The <em>r.colors.cubehelix</em> module generates
+a cubehelix color table and assigns it to given raster map
+if requested.
+
+The color table is generated using <em>seaborn</em> Python
+package. Several pararameters are available to control the
+cubehelix.
+
+When option <b>map</b> is specified <em>r.colors.cubehelix</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.
+
+
+<h2>NOTES</h2>
+
+This module depends on
+<em><a href="https://stanford.edu/~mwaskom/software/seaborn/index.html">seaborn</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.
+
+
+<h2>EXAMPLES</h2>
+
+<h3>Creating a color table as GRASS color rules</h3>
+
+We do 0.6 rotation around the axis and use discrete (interval) color table
+rather than the standard continuous.
+If we don't specify output file, it is printed to standard output:
+
+<div class="code"><pre>
+r.colors.cubehelix -d ncolors=5 nrotations=0.6
+</pre></div>
+<pre>
+0.000% 218:222:192
+20.000% 218:222:192
+20.000% 198:166:136
+40.000% 198:166:136
+40.000% 173:108:112
+60.000% 173:108:112
+60.000% 119:61:98
+80.000% 119:61:98
+80.000% 48:28:59
+100.000% 48:28:59
+</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.cubehelix -d ncolors=8 nrotations=0.6 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>
+
+Here we set continuous color table with more colors
+
+<div class="code"><pre>
+r.colors.cubehelix nrotations=1.4 start=4 map=elevation
+</pre></div>
+
+<center>
+<img src="r_colors_cubehelix_two_colors.png">
+<img src="r_colors_cubehelix.png">
+</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.cubehelix -i rot=0.6 output=cubehelix.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=cubehelix.txt
+</pre></div>
+
+Color table for 3D raster map can be set in the same way.
+
+
+<h2>REFERENCES</h2>
+
+<li>
+ Green, D. A., 2011, <a href="http://astron-soc.in/bulletin/11June/289392011.pdf">
+ <i>A colour scheme for the display of astronomical intensity images</i></a>,
+ Bulletin of the Astronomical Society of India, 39, 289.
+</li>
+
+<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.matplotlib</a>
+</em>
+
+<p>
+seaborn
+<a href="https://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.cubehelix_palette.html">cubehelix_palette</a>
+function documentation and an
+<a href="https://stanford.edu/~mwaskom/software/seaborn/examples/cubehelix_palette.html">example
+
+<h2>AUTHOR</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.cubehelix/r.colors.cubehelix.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.cubehelix/r.colors.cubehelix.py
===================================================================
--- grass-addons/grass7/raster/r.colors.cubehelix/r.colors.cubehelix.py (rev 0)
+++ grass-addons/grass7/raster/r.colors.cubehelix/r.colors.cubehelix.py 2016-05-30 17:40:22 UTC (rev 68537)
@@ -0,0 +1,251 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+############################################################################
+#
+# MODULE: r.colors.cubehelix
+# AUTHOR: Vaclav Petras <wenzeslaus gmail com>
+# PURPOSE: Convert a GMT color table into a GRASS color rules file
+# COPYRIGHT: (C) 2007 by Vaclav Petras, Anna Petrasova
+# 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: Create or apply a cubehelix color table to a GRASS raster map
+#% keyword: raster
+#% keyword: color table
+#% keyword: cubehelix
+#% keyword: seaborn
+#%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: start
+#% type: double
+#% description: The hue at the start of the helix
+#% options: 0-3
+#% answer: 0
+#% required: no
+#% guisection: Cubehelix
+#%end
+#%option
+#% key: nrotations
+#% type: double
+#% label: Rotations around the hue wheel
+#% description: Rotations around the hue wheel over the range of the color table
+#% options: 0-3
+#% answer: 0.4
+#% required: no
+#% guisection: Cubehelix
+#%end
+#%option
+#% key: gamma
+#% type: double
+#% description: Gamma factor to emphasize darker (<1) or lighter (>1) colors
+#% options: 0-
+#% answer: 1.0
+#% required: no
+#% guisection: Cubehelix
+#%end
+#%option
+#% key: hue
+#% type: double
+#% description: Saturation of the colors
+#% options: 0-1
+#% answer: 0.8
+#% required: no
+#% guisection: Cubehelix
+#%end
+#%option
+#% key: light
+#% type: double
+#% description: Intensity of the lightest color in the color table
+#% options: 0-1
+#% answer: 0.85
+#% required: no
+#% guisection: Cubehelix
+#%end
+#%option
+#% key: dark
+#% type: double
+#% description: Intensity of the darkest color in the color table
+#% options: 0-1
+#% answer: 0.15
+#% required: no
+#% guisection: Cubehelix
+#%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
+#% label: Reverse the order of colors (invert colors)
+#% description: If set, the color table will go from dark to light
+#% 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
+
+
+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.matplotlib
+# 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):
+ # TODO: inervals flag, s should be defaut behavior
+ n_colors = int(options['ncolors'])
+ discrete = flags['d']
+
+ fallback = True
+ try:
+ import seaborn as sns
+ fallback = False
+ except ImportError:
+ # perhaps this can be function in the core
+ gscript.error(_("{} Python package not installed.").format('seaborn'))
+ if not fallback:
+ cmap = sns.cubehelix_palette(
+ n_colors=n_colors,
+ start=float(options['start']),
+ rot=float(options['nrotations']),
+ gamma=float(options['gamma']),
+ hue=float(options['hue']),
+ light=float(options['light']),
+ dark=float(options['dark']),
+ reverse=flags['n'],
+ as_cmap=False)
+ # as_cmap ignores n_colors in 0.7.0
+ # but we want n_colors to be exact when we are exporting
+ # the color table or doing discrete one
+ import matplotlib.colors as clr
+ cmap = clr.LinearSegmentedColormap.from_list('from_list', cmap, N=n_colors)
+ else:
+ gscript.warning(_("Using Matplotlib cubehelix color table."
+ " Most of cubehelix parameters ignored"))
+ # we are very nice and provide a fallback
+ import matplotlib.pyplot as plt
+ name = 'cubehelix'
+ # Matplotlib one goes from dark to light but Seaborn goes
+ # the other way around by default
+ if not flags['n']:
+ name += '_r'
+ cmap = plt.get_cmap(name, lut=n_colors)
+
+ comments = []
+ comments.append(
+ "Cubehelix color table generated 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.cubehelix/r.colors.cubehelix.py
___________________________________________________________________
Added: svn:mime-type
+ text/x-python
Added: svn:eol-style
+ native
Added: grass-addons/grass7/raster/r.colors.cubehelix/r_colors_cubehelix.png
===================================================================
(Binary files differ)
Property changes on: grass-addons/grass7/raster/r.colors.cubehelix/r_colors_cubehelix.png
___________________________________________________________________
Added: svn:mime-type
+ image/png
Added: grass-addons/grass7/raster/r.colors.cubehelix/r_colors_cubehelix_two_colors.png
===================================================================
(Binary files differ)
Property changes on: grass-addons/grass7/raster/r.colors.cubehelix/r_colors_cubehelix_two_colors.png
___________________________________________________________________
Added: svn:mime-type
+ image/png
More information about the grass-commit
mailing list