topng.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Sep 9 22:51:41 2014
  4. @author: lucadelu
  5. """
  6. import os
  7. import subprocess
  8. try:
  9. import osgeo.gdal as gdal
  10. except ImportError:
  11. try:
  12. import gdal
  13. except ImportError:
  14. raise Exception('Python GDAL library not found, please install python-gdal')
  15. lstcolortable = "\
  16. 0 255 255 255\n\
  17. 9657.5 0 0 40\n\
  18. 11657.5 91 10 168\n\
  19. 12407.5 91 50 128\n\
  20. 12657.5 50 0 150\n\
  21. 12907.5 4 25 130\n\
  22. 13157.5 8 54 106\n\
  23. 13257.5 4 20 150\n\
  24. 13407.5 0 50 255\n\
  25. 13557.5 8 10 118\n\
  26. 13654.42755 8 113 155\n\
  27. 13658.19275 8 118 157\n\
  28. 13662.029699999999 8 123 158\n\
  29. 13665.93835 8 128 159\n\
  30. 13669.918750000001 8 133 159\n\
  31. 13673.97085 8 136 160\n\
  32. 13678.094649999999 8 140 162\n\
  33. 13682.290199999999 8 145 163\n\
  34. 13686.55745 8 150 165\n\
  35. 13690.8964 8 156 166\n\
  36. 13695.3071 8 161 167\n\
  37. 13699.789500000001 8 166 169\n\
  38. 13704.343650000001 8 172 172\n\
  39. 13708.969450000001 8 177 177\n\
  40. 13713.66705 8 182 182\n\
  41. 13718.436299999999 8 187 187\n\
  42. 13723.2773 8 192 192\n\
  43. 13728.190049999999 8 197 197\n\
  44. 13733.17445 8 200 200\n\
  45. 13738.230600000001 8 204 204\n\
  46. 13743.3585 8 209 209\n\
  47. 13748.5581 8 213 213\n\
  48. 13753.829400000001 8 216 216\n\
  49. 13759.172399999999 8 219 219\n\
  50. 13764.587149999999 8 222 222\n\
  51. 13770.07365 8 225 225\n\
  52. 13775.631799999999 8 227 227\n\
  53. 13781.261699999999 8 230 230\n\
  54. 13786.96335 8 233 233\n\
  55. 13792.736699999999 8 235 235\n\
  56. 13798.581749999999 8 237 236\n\
  57. 13804.4985 8 238 234\n\
  58. 13810.486999999999 8 238 229\n\
  59. 13816.547200000001 8 238 223\n\
  60. 13822.67915 8 237 216\n\
  61. 13828.882799999999 8 235 209\n\
  62. 13835.158149999999 8 234 202\n\
  63. 13841.50525 8 232 192\n\
  64. 13847.92405 8 231 178\n\
  65. 13854.4146 8 230 162\n\
  66. 13860.976849999999 8 229 147\n\
  67. 13867.6108 8 227 133\n\
  68. 13874.31645 8 226 123\n\
  69. 13881.093849999999 8 224 116\n\
  70. 13887.942999999999 8 221 114\n\
  71. 13894.863799999999 8 219 114\n\
  72. 13901.856400000001 8 216 115\n\
  73. 13908.92065 8 213 117\n\
  74. 13916.05665 8 209 118\n\
  75. 13923.264349999999 8 204 118\n\
  76. 13930.543799999999 8 199 117\n\
  77. 13937.894899999999 8 193 115\n\
  78. 13945.317800000001 8 188 112\n\
  79. 13952.81235 8 184 107\n\
  80. 13960.378650000001 8 181 99\n\
  81. 13968.0167 8 178 91\n\
  82. 13975.72645 8 175 84\n\
  83. 13983.507900000001 8 172 81\n\
  84. 13991.36105 8 169 80\n\
  85. 13999.28595 8 164 80\n\
  86. 14007.28255 8 159 80\n\
  87. 14015.350899999999 8 153 80\n\
  88. 14023.490949999999 8 148 79\n\
  89. 14031.7027 8 143 79\n\
  90. 14039.986199999999 8 138 79\n\
  91. 14048.341399999999 8 134 78\n\
  92. 14056.76835 8 132 75\n\
  93. 14065.267 8 134 70\n\
  94. 14073.83735 8 137 63\n\
  95. 14082.4794 8 140 56\n\
  96. 14091.1932 8 140 48\n\
  97. 14099.97875 8 139 37\n\
  98. 14108.835950000001 9 138 24\n\
  99. 14117.7649 14 139 14\n\
  100. 14126.765600000001 24 141 9\n\
  101. 14135.838 37 146 8\n\
  102. 14144.982099999999 47 151 8\n\
  103. 14154.197899999999 52 156 8\n\
  104. 14163.48545 55 161 8\n\
  105. 14172.84475 57 166 8\n\
  106. 14182.2757 60 172 8\n\
  107. 14191.778399999999 65 177 8\n\
  108. 14201.352849999999 70 182 8\n\
  109. 14210.998949999999 75 187 8\n\
  110. 14220.716850000001 80 192 8\n\
  111. 14230.5064 85 196 8\n\
  112. 14240.367700000001 89 199 8\n\
  113. 14250.3007 95 201 8\n\
  114. 14260.30545 103 204 8\n\
  115. 14270.3819 110 206 8\n\
  116. 14280.530049999999 118 209 8\n\
  117. 14290.749949999999 126 211 8\n\
  118. 14301.04155 134 214 8\n\
  119. 14311.404850000001 142 217 8\n\
  120. 14321.839900000001 151 219 8\n\
  121. 14332.346649999999 160 222 8\n\
  122. 14342.925149999999 169 225 8\n\
  123. 14353.575349999999 179 227 8\n\
  124. 14364.29725 187 229 8\n\
  125. 14375.090899999999 195 230 8\n\
  126. 14385.956249999999 205 230 8\n\
  127. 14396.8933 215 230 8\n\
  128. 14407.902099999999 223 229 8\n\
  129. 14418.982599999999 226 227 8\n\
  130. 14430.13485 225 224 8\n\
  131. 14441.3588 224 221 8\n\
  132. 14452.65445 224 217 8\n\
  133. 14464.021849999999 224 211 8\n\
  134. 14475.460950000001 224 205 8\n\
  135. 14486.971750000001 224 198 8\n\
  136. 14498.5543 224 191 8\n\
  137. 14510.208549999999 224 185 8\n\
  138. 14521.93455 224 180 8\n\
  139. 14533.7322 224 174 8\n\
  140. 14545.601650000001 224 166 8\n\
  141. 14557.542750000001 224 159 8\n\
  142. 14569.5556 224 151 8\n\
  143. 14581.6402 224 143 8\n\
  144. 14593.79645 224 135 8\n\
  145. 14606.024450000001 224 127 8\n\
  146. 14618.324199999999 224 119 8\n\
  147. 14630.69565 224 111 8\n\
  148. 14643.138800000001 224 103 8\n\
  149. 14655.65365 224 95 8\n\
  150. 14668.240250000001 224 89 8\n\
  151. 14680.8986 224 84 8\n\
  152. 14693.6286 224 78 8\n\
  153. 14706.430350000001 223 70 8\n\
  154. 14719.30385 223 63 8\n\
  155. 14732.24905 223 55 8\n\
  156. 14745.265950000001 222 46 8\n\
  157. 14758.35455 221 37 8\n\
  158. 14771.5149 219 28 8\n\
  159. 14784.746950000001 216 19 8\n\
  160. 14798.05075 213 12 8\n\
  161. 14811.42625 209 9 8\n\
  162. 14824.873449999999 204 8 8\n\
  163. 14838.392400000001 200 8 8\n\
  164. 14851.983050000001 196 8 8\n\
  165. 14865.64545 190 8 8\n\
  166. 14879.37955 182 8 8\n\
  167. 14893.18535 175 8 8\n\
  168. 14907.06285 167 8 8\n\
  169. 14921.0121 159 8 8\n\
  170. 14935.033100000001 151 8 8\n\
  171. 14949.125749999999 143 8 8\n\
  172. 14963.290199999999 135 8 8\n\
  173. 14977.5263 127 8 8\n\
  174. 14991.834150000001 120 8 8\n\
  175. 15006.2137 115 8 8\n\
  176. 15020.665000000001 110 8 8\n\
  177. 15035.18795 104 8 8\n\
  178. 15049.7827 108 13 13\n\
  179. 15064.4491 112 19 19\n\
  180. 15079.187250000001 114 22 22\n\
  181. 15093.997149999999 115 26 26\n\
  182. 15108.87875 118 30 30\n\
  183. 15123.832050000001 119 34 33\n\
  184. 15138.857050000001 121 38 38\n\
  185. 15153.953799999999 123 41 41\n\
  186. 15169.12225 125 45 45\n\
  187. 15184.362450000001 127 49 49\n\
  188. 15199.674349999999 128 52 53\n\
  189. 15215.05795 130 56 56\n\
  190. 15230.513300000001 132 60 60\n\
  191. 15246.040349999999 134 64 63\n\
  192. 15261.639150000001 136 67 67\n\
  193. 15277.309649999999 138 71 70\n\
  194. 15293.05185 140 75 74\n\
  195. 15308.865750000001 142 79 78\n\
  196. 15324.751399999999 143 82 82\n\
  197. 15340.7088 145 85 85\n\
  198. 15356.73785 147 89 90\n\
  199. 15372.83865 149 93 93\n\
  200. 15389.011200000001 150 97 97\n\
  201. 15405.255450000001 152 100 100\n\
  202. 15421.571400000001 154 104 104\n\
  203. 15437.959049999999 156 107 108\n\
  204. 15454.418449999999 158 112 111\n\
  205. 15470.9496 160 115 115\n\
  206. 15487.5524 162 119 119\n\
  207. 15504.22695 164 123 123\n\
  208. 15520.973249999999 165 126 126\n\
  209. 15537.7912 168 130 130\n\
  210. 15554.68095 169 134 134\n\
  211. 15571.64235 170 138 137\n\
  212. 15588.675499999999 173 141 141\n\
  213. 15605.780350000001 175 145 145\n\
  214. 15622.95695 176 149 149\n\
  215. 15640.205250000001 178 153 153\n\
  216. 15657.525250000001 181 156 156\n\
  217. 15674.916999999999 182 159 159\n\
  218. 15692.380450000001 183 163 163\n\
  219. 15709.9156 185 167 167\n\
  220. 15727.522499999999 188 170 171\n\
  221. 15745.2011 190 174 174\n\
  222. 15762.95145 191 178 178\n\
  223. 15780.773499999999 193 182 182\n\
  224. 15798.66725 195 185 185\n\
  225. 15816.632750000001 197 189 189\n\
  226. 15834.66995 199 192 193\n\
  227. 15852.778850000001 200 197 197\n\
  228. 15870.959500000001 202 200 201\n\
  229. "
  230. #
  231. #lstcolortable = "\
  232. #0 255 255 255\n\
  233. #9657 0 0 40\n\
  234. #15870 202 200 201\n\
  235. #"
  236. def hdf2png(hdffile, layer=1):
  237. src_ds = gdal.Open(hdffile)
  238. layers = src_ds.GetSubDatasets()
  239. output = hdffile.split('.')[:-1]
  240. output.append('png')
  241. outname = '.'.join(output)
  242. pid = os.getpid()
  243. colortable = open('colortable_{p}'.format(p=pid), 'w')
  244. colortable.write(lstcolortable)
  245. colortable.close()
  246. subprocess.call(['gdaldem', 'color-relief', '-of', 'PNG', layers[layer][0],
  247. colortable.name, outname])
  248. #os.remove(colortable.name)
  249. return outname
  250. def tif2png(tiffile):
  251. output = tiffile.split('.')[:-1]
  252. output.append('png')
  253. pid = os.getpid()
  254. colortable = open('colortable_{p}'.format(p=pid), 'w')
  255. colortable.write(lstcolortable)
  256. colortable.close()
  257. subprocess.call(['gdaldem', 'color-relief', '-of', 'PNG', tiffile,
  258. colortable.name, '.'.join(output)])
  259. os.remove(colortable.name)
  260. return '.'.join(output)