-
Notifications
You must be signed in to change notification settings - Fork 2
/
forLim.py
321 lines (270 loc) · 11.8 KB
/
forLim.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
# -*- coding: utf-8 -*-
from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication
from qgis.PyQt.QtGui import QIntValidator, QDoubleValidator, QIcon
from qgis.PyQt.QtWidgets import QAction, QFileDialog, QApplication, QMessageBox
from qgis.core import QgsVectorLayer, QgsRasterLayer
from qgis.core import QgsCoordinateReferenceSystem
from qgis.gui import QgsMessageBar
# Initialize Qt resources from file resources.py
from . import resources
from .forLim_dialog import forLimDialog
from datetime import datetime
from osgeo import ogr
from .delaunay import delaunayMethod
from processing import *
import os
from uuid import uuid4 as uuid4
from .delaunay.postProcessing import merge, clip
class forLim:
"""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,
'forLim_{}.qm'.format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
if qVersion() > '5.6.0':
QCoreApplication.installTranslator(self.translator)
# Create the dialog (after translation) and keep reference
self.dlg = forLimDialog()
# Declare instance attributes
self.actions = []
self.menu = self.tr(u'&forLim')
self.toolbar = self.iface.addToolBar(u'forLim')
self.toolbar.setObjectName(u'forLim')
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
"""
return QCoreApplication.translate('forLim', message)
def add_action(
self,
icon_path,
text,
callback,
enabled_flag=True,
add_to_menu=True,
add_to_toolbar=True,
status_tip=None,
whats_this=None,
parent=None):
icon = QIcon(icon_path)
action = QAction(icon, text, parent)
action.triggered.connect(callback)
action.setEnabled(enabled_flag)
if status_tip is not None:
action.setStatusTip(status_tip)
if whats_this is not None:
action.setWhatsThis(whats_this)
if add_to_toolbar:
self.toolbar.addAction(action)
if add_to_menu:
self.iface.addPluginToMenu(
self.menu,
action)
self.actions.append(action)
self.messageBar = self.iface.messageBar()
return action
def initGui(self):
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
# Input
global last_path_input
last_path_input = self.dlg.LE_input.text()
self.dlg.PB_input.clicked.connect(self.select_input_files)
self.dlg.LE_input.editingFinished.connect(self.check_input_path)
# Output
global last_path_output, output_message
last_path_output = self.dlg.LE_output.text()
output_message = True
self.dlg.PB_output.clicked.connect(self.select_output_directory)
self.dlg.LE_output.editingFinished.connect(self.check_output_path)
self.dlg.PB_quit.clicked.connect(self.quit_plugin)
self.dlg.PB_ok.clicked.connect(self.run)
self.dlg.tabWidget.setCurrentIndex(0)
# Set current line edit
self.dlg.LE_input.setFocus()
# Check validation
self.dlg.LE_minHeightThres.setValidator(QIntValidator())
self.dlg.LE_maxHeightThres.setValidator(QIntValidator())
self.dlg.LE_DRFD.setValidator(QDoubleValidator())
self.dlg.LE_DRPB.setValidator(QDoubleValidator())
self.dlg.txt_triangle_peri.setValidator(QDoubleValidator())
self.dlg.LE_minWidthThres.setValidator(QIntValidator())
self.dlg.LE_minForSurfThres.setValidator(QIntValidator())
self.dlg.LE_minClearingSurfThres.setValidator(QIntValidator())
self.dlg.LE_gradConvDiameter.setValidator(QIntValidator())
# Set add result to canevas as default
self.dlg.CB_addLayer.setCheckState(2)
# set progress bar settings
self.dlg.progressBar.setMinimum(0)
self.dlg.progressBar.setValue(0)
icon_path = ':/plugins/forLim/icon.png'
self.add_action(
icon_path,
text=self.tr(u'forLim'),
callback=self.run,
parent=self.iface.mainWindow())
def select_input_files(self):
filenames = QFileDialog.getOpenFileNames(filter="Images (*.tif)")
files_to_process = ''
if filenames:
for f in filenames[0]:
files_to_process += f + ';'
files_to_process = files_to_process[:-1]
self.dlg.LE_input.setText(files_to_process)
def check_input_path(self):
global last_path_input
path_input = self.dlg.LE_input.text()
if path_input and not path_input == last_path_input:
last_path_input = path_input
files = path_input.split(";")
f_exist = True
for f in files:
if not os.path.exists(f):
self.messageBar.pushMessage("Input"
"Le champ '" + f +
"' n'existe pas.",
QgsMessageBar.CRITICAL, 7)
f_exist = False
def select_output_directory(self):
self.dlg.LE_output.setFocus()
folder = QFileDialog.getExistingDirectory()
if folder:
if not os.path.exists(folder):
self.messageBar.pushMessage("Output",
"Le champ 'Dossier Destination '" +
"est invalide.",
QgsMessageBar.CRITICAL, 7)
else:
self.dlg.LE_output.setText(folder)
def check_output_path(self):
path_output = self.dlg.LE_output.text()
global last_path_output
if not path_output == last_path_output:
if os.path.exists(path_output):
last_path_output = path_output
elif path_output:
self.messageBar.pushMessage("Attention",
"Le champ 'Dossier Destination' " +
"est invalide.",
QgsMessageBar.CRITICAL, 7)
def quit_plugin(self):
self.dlg.close()
def unload(self):
"""Removes the plugin menu item and icon from QGIS GUI."""
for action in self.actions:
self.iface.removePluginMenu(
self.tr(u'&forLim'),
action)
self.iface.removeToolBarIcon(action)
del self.toolbar
def run(self):
"""Run method that performs all the real work"""
self.dlg.show()
ok_result = self.dlg.exec_()
if ok_result:
c = False
error_msg = str()
if not self.dlg.LE_input.text():
error_msg = error_msg + "Ajouter un chemin d'acces " + \
"au(x) fichier(s) source.\n\n"
c = True
if not self.dlg.LE_output.text():
error_msg = error_msg + "Ajouter un chemin d'acces pour " + \
"le dossier de destination ou seront déposes " + \
"les fichiers produits.\n\n"
c = True
if c:
QMessageBox.warning(None, "Erreur(s)", error_msg)
else:
options = {
# Chemin d'accès au Modèle Numérique de Canopée (entrée)
"Path_input":
str(self.dlg.LE_input.text()),
# Chemin d'accès au dossier de sortie
"Path_output":
str(self.dlg.LE_output.text()),
# Hauteur de la fenêtre de lissage
"WinRad":
int(self.dlg.LE_gradConvDiameter.text()),
# Hauteur minimale des arbres
"MinHeightThres":
int(self.dlg.LE_minHeightThres.text()),
# Hauteur maximale des arbres
"MaxHeightThres":
int(self.dlg.LE_maxHeightThres.text()),
# Degré de recouvrement pour la foret dense
"forestRatio":
float(self.dlg.LE_DRFD.text()),
# Degré de recouvrement pour le paturage boise
"woodenPastureRatio":
float(self.dlg.LE_DRPB.text()),
# largeur minimale forêt
"WidthThres":
int(self.dlg.LE_minWidthThres.text()),
# surface minimum forêt
"MinAreaThres":
int(self.dlg.LE_minForSurfThres.text()),
# surface minimum clairière
"MaxAreaThres":
int(self.dlg.LE_minClearingSurfThres.text()),
# Périmètre max des triangles de Delaunay
"MaxTrianglePerimeter":
float(self.dlg.txt_triangle_peri.text()),
# Calcul selon la convolution uniquement
"onlyConvolution":
self.dlg.chkConvolution.isChecked(),
"AddLayer":
self.dlg.CB_addLayer.isChecked(),
"plugin": True
}
# Set default values of process bar
self.dlg.progressBar.reset()
self.dlg.progressBar.setMinimum(0)
self.dlg.progressBar.setMaximum(1000)
# Print progress on user window
self.dlg.label_printActualProcess.setText("ForLim...")
self.dlg.progressBar.setValue(0)
QApplication.processEvents()
now_time = datetime.now()
name = "forLim_" + str(uuid4())
options["Path_output"] = \
os.path.join(options["Path_output"], name)
os.mkdir(options["Path_output"])
# Get file list
path_input = options["Path_input"]
files = options["Path_input"].split(";")
nfiles = len(files)
options['src'] = str(options['Path_input'])
options['dst'] = str(options['Path_output'])
f = open(options['Path_output'] + '/forlim_medatata.txt', 'w')
f.write(str(options))
f.close()
i = 0
for f in enumerate(files):
i += 1
self.dlg.label_printActualProcess \
.setText("Processing tile " + str(i) + "/" +
str(len((files))))
options['Path_input'] = f[1]
options['src'] = str(options['Path_input'])
delaunayMethod.main(self, options, i)
self.dlg.label_printActualProcess.setText(u'Calcul terminé')
self.iface.mapCanvas().zoomToFullExtent()