[GRASS-SVN] r66167 - in grass-addons/grass7/raster: . r.cpt2grass
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Sep 10 17:58:12 PDT 2015
Author: annakrat
Date: 2015-09-10 17:58:12 -0700 (Thu, 10 Sep 2015)
New Revision: 66167
Added:
grass-addons/grass7/raster/r.cpt2grass/
grass-addons/grass7/raster/r.cpt2grass/Makefile
grass-addons/grass7/raster/r.cpt2grass/r.cpt2grass.html
grass-addons/grass7/raster/r.cpt2grass/r.cpt2grass.py
grass-addons/grass7/raster/r.cpt2grass/r_cpt2grass_color_table_DEM_screen.png
grass-addons/grass7/raster/r.cpt2grass/r_cpt2grass_color_table_YlOrBr_09.png
Modified:
grass-addons/grass7/raster/Makefile
Log:
r.cpt2grass to convert cpt color tables rewritten to GRASS 7 and Python
Modified: grass-addons/grass7/raster/Makefile
===================================================================
--- grass-addons/grass7/raster/Makefile 2015-09-10 18:14:34 UTC (rev 66166)
+++ grass-addons/grass7/raster/Makefile 2015-09-11 00:58:12 UTC (rev 66167)
@@ -21,6 +21,7 @@
r.biodiversity \
r.bitpattern \
r.convergence \
+ r.cpt2grass \
r.crater \
r.damflood \
r.divergence \
Added: grass-addons/grass7/raster/r.cpt2grass/Makefile
===================================================================
--- grass-addons/grass7/raster/r.cpt2grass/Makefile (rev 0)
+++ grass-addons/grass7/raster/r.cpt2grass/Makefile 2015-09-11 00:58:12 UTC (rev 66167)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.cpt2grass
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script
Property changes on: grass-addons/grass7/raster/r.cpt2grass/Makefile
___________________________________________________________________
Added: svn:mime-type
+ text/x-makefile
Added: svn:eol-style
+ native
Added: grass-addons/grass7/raster/r.cpt2grass/r.cpt2grass.html
===================================================================
--- grass-addons/grass7/raster/r.cpt2grass/r.cpt2grass.html (rev 0)
+++ grass-addons/grass7/raster/r.cpt2grass/r.cpt2grass.html 2015-09-11 00:58:12 UTC (rev 66167)
@@ -0,0 +1,92 @@
+<h2>DESCRIPTION</h2>
+
+Module <em>r.cpt2grass</em> converts
+<a href="http://gmt.soest.hawaii.edu/">GMT</a> color palette
+(*.cpt) format
+to GRASS color table format and assigns it to given raster map.
+
+Input can be either cpt file given in <b>input</b> option or
+a URL of the cpt file specified in <b>url</b> option.
+Specifying URL is particularly useful when using color tables
+from <a href="http://soliton.vm.bytemark.co.uk/pub/cpt-city/">cpt-city</a>,
+because many color tables can be quickly tested without
+downloading the files.
+
+When option <b>map</b> is specified <em>r.cpt2grass</em>
+assigns the color rules to the given raster map.
+Depending on the values of the original cpt file,
+it may be advantageous to use the <b>-s</b> to stretch the
+colors based on the range of values of the map.
+
+
+
+
+
+<h2>NOTES</h2>
+RGB and HSV models are supported.
+The expected format of the cpt file is:
+<pre>
+# COLOR_MODEL = RGB
+value1 R G B value2 R G B
+value2 R G B value3 R G B
+...
+</pre>
+Named colors are not supported.
+
+<h2>EXAMPLES</h2>
+
+From <a href="http://soliton.vm.bytemark.co.uk/pub/cpt-city/">cpt-city</a>
+we download a
+<a href="http://soliton.vm.bytemark.co.uk/pub/cpt-city/jjg/misc/rainfall.cpt">rainfall</a>
+color table and convert it to GRASS color table.
+If we don't specify output file, it is printed to standard output:
+
+<div class="code"><pre>
+r.cpt2grass input=rainfall.cpt
+</pre></div>
+<pre>
+0.000 229:180:44
+20.000 229:180:44
+20.000 242:180:100
+40.000 242:180:100
+40.000 243:233:119
+60.000 243:233:119
+60.000 145:206:126
+80.000 145:206:126
+80.000 67:190:135
+100.000 67:190:135
+100.000 52:180:133
+120.000 52:180:133
+120.000 6:155:66
+140.000 6:155:66
+</pre>
+
+We set two different elevation color tables - continuous and discrete gradients.
+We have to stretch the color tables to fit the raster map range:
+
+<div class="code"><pre>
+r.cpt2grass url=http://soliton.vm.bytemark.co.uk/pub/cpt-city/td/DEM_screen.cpt map=elevation -s
+r.cpt2grass url=http://soliton.vm.bytemark.co.uk/pub/cpt-city/cb/seq/YlOrBr_09.cpt map=elevation -s
+</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_cpt2grass_color_table_DEM_screen.png" alt="DEM color table">
+<img src="r_cpt2grass_color_table_YlOrBr_09.png" alt="yellow to brown color table">
+</center>
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="r.colors.html">r.colors</a>
+</em>
+
+<h2>AUTHORS</h2>
+Anna Petrasova, <a href="http://gis.ncsu.edu/osgeorel/">NCSU OSGeoREL</a>
+
+<p>
+<i>Last changed: $Date$</i>
Property changes on: grass-addons/grass7/raster/r.cpt2grass/r.cpt2grass.html
___________________________________________________________________
Added: svn:mime-type
+ text/html
Added: svn:keywords
+ Author Date Id
Added: svn:eol-style
+ native
Added: grass-addons/grass7/raster/r.cpt2grass/r.cpt2grass.py
===================================================================
--- grass-addons/grass7/raster/r.cpt2grass/r.cpt2grass.py (rev 0)
+++ grass-addons/grass7/raster/r.cpt2grass/r.cpt2grass.py 2015-09-11 00:58:12 UTC (rev 66167)
@@ -0,0 +1,194 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+############################################################################
+#
+# MODULE: r.cpt2grass
+# AUTHOR(S): M. Hamish Bowman, Otago University, New Zealand
+# (original GRASS 6 implementation)
+# Anna Petrasova (rewritten to GRASS 7 in Python)
+# PURPOSE: Convert a GMT color table into a GRASS color rules file
+# COPYRIGHT: (c) 2007 by Hamish Bowman, 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.
+#
+# SEE ALSO: GMT: The Generic Mapping Tools
+# http://gmt.soest.hawaii.edu
+#
+#############################################################################
+
+#%module
+#% description: Convert or apply a GMT color table to a GRASS raster map
+#%end
+#%option G_OPT_F_INPUT
+#% description: Name of input GMT color table (.cpt file)
+#% required: no
+#% guisection: Input
+#%end
+#%option
+#% key: url
+#% type: string
+#% description: URL of the color table
+#% required: no
+#% guisection: Input
+#%end
+#%option G_OPT_R_INPUT
+#% key: map
+#% description: Raster map to apply it to
+#% required: no
+#% guisection: Input
+#%end
+#%option G_OPT_F_OUTPUT
+#% description: Name for new rules file
+#% required: no
+#%end
+#%flag
+#% key: s
+#% description: Stretch color scale to match map data extent
+#%end
+#%rules
+#% required: input,url
+#% exclusive: input,url
+#% requires: -s,map
+#%end
+
+import sys
+import grass.script as gscript
+
+
+def HSVtoRGB(h, s, v):
+ """Converts HSV to RGB.
+ Based on the Foley and Van Dam HSV algorithm used
+ by James Westervelt's (CERL) hsv.rgb.sh script from GRASS 4/5."""
+ # Hue: 0-360 degrees
+ # Satuaration: 0.0-1.0
+ # Value: 0.0-1.0
+ if v == 0.0:
+ return (0, 0, 0)
+ if v == 1.0:
+ return (255, 255, 255)
+
+ if h >= 360:
+ h -= 360
+ h = h / 60.0
+ i = int(h)
+ f = h - i
+ p = v * (1 - s)
+ q = v * (1 - s * f)
+ t = v * (1 - s * (1 - f))
+
+ # init/fallback
+ R = G = B = 0
+ # red
+ if i == 0:
+ R = v
+ if i == 1:
+ R = q
+ if i == 2:
+ R = p
+ if i == 3:
+ R = p
+ if i == 4:
+ R = t
+ if i == 5:
+ R = v
+
+ # green
+ if i == 0:
+ G = t
+ if i == 1:
+ G = v
+ if i == 2:
+ G = v
+ if i == 3:
+ G = q
+ if i == 4:
+ G = p
+ if i == 5:
+ G = p
+
+ # blue
+ if i == 0:
+ B = p
+ if i == 1:
+ B = p
+ if i == 2:
+ B = t
+ if i == 3:
+ B = v
+ if i == 4:
+ B = v
+ if i == 5:
+ B = q
+
+ return (R * 255, G * 255, B * 255)
+
+
+def main(options, flags):
+ input_file = options['input']
+ input_url = options['url']
+ if input_url:
+ try:
+ from urllib.request import urlopen
+ except ImportError:
+ from urllib import urlopen
+
+ txt = urlopen(input_url).readlines()
+ else:
+ with open(input_file, 'r') as f:
+ txt = f.readlines()
+
+ model = 'RGB' # assuming RGB
+ cpt_rules = []
+ for line in txt:
+ if not line.strip():
+ continue
+ if 'COLOR_MODEL' in line:
+ model = line.split('=')[-1].strip()
+ elif line[0] in ('B', 'F', 'N', '#'):
+ continue
+ else:
+ cpt_rules.append(line.strip())
+
+ if model not in ('RGB', 'HSV'):
+ gscript.fatal(_("Only the RGB and HSV color models are supported"))
+
+ rules = []
+ if flags['s']:
+ # sort?
+ cpt_min = float(cpt_rules[0].split()[0])
+ cpt_max = float(cpt_rules[-1].split()[4])
+ cpt_range = cpt_max - cpt_min
+ for line in cpt_rules:
+ try:
+ v1, r1, g1, b1, v2, r2, g2, b2 = line.split()
+ except ValueError:
+ gscript.fatal(_("Parsing input failed. The expected format is 'value1 R G B value2 R G B'"))
+ v1 = float(v1)
+ v2 = float(v2)
+ if model == 'HSV':
+ r1, b1, g1 = HSVtoRGB(int(r1), int(g1), int(b1))
+ r2, b2, g2 = HSVtoRGB(int(r2), int(g2), int(b2))
+ if flags['s']:
+ v1 = 100 * (cpt_range - (cpt_max - v1)) / cpt_range
+ v2 = 100 * (cpt_range - (cpt_max - v2)) / cpt_range
+ rules.append("{v:.3f}{perc} {r}:{g}:{b}".format(v=v1, perc='%' if flags['s'] else '',
+ r=r1, g=g1, b=b1))
+ rules.append("{v:.3f}{perc} {r}:{g}:{b}".format(v=v2, perc='%' if flags['s'] else '',
+ r=r2, g=g2, b=b2))
+ if options['map']:
+ gscript.write_command('r.colors', map=options['map'],
+ rules='-', stdin='\n'.join(rules))
+ if options['output']:
+ with open(options['output'], 'w') as f:
+ f.write('\n'.join(rules))
+ f.write('\n')
+ elif not options['map']:
+ print '\n'.join(rules) + '\n'
+
+
+if __name__ == '__main__':
+ options, flags = gscript.parser()
+ sys.exit(main(options, flags))
Property changes on: grass-addons/grass7/raster/r.cpt2grass/r.cpt2grass.py
___________________________________________________________________
Added: svn:mime-type
+ text/x-python
Added: svn:eol-style
+ native
Added: grass-addons/grass7/raster/r.cpt2grass/r_cpt2grass_color_table_DEM_screen.png
===================================================================
(Binary files differ)
Property changes on: grass-addons/grass7/raster/r.cpt2grass/r_cpt2grass_color_table_DEM_screen.png
___________________________________________________________________
Added: svn:mime-type
+ image/png
Added: grass-addons/grass7/raster/r.cpt2grass/r_cpt2grass_color_table_YlOrBr_09.png
===================================================================
(Binary files differ)
Property changes on: grass-addons/grass7/raster/r.cpt2grass/r_cpt2grass_color_table_YlOrBr_09.png
___________________________________________________________________
Added: svn:mime-type
+ image/png
More information about the grass-commit
mailing list