thumbnails.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #!/usr/bin/env python3
  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. # https://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 os
  16. import atexit
  17. import grass.script as grass
  18. tmp_grad_abs = None
  19. tmp_grad_rel = None
  20. def cleanup():
  21. if tmp_grad_rel:
  22. grass.run_command(
  23. "g.remove", flags="f", type="raster", name=tmp_grad_rel, quiet=True
  24. )
  25. if tmp_grad_abs:
  26. grass.run_command(
  27. "g.remove", flags="f", type="raster", name=tmp_grad_abs, quiet=True
  28. )
  29. def make_gradient(path):
  30. fh = open(path)
  31. text = fh.read()
  32. fh.close()
  33. lines = text.splitlines()
  34. records = list()
  35. for line in lines:
  36. if line.startswith("#"):
  37. # skip comments
  38. continue
  39. if len(line) == 0:
  40. # skip empty lines
  41. continue
  42. records.append(line.split())
  43. records = [
  44. record for record in records if record[0] != "nv" and record[0] != "default"
  45. ]
  46. relative = False
  47. absolute = False
  48. for record in records:
  49. if record[0].endswith("%"):
  50. relative = True
  51. record[0] = record[0].rstrip("%")
  52. else:
  53. absolute = True
  54. if absolute:
  55. if relative:
  56. minval = -0.04
  57. maxval = 0.04
  58. else:
  59. minval = float(records[0][0])
  60. # shift min up for floating point values so that
  61. # first color in color table is visible
  62. if "." in records[0][0]:
  63. # assumes that 1% of min does not go to the next value
  64. # and is still represented as float and does not make
  65. # too much difference in color
  66. # works better than 1% of the difference to the next value
  67. minval += abs(minval / 100)
  68. maxval = float(records[-1][0])
  69. maxval = min(maxval, 2500000)
  70. if os.path.basename(path) in ("ndvi", "ndwi", "ndwi2"):
  71. minval = -1.0
  72. maxval = 1.0
  73. if os.path.basename(path) == "ndvi_MODIS":
  74. minval = -10000.0
  75. maxval = 10000.0
  76. if os.path.basename(path) == "population_dens":
  77. maxval = 1000.0
  78. if os.path.basename(path) == "precipitation":
  79. maxval = 2000.0
  80. if os.path.basename(path) in ("terrain", "srtm", "srtm_plus"):
  81. minval = -500.0
  82. maxval = 3000.0
  83. grad = tmp_grad_abs
  84. grass.mapcalc(
  85. "$grad = "
  86. " float($min) + (col() - 1) * "
  87. " (float($max) - float($min)) / ncols()",
  88. grad=tmp_grad_abs,
  89. min=minval,
  90. max=maxval,
  91. quiet=True,
  92. )
  93. else:
  94. grad = tmp_grad_rel
  95. return grad
  96. def make_image(output_dir, table, grad, height, width):
  97. outfile = os.path.join(output_dir, "colortables", "%s.png" % table)
  98. os.environ["GRASS_RENDER_FILE"] = outfile
  99. grass.run_command("r.colors", map=grad, color=table, quiet=True)
  100. os.environ["GRASS_RENDER_FRAME"] = "%f,%f,%f,%f" % (0, height, 2, width - 2)
  101. grass.run_command("d.rast", map=grad, quiet=True)
  102. if 1:
  103. os.environ["GRASS_RENDER_FRAME"] = "%f,%f,%f,%f" % (0, height, 0, width)
  104. grass.write_command(
  105. "d.graph",
  106. quiet=True,
  107. flags="m",
  108. stdin="""
  109. width 1
  110. color {outcolor}
  111. polyline
  112. {x1} {y1}
  113. {x2} {y1}
  114. {x2} {y2}
  115. {x1} {y2}
  116. {x1} {y1}
  117. color {incolor}
  118. polyline
  119. {x3} {y3}
  120. {x4} {y3}
  121. {x4} {y4}
  122. {x3} {y4}
  123. {x3} {y3}
  124. """.format(
  125. x1=1,
  126. x2=width,
  127. y1=0,
  128. y2=height - 1,
  129. x3=2,
  130. x4=width - 1,
  131. y3=1,
  132. y4=height - 2,
  133. outcolor="white",
  134. incolor="black",
  135. ),
  136. )
  137. def main():
  138. global tmp_img, tmp_grad_abs, tmp_grad_rel
  139. height = 15
  140. width = 85
  141. os.environ["GRASS_OVERWRITE"] = "1"
  142. color_dir = os.path.join(os.environ["GISBASE"], "etc", "colors")
  143. output_dir = os.path.join(os.environ["GISBASE"], "docs", "html")
  144. if not os.path.exists(output_dir):
  145. os.makedirs(output_dir)
  146. pid = os.getpid()
  147. tmp_grad_abs = "tmp_grad_abs_%d" % pid
  148. tmp_grad_rel = "tmp_grad_rel_%d" % pid
  149. os.environ["GRASS_RENDER_WIDTH"] = "%d" % width
  150. os.environ["GRASS_RENDER_HEIGHT"] = "%d" % height
  151. os.environ["GRASS_RENDER_TRUECOLOR"] = "TRUE"
  152. # for multiple d commands (requires to delete/move image each time)
  153. os.environ["GRASS_RENDER_FILE_READ"] = "TRUE"
  154. os.environ["GRASS_RENDER_FILE_MAPPED"] = "FALSE"
  155. os.environ["GRASS_RENDER_TRANSPARENT"] = "FALSE"
  156. os.environ["GRASS_RENDER_BACKGROUNDCOLOR"] = "ffffff"
  157. os.environ["GRASS_RENDER_IMMEDIATE"] = "cairo"
  158. # for one pixel wide lines
  159. os.environ["GRASS_RENDER_ANTIALIAS"] = "none"
  160. for var in ["GRASS_RENDER_LINE_WIDTH"]:
  161. if var in os.environ:
  162. del os.environ[var]
  163. os.environ["GRASS_ANTIALIAS"] = "none"
  164. grass.use_temp_region()
  165. grass.run_command(
  166. "g.region",
  167. s=0,
  168. w=0,
  169. n=height,
  170. e=width,
  171. rows=height,
  172. cols=width,
  173. res=1,
  174. flags="a",
  175. )
  176. grass.mapcalc("$grad = float(col())", grad=tmp_grad_rel, quiet=True)
  177. for table in os.listdir(color_dir):
  178. path = os.path.join(color_dir, table)
  179. grad = make_gradient(path)
  180. make_image(output_dir, table, grad, height=height, width=width)
  181. grass.mapcalc("$grad = col()", grad=tmp_grad_abs, quiet=True)
  182. for table in ["grey.eq", "grey.log", "random"]:
  183. make_image(output_dir, table, tmp_grad_abs, height=height, width=width)
  184. if __name__ == "__main__":
  185. atexit.register(cleanup)
  186. main()