-
Notifications
You must be signed in to change notification settings - Fork 2
/
OrtoTools.py
173 lines (156 loc) · 6.88 KB
/
OrtoTools.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from qgis.PyQt.QtCore import QObject, pyqtSignal, QSettings
from qgis.PyQt.QtWidgets import QToolButton, QMenu, QAction
from qgis.core import QgsProject, QgsRasterLayer, QgsMessageLog
from .utils import WMS_SERVERS, WMS_SERVERS_GROUPS, tr, CustomMessageBox
from .config import Config
class OrtoAddingTool(object):
def __init__(self,
parent, button,
group_names=("WMS/WMTS")):
self.parent = parent
self.button = button
self.group_names = group_names
self.layer_actions_dict = {}
self.button.setToolTip(tr("Add WMS/WMTS services"))
self.button.setPopupMode(QToolButton.InstantPopup)
self.name_service = WMS_SERVERS
self.groups_for_names = WMS_SERVERS_GROUPS
self.services = []
self.create_menu()
self.connect_ortofotomapa_group()
def connect_ortofotomapa_group(self):
"""
Łączy sygnał wywoływany podczas zmiany widoczności w grupie o nazwie
self.group_name z funkcją self.create_menu.
"""
for group_name in self.group_names:
root = QgsProject.instance().layerTreeRoot()
group = root.findGroup(group_name)
if group:
group.visibilityChanged.connect(self.create_menu)
group.addedChildren.connect(self.create_menu)
group.removedChildren.connect(self.create_menu)
def disconnect_ortofotomapa_group(self):
"""
Odłącza sygnał wywoływany podczas zmiany widoczności w grupie o nazwie
self.group_name z funkcją self.create_menu.
"""
for group_name in self.group_names:
group = QgsProject.instance().layerTreeRoot().findGroup(
group_name)
if group:
try:
group.visibilityChanged.disconnect(self.create_menu)
group.addedChildren.disconnect(self.create_menu)
group.removedChildren.disconnect(self.create_menu)
except Exception:
QgsMessageLog.logMessage(
tr("Error, detaching signals from "
"groups in layers tree."
),
tag="GIAP-PolaMap(lite)"
)
def action_clicked(self, item):
"""
Uaktualnia widoczność warstw na podstawie akcji z menu przycisku.
"""
layer_name = self.layer_name_dict[item]
if QgsProject.instance().mapLayersByName(layer_name):
try:
for layer, action in self.layer_actions_dict.items():
if QgsProject.instance().layerTreeRoot().findLayer(layer):
QgsProject.instance().layerTreeRoot().findLayer(
layer).parent().setItemVisibilityChecked(True)
checked = action.isChecked()
QgsProject.instance().layerTreeRoot().findLayer(
layer).setItemVisibilityChecked(checked)
except RuntimeError:
item.setChecked(True)
self.add_to_map(layer_name)
def add_to_map(self, name):
url = self.name_service[name]
rlayer = QgsRasterLayer(url, name, 'wms')
if not QgsProject.instance().mapLayersByName(name):
root = QgsProject.instance().layerTreeRoot()
group_name = self.groups_for_names[name]
if rlayer.isValid():
group = root.findGroup(group_name)
if not group:
root.addGroup(group_name)
group = root.findGroup(group_name)
QgsProject.instance().addMapLayer(rlayer)
node_layer = root.findLayer(rlayer.id())
node_parent = node_layer.parent()
clone_node_layer = node_layer.clone()
group.insertChildNode(0, clone_node_layer)
clone_node_layer.setItemVisibilityCheckedParentRecursive(True)
node_parent.removeChildNode(node_layer)
else:
CustomMessageBox(
None, tr('Can\'t add layer ') + name).button_ok()
else:
lyr = QgsProject.instance().mapLayersByName(name)[0]
lyrontree = QgsProject.instance().layerTreeRoot().findLayer(
lyr.id())
lyrontree.setItemVisibilityChecked(
not lyrontree.isItemVisibilityCheckedRecursive())
def create_menu(self):
layers_names = []
self.layer_name_dict = {}
self.layer_actions_dict = {}
menu = QMenu(self.parent)
for group_name in self.group_names:
group = QgsProject.instance().layerTreeRoot().findGroup(group_name)
if group:
for lr in group.findLayers():
layer = lr.layer()
layers_names.append(layer.name())
action = QAction(layer.name(), self.parent)
action.setCheckable(True)
action.triggered.connect(
lambda checked, item=action: self.action_clicked(item))
self.layer_name_dict[action] = layer.name()
self.layer_actions_dict[layer] = action
values = list(self.layer_actions_dict.values())
values.sort(key=lambda x: x.text())
list(map(menu.addAction, values))
self.services = []
for name, service in self.name_service.items():
if name not in layers_names:
action = QAction(name, self.parent)
action.setCheckable(True)
group_name = self.groups_for_names[name]
self.layer_name_dict[action] = name
self.services.append(
OrtoActionService(
action,
service,
name,
group_name,
parent=self
)
)
menu.addAction(action)
menu.aboutToShow.connect(self.ortocheck)
self.button.setMenu(menu)
self.ortomenu = menu
def ortocheck(self):
checked = [layer.name() for layer in
QgsProject.instance().layerTreeRoot().checkedLayers()]
for orto in self.ortomenu.actions():
orto.setChecked(orto.text() in checked)
class OrtoActionService(QObject):
orto_added = pyqtSignal()
orto_group_added = pyqtSignal()
def __init__(self, action, url, name,
default_group="WMS/WMTS", parent=None):
QObject.__init__(self)
self.parent = parent
self.button = action
self.url = url
self.name = name
self.group_name = default_group
self.button.triggered.connect(
lambda checked, item=self.button: self.parent.action_clicked(item))