
    ը	f%}                        d Z ddlZddlZddlZddlZddlmZmZ ddlm	Z	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZmZmZmZ dd	lmZmZ ddlmc mZ dd
lmZ ddlm Z   G d d      Z! G d d      Z" G d d      Z#d Z$d Z%d Z& G d d      Z' G d de'      Z( G d de'      Z) G d d      Z*d Z+d"dZ,d Z-d  Z.e/d!k(  r e.        yy)#a  
@package animation.provider

@brief Animation files and bitmaps management

Classes:
 - mapwindow::BitmapProvider
 - mapwindow::BitmapRenderer
 - mapwindow::BitmapComposer
 - mapwindow::DictRefCounter
 - mapwindow::MapFilesPool
 - mapwindow::BitmapPool
 - mapwindow::CleanUp

(C) 2013 by the GRASS Development Team

This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.

@author Anna Petrasova <kratochanna gmail.com>
    N)ProcessQueue)
GExceptionDecodeString)UserSettings)Debug)autoCropImageFromFile)HashCmdHashCmdsGetFileFromCmdGetFileFromCmds)EmptyBitmapBitmapFromImage)cmdlist_to_tuple)Signalc                   `    e Zd ZdZ	 ddZddZd Zd Zd Zd Z	d	 Z
dd
Zd Zd Zd Zd Zy)BitmapProviderzClass for management of image files and bitmaps.

    There is one instance of this class in the application.
    It handles both 2D and 3D animations.
    c                 J   || _         || _        || _        || _        || _        g | _        g | _        g | _        g | _        d | _	        g | _
        g | _        t        | j                  | j                  | j                  | j                        | _        t        | j                  | j                  | j                   | j                  | j                        | _        t!        d      | _        t!        d      | _        t!        d      | _        t!        d      | _        t!        d      | _        t!        d      | _        t!        d      | _        | j                  j&                  j1                  | j&                         | j                  j(                  j1                  | j(                         y )NzBitmapProvider.renderingStartedz!BitmapProvider.compositionStartedz!BitmapProvider.renderingContinuesz#BitmapProvider.compositionContinuesz BitmapProvider.renderingFinishedz"BitmapProvider.compositionFinishedzBitmapProvider.mapsLoaded)_bitmapPool_mapFilesPool
imageWidthimageHeight_tempDir_uniqueCmds_cmdsForComposition
_opacities_cmds3D_regionFor3D_regions_regionsForUniqueCmdsBitmapRenderer	_rendererBitmapComposer	_composerr   renderingStartedcompositionStartedrenderingContinuescompositionContinuesrenderingFinishedcompositionFinished
mapsLoadedconnect)self
bitmapPoolmapFilesPooltempDirr   r   s         3/usr/lib/grass83/gui/wxpython/animation/provider.py__init__zBitmapProvider.__init__0   sc    &) 	 '#%  %'"'t@P@P
 (MMOO
 !''H I"()L"M"()L"M$*+P$Q!!'(J!K#)*N#O  !<=))11$2I2IJ++33D4M4MN    Nc                 $   t        j                  ddj                  t        |                   | j                  j                  |       | j                  j                  |       | j                  j                  |       | j                          y)a  Sets commands to be rendered with opacity levels.
        Applies to 2D mode.

        :param cmdsForComposition: list of lists of command lists
                                   [[['d.rast', 'map=elev_2001'], ['d.vect', 'map=points']], # g.pnmcomp
                                   [['d.rast', 'map=elev_2002'], ['d.vect', 'map=points']],
                                   ...]
        :param opacities: list of opacity values
        :param regions: list of regions
           z!BitmapProvider.SetCmds: {n} lists)nN)	r   msgformatlenr   extendr   r   _getUniqueCmds)r-   cmdsForComposition	opacitiesregionss       r1   SetCmdszBitmapProvider.SetCmdsZ   sq     			299C@R<S9T	
 	  ''(:;y)W%r3   c                 ~    t        j                  ddj                  t        |                   || _        || _        y)zSets commands for 3D rendering.

        :param cmds: list of commands m.nviz.image (cmd as a list)
        :param region: for 3D rendering
        r5   z&BitmapProvider.SetCmds3D: {c} commandscN)r   r7   r8   r9   r   r   )r-   cmdsregions      r1   	SetCmds3DzBitmapProvider.SetCmds3Dn   s4     			!=DDs4yDQR"r3   c                    t               }t        | j                  | j                        D ]g  \  }}|D ]]  }|r=|j	                  t        |      t        t        |j                                     f       B|j	                  t        |      df       _ i t        t        |            }|D cg c]  }|d   	 c}| _	        | j                  j                  |D cg c]  }|d   rt        |d         nd c}       yc c}w c c}w )zPReturns list of unique commands.
        Takes into account the region assigned.Nr      )listzipr   r   appendtuplesorteditemssetr   r    r:   dict)r-   uniquecmdListrD   cmdcmdAndRegions         r1   r;   zBitmapProvider._getUniqueCmdsx   s     "4#;#;T]]K 	6OGV 6MM5:uVFLLN5K/L"MNMM5:t"45	6	6 c&k"@FGLOG"")) %+  *6a\!_%dB	
 Hs   -C>Dc                    t        j                  dd       | j                  rt        | j                  | j
                        D ]  \  }}| j                  t        ||      =  t        | j                  | j                        D ]  \  }}| j                  t        ||      =  g | _        g | _        g | _        g | _        g | _        | j                  rg | _        d| _        yy)z[Unloads currently loaded data.
        Needs to be called before setting new data.
        r5   zBitmapProvider.UnloadN)r   r7   r   rI   r   r    r   r
   r   r   r   r   r   r   )r-   rR   rD   rQ   s       r1   UnloadzBitmapProvider.Unload   s     			!,-##"4#3#3T5O5OP =V&&wsF';<= $'t'?'?#O @$$Xgv%>?@!D')D$ DODM)+D&<<DL $D r3   c                 v   d}t        ||      D ]  \  }}t        | j                  ||      }|s^t        j                  j                  |      r?| j                  j                  t        ||            | j                  | j                  fk(  r}|dz  } t        j                  ddj                  |             |S )zDetermines how many files will be rendered.

        :param uniqueCmds: list of commands which are to be rendered
        :param force: if forced rerendering
        :param regions: list of regions assigned to the commands
        r   rG      z3BitmapProvider._dryRender: {c} files to be renderedrA   )rI   r   r   ospathexistsr   GetSizer
   r   r   r   r7   r8   )r-   
uniqueCmdsr>   forcecountrR   rD   filenames           r1   
_dryRenderzBitmapProvider._dryRender   s     z73 		KC%dmmS&AHGGNN8,&&..wsF/CDOOT%5%567 QJE		 			DKKeKT	
 r3   c                 >   d}t        ||      D ]e  \  }}|sYt        ||      | j                  v rA| j                  t        ||         j                         | j                  | j
                  fk(  ra|dz  }g t        j                  ddj                  |             |S )a  Determines how many lists of (commands) files
        will be composed (with g.pnmcomp).

        :param cmdLists: list of commands lists which are to be composed
        :param regions: list of regions assigned to the commands
        :param force: if forced rerendering
        r   rG   r5   z4BitmapProvider._dryCompose: {c} files to be composedrA   )	rI   r   r   r[   r   r   r   r7   r8   )r-   cmdListsr>   r]   r^   rQ   rD   s          r1   _dryComposezBitmapProvider._dryCompose   s     "8W5 	OGVWf-1A1AA$$Xgv%>?GGIOOT%5%567 QJE	 			ELLuLU	
 r3   c                 `   t        j                  ddj                  |||             g }g }| j                  r6|j	                  | j                         |j	                  | j
                         | j                  rC|j	                  | j                         |j	                  dgt        | j                        z         | j                  |||      }| j                  j                  |       d| j                  vs|r-t        | j                  | j                        | j                  d<   | j                  j!                  ||| j"                  |||      }| j$                  j                          |s| j&                  j                          y| j(                  r| j+                  | j(                  | j,                  |      }| j.                  j                  |       | j0                  j3                  | j(                  | j,                  | j4                  |||       | j6                  j                          | j                  rS| j                  D ]D  }t9        j:                  t=        | j>                  |d            | j                  tA        |gd      <   F | j&                  j                          y)	an  Loads data, both 2D and 3D. In case of 2D, it creates composites,
        even when there is only 1 layer to compose (to be changed for speedup)

        :param force: if True reload all data, otherwise only missing data
        :param bgcolor: background color as a tuple of 3 values 0 to 255
        :param nprocs: number of procs to be used for rendering
        r5   z7BitmapProvider.Load: force={f}, bgcolor={b}, nprocs={n})fbr6   N)r]   r^   )regionFor3Dbgcolorr]   nprocs)ri   r]   rj   )!r   r7   r8   r   r:   r    r   r9   r`   r%   emitr   createNoDataBitmapr   r   r"   Renderr   r)   r+   r   rc   r   r&   r$   Composer   r*   wxBitmapr   r   r   )	r-   r]   ri   rj   rC   r>   r^   okrR   s	            r1   LoadzBitmapProvider.Load   sC    			117%7f1U	

 KK(()NN4556<<KK%NND6C$556gU;"""/ t'''5%7!1!1&DT" ^^"")) # 
 	##%OO  "##$$(($--u % E ##((u(5NN""(( #  $$))+<<|| :<))"4==#t<;  3%!67
 	r3   c                     t        j                  dd       | j                  j                          | j                  j                          y)z&Requests to stop rendering/compositionr5   z#BitmapProvider.RequestStopRenderingN)r   r7   r"   RequestStopRenderingr$   RequestStopComposingr-   s    r1   rt   z#BitmapProvider.RequestStopRendering  s1    		!:;++-++-r3   c                 d    	 | j                   |   }|S # t        $ r | j                   d   }Y |S w xY w)zReturns bitmap with given key
        or 'no data' bitmap if no such key exists.

        :param dataId: name of bitmap
        N)r   KeyError)r-   dataIdbitmaps      r1   	GetBitmapzBitmapProvider.GetBitmap  sD    	,%%f-F   	,%%d+F	,s    //c                     t        j                  ddj                  ||             ||c| _        | _        |x| j
                  _        | j                  _        |x| j
                  _        | j                  _        y)z.Sets size when size of related window changes.   z.BitmapProvider.WindowSizeChanged: w={w}, h={h})whN)r   r7   r8   r   r   r$   r"   )r-   widthheights      r1   WindowSizeChangedz BitmapProvider.WindowSizeChanged$  sg    		<CCevCV	

 -26))@EE!DNN$=BHH"T^^%?r3   c                    t        j                  ddj                  |             t        j                  d      \  }}t        j                  |       t        | j                  | j                  |dd       t        j                  d	d
t        |      z          t        |      }t        |d   fi |d	   \  }}}|dk(  rt        t        |            S t        j                  |       t!        |      )zCreates raster legend with d.legend

        :param cmd: d.legend command as a list

        :return: bitmap with legend
        r}   z#BitmapProvider.LoadOverlay: cmd={c}rA   .png)suffixT)r   r   r   transparentri   rG   zRender raster legend r   )r   r7   r8   tempfilemkstemprX   close_setEnvironmentr   r   strr   read2_commandr   r	   remover   )r-   rR   fileHandlerr_   cmdTuple
returncodestdoutmessagess           r1   LoadOverlayzBitmapProvider.LoadOverlay0  s     			!:AACAHI ( 0 0 ?X
OO	
 			!,s8}<=#C('4Xa['PHQK'P$
FH?"#8#BCCIIhX&&r3   )    N)F)   r   r      )__name__
__module____qualname____doc__r2   r?   rE   r;   rU   r`   rc   rr   rt   r{   r   r    r3   r1   r   r   )   sN     NQ(OT(#
&%(22?B.

I'r3   r   c                   "    e Zd ZdZd Zd Zd Zy)r!   z.Class which renders 2D and 3D images to files.c                 x    || _         || _        || _        || _        t	        d      | _        d| _        d| _        y )Nz!BitmapRenderer.renderingContinuesF)r   r   r   r   r   r'   _stopRendering_isRendering)r-   r/   r0   r   r   s        r1   r2   zBitmapRenderer.__init__R  s>    )$&"()L"M#!r3   c                    t        j                  dd       d}d}g }	g }
g }g }t        ||      D ]  \  }}|d   dk(  rd}t        | j                  ||      }|swt
        j                  j                  |      rX| j                  j                  t        ||            | j                  | j                  fk(  r|| j                  t        ||      <   |j                  ||f        t        |      }d}d| _        |D ]  \  }}|dz  }t!               }|d   dk(  r7t#        t$        | j                  | j                  | j                  ||||f	      }n6t#        t&        | j                  | j                  | j                  ||||f	      }|j)                          |
j                  |       |	j                  |       |j                  ||f       |dz  }||k(  s||k(  rt+        t        |            D ]  }|	|   j-                          |
|   j/                         }|| j                  t        ||   d   ||   d         <   | j                  j1                  t        ||   d   ||   d         | j                  | j                  f        d}g }	g }
g }| j2                  j5                  |t7        d
             | j8                  sd| _        d} n d| _        | S )a  Renders all maps and stores files.

        :param cmdList: list of rendering commands to run
        :param regions: regions for 2D rendering assigned to commands
        :param regionFor3D: region for setting 3D view
        :param bgcolor: background color as a tuple of 3 values 0 to 255
        :param force: if True reload all data, otherwise only missing data
        :param nprocs: number of procs to be used for rendering
        rW   zBitmapRenderer.Renderr   zm.nviz.imageNFTrG   targetargszRendering map layerscurrenttext)r   r7   rI   r   r   rX   rY   rZ   r   r[   r
   r   r   rJ   r9   r   r   r   RenderProcess3DRenderProcess2DstartrangejoingetSetSizer'   rk   _r   )r-   rQ   r>   rh   ri   r]   rj   r^   
proc_count	proc_list
queue_listcmd_listfilteredCmdListrR   rD   r_   mapNumstoppedqpis                        r1   rm   zBitmapRenderer.Render\  s    			!,- 
	
w0 	2KC1v'%dmmS&AHGGNN8,&&..wsF/CDOOT%5%567 <D""73#78""C=1	2 _% * =	KCQJE A1v'*((# *(( GGIa QOOS&M*!OJV#us8}- 	AaL%%')!}002H ! &&AA? &&..AA?$*:*:;	 
	
##((Q?U=V(W""&+#{=	~ "{r3   c                 ,    | j                   rd| _        yy)zRequests to stop rendering.TN)r   r   rv   s    r1   rt   z#BitmapRenderer.RequestStopRendering      "&D r3   N)r   r   r   r   r2   rm   rt   r   r3   r1   r!   r!   O  s    8"fP'r3   r!   c                   "    e Zd ZdZd Zd Zd Zy)r#   zBClass which handles the composition of image files with g.pnmcomp.c                     || _         || _        || _        || _        || _        t        d      | _        d| _        d| _        y )Nz!BitmapComposer.composingContinuesF)	r   r   r   r   r   r   r(   _stopComposing_isComposing)r-   r0   r/   r.   r   r   s         r1   r2   zBitmapComposer.__init__  sF    )%$&$*+N$O!#!r3   c                 x   t        j                  dd       d}d}g }	g }
g }g }t        ||      D ]  \  }}|st        ||      | j                  v rq| j                  t        ||         j                         | j                  | j                  fk(  r1| j                  t        ||         | j                  t        ||      <   |j                  ||f        t        |      }d| _
        |D ]  \  }}|dz  }t               }t        t        | j                  | j                  | j                  |||||f      }|j                          |
