-
Notifications
You must be signed in to change notification settings - Fork 0
/
ways_calc.py
257 lines (199 loc) · 10.7 KB
/
ways_calc.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# -*- coding: utf-8 -*-
"""
/***************************************************************************
WaysCalc
A QGIS plugin
ways calculator
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2021-02-25
git sha : $Format:%H$
copyright : (C) 2021 by Zlatanov Evgeniy
email : johnzet@yandex.ru
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication, Qt, QObject
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction, QTableWidgetItem, QToolButton, QMenu
from qgis.core import QgsProject, QgsVectorLayer, QgsWkbTypes, QgsRectangle, QgsMapLayerProxyModel
from qgis.gui import QgsMapToolEmitPoint
# Initialize Qt resources from file resources.py
from .resources import *
from .ways_calc_tools.intersection_ways import IntersectionWays
from .ways_calc_tools.intersection_all_ways import IntersectionAllWays
# Import the code for the DockWidget
from .ways_calc_dockwidget import WaysCalcDockWidget
# from .ways_calc_select_layers_dialog import WaysCalcSelectLayerDialog
# from .station_ways_click_res_dialog import WaysCalcClickResDialog
import os.path
# import json
class WaysCalc:
"""QGIS Plugin Implementation."""
def __init__(self, iface):
"""Constructor.
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
"""
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
# initialize locale
locale = QSettings().value('locale/userLocale')[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'WaysCalc_{}.qm'.format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
QCoreApplication.installTranslator(self.translator)
# Declare instance attributes
self.actions = []
self.menu = self.tr(u'&WaysCalc')
# TODO: We are going to let the user set this up in a future iteration
# self.toolbar = self.iface.addToolBar(u'WaysCalc')
# self.toolbar.setObjectName(u'WaysCalc')
#print "** INITIALIZING WaysCalc"
self.pluginIsActive = False
self.dockwidget_inters_ways = None
self.dockwidget_inters_allways = None
# self.settings = None
self.IW = None #IntersectionWays
self.IAW = None #IntersectionAllWays
# noinspection PyMethodMayBeStatic
def tr(self, message):
"""Get the translation for a string using Qt translation API.
We implement this ourselves since we do not inherit QObject.
:param message: String for translation.
:type message: str, QString
:returns: Translated version of message.
:rtype: QString
"""
# noinspection PyTypeChecker,PyArgumentList,PyCallByClass
return QCoreApplication.translate('WaysCalc', message)
def initGui(self):
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
# выпадающая кнопка для пересекающихся маршрутов
self.toolButton = QToolButton()
self.toolButton.setMenu(QMenu())
# setPopupMode: DelayedPopup, MenuButtonPopup, InstantPopup
self.toolButton.setPopupMode(QToolButton.InstantPopup)
self.toolButton.setAutoRaise(True)
self.toolButton_action = self.iface.addToolBarWidget(self.toolButton)
self.action_intersection = QAction(
QIcon(":/plugins/_Ways_calc/icon.png"),
u'Поиск пересечений для линейного объекта',
self.iface.mainWindow())
self.action_initLayerWays = QAction(
u"Выбор слоя для поиска пересечений",
self.iface.mainWindow())
self.action_allintersection = QAction(
QIcon(":/plugins/_Ways_calc/icon.png"),
u'Поиск пересечений всех линейных объектов',
self.iface.mainWindow())
self.iface.addPluginToMenu(u"WaysCalc", self.action_intersection)
self.iface.addPluginToMenu(u"WaysCalc", self.action_initLayerWays)
self.iface.addPluginToMenu(u"WaysCalc", self.action_allintersection)
# self.iface.addToolBarIcon(self.action_intersection)
m = self.toolButton.menu()
m.addAction(self.action_intersection)
m.addAction(self.action_initLayerWays)
m.addSeparator()
m.addAction(self.action_allintersection)
self.toolButton.setDefaultAction(self.action_intersection)
self.action_intersection.triggered.connect(self.run_intersection) # пересекающиеся маршруты
self.action_allintersection.triggered.connect(self.run_allintersection) # пересекающиеся все маршруты
self.action_initLayerWays.triggered.connect(self.run_initLayerWays) # выбор слоя сравнения
self.pointEmitterIntersection = QgsMapToolEmitPoint(self.iface.mapCanvas())
self.pointEmitterIntersection.setAction(self.action_intersection)
self.pointEmitterIntersection.canvasClicked.connect(self.pointEmitterIntersectioncanvasClicked)
self.iface.mapCanvas().currentLayerChanged.connect(self.onCurrentLayerChanged)
#--------------------------------------------------------------------------
def onClosePlugin(self):
"""Cleanup necessary items here when plugin dockwidget is closed"""
# disconnects
try:
self.dockwidget_inters_ways.closingPlugin.disconnect(self.onClosePlugin)
except:
pass
try:
self.dockwidget_inters_allways.closingPlugin.disconnect(self.onClosePlugin)
except:
pass
self.pluginIsActive = False
def unload(self):
"""Removes the plugin menu item and icon from QGIS GUI."""
self.iface.removePluginMenu(u"WaysCalc", self.action_intersection)
self.iface.removePluginMenu(u"WaysCalc", self.action_allintersection)
self.iface.removePluginMenu(u"WaysCalc", self.action_initLayerWays)
self.iface.removeToolBarIcon(self.action_intersection)
self.iface.removeToolBarIcon(self.action_intersection)
self.iface.removeToolBarIcon(self.toolButton_action)
self.action_intersection.triggered.disconnect(self.run_intersection) # пересекающиеся маршруты
self.action_allintersection.triggered.disconnect(self.run_allintersection) # пересекающиеся все маршруты
self.action_initLayerWays.triggered.disconnect(self.run_initLayerWays) # выбор слоя сравнения
self.pointEmitterIntersection.canvasClicked.connect(self.pointEmitterIntersectioncanvasClicked)
self.iface.mapCanvas().currentLayerChanged.disconnect(self.onCurrentLayerChanged)
if self.IW is not None:
self.IW.onUnLoadModule()
def init_dock_inters_ways(self, dock):
"""Run method that loads and starts the plugin"""
# if not self.pluginIsActive:
self.pluginIsActive = True
if self.dockwidget_inters_ways == None:
self.dockwidget_inters_ways = WaysCalcDockWidget()
self.dockwidget_inters_ways.closingPlugin.connect(self.onClosePlugin)
self.iface.addDockWidget(Qt.RightDockWidgetArea, self.dockwidget_inters_ways)
self.dockwidget_inters_ways.hide()
self.dockwidget_inters_ways.setWindowTitle(u'WaysCalc: Поиск пересечений для линейного объекта')
def init_dock_inters_allways(self, dock):
"""Run method that loads and starts the plugin"""
# if not self.pluginIsActive:
self.pluginIsActive = True
if self.dockwidget_inters_allways == None:
self.dockwidget_inters_allways = WaysCalcDockWidget()
self.dockwidget_inters_allways.closingPlugin.connect(self.onClosePlugin)
self.iface.addDockWidget(Qt.RightDockWidgetArea, self.dockwidget_inters_allways)
self.dockwidget_inters_allways.hide()
self.dockwidget_inters_allways.setWindowTitle(u'WaysCalc: Поиск пересечений всех линейных объектов')
def onCurrentLayerChanged(self):
self.iface.mapCanvas().unsetMapTool(self.pointEmitterIntersection)
#--------------------------------------------------------------------------
# def loadSettings(self):
# with open(os.path.join(self.plugin_dir, "settings.json"), "r") as read_file:
# self.settings = json.load(read_file)
#--------INTERSECTION WAYS-------------------------------------------------
def run_initLayerWays(self):
if self.IW is None:
self.init_IW()
self.IW.initLayerWays()
def run_intersection(self):
self.toolButton.setDefaultAction(self.action_intersection)
self.init_dock_inters_ways(self.dockwidget_inters_ways)
if self.IW is None:
self.init_IW()
if self.IW.checkLayers():
self.iface.mapCanvas().setMapTool(self.pointEmitterIntersection)
def pointEmitterIntersectioncanvasClicked(self, point, button):
self.IW.insersection_take_way(point)
def init_IW(self):
self.IW = IntersectionWays(self.iface, self.dockwidget_inters_ways)
#-------- END INTERSECTION WAYS---------------------------------------------
def init_IAW(self):
self.IAW = IntersectionAllWays(self.iface, self.dockwidget_inters_allways)
def run_allintersection(self):
self.init_dock_inters_allways(self.dockwidget_inters_allways)
if self.IAW is None:
self.init_IAW()
self.IAW.showClickedFeaturesList()