
    ը	f4                         d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ  G d d      Z G d	 d
e      Z G d de      Z G d de      Zy)a  
@package mapwin.analysis

@brief Map display controllers for analyses (profiling, measuring)

Classes:
 - analysis::AnalysisControllerBase
 - analysis::ProfileController
 - analysis::MeasureDistanceController

(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)
RunCommand)Notification)Signal)parse_key_valc                   N    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zdd
Zd Zy)AnalysisControllerBasezBBase class for analysis which require drawing line in map display.c                 X    || _         || _        d| _        d| _        d| _        d| _        y)zi

        :param giface: grass interface
        :param mapWindow: instance of BufferedMapWindow
        N)_giface
_mapWindow_registeredGraphics_graphicsType_oldMouseUse
_oldCursorselfgiface	mapWindows      0/usr/lib/grass83/gui/wxpython/mapwin/analysis.py__init__zAnalysisControllerBase.__init__"   s1     ##' !     c                 ,    t        | j                        S )z+Returns True if analysis mode is activated.)boolr   r   s    r   IsActivezAnalysisControllerBase.IsActive1   s    D,,--r   c                 X   | j                   j                         s2| j                   j                  ||gg      }|j                  dd       y| j                   j	                  d      j                         d   }| j                  j                  |      | j                  j                  d<   y)zHandles the actual start of drawing line
        and adding each new point.

        :param x,y: east north coordinates
        coordspenNameanalysisPenr   beginN)	r   GetAllItemsAddItemSetPropertyValGetItem	GetCoordsr   
Cell2Pixelmouse)r   xyitemr   s        r   _startzAnalysisControllerBase._start5   s     ''335++33QF83DD	=9 --55a8BBDRHF-1__-G-G-ODOO!!'*r   c                    | j                   j                  d   }| j                   j                  d   }||k(  ry| j                  j                  d      }|j	                         ||ggz   }|j                  |       | j                   j                          | j                  j                          | j                   j                          t        j                          | j                  |       y)ENew point added.

        :param x,y: east north coordinates
        r!   endNr   )r   r(   r   r%   r&   	SetCoords
ClearLinesDrawRefreshwx	SafeYield_doAnalysis)r   r)   r*   r!   r/   r+   r   s          r   	_addPointz AnalysisControllerBase._addPointD   s     %%g.oo##E*C<''//2!aVH,v""$  %%'!
 r   c                     t               )zxPerform the required analysis
        (compute distance, update profile)

        :param coords: EN coordinates
        NotImplementedErrorr   r   s     r   r6   z"AnalysisControllerBase._doAnalysisZ   s     "##r   c                     t               )z5Disconnect all mouse signals
        to stop drawing.r9   r   s    r   _disconnectAllz%AnalysisControllerBase._disconnectAllb   s     "##r   c                     t               )z"Connect all mouse signals to draw.r9   r   s    r   _connectAllz"AnalysisControllerBase._connectAllg       !##r   c                     t               )zReturns wx.Pen instance.r9   r   s    r   _getPenzAnalysisControllerBase._getPenk   r@   r   c                 
   | j                   j                  | j                   j                         | j                   j                  d   | j                   j                  d<   | j	                          | j                   j                  | j                         d| _        | j                   j                  d       |rI| j                   j                  | j                         | j                  | j                   j                  d<   yy)zdAnalysis mode is stopped.

        :param restore: if restore previous cursor, mouse['use']
        )pdcr!   r/   NF)renderuse)r   r1   pdcTmpr(   r=   UnregisterGraphicsToDrawr   	UpdateMapSetNamedCursorr   r   r   restores     r   StopzAnalysisControllerBase.Stopo   s    
 	""t'='=">'+'<'<W'Ee$001I1IJ#' !!!/ OO**4??;+/+<+<DOO!!%(	 r   c                 N   | j                   j                  d   | _        | j                   j                         | _        | j                   j                  | j                  d      | _        | j                          d| j                   j                  d<   d| j                   j                  d<   t        j                  ddt        j                  	      | j                   _        | j                  j                  d
| j                                | j                   j                  d       y)z`Init analysis: register graphics to map window,
        connect required mouse signals.
        rF   T)graphicsType	mapCoordsNlineboxred   colourwidthstyler   pencil)r   r(   r   GetNamedCursorr   RegisterGraphicsToDrawr   r   r?   r4   Pen
SHORT_DASHpenAddPenrB   rJ   r   s    r   StartzAnalysisControllerBase.Start   s     !OO11%8//88:#'??#I#I++t $J $
  	 (,e$'-e$ ffE"--P  ''t||~F 	&&x0r   NT)__name__
__module____qualname____doc__r   r   r,   r7   r6   r=   r?   rB   rM   r`    r   r   r   r      s8    L.P!,$$
$$=(1r   r   c                   6    e Zd ZdZd Zd Zd Zd Zd Zd	dZ	y)
ProfileControllerzWClass controls profiling in map display.
    It should be used inside ProfileFrame
    c                 b    t         j                  | ||       t        d      | _        d| _        y )Nr   r   z!ProfileController.transectChangedrQ   )r   r   r   transectChangedr   r   s      r   r   zProfileController.__init__   s-    ''Vy'Q%&IJ#r   c                 <    | j                   j                  |       y)z\Informs profile dialog that profile changed.

        :param coords: EN coordinates
        r   N)rk   emitr;   s     r   r6   zProfileController._doAnalysis   s    
 	!!!0r   c                     | j                   j                  j                  | j                         | j                   j                  j                  | j
                         y N)r   mouseLeftDown
