-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatrac.py
executable file
·354 lines (298 loc) · 16 KB
/
patrac.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# -*- coding: utf-8 -*-
#******************************************************************************
#
# Patrac
# ---------------------------------------------------------
# Podpora hledání pohřešované osoby
#
# Copyright (C) 2017-2019 Jan Růžička ([email protected])
#
# This source 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.
#
# This code is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# A copy of the GNU General Public License is available on the World Wide Web
# at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
# to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA.
#
#******************************************************************************
import os, subprocess, sys
from os import path
from shutil import copy
from glob import glob
import configparser
from qgis.PyQt.QtWidgets import *
from qgis.PyQt.QtCore import *
from qgis.PyQt.QtGui import *
from qgis.core import *
from qgis.gui import *
from . import patracdockwidget
from .connect.connect import *
# Debugger
from . import debug
class NoClose(QObject):
def __init__(self, parent=None):
super().__init__(parent)
def ok_to_close(self):
self.pluginPath = path.dirname(__file__)
if path.exists(self.pluginPath + "/config/lastprojectpath.txt"):
with open(self.pluginPath + "/config/lastprojectpath.txt", "r") as f:
projectPath = f.read()
print(projectPath + "/search/result.xml")
# TODO stop QGIS from exiting
if not path.exists(projectPath + "/search/result.xml"):
return False
else:
return True
else:
return True
def eventFilter(self, object, event):
# print("CLOSE FILTER")
if isinstance(event, QCloseEvent):
if not self.ok_to_close():
reply = QMessageBox.question(None,
QApplication.translate("Patrac", 'Step', None), QApplication.translate("Patrac", 'You did not enter the result of the previous search. Do you want to close app?', None),
QMessageBox.Yes, QMessageBox.No)
if reply == QMessageBox.No:
event.ignore()
return True
return super().eventFilter( object, event )
class PatracPlugin(object):
singleBandStyles = ["paletted",
"singlebandgray",
"singlebandpseudocolor"
]
def __init__(self, iface):
# debug.RemoteDebugger.setup_remote_pydev_debug('localhost',10999)
#QgsApplication.setQuitOnLastWindowClosed(False)
#QgsApplication.lastWindowClosed.connect(self.exiting)
self.iface = iface
self.canvas = self.iface.mapCanvas()
self.no_close = NoClose()
self.iface.mainWindow().installEventFilter(self.no_close)
self.layer = None
self.toolBar = None
self.toolbar = self.iface.addToolBar("Patrac Toolbar")
self.toolbar.setObjectName("Patrac Toolbar")
# self.qgsVersion = str(QGis.QGIS_VERSION_INT)
userPluginPath = QFileInfo(QgsApplication.qgisUserDatabaseFilePath()).path() + "/python/plugins/qgis_patrac"
systemPluginPath = QgsApplication.prefixPath() + "/python/plugins/qgis_patrac"
overrideLocale = bool(QSettings().value("locale/overrideFlag", False))
if not overrideLocale:
localeFullName = QLocale.system().name()
else:
localeFullName = QSettings().value("locale/userLocale", "")
if QFileInfo(userPluginPath).exists():
translationPath = userPluginPath + "/i18n/qgis_patrac_" + localeFullName + ".qm"
else:
translationPath = systemPluginPath + "/i18n/qgis_patrac_" + localeFullName + ".qm"
self.localePath = translationPath
if QFileInfo(self.localePath).exists():
self.translator = QTranslator()
self.translator.load(self.localePath)
QCoreApplication.installTranslator(self.translator)
self.checkSettings()
self.copyDoc()
self.checkRequests = CheckRequests(userPluginPath + "/settings.db")
self.checkRequests.start()
self.saveProject = SaveProject()
self.saveProject.start()
self.iface.actionMapTips().setChecked(True)
def checkSettings(self):
pluginPath = path.dirname(__file__)
profilePath = pluginPath + "/../../../"
if not os.path.isdir(profilePath + "qgis_patrac_settings"):
os.mkdir(profilePath + "qgis_patrac_settings")
os.mkdir(profilePath + "qgis_patrac_settings/config")
os.mkdir(profilePath + "qgis_patrac_settings/styles")
os.mkdir(profilePath + "qgis_patrac_settings/grass")
copy(pluginPath + "/config/systemid.txt", profilePath + "qgis_patrac_settings/config/")
copy(pluginPath + "/config/config.json", profilePath + "qgis_patrac_settings/config/")
copy(pluginPath + "/config/paths.txt", profilePath + "qgis_patrac_settings/config/")
copy(pluginPath + "/grass/maxtime.txt", profilePath + "qgis_patrac_settings/grass/")
copy(pluginPath + "/grass/units.txt", profilePath + "qgis_patrac_settings/grass/")
copy(pluginPath + "/grass/weightlimit.txt", profilePath + "qgis_patrac_settings/grass/")
copy(pluginPath + "/grass/radialsettings.txt", profilePath + "qgis_patrac_settings/grass/")
copy(pluginPath + "/grass/distancesUser.txt", profilePath + "qgis_patrac_settings/grass/")
copy(pluginPath + "/grass/buffer.csv", profilePath + "qgis_patrac_settings/grass/")
copy(pluginPath + "/grass/units_times.csv", profilePath + "qgis_patrac_settings/grass/")
for file in glob(pluginPath + "/styles/*"):
copy(file, profilePath + "qgis_patrac_settings/styles/")
else:
if not os.path.isfile(profilePath + "qgis_patrac_settings/grass/buffer.csv"):
copy(pluginPath + "/grass/buffer.csv", profilePath + "qgis_patrac_settings/grass/")
if not os.path.isfile(profilePath + "qgis_patrac_settings/grass/units_times.csv"):
copy(pluginPath + "/grass/units_times.csv", profilePath + "qgis_patrac_settings/grass/")
if not os.path.isfile(profilePath + "qgis_patrac_settings/config/config.json"):
copy(pluginPath + "/config/config.json", profilePath + "qgis_patrac_settings/config/")
for file in glob(pluginPath + "/styles/*"):
copy(file, profilePath + "qgis_patrac_settings/styles/")
def copyDocDir(self, DATAPATH, pluginPath, name):
if not os.path.isdir(DATAPATH + "doc/" + name):
os.mkdir(DATAPATH + "doc/" + name)
for file in glob(pluginPath + "/doc/" + name + "/*"):
copy(file, DATAPATH + "doc/" + name + "/")
def copyDoc(self):
pluginPath = path.dirname(__file__)
DATAPATH = self.getPatracDataPath()
if not os.path.isdir(DATAPATH + "doc"):
os.mkdir(DATAPATH + "doc")
copy(pluginPath + "/doc/index.html", DATAPATH + "doc/")
self.copyDocDir(DATAPATH, pluginPath, "css")
self.copyDocDir(DATAPATH, pluginPath, "fonts")
self.copyDocDir(DATAPATH, pluginPath, "images")
self.copyDocDir(DATAPATH, pluginPath, "js")
def getPatracDataPath(self):
DATAPATH = ''
letters = "CDEFGHIJKLMNOPQRSTUVWXYZ"
drives = [letters[i] + ":/" for i in range(len(letters))]
for drive in drives:
if os.path.isfile(drive + 'patracdata/cr/projekty/simple/simple.qgs'):
DATAPATH = drive + 'patracdata/cr/projekty/simple/'
break
if os.path.isfile('/data/patracdata/cr/projekty/simple/simple.qgs'):
DATAPATH = '/data/patracdata/cr/projekty/simple/'
return DATAPATH
def initGui(self):
# if int(self.qgsVersion) < 10900:
# qgisVersion = self.qgsVersion[0] + "." + self.qgsVersion[2] + "." + self.qgsVersion[3]
# QMessageBox.warning(self.iface.mainWindow(),
# "Patrac",
# QCoreApplication.translate("Patrac", "QGIS version detected: ") + qgisVersion +
# QCoreApplication.translate("Patrac", "Je potřeba minimálně verze 2.0.\nPlugin nebude fungovat."))
# return None
self.dockWidget = None
pluginPath = path.dirname(__file__)
self.actionDock = QAction(QIcon(pluginPath + "/icons/patrac.png"), "Patrac", self.iface.mainWindow())
self.actionDock.setStatusTip(QCoreApplication.translate("Patrac", "Show/hide Patrac dockwidget"))
self.actionDock.setWhatsThis(QCoreApplication.translate("Patrac", "Show/hide Patrac dockwidget"))
self.actionDock.triggered.connect(self.showWidget)
self.iface.addPluginToMenu(QCoreApplication.translate("Patrac", "Patrac"), self.actionDock)
self.dockWidget = patracdockwidget.PatracDockWidget(self)
self.iface.addDockWidget(Qt.LeftDockWidgetArea, self.dockWidget)
self.iface.currentLayerChanged.connect(self.layerChanged)
self.iface.initializationCompleted.connect(self.hideToolbars)
self.layerChanged()
self.createToolbar()
self.hideToolbars()
def runHDS(self, array_where_to_append_output):
self.dockWidget.testHds(array_where_to_append_output)
def exiting(self):
print("EXITING")
def hideToolbars(self):
self.iface.advancedDigitizeToolBar().setVisible(False)
self.iface.attributesToolBar().setVisible(False)
self.iface.databaseToolBar().setVisible(False)
self.iface.dataSourceManagerToolBar().setVisible(False)
self.iface.digitizeToolBar().setVisible(False)
self.iface.fileToolBar().setVisible(False)
self.iface.helpToolBar().setVisible(False)
self.iface.layerToolBar().setVisible(False)
self.iface.mapNavToolToolBar().setVisible(False)
self.iface.pluginToolBar().setVisible(False)
self.iface.rasterToolBar().setVisible(False)
self.iface.vectorToolBar().setVisible(False)
self.iface.webToolBar().setVisible(False)
def createToolbar(self):
self.toolbar.addAction(self.actionDock)
self.toolbar.addAction(self.iface.actionOpenProject())
self.toolbar.addAction(self.iface.actionSaveProject())
self.toolbar.addAction(self.iface.actionShowLayoutManager())
self.toolbar.addAction(self.iface.actionPan())
self.toolbar.addAction(self.iface.actionZoomIn())
self.toolbar.addAction(self.iface.actionZoomOut())
self.toolbar.addAction(self.iface.actionZoomToLayer())
self.toolbar.addAction(self.iface.actionZoomToSelected())
self.toolbar.addAction(self.iface.actionIdentify())
self.toolbar.addAction(self.iface.actionOpenTable())
self.toolbar.addAction(self.iface.actionOpenFieldCalculator())
self.toolbar.addAction(self.iface.actionSelect())
self.toolbar.addAction(self.iface.actionToggleEditing())
self.toolbar.addAction(self.iface.actionAddFeature())
self.toolbar.addAction(self.iface.actionMoveFeature())
self.toolbar.addAction(self.iface.actionDeleteSelected())
self.toolbar.addAction(self.iface.actionSplitFeatures())
self.toolbar.addAction(self.iface.actionSaveEdits())
self.addRecalculateButton()
self.toolbar.addAction(self.iface.actionMeasure())
self.toolbar.addAction(self.iface.actionMeasureArea())
self.toolbar.addAction(self.iface.actionAddRasterLayer())
self.toolbar.addAction(self.iface.actionAddOgrLayer())
self.addVectorsForSplitByLineButton()
self.addSplitByLineButton()
def addRecalculateButton(self):
pluginPath = path.dirname(__file__)
self.recalculateSectorsAction = QAction(QIcon(pluginPath + "/icons/number_sectors.png"), "Patrac", self.iface.mainWindow())
self.recalculateSectorsAction.setStatusTip(QCoreApplication.translate("Patrac", "Recalculate sectors"))
self.recalculateSectorsAction.setWhatsThis(QCoreApplication.translate("Patrac", "Recalculate sectors"))
self.recalculateSectorsAction.triggered.connect(self.dockWidget.recalculateSectorsExpert)
self.toolbar.addAction(self.recalculateSectorsAction)
def addSplitByLineButton(self):
pluginPath = path.dirname(__file__)
self.splitByLineAction = QAction(QIcon(pluginPath + "/icons/split_by_line.png"), "Patrac", self.iface.mainWindow())
self.splitByLineAction.setStatusTip(QCoreApplication.translate("Patrac", "Split by line"))
self.splitByLineAction.setWhatsThis(QCoreApplication.translate("Patrac", "Split by line"))
self.splitByLineAction.triggered.connect(self.dockWidget.splitByLine)
self.toolbar.addAction(self.splitByLineAction)
def addVectorsForSplitByLineButton(self):
pluginPath = path.dirname(__file__)
self.addVectorsForSplitByLineAction = QAction(QIcon(pluginPath + "/icons/add_vectors_for_split_by_line.png"), "Patrac", self.iface.mainWindow())
self.addVectorsForSplitByLineAction.setStatusTip(QCoreApplication.translate("Patrac", "Add vectors for Split by line"))
self.addVectorsForSplitByLineAction.setWhatsThis(QCoreApplication.translate("Patrac", "Add vectors for Split by line"))
self.addVectorsForSplitByLineAction.triggered.connect(self.dockWidget.addVectorsForSplitByLine)
self.toolbar.addAction(self.addVectorsForSplitByLineAction)
def createShowWidgetAction(self):
icon = QIcon(icon_path)
action = QAction(icon, text, parent)
action.triggered.connect(callback)
action.setEnabled(enabled_flag)
def unload(self):
# if not self.no_close.ok_to_close():
# if sys.platform.startswith('win'):
# try:
# subprocess.Popen(("C:/OSGeo4W64/bin/qgis.bat"))
# except:
# print("CAN NOT FIND QGIS TO RELOAD")
# else:
# try:
# subprocess.Popen(("qgis", "--profiles-path", "/home/jencek/qgis3_profiles", "--profile", "default"))
# except:
# print("CAN NOT FIND QGIS TO RELOAD")
self.iface.currentLayerChanged.disconnect(self.layerChanged)
self.iface.removePluginMenu(QCoreApplication.translate("Patrac", "Patrac"), self.actionDock)
self.dockWidget.close()
del self.dockWidget
self.dockWidget = None
def showWidget(self):
self.dockWidget.show()
self.hideToolbars()
for x in self.iface.mainWindow().findChildren(QDockWidget):
if x.objectName() == 'Layers' or x.objectName() == 'PatracDockWidget':
x.setVisible(True)
x.setFloating(False)
def layerChanged(self):
self.layer = self.iface.activeLayer()
if self.layer is None:
return
if self.layer.type() != QgsMapLayer.RasterLayer:
self.dockWidget.disableOrEnableControls(False)
return
if self.layer.providerType() not in ["gdal", "grass"]:
self.dockWidget.disableOrEnableControls(False)
return
if self.layer.bandCount() > 1 and self.layer.renderer().type() not in self.singleBandStyles:
self.dockWidget.disableOrEnableControls(False)
return
band = self.layer.renderer().usesBands()[0]
stat = self.layer.dataProvider().bandStatistics(band)
maxValue = int(stat.maximumValue)
minValue = int(stat.minimumValue)
self.dockWidget.updateSliders(maxValue, minValue)
self.dockWidget.disableOrEnableControls(True)