U
    TGˆhú$  ã                	   @   sÌ   d Z dZdZdZddlZddl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Z ddlmZ ddlmZ ddlmZmZ ddlmZ eddddddddgƒZG dd„ deƒZdS )zelpaso@itopen.itz
2022-10-27zGPL version 3zCopyright 2022, Gis3wé    N)Ú
namedtuple)ÚOptional)ÚQgisÚQgsMapLayerÚ
QgsProjectÚQgsVectorLayer)ÚQBufferÚ	QIODevice)ÚQImage)ÚQgsServerFilter)Ú
find_layer)ÚLoggerÚexception_handler)Úto_boolÚCategoryÚruleKeyÚcheckedÚparentRuleKeyÚscaleMaxDenomÚscaleMinDenomÚ
expressionÚtitlec                   @   sf   e Zd ZdZdZeeeej	 dœdd„ƒZ
eedœdd„ƒZed	d
„ ƒZedeeeedœdd„ƒZdS )ÚGetLegendGraphicFilterz„ Add "ruleKey" to GetLegendGraphic for categorized and rule-based
    only works for single LAYER and STYLE(S) and JSON format.
    u   (.*) \[â‰ˆ?(?:\d+|N/A)\])Úsymbol_labelÚreturnc                 C   s   t  | j|¡S )z8Regexp for extracting the feature count from the label. )ÚreÚmatchÚFEATURE_COUNT_REGEXP)Úclsr   © r   úB/var/www/lizmap/lizmap/plugins/lizmap_server/get_legend_graphic.pyÚmatch_label_feature_count#   s    z0GetLegendGraphicFilter.match_label_feature_count)r   c                 C   s>   t ƒ }| tj¡ tdƒ}| |d¡ t| ¡  ¡  ¡ ƒ 	¡ S )z Warning icon as base 64. z(:/images/themes/default/mIconWarning.svgZPNG)
r   Úopenr	   Z	WriteOnlyr
   ÚsaveÚbytesÚdataZtoBase64Údecode)r   ÚbufferZqpr   r   r    Úwarning_icon(   s
    z#GetLegendGraphicFilter.warning_iconc              
   C   sì  |   ¡  ¡ }tƒ }|s$| d¡ d S | ¡ }| dd¡ ¡ dkrDd S | dd¡ ¡ dkr\d S | dd¡ ¡ dkrtd S | d	d¡}|dkrŒd S d
