Browse Source

Generate thumbnails for r.colors HTML file during build

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@39164 15284696-431f-4ddb-bdfa-cd5b030d7da7
Glynn Clements 15 years ago
parent
commit
9e56f6c48d
32 changed files with 165 additions and 6 deletions
  1. 14 6
      raster/r.colors/Makefile
  2. 151 0
      raster/r.colors/thumbnails.py
  3. BIN
      raster/r.colors/thumbnails/Colortable_aspect.png
  4. BIN
      raster/r.colors/thumbnails/Colortable_aspectcolr.png
  5. BIN
      raster/r.colors/thumbnails/Colortable_bcyr.png
  6. BIN
      raster/r.colors/thumbnails/Colortable_bgyr.png
  7. BIN
      raster/r.colors/thumbnails/Colortable_byg.png
  8. BIN
      raster/r.colors/thumbnails/Colortable_byr.png
  9. BIN
      raster/r.colors/thumbnails/Colortable_celsius.png
  10. BIN
      raster/r.colors/thumbnails/Colortable_corine.png
  11. BIN
      raster/r.colors/thumbnails/Colortable_curvature.png
  12. BIN
      raster/r.colors/thumbnails/Colortable_differences.png
  13. BIN
      raster/r.colors/thumbnails/Colortable_elevation.png
  14. BIN
      raster/r.colors/thumbnails/Colortable_etopo2.png
  15. BIN
      raster/r.colors/thumbnails/Colortable_evi.png
  16. BIN
      raster/r.colors/thumbnails/Colortable_gdd.png
  17. BIN
      raster/r.colors/thumbnails/Colortable_grey.png
  18. BIN
      raster/r.colors/thumbnails/Colortable_grey1.0.png
  19. BIN
      raster/r.colors/thumbnails/Colortable_grey255.png
  20. BIN
      raster/r.colors/thumbnails/Colortable_gyr.png
  21. BIN
      raster/r.colors/thumbnails/Colortable_ndvi.png
  22. BIN
      raster/r.colors/thumbnails/Colortable_population.png
  23. BIN
      raster/r.colors/thumbnails/Colortable_precipitation.png
  24. BIN
      raster/r.colors/thumbnails/Colortable_rainbow.png
  25. BIN
      raster/r.colors/thumbnails/Colortable_ramp.png
  26. BIN
      raster/r.colors/thumbnails/Colortable_ryb.png
  27. BIN
      raster/r.colors/thumbnails/Colortable_ryg.png
  28. BIN
      raster/r.colors/thumbnails/Colortable_sepia.png
  29. BIN
      raster/r.colors/thumbnails/Colortable_slope.png
  30. BIN
      raster/r.colors/thumbnails/Colortable_srtm.png
  31. BIN
      raster/r.colors/thumbnails/Colortable_terrain.png
  32. BIN
      raster/r.colors/thumbnails/Colortable_wave.png

+ 14 - 6
raster/r.colors/Makefile

@@ -10,9 +10,17 @@ include $(MODULE_TOPDIR)/include/Make/Module.make
 default: cmd
 
 # Insert thumbnail previews
-#  Try overriding the default rule with:
-#r.colors.tmp.html: $(HTMLSRC)
-#	$(call htmldesc,$<,$@)
-#	sed 's!^<DD><b>\([a-z0-9.]*\)</b>:!<DD><img width="80" height="12" \
-#		src="Colortable_\1_i.png"><b>\1</b>:!' $@ > $@.tmp
-#	mv -f $@.tmp $@
+r.colors.tmp.html: $(HTMLSRC) thumbnails.py
+	$(call htmldesc,$<,$@)
+	sed 's!^<DD><b>\([a-z0-9.]*\)</b>:!<DD><img width="80" height="12" src="Colortable_\1.png"><b>\1</b>:!' $@ > $@.tmp
+	mv -f $@.tmp $@
+	$(MAKE) thumbnails
+
+thumbnails: $(BIN)/r.mapcalc$(EXE)
+	$(call run_grass, ./thumbnails.py)
+
+$(BIN)/r.mapcalc$(EXE):
+	$(MAKE) -C ../r.mapcalc
+
+.PHONY: thumbnails
+

