-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathposition_correction_dockwidget.py
226 lines (170 loc) · 7.93 KB
/
position_correction_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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
PositionCorrectionDockWidget
A QGIS plugin
Defines the interactions between widgets in GUI.
-------------------
begin : 2016-02-12
git sha : $Format:%H$
copyright : (C) 2016 by Ondřej Pešek
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 qgis.PyQt import QtGui, uic
from qgis.PyQt.QtCore import pyqtSignal, QThread
from qgis.PyQt.QtWidgets import QDockWidget, QFileDialog, QProgressBar, QPushButton
from qgis.gui import QgsProjectionSelectionDialog, QgsMessageBar
from qgis.core import QgsCoordinateReferenceSystem, Qgis
from .move import Move
from . import show_as_layer
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'position_correction_dockwidget_base.ui'))
class PositionCorrectionDockWidget(QDockWidget, FORM_CLASS):
closingPlugin = pyqtSignal()
def __init__(self, iface, parent=None):
"""Constructor"""
super(PositionCorrectionDockWidget, 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.stylePath = None
self.iface = iface
self.computationRunning = False
self.inputButton.clicked.connect(self.select_input)
self.outputButton.clicked.connect(self.select_output)
self.style.clicked.connect(self.select_style)
self.showInput.clicked.connect(self.show_input)
self.ellipsoidSelector.clicked.connect(self.select_ellipsoid)
self.input.textChanged.connect(self.able_solve) # enable solve button
self.output.textChanged.connect(self.able_solve)
self.value.textChanged.connect(self.able_solve)
self.input.textChanged.connect(self.able_show) # enable showInput btn
self.solve.clicked.connect(self.solve_clicked)
def select_input(self):
"""select csv file to edit"""
self.filePath = QFileDialog.getOpenFileName(
self, 'Load file', '.', 'Comma Seperated Values (*.csv)')
if self.filePath:
self.filePath = os.path.normpath(self.filePath[0])
else:
return
self.input.setText(self.filePath)
self.output.setText(self.filePath[:-4]+'_shifted.csv')
def select_style(self):
"""select qml style file"""
self.stylePath = QFileDialog.getOpenFileName(
self, 'Load file (Cancel causes "No style")',
os.path.join(os.path.dirname(__file__), 'styles'),
'Qt Meta Language (*.qml)')
if self.stylePath:
self.stylePath = os.path.normpath(self.stylePath[0])
styleName = self.stylePath.split(os.path.sep)
styleName = styleName[len(styleName)-1][:-4]
self.style.setText(styleName)
else:
self.stylePath = None
self.style.setText('No style')
def select_output(self):
"""choose directory to save returned data"""
self.outputDir = QtGui.QFileDialog.getExistingDirectory(self,
'Save to file')
if not self.outputDir:
return
self.directory = os.path.normpath(self.outputDir) + os.path.sep + \
'shifted_data.csv'
self.output.setText(self.directory)
def able_solve(self):
"""set Solve button enable"""
if self.input.text() and self.output.text() and self.value.text():
self.solve.setEnabled(True)
else:
self.solve.setEnabled(False)
def able_show(self):
"""set showInput button enable"""
if self.input.text():
self.showInput.setEnabled(True)
else:
self.showInput.setEnabled(False)
def solve_clicked(self):
"""call computing"""
if self.computationRunning is not True:
self.start_computing()
def show_input(self):
"""show input csv as layer"""
show_as_layer.show(self.input.text(), self.stylePath)
def select_ellipsoid(self):
"""raise a dialog to choose the desired ellipsoid"""
projSelector = QgsProjectionSelectionDialog()
projSelector.exec_()
crs = projSelector.crs()
if crs.isValid():
if crs.ellipsoidAcronym() != '':
self.ellipsoidSelector.setText(crs.ellipsoidAcronym())
else:
self.ellipsoidSelector.setText('WGS84')
def start_computing(self):
"""computing called in new thread and showing a progressBar"""
computation = Move(self.input.text(), self.output.text(),
self.ellipsoidSelector.text(), self.units.currentText(),
self.value.text())
messageBar = self.iface.messageBar().createMessage('Computing...')
progressBar = QProgressBar()
cancelButton = QPushButton()
cancelButton.setText('Cancel')
messageBar.layout().addWidget(progressBar)
messageBar.layout().addWidget(cancelButton)
computationThread = QThread(self)
computation.moveToThread(computationThread)
computation.finished.connect(self.computing_finished)
computation.progress.connect(progressBar.setValue)
cancelButton.clicked.connect(self.cancel_computing)
computationThread.started.connect(computation.shift)
computation.error.connect(self.value_error)
self.iface.messageBar().pushWidget(messageBar, Qgis.Info)
if not computationThread.isRunning():
self.computationRunning = True
computationThread.start()
self.computation = computation
self.computationThread = computationThread
self.messageBar = messageBar
def computing_finished(self):
"""show new layer and clean up after threads"""
if self.computation.abort is not True:
show_as_layer.show(self.output.text(), self.stylePath)
self.computation.deleteLater()
self.computationThread.quit()
self.computationThread.wait()
self.computationThread.deleteLater()
self.iface.messageBar().popWidget(self.messageBar)
self.computationRunning = False
def cancel_computing(self):
"""clicked on the Cancel button in messageBar"""
self.computation.abort = True
os.remove(self.output.text())
def value_error(self, e):
"""error in input values"""
QtGui.QMessageBox.critical(None,
"ERROR: Invalid number of values",
"{0}".format(e), QtGui.QMessageBox.Abort)
self.computation.deleteLater()
self.computationThread.quit()
self.computationThread.wait()
self.computationThread.deleteLater()
self.iface.messageBar().popWidget(self.messageBar)
self.computationRunning = False
def close_event(self, event): # closeEvent
self.closingPlugin.emit()
event.accept()