thumbnails.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/env python
  2. #
  3. # thumbnails.py: Create thumbnail images of the GRASS color tables
  4. #
  5. # AUTHOR: Vaclav Petras (non-PPM version using r.mapcalc)
  6. # Glynn Clements (Python version)
  7. # Earlier Bourne script version by Hamish Bowman,
  8. # http://grasswiki.osgeo.org/wiki/Talk:Color_tables
  9. #
  10. # (C) 2009-2017 by the GRASS Development Team
  11. # This program is free software under the GNU General Public
  12. # License (>=v2). Read the file COPYING that comes with GRASS
  13. # for details.
  14. #
  15. import sys
  16. import os
  17. import atexit
  18. import grass.script as grass
  19. tmp_grad_abs = None
  20. tmp_grad_rel = None
  21. def cleanup():
  22. if tmp_grad_rel:
  23. grass.run_command('g.remove', flags='f', type='raster',
  24. name=tmp_grad_rel, quiet=True)
  25. if tmp_grad_abs:
  26. grass.run_command('g.remove', flags='f', type='raster',
  27. name=tmp_grad_abs, quiet=True)
  28. def make_gradient(path):
  29. fh = open(path)
  30. text = fh.read()
  31. fh.close()
  32. lines = text.splitlines()
  33. records = list()
  34. for line in lines:
  35. if line.startswith("#"):
  36. # skip comments
  37. continue
  38. if len(line) == 0:
  39. # skip empty lines
  40. continue
  41. records.append(line.split())
  42. records = [record for record in records
  43. if record[0] != 'nv' and record[0] != 'default']
  44. relative = False
  45. absolute = False
  46. for record in records:
  47. if record[0].endswith("%"):
  48. relative = True
  49. record[0] = record[0].rstrip("%")
  50. else:
  51. absolute = True
  52. if absolute:
  53. if relative:
  54. minval = -0.04
  55. maxval = 0.04
  56. else:
  57. minval = float(records[0][0])
  58. # shift min up for floating point values so that
  59. # first color in color table is visible
  60. if '.' in records[0][0]:
  61. # assumes that 1% of min does not go to the next value
  62. # and is still represented as float and does not make
  63. # too much difference in color
  64. # works better than 1% of the difference to the next value
  65. minval += abs(minval / 100)
  66. maxval = float(records[-1][0])
  67. maxval = min(maxval, 2500000)
  68. grad = tmp_grad_abs
  69. # alternatively, only simpler expression would suffice if frames
  70. # are used to render raster and then the borders
  71. grass.mapcalc("$grad = if(col() > 2 && col() < ncols() - 1,"
  72. " float($min) + (col() - 3) * "
  73. " (float($max) - float($min)) / (ncols() - 2),"
  74. " null())",
  75. grad=tmp_grad_abs, min=minval, max=maxval, quiet=True)
  76. else:
  77. grad = tmp_grad_rel
  78. return grad
  79. def make_image(output_dir, table, grad, height, width):
  80. outfile = os.path.join(output_dir, "colortables", "%s.png" % table)
  81. os.environ['GRASS_RENDER_FILE'] = outfile
  82. grass.run_command("r.colors", map=grad, color=table, quiet=True)
  83. grass.run_command("d.rast", map=grad, quiet=True)
  84. if 1:
  85. grass.write_command("d.graph", quiet=True, flags='m', stdin="""
  86. width 1
  87. color {outcolor}
  88. polyline
  89. {x1} {y1}
  90. {x2} {y1}
  91. {x2} {y2}
  92. {x1} {y2}
  93. {x1} {y1}
  94. color {incolor}
  95. polyline
  96. {x3} {y3}
  97. {x4} {y3}
  98. {x4} {y4}
  99. {x3} {y4}
  100. {x3} {y3}
  101. """.format(x1=1, x2=width, y1=0, y2=height - 1,
  102. x3=2, x4=width - 1, y3=1, y4=height - 2,
  103. outcolor='white', incolor='black'))
  104. def main():
  105. global tmp_img, tmp_grad_abs, tmp_grad_rel
  106. height = 15
  107. width = 85
  108. os.environ['GRASS_OVERWRITE'] = '1'
  109. color_dir = os.path.join(os.environ['GISBASE'], "etc", "colors")
  110. output_dir = os.path.join(os.environ['GISBASE'], "docs", "html")
  111. if not os.path.exists(output_dir):
  112. os.makedirs(output_dir)
  113. pid = os.getpid()
  114. tmp_grad_abs = "tmp_grad_abs_%d" % pid
  115. tmp_grad_rel = "tmp_grad_rel_%d" % pid
  116. os.environ['GRASS_RENDER_WIDTH'] = '%d' % width
  117. os.environ['GRASS_RENDER_HEIGHT'] = '%d' % height
  118. os.environ['GRASS_RENDER_FRAME'] = '%f,%f,%f,%f' % (0, height, 0, width)
  119. os.environ['GRASS_RENDER_TRUECOLOR'] = 'TRUE'
  120. # for multiple d commands (requires to delete/move image each time)
  121. os.environ['GRASS_RENDER_FILE_READ'] = 'TRUE'
  122. os.environ['GRASS_RENDER_FILE_MAPPED'] = 'FALSE'
  123. os.environ['GRASS_RENDER_TRANSPARENT'] = 'FALSE'
  124. os.environ['GRASS_RENDER_BACKGROUNDCOLOR'] = 'ffffff'
  125. os.environ['GRASS_RENDER_IMMEDIATE'] = 'cairo'
  126. # for one pixel wide lines
  127. os.environ['GRASS_RENDER_ANTIALIAS'] = 'none'
  128. for var in ['GRASS_RENDER_LINE_WIDTH']:
  129. if var in os.environ:
  130. del os.environ[var]
  131. grass.use_temp_region()
  132. grass.run_command('g.region', s=0, w=0, n=height, e=width,
  133. rows=height, cols=width, res=1, flags='a')
  134. grass.mapcalc("$grad = if(col() > 2 && col() < ncols() - 1,"
  135. " float(col()), null())", grad=tmp_grad_rel, quiet=True)
  136. for table in os.listdir(color_dir):
  137. path = os.path.join(color_dir, table)
  138. grad = make_gradient(path)
  139. make_image(output_dir, table, grad,
  140. height=height, width=width)
  141. grass.mapcalc("$grad = if(col() > 2 && col() < ncols() - 1,"
  142. " col(), null())", grad=tmp_grad_abs, quiet=True)
  143. for table in ['grey.eq', 'grey.log', 'random']:
  144. make_image(output_dir, table, tmp_grad_abs,
  145. height=height, width=width)
  146. if __name__ == "__main__":
  147. atexit.register(cleanup)
  148. main()