-
Notifications
You must be signed in to change notification settings - Fork 3
/
batch_hillshader_dialog.py
481 lines (411 loc) · 20.8 KB
/
batch_hillshader_dialog.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Batch_Hillshader
A QGIS plugin to generate a three light
exposure hillshade (shaded relief by
combining three light exposures)
For more information, see the program documentation.
Plugin uses LASzip, see <https://laszip.org/>
-------------------
begin : 2016-07-13
git sha : $Format:%H$
copyright : (C) 2017 by PANOimagen S.L.
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 3 of the License, or *
* (at your option) any later version. *
* *
* This program 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. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/> *
***************************************************************************/
"""
import os
from osgeo import gdal
from qgis.PyQt import QtWidgets, uic
from qgis.gui import QgsMessageBar
from . import hillshader_process
from .plugin_utils import raster_funs
from .plugin_utils import files_and_dirs_funs as dir_fns
try:
from qgis.core import Qgis
MESSAGE_LEVEL = Qgis.MessageLevel(0)
except ImportError:
MESSAGE_LEVEL = QgsMessageBar.INFO
from .bh_errors import LasPyNotFoundError
try:
from . import laspy_utils
HAS_LASPY = True
except LasPyNotFoundError as e:
HAS_LASPY = False
try:
from . import version
except ImportError:
class version(object):
VERSION = "devel"
try:
# Qgis 3 compat
# getOpenFileNamesandFilter in PyQt4 becomes getOpenFileNames in PyQt5
getOpenFileNames = QtWidgets.QFileDialog.getOpenFileNamesAndFilter
except AttributeError:
getOpenFileNames = QtWidgets.QFileDialog.getOpenFileNames
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'batch_hillshader_dialog_base.ui'))
class batchHillshaderDialog(QtWidgets.QDialog, FORM_CLASS):
def __init__(self, iface, parent=None):
"""Constructor."""
super(batchHillshaderDialog, 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.iface = iface
self.setupUi(self)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.accepted.connect(self.preparingProcess)
self.buttonBox.rejected.connect(self.reject)
self.loadHillShadeCheckBox.setChecked(True)
self.loadPartialsCheckBox.setChecked(False)
self.laspyGroupBox.setEnabled(HAS_LASPY)
self.copyrightLabel.setText('(C) 2017 by Panoimagen S.L.')
# self.laspyRecomendedPixelLabel.setText(
# 'Recomended pixel size: - meters')
self.currentDEMPixelSizeLabel.setText(
'Input DEM pixel size: - x - meters')
self.currentPxSizeLabel.setText(
'Selected pixel size for hillshade results: - x - meters')
self.laspyToolButton.clicked.connect(self.laspyProcess)
self.outputFolderToolButton.clicked.connect(self.setOutPath)
self.inputDEMToolButton.clicked.connect(self.DEMProcess)
self.InputFilesOptions.currentChanged.connect(self.updateUi)
self.InputFilesOptions.currentChanged.connect(self.hillshadePixelSize)
self.inputDEMLineEdit.textChanged.connect(self.updateDEMPixelSize)
self.laspyPixelSizeDoubleSpinBox.valueChanged.connect(
self.hillshadePixelSize)
self._initVersion()
self.initLaspyUi()
if HAS_LASPY:
self.InputFilesOptions.setCurrentIndex(0)
else:
self.InputFilesOptions.setCurrentIndex(1)
# TODO: connect this objects: laspyLineEdit, laspyPixelSizeDoubleSpinBox, laspyRecomendedPixelLabel, laspyGroupBox
def _initVersion(self):
self.versionLabel.setText('Batch Hillshader version {}'.format(
version.VERSION))
def initLaspyUi(self):
# TODO: user can select both (terrain and surfaces)
# results_options = ['Terrain', 'Surfaces',
# 'Both (Surfaces and Terrain)']
results_options = ['Terrain', 'Surfaces']
interpolate_methods = ['nearest', 'linear', 'cubic']
self.interpolatingMethodComboBox.addItems(interpolate_methods)
self.surfaceTerrainComboBox.addItems(results_options)
laspy_text = 'LasPy Library is {}'
if not HAS_LASPY:
surname_label = (u'not installed. Read plugin documentation to' +
u' install LasPy Library')
label_color = 'color: red'
else:
surname_label = u'installed'
label_color = 'color: black'
self.LiDARLasPyTab.setEnabled(HAS_LASPY)
self.laspyImportLabel.setText(laspy_text.format(surname_label))
self.laspyImportLabel.setStyleSheet(label_color)
def updateUi(self):
"""Update UI QObjects
"""
if self.InputFilesOptions.currentIndex() == 0:
self.laspy_checked = True
else:
self.laspy_checked = False
if self.laspy_checked:
disable_DEM = True
self.inputDEMLineEdit.clear()
self.currentDEMPixelSizeLabel.setText(
'Input DEM pixel size: - x - meters')
else:
disable_DEM = False
self.laspyLineEdit.clear()
self.inputDEMLineEdit.setEnabled(not disable_DEM)
self.inputDEMToolButton.setEnabled(not disable_DEM)
self.currentDEMPixelSizeLabel.setEnabled(not disable_DEM)
self.inputDEMLabel.setEnabled(not disable_DEM)
self.laspyGroupBox.setEnabled(self.laspy_checked)
def updateDEMPixelSize(self):
"""Set input DEM pixel size when the process starts with an input
DEM
"""
dem_path = self.inputDEMLineEdit.text()
if dem_path:
dem_ds = gdal.Open(dem_path)
try:
self.dem_geo_info = dem_ds.GetGeoTransform()
pixel_with = self.dem_geo_info[1]
pixel_height = self.dem_geo_info[5]
pixel_size_label = ('Input DEM pixel size' +
': {} x {} meters'.format(
pixel_with, pixel_height))
self.currentDEMPixelSizeLabel.setText(pixel_size_label)
self.hillshadePixelSize()
except AttributeError:
self.currentDEMPixelSizeLabel.setText(
'Input DEM pixel size: - x - meters')
else:
self.dem_geo_info = None
def hillshadePixelSize(self):
"""Set the hillshade results pixel size and update the label
"""
try:
if self.InputFilesOptions.currentIndex() == 0:
dem_pixel_size = [self.laspyPixelSizeDoubleSpinBox.value(),
-(self.laspyPixelSizeDoubleSpinBox.value())]
self.updateHillshadeSizeLabel(dem_pixel_size[0],
dem_pixel_size[-1])
return
except AttributeError:
pass
try:
if self.dem_geo_info is None:
self.updateHillshadeSizeLabel('-','-')
else:
self.hillshadePxSize = [self.dem_geo_info[1],
self.dem_geo_info[5]]
self.updateHillshadeSizeLabel(self.hillshadePxSize[0],
self.hillshadePxSize[-1])
except AttributeError:
self.updateHillshadeSizeLabel('-','-')
def updateHillshadeSizeLabel(self, x, y):
"""Set label for hillshade results pixel size
"""
self.currentPxSizeLabel.setText(
'Selected pixel size for hillshade results:' +
' {} x {} meters'.format(str(x), str(y)))
def laspyProcess(self):
""" Processing with Lidar data. Using laspy library Set input file
and start output folder
"""
fileNames = getOpenFileNames(self,
"Select the input LiDAR file/s",
self.laspyToolButton.text(),
("LiDAR files (*.las *.LAS);;" +
" All files (*)"))
if fileNames:
# quoted = ['"{}"'.format(fn) for fn in fileNames]
self.laspyLineEdit.setText(", ".join(fileNames[0]))
if not self.outputFolderLineEdit.text():
try:
outPath = os.path.join(
os.path.split(os.path.abspath(fileNames[0][0]))[0],
'batch_hillshader_output')
self.outputFolderLineEdit.setText(outPath)
except IndexError:
pass
def DEMProcess(self):
"""Processing with DEM data. Set input file and start output folder
"""
fileNames = getOpenFileNames(self,
"Select the DEM input file/s",
self.inputDEMToolButton.text(),
("Raster files (*.tif *.tiff *.TIF *.TIFF *.asc *.ASC);;" +
"GEOTiff (*.tif *.tiff *.TIF *.TIFF);;" +
" ASCII Grid (*.asc *.ASC);; All files (*)"))
if fileNames:
# quoted = ['"{}"'.format(fn) for fn in fileNames]
self.inputDEMLineEdit.setText(", ".join(fileNames[0]))
if not self.outputFolderLineEdit.text():
outPath = os.path.join(
os.path.split(os.path.abspath(fileNames[0][0]))[0],
'batch_hillshader_output')
self.outputFolderLineEdit.setText(outPath)
def setOutPath(self):
"""Function to select the output folder and update the LineEdit
"""
outPath = QtWidgets.QFileDialog.getExistingDirectory(self,
"Select the output folder",
self.outputFolderToolButton.text())
if outPath:
self.outputFolderLineEdit.setText(os.path.join(
outPath, 'batch_hillshader_output'))
def preparingProcess(self):
"""Set process mode and check inputs.
This function launch also the function to set the
process parameters
"""
if self.InputFilesOptions.currentIndex() == 0:
filenames = self.laspyLineEdit.text()
self.processMode = 'laspyInput'
else:
filenames = self.inputDEMLineEdit.text()
self.processMode = 'DEMInput'
if not filenames:
self.showQMessage("Error: Not input file selected!\nPlease," +
"select one.")
outPath = self.outputFolderLineEdit.text()
if not outPath:
self.showQMessage("Error: Not output folder selected!\n" +
"Please, select one.")
self.process_option = self.surfaceTerrainComboBox.currentText()
if filenames and self.processMode:
for f in filenames.split(","):
full_filename = f.strip()
_, filename = os.path.split(full_filename)
if outPath:
self.settingProcessParams(full_filename, outPath)
def settingProcessParams(self, full_filename, outPath):
"""Set the params that are used in the process and call to the
process module
"""
self.createDictParams()
partialsCreateAndLoad = self.loadPartialsCheckBox.isChecked()
sombrasOutResults = self.loadHillShadeCheckBox.isChecked()
if self.InputFilesOptions.currentIndex() == 0:
sizeDEM = self.laspyPixelSizeDoubleSpinBox.value()
_, filename = os.path.split(full_filename)
base_name, ext = os.path.splitext(filename)
start_index = 1
out_path = os.path.join(
outPath, (base_name + '_r' + str(start_index)))
if os.path.exists(out_path):
import glob
key_for_glob = os.path.join(outPath, (base_name + '_r*' ))
dirs_list = glob.glob(key_for_glob)
indexes = []
for directory in dirs_list:
_, filename = os.path.split(directory)
parts_filename = filename.split('_r')
fn_index = int(parts_filename[-1])
indexes.append(fn_index)
max_index = max(indexes)
next_index = max_index + 1
out_path = os.path.join(outPath,
(base_name + '_r' + str(next_index)))
self.dir_funs = dir_fns.DirAndPaths()
if self.InputFilesOptions.currentIndex() == 0:
self.showMessage('Starting processing LiDAR data {}'.format(
base_name) +
u' with LasPy Library', MESSAGE_LEVEL)
self.inter_method = self.interpolatingMethodComboBox.currentText()
# TODO
if self.process_option == 'Surfaces':
terrain = False
surfaces = True
elif self.process_option == 'Terrain':
terrain = True
surfaces = False
#TODO: User can select both results
# elif self.process_option == 'Both (Surfaces and Terrain)':
# terrain = True
# surfaces = True
if not os.path.exists(out_path):
os.makedirs(out_path)
self.laspyLidar = laspy_utils.LiDAR(full_filename,
out_path,
partialsCreateAndLoad,
terrain, surfaces)
try:
self.lidar_arrays_list, self.las_file_extent, self.density =\
self.laspyLidar.process()
except ValueError as message:
self.showQMessage(str(message))
self.showMessage('Batch Hillshader stoped process',
Qgis.MessageLevel(1))
return
self.lidar_results = [self.lidar_arrays_list,
self.las_file_extent,
self.density]
self.laspyRasterize = laspy_utils.RasterizeLiDAR(
full_filename,
self.lidar_results,
out_path,
terrain, surfaces,
self.inter_method,
sizeDEM)
self.interpolated_grid = self.laspyRasterize.interpolate_grid()
dem_full_path = self.laspyRasterize.array_2_raster(
self.interpolated_grid)
self.dem_array, self.no_data_value = raster_funs.raster_2_array(
dem_full_path)
if partialsCreateAndLoad:
dem_filename, _ = os.path.splitext(
os.path.split(dem_full_path)[-1])
raster_funs.load_raster_layer(
dem_full_path,
dem_filename)
self.showMessage('Processing data... {}'.format(base_name),
MESSAGE_LEVEL)
self.HillDEM = hillshader_process.HillshaderDEM(
dem_full_path,
self.dem_array,
self.no_data_value,
partialsCreateAndLoad,
sombrasOutResults,
self.hill_params,
out_path)
if not partialsCreateAndLoad:
self.dir_funs.remove_temp_file(dem_full_path)
intermediate_folder = os.path.split(
self.laspyRasterize.dirs['dem'])[0]
self.dir_funs.remove_temp_dir(self.laspyRasterize.dirs['dem'])
self.dir_funs.remove_temp_dir(intermediate_folder)
self.showMessage('Process finisehd: {} file created'.format(
self.HillDEM.file_templates['composed_hillshade'].format(
base_name)), MESSAGE_LEVEL)
else:
self.showMessage('Starting processing DEM data {}'.format(
base_name), MESSAGE_LEVEL)
self.dem_array, self.no_data_value = raster_funs.raster_2_array(
full_filename)
self.HillDEM = hillshader_process.HillshaderDEM(
full_filename,
self.dem_array,
self.no_data_value,
partialsCreateAndLoad,
sombrasOutResults,
self.hill_params,
out_path)
self.showMessage('Process finisehd: {} file created'.format(
self.HillDEM.file_templates['composed_hillshade'].format(
base_name)), MESSAGE_LEVEL)
def createDictParams(self):
"""This function starts the dictionaries used in process module.
One dictionary for FUSION catalog report and the other for
the three light exposures of the hillshades
"""
self.hill_params = {
'azimuth1': self.azimuth1DoubleSpinBox.value(),
'azimuth2': self.azimuth2DoubleSpinBox.value(),
'azimuth3': self.azimuth3DoubleSpinBox.value(),
'angle_altitude1':\
self.angleAltitude1DoubleSpinBox.value(),
'angle_altitude2':\
self.angleAltitude2DoubleSpinBox.value(),
'angle_altitude3':\
self.angleAltitude3DoubleSpinBox.value(),
'transparency1':\
self.transparency1DoubleSpinBox.value()/100,
'transparency2':\
self.transparency2DoubleSpinBox.value()/100,
'transparency3':\
self.transparency3DoubleSpinBox.value()/100
}
def showMessage(self, message, msg_level):
"""This function shows a QGIS message bar when is called with the
message and the message Level -i.e.:INFO-
"""
self.iface.messageBar().pushMessage(
message, level=msg_level)
def showQMessage(self, message, msg_level = "Error message"):
"""This function shows a Qt message dialog when is called with the
message and the message Level-
"""
QtWidgets.QMessageBox.warning(self, msg_level, message)