-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathground_radiation_monitoring_dockwidget.py
345 lines (268 loc) · 15.6 KB
/
ground_radiation_monitoring_dockwidget.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
GroundRadiationMonitoringDockWidget
A QGIS plugin
This plugin calculates the amount of received radiation.
-------------------
begin : 2017-01-10
git sha : $Format:%H$
copyright : (C) 2017 by Michael Kala
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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
from PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication, Qt, QFileInfo, QThread, pyqtSignal
from PyQt4.QtGui import QComboBox, QAction, QIcon, QToolButton, QFileDialog, QMessageBox, QProgressBar, QCheckBox
from qgis.core import QgsMapLayerRegistry, QgsMapLayer, QGis, QgsPoint, QgsRaster, QgsProject, QgsProviderRegistry, QgsDistanceArea
from qgis.utils import QgsMessageBar, iface
from qgis.gui import QgsMapLayerComboBox,QgsMapLayerProxyModel
from osgeo import gdal, ogr
from math import ceil
from array import array
import time
from PyQt4 import QtGui, uic
from ground_radiation_monitoring_computation import GroundRadiationMonitoringComputation
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'ground_radiation_monitoring_dockwidget_base.ui'))
class GroundRadiationMonitoringDockWidget(QtGui.QDockWidget, FORM_CLASS):
closingPlugin = pyqtSignal()
def __init__(self, parent=None):
"""Constructor."""
super(GroundRadiationMonitoringDockWidget, self).__init__(parent)
# Set up the user interface from Designer.
# After setupUI you can access any designer object by doing
# self.<objectname>, and you can use autoconnect slots - see
# http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
# #widgets-and-dialogs-with-auto-connect
self.setupUi(self)
self.settings = QSettings("CTU","GRMplugin")
self.iface = iface
# Set filters for QgsMapLayerComboBoxes
self.raster_box.setFilters(QgsMapLayerProxyModel.RasterLayer)
self.track_box.setFilters(QgsMapLayerProxyModel.LineLayer)
self.load_raster.clicked.connect(self.onLoadRaster)
self.load_track.clicked.connect(self.onLoadTrack)
self.save_button.setEnabled(False)
self.report_button.clicked.connect(self.onReportButton)
#self.dir_button.clicked.connect(self.onCsvButton)
self.shp_button.clicked.connect(self.onShpButton)
self.save_button.clicked.connect(self.onExportRasterValues)
def closeEvent(self, event):
self.closingPlugin.emit()
event.accept()
def onLoadRaster(self):
"""Open 'Add raster layer dialog'."""
sender = u'{}-lastUserFilePath'.format(self.sender().objectName())
lastUsedFilePath = self.settings.value(sender, '')
fileName = QFileDialog.getOpenFileName(self,self.tr(u'Open raster'),
self.tr(u'{}').format(lastUsedFilePath),
QgsProviderRegistry.instance().fileRasterFilters())
if fileName:
self.iface.addRasterLayer(fileName, QFileInfo(fileName).baseName())
self.settings.setValue(sender, os.path.dirname(fileName))
def onLoadTrack(self):
"""Open 'Add track layer dialog'."""
sender = u'{}-lastUserFilePath'.format(self.sender().objectName())
lastUsedFilePath = self.settings.value(sender, '')
fileName = QFileDialog.getOpenFileName(self,self.tr(u'Open track'),
self.tr(u'{}').format(lastUsedFilePath),
QgsProviderRegistry.instance().fileVectorFilters())
if fileName:
self.iface.addVectorLayer(fileName, QFileInfo(fileName).baseName(), "ogr")
self.settings.setValue(sender, os.path.dirname(fileName))
name, fileExtension = os.path.splitext(fileName)
if fileExtension not in QgsProviderRegistry.instance().fileVectorFilters():
return
# TODO: make this work for multiple layer loading
if self.iface.activeLayer().geometryType() != QGis.Line:
self.sendMessage(u'Info', u'{} does not have lineString type.'.format(QFileInfo(fileName).baseName()), 'INFO')
def onReportButton(self):
"""Get destination of report, csv and shape file.
Set path and name for csv and shape file by default as file path for report file.
Set default name for report file same as track layer name"""
sender = u'{}-lastUserFilePath'.format(self.sender().objectName())
lastUsedFilePath = self.settings.value(sender, '')
self.saveReportName = QFileDialog.getSaveFileName(self, self.tr(u'Select destination file'),
self.tr(u'{}{}.txt').format(lastUsedFilePath,os.path.sep),
filter ="TXT (*.txt)")
self.saveShpName = self.tr(u'{}.shp').format(self.saveReportName.split('.')[0])
self.report_file.setText(self.tr(u'{}').format(self.saveReportName))
if self.saveReportName:
self.shp_file.setText(self.tr(u'{}').format(self.saveShpName))
self.settings.setValue(sender, os.path.dirname(self.saveReportName))
# Enable the saveButton if file is chosen
if not self.report_file.text():
self.save_button.setEnabled(False)
else:
self.save_button.setEnabled(True)
def onShpButton(self):
"""Get destination of shp file."""
sender = u'{}-lastUserFilePath'.format(self.sender().objectName())
lastUsedFilePath = self.settings.value(sender, '')
self.saveShpName = QFileDialog.getSaveFileName(self, self.tr(u'Select destination file'),
self.tr(u'{}{}.shp').format(lastUsedFilePath,os.path.sep),
filter ="ESRI Shapefile (*.shp)")
self.shp_file.setText(u'{}'.format(self.saveShpName))
if self.saveShpName:
self.settings.setValue(sender, os.path.dirname(self.saveShpName))
if not (self.report_file.text() and self.shp_file.text()):
self.save_button.setEnabled(False)
else:
self.save_button.setEnabled(True)
def onExportRasterValues(self):
"""Export sampled raster values to output CSV file.
Prints error when user selected length of segment or speed is not positive real number
and computation is not performed.
When no raster or track vector layer given, than computation
is not performed.
If shapefile that will be created has the same name as one of the layers in
map canvas, that layer will be removed from map layer registry.
"""
noSamplingFlag = False
if not self.vertex_dist.text().strip():
distanceBetweenVertices = 0
noSamplingFlag = True
if noSamplingFlag == False:
try:
distanceBetweenVertices = float(self.vertex_dist.text().replace(',', '.'))
except ValueError:
self.sendMessage(u'Error', u'"{}" is not a number. (distance between vertices)'.format(self.vertex_dist.text()), 'CRITICAL')
return
if distanceBetweenVertices <= 0 and noSamplingFlag == False:
self.sendMessage(u'Error', u'"{}" is not a positive number. (distance between vertices)'.format(distanceBetweenVertices), 'CRITICAL')
return
if not self.speed.text():
self.sendMessage(u'Error', u'No speed given.', 'CRITICAL')
return
try:
speed = float(self.speed.text().replace(',', '.'))
except ValueError:
self.sendMessage(u'Error', u'"{}" is not a number. (speed)'.format(self.speed.text()), 'CRITICAL')
return
if speed <= 0:
self.sendMessage(u'Error', u'"{}" is not a positive number. (speed)'.format(speed), 'CRITICAL')
return
if not self.raster_box.currentLayer() or not self.track_box.currentLayer():
self.sendMessage(u'Error', u'No raster/track layer chosen.', 'CRITICAL')
return
# remove layers with same name as newly created layer
for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
if lyr.source() == self.saveShpName:
QgsMapLayerRegistry.instance().removeMapLayer(lyr.id())
self.saveCsvName = None
if self.create_csv.isChecked():
self.saveCsvName = self.tr(u'{}.csv').format(self.saveShpName.split('.')[0])
self.computeThread = GroundRadiationMonitoringComputation(self.raster_box.currentLayer().id(),
self.track_box.currentLayer().id(),
self.saveReportName,
self.saveCsvName,
self.saveShpName,
distanceBetweenVertices,
speed,
self.unit_box.currentText())
self.saveShpNameOriginal = self.saveShpName
self.saveReportNameOriginal = self.saveReportName
self.saveCsvNameOriginal = self.saveCsvName
self.computeThread.computeEnd.connect(self.addNewLayer)
self.computeThread.computeStat.connect(self.setStatus)
self.computeThread.computeProgress.connect(self.progressBar)
self.computeThread.computeMessage.connect(self.sendMessage)
if not self.computeThread.isRunning():
self.computeThread.start()
self.save_button.setEnabled(False)
def onCancelButton(self):
"""Show message box with question on canceling. Cancel computation."""
reply = QMessageBox.question(self, u'Ground Radiation Monitoring',
u"Cancel computation?{ls}(All created files will be lost.)".format(ls = 2*os.linesep),
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
QtGui.QMessageBox.Yes)
if reply == QMessageBox.Yes:
GroundRadiationMonitoringComputation.abortThread(self.computeThread)
# kill progress bar if it is still on (if computation is still on)
try:
self.progress.setParent(None)
self.iface.messageBar().popWidget(self.progressMessageBar)
except:
pass
self.cleanCreatedFiles()
if not (self.report_file.text() and self.shp_file.text()):
self.save_button.setEnabled(False)
else:
self.save_button.setEnabled(True)
def cleanCreatedFiles(self):
"""Remove created files."""
# remove layers with same name as newly created layer (if is created
# and added to map canvas) so shape file could be removed
for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
if lyr.source() == self.saveShpNameOriginal:
QgsMapLayerRegistry.instance().removeMapLayer(lyr.id())
# remove created files
if os.path.isfile(self.saveReportNameOriginal):
os.remove(self.saveReportNameOriginal)
try:
if os.path.isfile(self.saveCsvNameOriginal):
os.remove(self.saveCsvNameOriginal)
except:
pass
if os.path.isfile(self.saveShpNameOriginal):
os.remove(self.saveShpNameOriginal)
if os.path.isfile(self.saveShpNameOriginal.split('.')[0]+'.shx'):
os.remove(self.saveShpNameOriginal.split('.')[0]+'.shx')
if os.path.isfile(self.saveShpNameOriginal.split('.')[0]+'.dbf'):
os.remove(self.saveShpNameOriginal.split('.')[0]+'.dbf')
if os.path.isfile(self.saveShpNameOriginal.split('.')[0]+'.prj'):
os.remove(self.saveShpNameOriginal.split('.')[0]+'.prj')
def progressBar(self):
"""Initializing progress bar.
"""
self.progressMessageBar = iface.messageBar().createMessage(u"Ground Radiation Monitoring:",u" Computing...")
self.progress = QProgressBar()
self.progress.setMaximum(100)
self.progress.setAlignment(Qt.AlignLeft|Qt.AlignVCenter)
self.cancelButton = QtGui.QPushButton()
self.cancelButton.setText('Cancel')
self.progressMessageBar.layout().addWidget(self.cancelButton)
self.progressMessageBar.layout().addWidget(self.progress)
msgBar = self.iface.messageBar()
msgBar.pushWidget(self.progressMessageBar, iface.messageBar().INFO)
#hide x button
msgBar.findChildren(QToolButton)[0].setHidden(True)
self.cancelButton.clicked.connect(self.onCancelButton)
def setStatus(self, num, text):
"""Update progress status.
:num: progress percent
"""
self.progress.setFormat(text)
self.progress.setValue(num)
def addNewLayer(self):
"""End computeThread.
Ask to add new layer of computed points to map canvas. """
# Message box
reply = QMessageBox.question(self, u'Ground Radiation Monitoring',
u"Add new layer to map canvas?",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
QtGui.QMessageBox.Yes)
# add map layer to map canvas
if reply == QMessageBox.Yes:
newLayer = iface.addVectorLayer(u'{f}'.format(f=self.saveShpNameOriginal),
u'{f}'.format(f=QFileInfo(self.saveShpNameOriginal).baseName()), "ogr")
self.iface.messageBar().popWidget(self.progressMessageBar)
self.save_button.setEnabled(True)
def sendMessage(self, caption, message, type):
if type == 'CRITICAL':
self.iface.messageBar().pushMessage(self.tr(u'{}').format(caption),
self.tr(u'{}').format(message),
level = QgsMessageBar.CRITICAL, duration = 5)
elif type == 'INFO':
self.iface.messageBar().pushMessage(self.tr(u'{}').format(caption),
self.tr(u'{}').format(message),
level = QgsMessageBar.INFO, duration = 5)