j                  |       |	j                  |       |j                  ||f       |dz  }||k(  s||k(  rt!        t        |            D ]  }|	|   j#                          |
|   j%                         }|Ft'        | j                  | j                  d	      | j                  t        ||   d   ||   d         <   qt)        t+        j,                  |            | j                  t        ||   d   ||   d         <   t/        j0                  |        d}g }	g }
g }| j2                  j5                  |t7        d
             | j8                  sd| _         d| _
        y d| _
        y)a  Performs the composition of ppm/pgm files.

        :param cmdLists: lists of rendering commands lists to compose
        :param regions: regions for 2D rendering assigned to commands
        :param opacityList: list of lists of opacity values
        :param bgcolor: background color as a tuple of 3 values 0 to 255
        :param force: if True reload all data, otherwise only missing data
        :param nprocs: number of procs to be used for rendering
        rW   zBitmapComposer.Composer   TrG   r   NzFailed to render)r   zOverlaying map layersr   F)r   r7   rI   r   r   r[   r   r   rJ   r9   r   r   r   CompositeProcessr   r   r   r   r   rl   r   ro   ImagerX   r   r(   rk   r   r   )r-   rb   r>   opacityListri   r]   rj   r^   r   r   r   	cmd_listsfilteredCmdListsrQ   rD   numr   r   r   r_   s                       r1   rn   zBitmapComposer.Compose  s    			!-. 
	
	"8W5 	7OGVWf-1A1AA$$Xgv%>?GGIOOT%5%567
 ?C>N>NWf-?  '6!:; ##Wf$56	7 "# / 4	OGVQJEA'OO$$MM	A GGIa Qgv./!OJ V#u|s9~. ,AaL%%')!}002H' / OOT-=-=DV (($Yq\!_il1oF ,BHHX,>? (($Yq\!_il1oF 		(+, 
	
	%%**A&=$> +  ""&+#!m4	l "r3   c                 ,    | j                   rd| _        yy)z!Requests to stop the composition.TN)r   r   rv   s    r1   ru   z#BitmapComposer.RequestStopComposing7  r   r3   N)r   r   r   r   r2   rn   ru   r   r3   r1   r#   r#     s    L	"]"~'r3   r#   c                    t        |||      }d}t        | ||||       |r&t        j                  di |t        j
                  d<   t        |      }	t        |	d   fi |	d   \  }
}}|
dk7  r`t        j                  d|z          |j                  d       |rt        j
                  j                  d       t	        j                  |       y|rt        j
                  j                  d       |j                  |       y)	a  Render raster or vector files as ppm image and write the
       resulting ppm filename in the provided file queue

    :param imageWidth: image width
    :param imageHeight: image height
    :param tempDir: directory for rendering
    :param cmd: d.rast/d.vect command as a list
    :param region: region as a dict or None
    :param bgcolor: background color as a tuple of 3 values 0 to 255
    :param fileQueue: the inter process communication queue
                      storing the file name of the image
    Tr   GRASS_REGIONr   rG   Rendering failed:
Nr   )r   r   gcore
region_envrX   environr   r   warningputpopr   )r   r   r0   rR   rD   ri   	fileQueuer_   transparencyr   r   r   r   s                r1   r   r   =  s     gsF3HL K|W %*%5%5%?%?

>"$H#0!#L#L JQ+h67dJJNN>*
		(


~&MM(r3   c                    t        ||d      }t        j                  dddi|t        j                  d<   t        j                  ddt        |      z          t        |      }t        j                  j                  |      d   |d   d<   d	| |fz  |d   d
<   d|d   d<   dj                  |D 	cg c]  }	t        |	       c}	      x|d   d<   }t        |d   fi |d   \  }
}}|
dk7  rIt        j                  d|z          |j                  d       t        j                  j                  d       yt        j                  j                  d       |j                  |       yc c}	w )a  Renders image with m.nviz.image and writes the
       resulting ppm filename in the provided file queue

    :param imageWidth: image width
    :param imageHeight: image height
    :param tempDir: directory for rendering
    :param cmd: m.nviz.image command as a list
    :param region: region as a dict
    :param bgcolor: background color as a tuple of 3 values 0 to 255
    :param fileQueue: the inter process communication queue
                      storing the file name of the image
    Nregion3dTr   rG   zRender image to file r   outputz%d,%dsizeppmr8   :ri   r   r   )r   r   r   rX   r   r   r7   r   r   rY   splitextr   r   r   r   r   )r   r   r0   rR   rD   ri   r   r_   r   partr   r   r   s                r1   r   r   c  s@    gsD1H!&!1!1!J4!J6!JBJJ~	IIa(3x=89$HGG,,X6q9HQK!Z$==HQK!HQK'*xxw0OtT0O'PPHQK	W#0!#L#L JQ+h67d


~&JJNN>"MM( 1Ps   /E%c                    g }g }	|D ];  }
|j                  t        ||
|             |	j                  t        ||
|d             = t        |||      }t        | ||d|       |D cg c]  }t	        |       }}dj                  |D cg c]  }t	        |       c}      }t        ddddj                  t        |            z  ddj                  t        |	            z  ddj                  t        |            z  || ||		      \  }}}|d
k7  r?t        j                  d|z          |j                  d       t        j                  |       y|j                  |       yc c}w c c}w )a-  Performs the composition of image ppm files and writes the
       resulting ppm filename in the provided file queue

    :param imageWidth: image width
    :param imageHeight: image height
    :param tempDir: directory for rendering
    :param cmdList: list of d.rast/d.vect commands
    :param region: region as a dict or None
    :param opacites: list of opacities
    :param bgcolor: background color as a tuple of 3 values 0 to 255
    :param fileQueue: the inter process communication queue
                      storing the file name of the image
    pgmFr   r   z	g.pnmcompTz%s,)	overwriteinputmaskopacityri   r   r   r   r   zRendering composite failed:
N)rJ   r   r   r   r   r   r   reversedr   r   r   rX   r   )r   r   r0   rQ   rD   r=   ri   r   mapsmasksrR   r_   opr   r   r   r   s                    r1   r   r     sQ   " DE BN7C89^GS&%@AB w8HKug $--RR-I-hhg6dD	67G#0SXXhtn--CHHXe_--sxx 344
$ J Q5@Ad
		(MM() .6s   &E!	E&c                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
DictRefCounterzBase class storing map files/bitmaps (emulates dictionary).
    Counts the references to know which files/bitmaps to delete.
    c                      i | _         i | _        y r   )
dictionaryreferenceCountrv   s    r1   r2   zDictRefCounter.__init__  s     r3   c                      | j                   |   S r   r   r-   keys     r1   __getitem__zDictRefCounter.__getitem__  s    s##r3   c                     || j                   |<   || j                  vrd| j                  |<   n| j                  |xx   dz  cc<   t        j                  ddj	                  |             y )NrG   r}   z*DictRefCounter.__setitem__: +1 for key {k}k)r   r   r   r7   r8   )r-   r   values      r1   __setitem__zDictRefCounter.__setitem__  sa    $d)))'(D$$)$		!AHH3HOPr3   c                     || j                   v S r   r   r   s     r1   __contains__zDictRefCounter.__contains__  s    doo%%r3   c                 ~    | j                   |xx   dz  cc<   t        j                  ddj                  |             y )NrG   r}   z*DictRefCounter.__delitem__: -1 for key {k}r   )r   r   r7   r8   r   s     r1   __delitem__zDictRefCounter.__delitem__  s5    C A% 		!AHH3HOPr3   c                 6    | j                   j                         S r   )r   keysrv   s    r1   r   zDictRefCounter.keys  s    ##%%r3   c                     t        j                  dd       | j                  j                         j	                         D ]2  }|| j
                  |   dk  s| j                  |= | j
                  |= 4 y)z+Clears items which are not needed any more.r   zDictRefCounter.ClearNr   )r   r7   r   copyr   r   r   s     r1   ClearzDictRefCounter.Clear  sh    		!+,??'')..0 	1C&&s+q0,++C0		1r3   N)r   r   r   r   r2   r   r   r   r   r   r   r   r3   r1   r   r     s,    !$Q&Q&1r3   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)MapFilesPoolz Stores rendered images as files.c                 <    t         j                  |        i | _        y r   )r   r2   r   rv   s    r1   r2   zMapFilesPool.__init__  s    %	r3   c                 "    || j                   |<   y r   r   )r-   r   r   s      r1   r   zMapFilesPool.SetSize  s    		#r3   c                      | j                   |   S r   r  r   s     r1   r[   zMapFilesPool.GetSize  s    yy~r3   c                    t        j                  dd       t        | j                  j	                               D ]  }| j
                  |   dk  st        j                  j                  | j                  |         \  }}t        j                  | j                  |          |dk(  rt        j                  |dz          | j                  |= | j
                  |= | j                  |=  y)zVRemoves files which are not needed anymore.
        Removes both ppm and pgm.
        r   zMapFilesPool.Clearr   z.ppmz.pgmN)r   r7   rH   r   r   r   rX   rY   r   r   r   )r-   r   nameexts       r1   r   zMapFilesPool.Clear  s     			!)*,,./ 	#C""3'1,GG,,T__S-AB	c		$//#./&=IIdVm,OOC('',IIcN	#r3   N)r   r   r   r   r2   r   r[   r   r   r3   r1   r  r    s    *#r3   r  c                       e Zd ZdZd Zy)
BitmapPoolz+Class storing bitmaps (emulates dictionary)c                 .    t         j                  |        y r   )r   r2   rv   s    r1   r2   zBitmapPool.__init__  s    %r3   N)r   r   r   r   r2   r   r3   r1   r
  r
     s
    5&r3   r
  c                       e Zd ZdZd Zd Zy)CleanUpz&Responsible for cleaning up the files.c                     || _         y r   )r   )r-   r0   s     r1   r2   zCleanUp.__init__
  s	    r3   c                    dd l }t        j                  j                  | j                        rM	 |j                  | j                         t        j                  ddj                  | j                               y y # t        $ r; t        j                  t        d      j                  | j                               Y y w xY w)Nr   r}   zCleanUp: removed directory {t})tzDirectory {t} not removed.)shutilrX   rY   rZ   r   rmtreer   r7   r8   OSErrorr   r   r   )r-   r  s     r1   __call__zCleanUp.__call__  s    77>>$--(Wdmm,		!=DDt}}DUV )  Wa <=DDt}}DUVWs   AA< <AC ?C N)r   r   r   r   r2   r  r   r3   r1   r  r    s    0 Wr3   r  c           
      *   t        j                  ddj                  | ||||             t        |       t        j
                  d<   t        |      t        j
                  d<   t        j                  ddd	      }|t        j
                  d
<   dj                  |d   |d   |d         t        j
                  d<   dt        j
                  d<   |rdt        j
                  d<   ndt        j
                  d<   t        |      t        j
                  d<   y)a  Sets environmental variables for 2D rendering.

    :param width: rendering width
    :param height: rendering height
    :param filename: file name
    :param transparent: use transparency
    :param bgcolor: background color as a tuple of 3 values 0 to 255
    r}   zR_setEnvironment: width={w}, height={h}, filename={f}, transparent={t}, bgcolor={b})r~   r   re   r  rf   GRASS_RENDER_WIDTHGRASS_RENDER_HEIGHTdisplaydrivertype)groupr   subkeyGRASS_RENDER_IMMEDIATEz{r:02x}{g:02x}{b:02x}r   rG   r5   )rgrf   GRASS_RENDER_BACKGROUNDCOLORTRUEGRASS_RENDER_TRUECOLORGRASS_RENDER_TRANSPARENTFALSEGRASS_RENDER_FILEN)r   r7   r8   r   rX   r   r   Get)r   r   r_   r   ri   r  s         r1   r   r     s     
II		55;Vv[G 6< 6
 (+5zBJJ#$(+FBJJ$%I8FKF+1BJJ'(1H1O1O
!*
gaj 2P 2BJJ-. ,2BJJ'(17

-.18

-.&)(mBJJ"#r3   c                 6   t        j                  ddj                  | ||             t        | |      }t	        j
                         }|j                  |       |j                          t        |      }|j                  t	        j                  dt        j                  t        j                  t        j                               |j                  |      \  }}|j                  || |z
  dz  ||z
  dz         |j                  t        j                          |S )zCreates 'no data' bitmap.

    Used when requested bitmap is not available (loading data was not successful) or
    we want to show 'no data' bitmap.

    :param imageWidth: image width
    :param imageHeight: image height
    r   z*createNoDataBitmap: w={w}, h={h}, text={t})r~   r   r  (   )	pointSizefamilystyleweightr5   )r   r7   r8   r   ro   MemoryDCSelectObjectr   r   SetFontFontFONTFAMILY_SCRIPTFONTSTYLE_NORMALFONTWEIGHT_BOLDGetTextExtentDrawText
NullBitmap)r   r   r   rz   dctwths          r1   rl   rl   8  s     
II	4;;K4 	< 	
 [1F	BOOFHHJT7DJJ
''%%%%		
 d#FBKKzB1,{R/?A.EFOOBMM"Mr3   c                      t         j                  |d<   t         j                  |d<   t        j                  | i |}|j                         \  }}|j                  t        |      t        |      fS )Nr   stderr)r   PIPEstart_commandcommunicater   r   )r   kwargspsr   r;  s        r1   r   r   Z  s^    zzF8zzF8			d	-f	-B^^%NFF==,v.V0DDDr3   c                     dd l } ddlm}m} ddlm} ddlm} dd lm	} |j                           |       } |       }d|_        d|_        ddg|_        |j                  |        |       }d	|_        d
|_        g d|_        d|_        |j                  |       t!               }t#               }	d}
t$        j&                  j)                  |
      r| j+                  |
       t%        j,                  |
       t/        ||	|
dd      }|j0                  j3                  d        |j4                  j3                  d        |j6                  j3                  d        |j8                  j3                  d        |j:                  j3                  d         ||      }|j=                  ||D cg c]  }|j                   c}       t?        j@                         }|jC                  dd       |jE                         D ]I  }|||   jG                  t$        j&                  jI                  |
|dz         t>        jJ                         K y c c}w )Nr   )	LayerListLayer)	AnimLayer)layerListToCmdsMatrixstrdsJRzd.rastzmap=elev_2007_1mvectorbuildings_2009_approx)zd.vectzmap=buildings_2009_approxz
color=grey2   z	/tmp/testr   r   )r   r   c                 `    t         j                  j                  dj                  |             S )NzTotal number of maps: {c}
rA   sysr   writer8   rg   s    r1   <lambda>ztest.<locals>.<lambda>  s$    cjj&&'D'K'Ke'K'TU r3   c                 `    t         j                  j                  dj                  |             S )NzCurrent number: {c}
rA   rL  r   s     r1   rO  ztest.<locals>.<lambda>  s&    cjj..#**W*5
 r3   c                 `    t         j                  j                  dj                  |             S )Nz'Composition: total number of maps: {c}
rA   rL  rg   s    r1   rO  ztest.<locals>.<lambda>  s&    cjj&&6===F
 r3   c                 `    t         j                  j                  dj                  |             S )Nz!Composition: Current number: {c}
