-
Notifications
You must be signed in to change notification settings - Fork 1
/
style2layers.py
64 lines (55 loc) · 2.54 KB
/
style2layers.py
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
"""
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import os
import re
from qgis.PyQt.QtXml import QDomDocument
from qgis.PyQt.QtWidgets import QDialog, QApplication
from qgis.PyQt.uic import loadUiType
from qgis.core import Qgis, QgsMapLayerType
FORM_CLASS, _ = loadUiType(os.path.join(
os.path.dirname(__file__), 'ui/styleToLayer.ui'))
class StyleToLayers(QDialog, FORM_CLASS):
def __init__(self, iface, parent):
super(StyleToLayers, self).__init__(parent)
self.setupUi(self)
self.iface = iface
self.canvas = iface.mapCanvas()
self.fileWidget.setFilter("*.qml")
def accept(self):
path = self.fileWidget.filePath().strip()
reclassify = self.autoReclassifyCheckBox.isChecked()
if not path:
doc = QDomDocument()
text = QApplication.clipboard().text()
if not doc.setContent(text):
self.iface.messageBar().pushMessage("","Invalid clipboard style content", level=Qgis.Warning, duration=4)
return
total_layers = 0
success = 0
for layer in self.iface.layerTreeView().selectedLayersRecursive():
total_layers += 1
if path:
(msg, status) = layer.loadNamedStyle(path)
else:
try:
(status, msg) = layer.importNamedStyle(doc)
except Exception:
status = False
if status:
layer_type = layer.type()
if reclassify and layer_type == QgsMapLayerType.VectorLayer and layer.renderer().type() == 'graduatedSymbol':
renderer = layer.renderer()
nclass = len(renderer.ranges())
renderer.updateClasses(layer, nclass)
layer.triggerRepaint()
success += 1
self.iface.messageBar().pushMessage("","Style applied to {} out of {} layers".format(success, total_layers), level=Qgis.Info, duration=4)
self.close()