provider.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. # -*- coding: utf-8 -*-
  2. """!
  3. @package animation.provider
  4. @brief Animation files and bitmaps management
  5. Classes:
  6. - mapwindow::BitmapProvider
  7. - mapwindow::BitmapRenderer
  8. - mapwindow::BitmapComposer
  9. - mapwindow::DictRefCounter
  10. - mapwindow::MapFilesPool
  11. - mapwindow::BitmapPool
  12. - mapwindow::CleanUp
  13. (C) 2013 by the GRASS Development Team
  14. This program is free software under the GNU General Public License
  15. (>=v2). Read the file COPYING that comes with GRASS for details.
  16. @author Anna Petrasova <kratochanna gmail.com>
  17. """
  18. import os
  19. import sys
  20. import wx
  21. import tempfile
  22. from multiprocessing import Process, Queue
  23. if __name__ == '__main__':
  24. sys.path.append(os.path.join(os.environ['GISBASE'], "etc", "gui", "wxpython"))
  25. from core.gcmd import RunCommand, GException
  26. from core.settings import UserSettings
  27. from core.debug import Debug
  28. from core.utils import _, CmdToTuple, autoCropImageFromFile
  29. from animation.utils import HashCmd, HashCmds, GetFileFromCmd, GetFileFromCmds
  30. import grass.script.core as gcore
  31. from grass.pydispatch.signal import Signal
  32. class BitmapProvider:
  33. """!Class for management of image files and bitmaps.
  34. There is one instance of this class in the application.
  35. It handles both 2D and 3D animations.
  36. """
  37. def __init__(self, bitmapPool, mapFilesPool, tempDir,
  38. imageWidth=640, imageHeight=480):
  39. self._bitmapPool = bitmapPool
  40. self._mapFilesPool = mapFilesPool
  41. self.imageWidth = imageWidth # width of the image to render with d.rast or d.vect
  42. self.imageHeight = imageHeight # height of the image to render with d.rast or d.vect
  43. self._tempDir = tempDir
  44. self._uniqueCmds = []
  45. self._cmdsForComposition = []
  46. self._opacities = []
  47. self._cmds3D = []
  48. self._regionFor3D = None
  49. self._renderer = BitmapRenderer(self._mapFilesPool, self._tempDir,
  50. self.imageWidth, self.imageHeight)
  51. self._composer = BitmapComposer(self._tempDir, self._mapFilesPool,
  52. self._bitmapPool, self.imageWidth,
  53. self.imageHeight)
  54. self.renderingStarted = Signal('BitmapProvider.renderingStarted')
  55. self.compositionStarted = Signal('BitmapProvider.compositionStarted')
  56. self.renderingContinues = Signal('BitmapProvider.renderingContinues')
  57. self.compositionContinues = Signal('BitmapProvider.compositionContinues')
  58. self.renderingFinished = Signal('BitmapProvider.renderingFinished')
  59. self.compositionFinished = Signal('BitmapProvider.compositionFinished')
  60. self.mapsLoaded = Signal('BitmapProvider.mapsLoaded')
  61. self._renderer.renderingContinues.connect(self.renderingContinues)
  62. self._composer.compositionContinues.connect(self.compositionContinues)
  63. def SetCmds(self, cmdsForComposition, opacities):
  64. """!Sets commands to be rendered with opacity levels.
  65. Applies to 2D mode.
  66. @param cmdsForComposition list of lists of command lists
  67. [[['d.rast', 'map=elev_2001'], ['d.vect', 'map=points']], # g.pnmcomp
  68. [['d.rast', 'map=elev_2002'], ['d.vect', 'map=points']],
  69. ...]
  70. @param opacities list of opacity values
  71. """
  72. Debug.msg(2, "BitmapProvider.SetCmds: {} lists".format(len(cmdsForComposition)))
  73. self._cmdsForComposition.extend(cmdsForComposition)
  74. self._uniqueCmds = self._getUniqueCmds()
  75. self._opacities.extend(opacities)
  76. def SetCmds3D(self, cmds, region):
  77. """!Sets commands for 3D rendering.
  78. @param cmds list of commands m.nviz.image (cmd as a list)
  79. @param region for 3D rendering
  80. """
  81. Debug.msg(2, "BitmapProvider.SetCmds3D: {} commands".format(len(cmds)))
  82. self._cmds3D = cmds
  83. self._regionFor3D = region
  84. def _getUniqueCmds(self):
  85. """!Returns list of unique commands."""
  86. unique = set()
  87. for cmdList in self._cmdsForComposition:
  88. for cmd in cmdList:
  89. unique.add(tuple(cmd))
  90. return list(unique)
  91. def Unload(self):
  92. """!Unloads currently loaded data.
  93. Needs to be called before setting new data.
  94. """
  95. Debug.msg(2, "BitmapProvider.Unload")
  96. if self._cmdsForComposition:
  97. for cmd in self._uniqueCmds:
  98. del self._mapFilesPool[HashCmd(cmd)]
  99. for cmdList in self._cmdsForComposition:
  100. del self._bitmapPool[HashCmds(cmdList)]
  101. self._uniqueCmds = []
  102. self._cmdsForComposition = []
  103. self._opacities = []
  104. if self._cmds3D:
  105. self._cmds3D = []
  106. self._regionFor3D = None
  107. def _dryRender(self, uniqueCmds, force):
  108. """!Determines how many files will be rendered.
  109. @param uniqueCmds list of commands which are to be rendered
  110. @param force if forced rerendering
  111. """
  112. count = 0
  113. for cmd in uniqueCmds:
  114. filename = GetFileFromCmd(self._tempDir, cmd)
  115. if not force and os.path.exists(filename) and \
  116. self._mapFilesPool.GetSize(HashCmd(cmd)) == (self.imageWidth, self.imageHeight):
  117. continue
  118. count += 1
  119. Debug.msg(3, "BitmapProvider._dryRender: {} files to be rendered".format(count))
  120. return count
  121. def _dryCompose(self, cmdLists, force):
  122. """!Determines how many lists of (commands) files
  123. will be composed (with g.pnmcomp).
  124. @param cmdLists list of commands lists which are to be composed
  125. @param force if forced rerendering
  126. """
  127. count = 0
  128. for cmdList in cmdLists:
  129. if not force and HashCmds(cmdList) in self._bitmapPool and \
  130. self._bitmapPool[HashCmds(cmdList)].GetSize() == (self.imageWidth,
  131. self.imageHeight):
  132. continue
  133. count += 1
  134. Debug.msg(2, "BitmapProvider._dryCompose: {} files to be composed".format(count))
  135. return count
  136. def Load(self, force=False, bgcolor=(255, 255, 255), nprocs=4):
  137. """!Loads data, both 2D and 3D. In case of 2D, it creates composites,
  138. even when there is only 1 layer to compose (to be changed for speedup)
  139. @param force if True reload all data, otherwise only missing data
  140. @param bgcolor background color as a tuple of 3 values 0 to 255
  141. @param nprocs number of procs to be used for rendering
  142. """
  143. Debug.msg(2, "BitmapProvider.Load: "
  144. "force={}, bgcolor={}, nprocs={}".format(force, bgcolor, nprocs))
  145. cmds = []
  146. if self._uniqueCmds:
  147. cmds.extend(self._uniqueCmds)
  148. if self._cmds3D:
  149. cmds.extend(self._cmds3D)
  150. count = self._dryRender(cmds, force=force)
  151. self.renderingStarted.emit(count=count)
  152. # create no data bitmap
  153. if None not in self._bitmapPool or force:
  154. self._bitmapPool[None] = createNoDataBitmap(self.imageWidth, self.imageHeight)
  155. ok = self._renderer.Render(cmds, regionFor3D=self._regionFor3D,
  156. bgcolor=bgcolor, force=force, nprocs=nprocs)
  157. self.renderingFinished.emit()
  158. if not ok:
  159. self.mapsLoaded.emit() # what to do here?
  160. return
  161. if self._cmdsForComposition:
  162. count = self._dryCompose(self._cmdsForComposition, force=force)
  163. self.compositionStarted.emit(count=count)
  164. self._composer.Compose(self._cmdsForComposition, self._opacities,
  165. bgcolor=bgcolor, force=force, nprocs=nprocs)
  166. self.compositionFinished.emit()
  167. if self._cmds3D:
  168. for cmd in self._cmds3D:
  169. self._bitmapPool[HashCmds([cmd])] = \
  170. wx.Bitmap(GetFileFromCmd(self._tempDir, cmd))
  171. self.mapsLoaded.emit()
  172. def RequestStopRendering(self):
  173. """!Requests to stop rendering/composition"""
  174. Debug.msg(2, "BitmapProvider.RequestStopRendering")
  175. self._renderer.RequestStopRendering()
  176. self._composer.RequestStopComposing()
  177. def GetBitmap(self, dataId):
  178. """!Returns bitmap with given key
  179. or 'no data' bitmap if no such key exists.
  180. @param dataId name of bitmap
  181. """
  182. try:
  183. bitmap = self._bitmapPool[dataId]
  184. except KeyError:
  185. bitmap = self._bitmapPool[None]
  186. return bitmap
  187. def WindowSizeChanged(self, width, height):
  188. """!Sets size when size of related window changes."""
  189. Debug.msg(5, "BitmapProvider.WindowSizeChanged: w={}, h={}".format(width, height))
  190. self.imageWidth, self.imageHeight = width, height
  191. self._composer.imageWidth = self._renderer.imageWidth = width
  192. self._composer.imageHeight = self._renderer.imageHeight = height
  193. def LoadOverlay(self, cmd):
  194. """!Creates raster legend with d.legend
  195. @param cmd d.legend command as a list
  196. @return bitmap with legend
  197. """
  198. Debug.msg(5, "BitmapProvider.LoadOverlay: cmd={}".format(cmd))
  199. fileHandler, filename = tempfile.mkstemp(suffix=".png")
  200. os.close(fileHandler)
  201. # Set the environment variables for this process
  202. _setEnvironment(self.imageWidth, self.imageHeight, filename,
  203. transparent=True, bgcolor=(0, 0, 0))
  204. Debug.msg(1, "Render raster legend " + str(filename))
  205. cmdTuple = CmdToTuple(cmd)
  206. returncode, stdout, messages = read2_command(cmdTuple[0], **cmdTuple[1])
  207. if returncode == 0:
  208. return wx.BitmapFromImage(autoCropImageFromFile(filename))
  209. else:
  210. os.remove(filename)
  211. raise GException(messages)
  212. class BitmapRenderer:
  213. """!Class which renderes 2D and 3D images to files."""
  214. def __init__(self, mapFilesPool, tempDir,
  215. imageWidth, imageHeight):
  216. self._mapFilesPool = mapFilesPool
  217. self._tempDir = tempDir
  218. self.imageWidth = imageWidth
  219. self.imageHeight = imageHeight
  220. self.renderingContinues = Signal('BitmapRenderer.renderingContinues')
  221. self._stopRendering = False
  222. self._isRendering = False
  223. def Render(self, cmdList, regionFor3D, bgcolor, force, nprocs):
  224. """!Renders all maps and stores files.
  225. @param cmdList list of rendering commands to run
  226. @param regionFor3D region for setting 3D view
  227. @param bgcolor background color as a tuple of 3 values 0 to 255
  228. @param force if True reload all data, otherwise only missing data
  229. @param nprocs number of procs to be used for rendering
  230. """
  231. Debug.msg(3, "BitmapRenderer.Render")
  232. count = 0
  233. # Variables for parallel rendering
  234. proc_count = 0
  235. proc_list = []
  236. queue_list = []
  237. cmd_list = []
  238. filteredCmdList = []
  239. for cmd in cmdList:
  240. filename = GetFileFromCmd(self._tempDir, cmd)
  241. if not force and os.path.exists(filename) and \
  242. self._mapFilesPool.GetSize(HashCmd(cmd)) == (self.imageWidth, self.imageHeight):
  243. # for reference counting
  244. self._mapFilesPool[HashCmd(cmd)] = filename
  245. continue
  246. filteredCmdList.append(cmd)
  247. mapNum = len(filteredCmdList)
  248. stopped = False
  249. self._isRendering = True
  250. for cmd in filteredCmdList:
  251. count += 1
  252. # Queue object for interprocess communication
  253. q = Queue()
  254. # The separate render process
  255. if cmd[0] == 'm.nviz.image':
  256. p = Process(target=RenderProcess3D,
  257. args=(self.imageWidth, self.imageHeight, self._tempDir,
  258. cmd, regionFor3D, bgcolor, q))
  259. else:
  260. p = Process(target=RenderProcess2D,
  261. args=(self.imageWidth, self.imageHeight, self._tempDir, cmd, bgcolor, q))
  262. p.start()
  263. queue_list.append(q)
  264. proc_list.append(p)
  265. cmd_list.append(cmd)
  266. proc_count += 1
  267. # Wait for all running processes and read/store the created images
  268. if proc_count == nprocs or count == mapNum:
  269. for i in range(len(cmd_list)):
  270. proc_list[i].join()
  271. filename = queue_list[i].get()
  272. self._mapFilesPool[HashCmd(cmd_list[i])] = filename
  273. self._mapFilesPool.SetSize(HashCmd(cmd_list[i]),
  274. (self.imageWidth, self.imageHeight))
  275. proc_count = 0
  276. proc_list = []
  277. queue_list = []
  278. cmd_list = []
  279. self.renderingContinues.emit(current=count, text=_("Rendering map layers"))
  280. if self._stopRendering:
  281. self._stopRendering = False
  282. stopped = True
  283. break
  284. self._isRendering = False
  285. return not stopped
  286. def RequestStopRendering(self):
  287. """!Requests to stop rendering."""
  288. if self._isRendering:
  289. self._stopRendering = True
  290. class BitmapComposer:
  291. """!Class which handles the composition of image files with g.pnmcomp."""
  292. def __init__(self, tempDir, mapFilesPool, bitmapPool,
  293. imageWidth, imageHeight):
  294. self._mapFilesPool = mapFilesPool
  295. self._bitmapPool = bitmapPool
  296. self._tempDir = tempDir
  297. self.imageWidth = imageWidth
  298. self.imageHeight = imageHeight
  299. self.compositionContinues = Signal('BitmapComposer.composingContinues')
  300. self._stopComposing = False
  301. self._isComposing = False
  302. def Compose(self, cmdLists, opacityList, bgcolor, force, nprocs):
  303. """!Performs the composition of ppm/pgm files.
  304. @param cmdLisst lists of rendering commands lists to compose
  305. @param opacityList list of lists of opacity values
  306. @param bgcolor background color as a tuple of 3 values 0 to 255
  307. @param force if True reload all data, otherwise only missing data
  308. @param nprocs number of procs to be used for rendering
  309. """
  310. Debug.msg(3, "BitmapComposer.Compose")
  311. count = 0
  312. # Variables for parallel rendering
  313. proc_count = 0
  314. proc_list = []
  315. queue_list = []
  316. cmd_lists = []
  317. filteredCmdLists = []
  318. for cmdList in cmdLists:
  319. if not force and HashCmds(cmdList) in self._bitmapPool and \
  320. self._bitmapPool[HashCmds(cmdList)].GetSize() == (self.imageWidth,
  321. self.imageHeight):
  322. # TODO: find a better way than to assign the same to increase the reference
  323. self._bitmapPool[HashCmds(cmdList)] = self._bitmapPool[HashCmds(cmdList)]
  324. continue
  325. filteredCmdLists.append(cmdList)
  326. num = len(filteredCmdLists)
  327. self._isComposing = True
  328. for cmdList in filteredCmdLists:
  329. count += 1
  330. # Queue object for interprocess communication
  331. q = Queue()
  332. # The separate render process
  333. p = Process(target=CompositeProcess,
  334. args=(self.imageWidth, self.imageHeight, self._tempDir,
  335. cmdList, opacityList, bgcolor, q))
  336. p.start()
  337. queue_list.append(q)
  338. proc_list.append(p)
  339. cmd_lists.append(cmdList)
  340. proc_count += 1
  341. # Wait for all running processes and read/store the created images
  342. if proc_count == nprocs or count == num:
  343. for i in range(len(cmd_lists)):
  344. proc_list[i].join()
  345. filename = queue_list[i].get()
  346. if filename is None:
  347. self._bitmapPool[HashCmds(cmd_lists[i])] = \
  348. createNoDataBitmap(self.imageWidth, self.imageHeight,
  349. text="Failed to render")
  350. else:
  351. self._bitmapPool[HashCmds(cmd_lists[i])] = \
  352. wx.BitmapFromImage(wx.Image(filename))
  353. os.remove(filename)
  354. proc_count = 0
  355. proc_list = []
  356. queue_list = []
  357. cmd_lists = []
  358. self.compositionContinues.emit(current=count, text=_("Overlaying map layers"))
  359. if self._stopComposing:
  360. self._stopComposing = False
  361. break
  362. self._isComposing = False
  363. def RequestStopComposing(self):
  364. """!Requests to stop the composition."""
  365. if self._isComposing:
  366. self._stopComposing = True
  367. def RenderProcess2D(imageWidth, imageHeight, tempDir, cmd, bgcolor, fileQueue):
  368. """!Render raster or vector files as ppm image and write the
  369. resulting ppm filename in the provided file queue
  370. @param imageWidth image width
  371. @param imageHeight image height
  372. @param tempDir directory for rendering
  373. @param cmd d.rast/d.vect command as a list
  374. @param bgcolor background color as a tuple of 3 values 0 to 255
  375. @param fileQueue the inter process communication queue
  376. storing the file name of the image
  377. """
  378. filename = GetFileFromCmd(tempDir, cmd)
  379. transparency = True
  380. # Set the environment variables for this process
  381. _setEnvironment(imageWidth, imageHeight, filename,
  382. transparent=transparency, bgcolor=bgcolor)
  383. cmdTuple = CmdToTuple(cmd)
  384. returncode, stdout, messages = read2_command(cmdTuple[0], **cmdTuple[1])
  385. if returncode != 0:
  386. gcore.warning("Rendering failed:\n" + messages)
  387. fileQueue.put(None)
  388. os.remove(filename)
  389. return
  390. fileQueue.put(filename)
  391. def RenderProcess3D(imageWidth, imageHeight, tempDir, cmd, region, bgcolor, fileQueue):
  392. """!Renders image with m.nviz.image and writes the
  393. resulting ppm filename in the provided file queue
  394. @param imageWidth image width
  395. @param imageHeight image height
  396. @param tempDir directory for rendering
  397. @param cmd m.nviz.image command as a list
  398. @param bgcolor background color as a tuple of 3 values 0 to 255
  399. @param fileQueue the inter process communication queue
  400. storing the file name of the image
  401. """
  402. filename = GetFileFromCmd(tempDir, cmd)
  403. os.environ['GRASS_REGION'] = gcore.region_env(region3d=True, **region)
  404. Debug.msg(1, "Render image to file " + str(filename))
  405. cmdTuple = CmdToTuple(cmd)
  406. cmdTuple[1]['output'] = os.path.splitext(filename)[0]
  407. # set size
  408. cmdTuple[1]['size'] = '%d,%d' % (imageWidth, imageHeight)
  409. # set format
  410. cmdTuple[1]['format'] = 'ppm'
  411. cmdTuple[1]['bgcolor'] = bgcolor = ':'.join([str(part) for part in bgcolor])
  412. returncode, stdout, messages = read2_command(cmdTuple[0], **cmdTuple[1])
  413. if returncode != 0:
  414. gcore.warning("Rendering failed:\n" + messages)
  415. fileQueue.put(None)
  416. os.environ.pop('GRASS_REGION')
  417. return
  418. os.environ.pop('GRASS_REGION')
  419. fileQueue.put(filename)
  420. def CompositeProcess(imageWidth, imageHeight, tempDir, cmdList, opacities, bgcolor, fileQueue):
  421. """!Performs the composition of image ppm files and writes the
  422. resulting ppm filename in the provided file queue
  423. @param imageWidth image width
  424. @param imageHeight image height
  425. @param tempDir directory for rendering
  426. @param cmdList list of d.rast/d.vect commands
  427. @param opacities list of opacities
  428. @param bgcolor background color as a tuple of 3 values 0 to 255
  429. @param fileQueue the inter process communication queue
  430. storing the file name of the image
  431. """
  432. maps = []
  433. masks = []
  434. for cmd in cmdList:
  435. maps.append(GetFileFromCmd(tempDir, cmd))
  436. masks.append(GetFileFromCmd(tempDir, cmd, 'pgm'))
  437. filename = GetFileFromCmds(tempDir, cmdList)
  438. # Set the environment variables for this process
  439. _setEnvironment(imageWidth, imageHeight, filename,
  440. transparent=False, bgcolor=bgcolor)
  441. opacities = [str(op) for op in opacities]
  442. bgcolor = ':'.join([str(part) for part in bgcolor])
  443. returncode, stdout, messages = read2_command('g.pnmcomp',
  444. overwrite=True,
  445. input='%s' % ",".join(reversed(maps)),
  446. mask='%s' % ",".join(reversed(masks)),
  447. opacity='%s' % ",".join(reversed(opacities)),
  448. bgcolor=bgcolor,
  449. width=imageWidth,
  450. height=imageHeight,
  451. output=filename)
  452. if returncode != 0:
  453. gcore.warning("Rendering composite failed:\n" + messages)
  454. fileQueue.put(None)
  455. os.remove(filename)
  456. return
  457. fileQueue.put(filename)
  458. class DictRefCounter:
  459. """!Base class storing map files/bitmaps (emulates dictionary).
  460. Counts the references to know which files/bitmaps to delete."""
  461. def __init__(self):
  462. self.dictionary = {}
  463. self.referenceCount = {}
  464. def __getitem__(self, key):
  465. return self.dictionary[key]
  466. def __setitem__(self, key, value):
  467. self.dictionary[key] = value
  468. if key not in self.referenceCount:
  469. self.referenceCount[key] = 1
  470. else:
  471. self.referenceCount[key] += 1
  472. Debug.msg(5, 'DictRefCounter.__setitem__: +1 for key {}'.format(key))
  473. def __contains__(self, key):
  474. return key in self.dictionary
  475. def __delitem__(self, key):
  476. self.referenceCount[key] -= 1
  477. Debug.msg(5, 'DictRefCounter.__delitem__: -1 for key {}'.format(key))
  478. def keys(self):
  479. return self.dictionary.keys()
  480. def Clear(self):
  481. """!Clears items which are not needed any more."""
  482. Debug.msg(4, 'DictRefCounter.Clear')
  483. for key in self.dictionary.keys():
  484. if key is not None:
  485. if self.referenceCount[key] <= 0:
  486. del self.dictionary[key]
  487. del self.referenceCount[key]
  488. class MapFilesPool(DictRefCounter):
  489. """!Stores rendered images as files."""
  490. def __init__(self):
  491. DictRefCounter.__init__(self)
  492. self.size = {}
  493. def SetSize(self, key, size):
  494. self.size[key] = size
  495. def GetSize(self, key):
  496. return self.size[key]
  497. def Clear(self):
  498. """!Removes files which are not needed anymore.
  499. Removes both ppm and pgm.
  500. """
  501. Debug.msg(4, 'MapFilesPool.Clear')
  502. for key in self.dictionary.keys():
  503. if self.referenceCount[key] <= 0:
  504. name, ext = os.path.splitext(self.dictionary[key])
  505. os.remove(self.dictionary[key])
  506. if ext == '.ppm':
  507. os.remove(name + '.pgm')
  508. del self.dictionary[key]
  509. del self.referenceCount[key]
  510. del self.size[key]
  511. class BitmapPool(DictRefCounter):
  512. """!Class storing bitmaps (emulates dictionary)"""
  513. def __init__(self):
  514. DictRefCounter.__init__(self)
  515. class CleanUp:
  516. """!Responsible for cleaning up the files."""
  517. def __init__(self, tempDir):
  518. self._tempDir = tempDir
  519. def __call__(self):
  520. import shutil
  521. if os.path.exists(self._tempDir):
  522. try:
  523. shutil.rmtree(self._tempDir)
  524. Debug.msg(5, 'CleanUp: removed directory {}'.format(self._tempDir))
  525. except OSError:
  526. gcore.warning(_("Directory {} not removed.").format(self._tempDir))
  527. def _setEnvironment(width, height, filename, transparent, bgcolor):
  528. """!Sets environmental variables for 2D rendering.
  529. @param width rendering width
  530. @param height rendering height
  531. @param filename file name
  532. @param transparent use transparency
  533. @param bgcolor background color as a tuple of 3 values 0 to 255
  534. """
  535. Debug.msg(5, "_setEnvironment: width={}, height={}, "
  536. "filename={}, transparent={}, bgcolor={}".format(width, height, filename,
  537. transparent, bgcolor))
  538. os.environ['GRASS_WIDTH'] = str(width)
  539. os.environ['GRASS_HEIGHT'] = str(height)
  540. driver = UserSettings.Get(group='display', key='driver', subkey='type')
  541. os.environ['GRASS_RENDER_IMMEDIATE'] = driver
  542. os.environ['GRASS_BACKGROUNDCOLOR'] = '{:02x}{:02x}{:02x}'.format(*bgcolor)
  543. os.environ['GRASS_TRUECOLOR'] = "TRUE"
  544. if transparent:
  545. os.environ['GRASS_TRANSPARENT'] = "TRUE"
  546. else:
  547. os.environ['GRASS_TRANSPARENT'] = "FALSE"
  548. os.environ['GRASS_PNGFILE'] = str(filename)
  549. def createNoDataBitmap(imageWidth, imageHeight, text="No data"):
  550. """!Creates 'no data' bitmap.
  551. Used when requested bitmap is not available (loading data was not successful) or
  552. we want to show 'no data' bitmap.
  553. @param imageWidth image width
  554. @param imageHeight image height
  555. """
  556. Debug.msg(4, "createNoDataBitmap: w={}, h={}, text={}".format(imageWidth,
  557. imageHeight, text))
  558. bitmap = wx.EmptyBitmap(imageWidth, imageHeight)
  559. dc = wx.MemoryDC()
  560. dc.SelectObject(bitmap)
  561. dc.Clear()
  562. text = _(text)
  563. dc.SetFont(wx.Font(pointSize=40, family=wx.FONTFAMILY_SCRIPT,
  564. style=wx.FONTSTYLE_NORMAL, weight=wx.FONTWEIGHT_BOLD))
  565. tw, th = dc.GetTextExtent(text)
  566. dc.DrawText(text, (imageWidth - tw) / 2, (imageHeight - th) / 2)
  567. dc.SelectObject(wx.NullBitmap)
  568. return bitmap
  569. def read2_command(*args, **kwargs):
  570. kwargs['stdout'] = gcore.PIPE
  571. kwargs['stderr'] = gcore.PIPE
  572. ps = gcore.start_command(*args, **kwargs)
  573. stdout, stderr = ps.communicate()
  574. return ps.returncode, stdout, stderr
  575. def test():
  576. import shutil
  577. from core.layerlist import LayerList, Layer
  578. from animation.data import AnimLayer
  579. from animation.utils import layerListToCmdsMatrix
  580. layerList = LayerList()
  581. layer = AnimLayer()
  582. layer.mapType = 'strds'
  583. layer.name = 'JR'
  584. layer.cmd = ['d.rast', 'map=elev_2007_1m']
  585. layerList.AddLayer(layer)
  586. layer = Layer()
  587. layer.mapType = 'vect'
  588. layer.name = 'buildings_2009_approx'
  589. layer.cmd = ['d.vect', 'map=buildings_2009_approx',
  590. 'color=grey']
  591. layer.opacity = 50
  592. layerList.AddLayer(layer)
  593. bPool = BitmapPool()
  594. mapFilesPool = MapFilesPool()
  595. tempDir = '/tmp/test'
  596. if os.path.exists(tempDir):
  597. shutil.rmtree(tempDir)
  598. os.mkdir(tempDir)
  599. # comment this line to keep the directory after prgm ends
  600. # cleanUp = CleanUp(tempDir)
  601. # import atexit
  602. # atexit.register(cleanUp)
  603. prov = BitmapProvider(bPool, mapFilesPool, tempDir,
  604. imageWidth=640, imageHeight=480)
  605. prov.renderingStarted.connect(
  606. lambda count: sys.stdout.write("Total number of maps: {}\n".format(count)))
  607. prov.renderingContinues.connect(
  608. lambda current, text: sys.stdout.write("Current number: {}\n".format(current)))
  609. prov.compositionStarted.connect(
  610. lambda count: sys.stdout.write("Composition: total number of maps: {}\n".format(count)))
  611. prov.compositionContinues.connect(
  612. lambda current, text: sys.stdout.write("Composition: Current number: {}\n".format(current)))
  613. prov.mapsLoaded.connect(
  614. lambda: sys.stdout.write("Maps loading finished\n"))
  615. cmdMatrix = layerListToCmdsMatrix(layerList)
  616. prov.SetCmds(cmdMatrix, [l.opacity for l in layerList])
  617. app = wx.App()
  618. prov.Load(bgcolor=(13, 156, 230), nprocs=4)
  619. for key in bPool.keys():
  620. if key is not None:
  621. bPool[key].SaveFile(os.path.join(tempDir, key + '.png'), wx.BITMAP_TYPE_PNG)
  622. # prov.Unload()
  623. # prov.SetCmds(cmdMatrix, [l.opacity for l in layerList])
  624. # prov.Load(bgcolor=(13, 156, 230))
  625. # prov.Unload()
  626. # newList = LayerList()
  627. # prov.SetCmds(layerListToCmdsMatrix(newList), [l.opacity for l in newList])
  628. # prov.Load()
  629. # prov.Unload()
  630. # mapFilesPool.Clear()
  631. # bPool.Clear()
  632. # print bPool.keys(), mapFilesPool.keys()
  633. if __name__ == '__main__':
  634. test()