disconnectr,   mouseLeftUpr7   r   s    r   r=   z ProfileController._disconnectAll   s:    %%00=##..t~~>r   c                     | j                   j                  j                  | j                         | j                   j                  j                  | j
                         y ro   )r   rp   connectr,   rr   r7   r   s    r   r?   zProfileController._connectAll   s:    %%--dkk:##++DNN;r   c                 x    t        j                  t        j                  ddd      dt         j                        S )Nr   d   rT   rU   )r4   r\   Colourr]   r   s    r   rB   zProfileController._getPen   s&    vvRYYq#q1"--PPr   c                 j    t         j                  | |       | j                  j                  g        y )NrL   r   )r   rM   rk   rm   rK   s     r   rM   zProfileController.Stop   s,    ##D'#:!!!,r   Nra   )
rb   rc   rd   re   r   r6   r=   r?   rB   rM   rf   r   r   rh   rh      s&    $1?<Q-r   rh   c                   B    e Zd ZdZd Zd Zd Zd Zd ZddZ	d Z
d	 Zy
)MeasureDistanceControllerz1Class controls measuring distance in map display.c                     t         j                  | ||       | j                  j                  j                  | _        d| _        d| _        d| _        y )Nrj           FrQ   )	r   r   r   Mapprojinfo	_projInfo
_totaldist
_useCtypesr   r   s      r   r   z"MeasureDistanceController.__init__   sD    ''Vy'Q,,55#r   c                 4    | j                  |d   |d          y)r.   r    N)MeasureDistr;   s     r   r6   z%MeasureDistanceController._doAnalysis   s    
 	VBZ0r   c                    | j                   j                  j                  | j                         | j                   j                  j                  | j
                         | j                   j                  j                  | j                         y ro   r   rp   rq   r,   rr   r7   mouseDClickrM   r   s    r   r=   z(MeasureDistanceController._disconnectAll   V    %%00=##..t~~>##..tyy9r   c                    | j                   j                  j                  | j                         | j                   j                  j                  | j
                         | j                   j                  j                  | j                         y ro   r   rp   rt   r,   rr   r7   r   rM   r   s    r   r?   z%MeasureDistanceController._connectAll   V    %%--dkk:##++DNN;##++DII6r   c                 N    t        j                  ddt         j                        S NgreenrT   rU   )r4   r\   r]   r   s    r   rB   z!MeasureDistanceController._getPen   s    vvWAR]]CCr   c                     | j                         sy t        j                  | |       | j                  j	                  t        d             y Nry   zMeasuring finishedr   r   rM   r
   WriteCmdLog_rK   s     r   rM   zMeasureDistanceController.Stop   9    }}##D'#:  #7!89r   c                    | j                         ryt        j                  |        d| _        | j                  j                  t        d      t        j                  z         | j                  d   dk7  r=| j                  d   }| j                  j                  t        d      dz   |z   d	z          n$| j                  j                  t        d
             | j                  d   dk(  r"	 ddlmc m} |j                          d| _        yy# t         $ r1}| j                  j                  t        d|z               Y d}~yd}~ww xY w)zjInit measurement routine that calculates map distance
        along transect drawn on map display
        Nr}   ZClick and drag with left mouse button to measure.%sDouble click with left button to clear.projxyunitszMeasuring distancez (z):zMeasuring distance:llr   Tz:Geodesic distance calculation is not available.
Reason: %s)r   r   r`   r   r
   WriteWarningr   oslinesepr   r   grass.lib.gislibgisG_begin_distance_calculationsr   ImportError)r   mapunitsgislibes       r   r`   zMeasureDistanceController.Start   s'    ==?$$T* 	!!:
 zz	
 >>&!T)~~g.HLL$$Q';%<t%Ch%NQU%UVLL$$Q'<%=>>>&!T)..446"& *  ))%'() s   ) D 	E'E  Ec                 `   | j                   j                  ||d      \  }\  }}t        |d      }| j                  d   }|dk(  r| j                  rd}t        j                  ||      \  }}| xj                  |z  c_        t        j                  | j                  |      \  }	}