+ 151 - 0
raster/r.colors/thumbnails.py

@@ -0,0 +1,151 @@
+#!/usr/bin/env python
+
+import sys
+import os
+import atexit
+import string
+import array
+import grass.script as grass
+
+tmp_img = None
+tmp_grad_abs = None
+tmp_grad_rel = None
+
+height = 85
+width = 15
+
+def cleanup():
+    if tmp_img:
+	grass.try_remove(tmp_img)
+    if tmp_grad_rel:
+	grass.run_command('g.remove', rast = tmp_grad_rel, quiet = True)
+    if tmp_grad_abs:
+	grass.run_command('g.remove', rast = tmp_grad_abs, quiet = True)
+
+# def rotate(src, dst):
+#     grass.call(["convert", "-rotate", "90", src, dst])
+
+def read_ppm(src):
+    fh = open(src, "rb")
+    text = fh.read()
+    fh.close()
+    i = 0
+    j = text.find('\n', i)
+    if text[i:j] != 'P6':
+	raise IOError
+    i = j + 1
+    j = text.find('\n', i)
+    w, h = text[i:j].split()
+    if int(w) != width or int(h) != height:
+	raise IOError
+    i = j + 1
+    j = text.find('\n', i)
+    maxval = text[i:j]
+    if int(maxval) != 255:
+	raise IOError
+    i = j + 1
+    return array.array('B', text[i:])
+
+def write_ppm(dst, data):
+    w = height
+    h = width
+    fh = open(dst, "wb")
+    fh.write("P6\n%d %d\n%d\n" % (w, h, 255))
+    data.tofile(fh)
+    fh.close()
+
+def rotate_ppm(srcd):
+    dstd = array.array('B', len(srcd) * '\0')
+    for y in xrange(height):
+	for x in xrange(width):
+	    for c in xrange(3):
+		dstd[(x * height + (height - 1 - y)) * 3 + c] = srcd[(y * width + x) * 3 + c]
+    return dstd
+
+def ppmtopng(dst, src):
+    fh = open(dst, 'wb')
+    grass.call(["pnmtopng", tmp_img], stdout = fh)
+    fh.close()
+
+def convert_and_rotate(src, dst):
+    if grass.find_program("convert"):
+	grass.call(["convert", "-rotate", "90", src, dst])
+    elif grass.find_program("pnmtopng"):
+	srcd = read_ppm(src)
+	dstd = rotate_ppm(srcd)
+	write_ppm(tmp_img, dstd)
+	ppmtopng(dst, tmp_img)
+    else:
+	grass.fatal(_("Cannot find either convert or pnmtopng"))
+
+def main():
+    global tmp_img, tmp_grad_abs, tmp_grad_rel
+
+    os.environ['GRASS_OVERWRITE'] = '1'
+
+    color_dir = os.path.join(os.environ['GISBASE'], "etc", "colors")
+    output_dir = os.path.join(os.environ['GISBASE'], "docs", "html")
+
+    if not os.path.exists(output_dir):
+	os.makedirs(output_dir)
+
+    pid = os.getpid()
+    tmp_grad_abs = "tmp_grad_abs_%d" % pid
+    tmp_grad_rel = "tmp_grad_rel_%d" % pid
+    tmp_img = grass.tempfile() + ".ppm"
+
+    os.environ['GRASS_WIDTH'] = '%d' % width
+    os.environ['GRASS_HEIGHT'] = '%d' % height
+    os.environ['GRASS_FRAME'] = '%f,%f,%f,%f' % (0,height,0,width)
+    os.environ['GRASS_PNGFILE'] = tmp_img
+    os.environ['GRASS_TRUECOLOR'] = 'TRUE'
+    os.environ['GRASS_PNG_READ'] = 'FALSE'
+    os.environ['GRASS_PNG_MAPPED'] = 'FALSE'
+    os.environ['GRASS_TRANSPARENT'] = 'FALSE'
+    os.environ['GRASS_BACKGROUNDCOLOR'] = 'ffffff'
+
+    grass.use_temp_region()
+    grass.run_command('g.region', rows = 100, cols = 100)
+
+    grass.mapcalc("$grad = row()/1.0", grad = tmp_grad_rel)
+
+    for table in os.listdir(color_dir):
+        path = os.path.join(color_dir, table)
+        fh = open(path)
+        text = fh.read()
+        fh.close()
+        lines = text.splitlines()
+        records = [line.split() for line in lines]
+        records = [record for record in records if record[0] != 'nv']
+        relative = False
+        absolute = False
+        for record in records:
+            if record[0].endswith("%"):
+                relative = True
+                record[0] = record[0].rstrip("%")
+            else:
+                absolute = True
+
+        if absolute:
+	    if relative:
+		minval = -0.04
+		maxval = 0.04
+	    else:
+		minval = float(records[0][0])
+		maxval = float(records[-1][0])
+		maxval = min(maxval, 2500000)
+	    grad = tmp_grad_abs
+	    grass.mapcalc("$grad = if(row()==1, float($min), float($max))",
+                          grad = tmp_grad_abs, min = minval, max = maxval)
+	else:
+            grad = tmp_grad_rel
+
+        grass.run_command("r.colors", map = grad, color = table, quiet = True)
+        grass.run_command("d.colortable", flags = 'n', map = grad)
+
+	outfile = os.path.join(output_dir, "Colortable_%s.png" % table)
+	convert_and_rotate(tmp_img, outfile)
+
+if __name__ == "__main__":
+    atexit.register(cleanup)
+    main()