|kr˜d S t ¡ }| dd¡}|s¼| dd¡}t	| d¡ƒ}d}t
||ƒ}	|	sàd S |	 ¡ sB| d|› d| ¡ › d¡ dd||  ¡ ddœgdœ}
| ¡  | t |
¡ d¡¡ d S |	 ¡ tjjkrj| d|› d¡ d S zNz|	 ¡  ¡ }|r¢|r¢||kr¢|	 ¡  |¡ |r¾|	 ¡ }|r¾| ¡  |	 ¡  ¡ dkrz| ¡ }t  t!|ƒ¡}
|
d d  d¡}|s|
d }g }|  "|	|| ¡ ¡}t#t$|ƒƒD ]}|| }|d }|rx|  %|¡}|r`| &d¡}n| d '| j(|d ¡¡ zŽ|| }|j)|d< |j*|d < |j+|d!< d"|krÀ|j,dkrÀ|j,|d"< d#|krà|j-dkrà|j-|d#< |j.|d$< |d |j/kr|j/|d< W n t0t1fk
r    Y nX | 2|¡ q(d|
d d krT||
d d d< n||
d< | ¡  | t |
¡ d¡¡ W n: t3k
r¶ } z| d% '|t4|ƒ¡¡ W 5 d }~X Y nX W 5 |	ræ|ræ|ræ||kræ|	 ¡  |¡ X d S )&NzMGetLegendGraphicFilter plugin cannot be run in multithreading mode, skipping.ÚSERVICEÚ ÚWMSÚREQUEST)ZGETLEGENDGRAPHICZGETLEGENDGRAPHICSÚFORMATzAPPLICATION/JSONÚLAYERú,ZSTYLESZSTYLEZSHOWFEATURECOUNTzLayer 'zD' is not valid, returning a warning icon in the legend for project 'ú'ÚlayerF)Útyper   ÚiconZvalid)r   ÚnodesÚutf8zSkipping the layer 'z!' because it's not a vector layer)ZcategorizedSymbolZRuleRendererZgraduatedSymbolr4   r   Úsymbolsr   é   z<GetLegendGraphic JSON: symbol label does not match '{}' '{}'r   r   r   r   r   r   zeError getting layer "{}" when setting up legend graphic for json output when configuring OWS call: {})5ÚserverInterfaceÚrequestHandlerr   ÚcriticalÚparameterMapÚgetÚupperr   Úinstancer   r   ÚisValidÚwarningÚhomePathr(   Z	clearBodyÚ
appendBodyÚjsonÚdumpsÚencoder2   r   Ú	LayerTypeÚVectorLayerÚinfoÚstyleManagerZsetCurrentStyleZcurrentStyleÚcountSymbolFeaturesÚwaitForFinishedÚrendererÚbodyÚloadsr$   Ú_extract_categoriesÚrangeÚlenr!   ÚgroupÚformatr   r   r   r   r   r   r   r   Ú
IndexErrorÚKeyErrorÚappendÚ	ExceptionÚstr)ÚselfÚhandlerÚloggerÚparamsÚ
layer_nameÚprojectÚstyleÚshow_feature_countZcurrent_styler1   Z	json_dataZcounterrM   r6   Znew_symbolsÚ
categoriesÚidxÚsymbolr   Zmatch_labelÚcategoryÚexr   r   r    ÚresponseComplete1   sÎ    ÿ

ÿüþ	
 ÿÿ





 ÿÿz'GetLegendGraphicFilter.responseCompleteFr*   )r1   r`   Úproject_pathr   c              
   C   s"  |  ¡ }i }| ¡ D ]}| ¡ }|rh| ¡  ¡  ¡ }| | ¡ ¡}	|d |rRdnd|	dkr`|	nd¡7 }d}
t	j
dkr®| | ¡ |¡\}
}|s®t d|› d| ¡ › d	¡ d}
| ¡ | ¡ krät d
| ¡ › d|› d| ¡ › d¡ t| ¡ | | ¡ ¡| ¡ | ¡ | ¡ |
|d|| ¡ < q|S )z+ Extract categories from the layer legend. z [{}{}]u   â‰ˆr*   éÿÿÿÿzN/AiX  zThe expression in the project 'z
', layer 'zM' has not been generated correctly, setting the expression to an empty stringzThe label key 'z@' is not unique, expect the legend to be broken in the project 'z'.)r   r   r   r   r   r   r   )rL   ÚlegendSymbolItemsÚlabelÚdataProviderÚuriÚuseEstimatedMetadataÚfeatureCountr   rS   r   ÚQGIS_VERSION_INTÚlegendKeyToExpressionr   r@   ÚnameÚkeysr   ÚlegendSymbolItemCheckedr   r   r   )r   r1   r`   rg   rL   ra   Úitemr   Zestimated_countÚcountr   Úresultr   r   r    rO   Á   sB    
þ
ÿÿù	z*GetLegendGraphicFilter._extract_categoriesN)Fr*   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚclassmethodrX   r   r   ÚMatchr!   r(   r   rf   r   ÚboolÚdictrO   r   r   r   r    r      s$   
    ÿ  þr   )Ú
__author__Ú__date__Ú__license__Ú__copyright__rC   r   Úcollectionsr   Útypingr   Ú	qgis.corer   r   r   r   Zqgis.PyQt.QtCorer   r	   Úqgis.PyQt.QtGuir
   Úqgis.serverr   Úlizmap_server.corer   Úlizmap_server.loggerr   r   Úlizmap_server.toolsr   r   r   r   r   r   r    Ú<module>   s&   þ