|dk(  r|r|x}}
t        |      }t        |	      }| j                  d   dk(  sd	| j                  d
   vrst        t        j                  t        j                  ||            dz         }|dk  rd|z   }dt        d      ||t        d      ||
t        d      |t        d      df
z  }n(t        d      d|d|dt        d      d|d|
dd}| j                  j                  |t         j"                         |S )zeCalculate distance and print to output window.

        :param beginpt,endpt: EN coordinates
        F)screen   r   degreesmetersr   r   degreeunitg      ?r   ih  z#%s = %s %s
%s = %s %s
%s = %d %s
%ssegmentztotal distancebearingz#degrees (clockwise from grid-north)z<------------------------------------------------------------z =  
notification)r   Distanceroundr   r   r   
formatDistr   strintmathr   atan2r   r
   WriteLogr   MAKE_VISIBLE)r   beginptendptdistnortheastr   ddunitstdtdunitsstrdist
strtotdistanglemstrings                  r   r   z%MeasureDistanceController.MeasureDist  s    #oo66we6TmudT1~>>'*y T__H$$T84	64&&tAGW''FWa&W
>>&!T)XT^^F=S-STZZt%<=CDE qye>)"#)78B G )"#G 	gL4M4MNr   Nra   )rb   rc   rd   re   r   r6   r=   r?   rB   rM   r`   r   rf   r   r   r{   r{      s/    ;$1:
7
D:)V7r   r{   c                   B    e Zd ZdZd Zd Zd Zd Zd ZddZ	d Z
d	 Zy
)MeasureAreaControllerz-Class controls measuring area in map display.c                 B    t         j                  | ||       d| _        y )Nrj   polygon)r   r   r   r   s      r   r   zMeasureAreaController.__init__M  s    ''Vy'Q&r   c                 &    | j                  |       y)zRNew point added.

        :param coords: east north coordinates as a list
        N)MeasureArear;   s     r   r6   z!MeasureAreaController._doAnalysisQ  s    
 	 r   c                    | j                   j                  j                  | j                         | j                   j                  j                  | j
                         | j                   j                  j                  | j                         y ro   r   r   s    r   r=   z$MeasureAreaController._disconnectAllX  r   r   c                    | j                   j                  j                  | j                         | j                   j                  j                  | j
                         | j                   j                  j                  | j                         y ro   r   r   s    r   r?   z!MeasureAreaController._connectAll]  r   r   c                 N    t        j                  ddt         j                        S r   )r4   r\   SOLIDr   s    r   rB   zMeasureAreaController._getPenb  s    vvWARXX>>r   c                     | j                         sy t        j                  | |       | j                  j	                  t        d             y r   r   rK   s     r   rM   zMeasureAreaController.Stope  r   r   c                    | j                         ryt        j                  |        | j                  j	                  t        d      t        j                  z         | j                  j                  t        d             y)z_Init measurement routine that calculates area of polygon
        drawn on map display.
        Nr   zMeasuring area:)	r   r   r`   r
   r   r   r   r   r   r   s    r   r`   zMeasureAreaController.Startl  se     ==?$$T*!!:
 zz	
 	  #4!56r   c           
      .   ||d   gz   }dj                  |D cg c]  }|D ]  }t        |        c}}      }t        dd|d      j                         }t	        |      }d|vr'| j
                  j                  t        d             d	}n|d   j                  d      d
   }d|vrt        d      j                  d|      }nt        d      j                  |d   |      }| j
                  j                  |t        j                         yc c}}w )zdCalculate area and print to output window.

        :param coords: list of E, N coordinates
        r   ,z	m.measuregT)flagscoordinatesreadr   z)Units not recognized, measurement failed.    areazArea: {area} {unit}
)r   r   r   N)joinr   r   stripr   r
   r   r   splitformatr   r   r   )r   r   r   sublistr+   resultr   texts           r   r   z!MeasureAreaController.MeasureArea~  s    q	{*hh&1F7gFdSYFYF
 s$

%' 	 v&& LL%%a(S&TUD'?((-a0D,-44!$4GD,-44&.t4TDd1J1JK Gs   D
Nra   )rb   rc   rd   re   r   r6   r=   r?   rB   rM   r`   r   rf   r   r   r   r   J  s.    7'!:
7
?:7$Lr   r   )re   r   r   r4   
core.unitsr   	core.gcmdr   core.gifacer   grass.pydispatch.signalr   grass.script.utilsr   r   rh   r{   r   rf   r   r   <module>r      s`   $ 
  	    $ * ,z1 z1z -.  -FH 6 HVKL2 KLr   