rA   rL  r   s     r1   rO  ztest.<locals>.<lambda>  s&    cjj..077'7B
 r3   c                  @    t         j                  j                  d      S )NzMaps loading finished
)rM  r   rN  r   r3   r1   rO  ztest.<locals>.<lambda>  s    CJJ$4$45N$O r3   )         r   )ri   rj   r   )&r  core.layerlistrB  rC  animation.datarD  animation.utilsrE  grass.temporaltemporalinitmapTyper  rR   AddLayerr   r
  r  rX   rY   rZ   r  mkdirr   r%   r,   r'   r&   r(   r+   r?   ro   Apprr   r   SaveFiler   BITMAP_TYPE_PNG)r  rB  rC  rD  rE  tgis	layerListlayerbPoolr/   r0   prov	cmdMatrixappr   s                  r1   testrj  b  s   /(5!IIKIKEEMEJ-.EIuGEEM(EJEEIEMuLE>LG	ww~~ggHHW %w3TWXD!!U 	##	

 	##	

 	%%	

 	OOOP%i0ILL	BuU]]BC
&&(CIInQI/zz| Y?#JWcFl CREWEWXY Cs   *I
__main__)zNo data)0r   rX   rM  ro   r   multiprocessingr   r   	core.gcmdr   r   core.settingsr   
core.debugr   
core.utilsr	   rY  r
   r   r   r   gui_core.wrapr   r   grass.script.corescriptcorer   grass.script.taskr   grass.pydispatch.signalr   r   r!   r#   r   r   r   r   r  r
  r  r   rl   r   rj  r   r   r3   r1   <module>rw     s   * 
 
 	  * . &  , N N 6 ! ! . *c' c'L	x' x'vp' p'f#L!H0f%1 %1P#> #<& &W W"4@DE@Y` zF r3   