BIN
raster/r.colors/thumbnails/Colortable_aspect.png


BIN
raster/r.colors/thumbnails/Colortable_aspectcolr.png


BIN
raster/r.colors/thumbnails/Colortable_bcyr.png


BIN
raster/r.colors/thumbnails/Colortable_bgyr.png


BIN
raster/r.colors/thumbnails/Colortable_byg.png


BIN
raster/r.colors/thumbnails/Colortable_byr.png


BIN
raster/r.colors/thumbnails/Colortable_celsius.png


BIN
raster/r.colors/thumbnails/Colortable_corine.png


BIN
raster/r.colors/thumbnails/Colortable_curvature.png


BIN
raster/r.colors/thumbnails/Colortable_differences.png


BIN
raster/r.colors/thumbnails/Colortable_elevation.png


BIN
raster/r.colors/thumbnails/Colortable_etopo2.png


BIN
raster/r.colors/thumbnails/Colortable_evi.png


BIN
raster/r.colors/thumbnails/Colortable_gdd.png


BIN
raster/r.colors/thumbnails/Colortable_grey.png


BIN
raster/r.colors/thumbnails/Colortable_grey1.0.png


BIN
raster/r.colors/thumbnails/Colortable_grey255.png


BIN
raster/r.colors/thumbnails/Colortable_gyr.png


BIN
raster/r.colors/thumbnails/Colortable_ndvi.png


BIN
raster/r.colors/thumbnails/Colortable_population.png


BIN
raster/r.colors/thumbnails/Colortable_precipitation.png


BIN
raster/r.colors/thumbnails/Colortable_rainbow.png


BIN
raster/r.colors/thumbnails/Colortable_ramp.png


BIN
raster/r.colors/thumbnails/Colortable_ryb.png


BIN
raster/r.colors/thumbnails/Colortable_ryg.png


BIN
raster/r.colors/thumbnails/Colortable_sepia.png


BIN
raster/r.colors/thumbnails/Colortable_slope.png


BIN
raster/r.colors/thumbnails/Colortable_srtm.png


BIN
raster/r.colors/thumbnails/Colortable_terrain.png


BIN
raster/r.colors/thumbnails/Colortable_wave.png