-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlegend.py
More file actions
89 lines (76 loc) · 3.04 KB
/
Copy pathlegend.py
File metadata and controls
89 lines (76 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
"""
legend.py — Génération des miniatures PNG de légende (points et lignes catégorisés)
à partir du renderer/symbologie QGIS d'une couche.
"""
import os
from qgis.core import QgsSymbolLayerUtils
from qgis.PyQt.QtCore import QSize
from .qt_compat import qenum
from .styles import normaliser_valeur_classification, taille_canevas_icone
def extraire_icones_symbologie(layer, nom_fichier_couche, styles_dir):
"""Génère des miniatures PNG pour la légende des points et lignes."""
icones_exportees = []
renderer = layer.renderer()
if not renderer:
return icones_exportees
os.makedirs(styles_dir, exist_ok=True)
def _exporter_icone(symbol, suffixe):
img_name = f"icon_{nom_fichier_couche}_{suffixe}.png"
img_path = os.path.join(styles_dir, img_name)
taille_px = taille_canevas_icone(symbol)
pixmap = QgsSymbolLayerUtils.symbolPreviewPixmap(
symbol, QSize(taille_px, taille_px)
)
pixmap.save(img_path, "PNG")
return f"styles_images/{img_name}"
if hasattr(renderer, "categories") and len(renderer.categories()) > 0:
for idx, cat in enumerate(renderer.categories()):
label = cat.label() if cat.label() else str(cat.value())
symbol = cat.symbol()
if symbol:
icones_exportees.append(
{
"valeur": normaliser_valeur_classification(cat.value()),
"label": label,
"img_path": _exporter_icone(symbol, str(idx)),
}
)
elif hasattr(renderer, "ranges") and len(renderer.ranges()) > 0:
for idx, rang in enumerate(renderer.ranges()):
label = (
rang.label()
if rang.label()
else "{:.2f} \u2013 {:.2f}".format(rang.lowerValue(), rang.upperValue())
)
symbol = rang.symbol()
if symbol:
icones_exportees.append(
{
"valeur": str(rang.lowerValue()),
"label": label,
"img_path": _exporter_icone(symbol, f"grad_{idx}"),
}
)
elif hasattr(renderer, "rootRule"):
for idx, rule in enumerate(renderer.rootRule().children()):
label = rule.label() if rule.label() else f"Règle {idx + 1}"
symbol = rule.symbol()
if symbol:
icones_exportees.append(
{
"valeur": label,
"label": label,
"img_path": _exporter_icone(symbol, f"rule_{idx}"),
}
)
elif hasattr(renderer, "symbol") and renderer.symbol():
symbol = renderer.symbol()
icones_exportees.append(
{
"valeur": "default",
"label": layer.name(),
"img_path": _exporter_icone(symbol, "unique"),
}
)
return icones_exportees