-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontents.py
302 lines (260 loc) · 11.7 KB
/
contents.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Contents
A QGIS plugin
The plugin to convert DEM to GeoTiff and Terrain RGB (Tiff).
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2021-05-31
git sha : $Format:%H$
copyright : (C) 2021 by MIERUNE Inc.
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
import webbrowser
from qgis.core import QgsProject, QgsRasterLayer
from qgis.gui import QgsFileWidget
from PyQt5.QtWidgets import QMessageBox
from .quick_dem_for_jp_dialog import QuickDEMforJPDialog
from .convert_fgd_dem.src.convert_fgd_dem.converter import Converter
from .progress_dialog import ProgressDialog
class Contents:
def __init__(self, iface):
progress_dialog = ProgressDialog(None)
self.iface = iface
self.dlg = QuickDEMforJPDialog()
self.dlg.mQgsFileWidget_inputPath.setFilePath(QgsProject.instance().homePath())
self.dlg.mQgsFileWidget_inputPath.setStorageMode(QgsFileWidget.GetMultipleFiles)
self.dlg.mQgsFileWidget_inputPath.setFilter("*.xml;;*.zip")
self.dlg.mQgsFileWidget_outputPath.setFilePath(QgsProject.instance().homePath())
self.dlg.mQgsFileWidget_outputPath.setFilter("*.tiff")
self.dlg.mQgsFileWidget_outputPath.setDialogTitle(
progress_dialog.translate("Set the output file")
)
self.dlg.mQgsFileWidget_outputPathTerrain.setFilePath(
QgsProject.instance().homePath()
)
self.dlg.mQgsFileWidget_outputPathTerrain.setFilter("*.tiff")
self.dlg.mQgsFileWidget_outputPathTerrain.setDialogTitle(
progress_dialog.translate("Set the output file")
)
# set terrain path if changed
self.dlg.mQgsFileWidget_outputPath.fileChanged.connect(self.set_terrain_path)
self.dlg.checkBox_outputTerrainRGB.stateChanged.connect(self.set_terrain_path)
self.dlg.mQgsProjectionSelectionWidget_outputCrs.setCrs(
QgsProject.instance().crs()
)
self.dlg.radioButton_xmlzipfile.toggled.connect(self.switch_input_type)
self.dlg.radioButton_folder.toggled.connect(self.switch_input_type)
self.dlg.button_box.accepted.connect(self.convert_DEM)
self.dlg.button_box.rejected.connect(self.dlg_cancel)
self.dlg.downloadButton.clicked.connect(self.on_download_page_clicked)
self.process_interrupted = False
def convert(self, output_path, filename, rgbify):
progress_dialog = ProgressDialog(None)
thread = Converter(
import_path=self.import_path,
output_path=output_path,
output_epsg=self.output_epsg,
file_name=filename,
rgbify=rgbify,
sea_at_zero=self.dlg.checkBox_sea_zero.isChecked(),
)
progress_dialog.abortButton.clicked.connect(
lambda: [
self.on_abort_clicked(thread, progress_dialog),
]
)
# progress dialog orchestation by process thread
thread.setMaximum.connect(progress_dialog.set_maximum)
thread.addProgress.connect(progress_dialog.add_progress)
thread.postMessage.connect(progress_dialog.set_message)
thread.setAbortable.connect(progress_dialog.set_abortable)
thread.processFinished.connect(progress_dialog.close)
thread.processFailed.connect(
lambda error_message: [
self.handle_process_failed(error_message, thread, progress_dialog)
]
)
thread.start()
progress_dialog.exec_()
def add_layer(self, output_path, tiff_name, layer_name):
layer = QgsRasterLayer(os.path.join(output_path, tiff_name), layer_name)
QgsProject.instance().addMapLayer(layer)
def convert_DEM(self):
progress_dialog = ProgressDialog(None)
do_GeoTiff = self.dlg.checkBox_outputGeoTiff.isChecked()
do_TerrainRGB = self.dlg.checkBox_outputTerrainRGB.isChecked()
if not do_GeoTiff and not do_TerrainRGB:
QMessageBox.information(
None,
progress_dialog.translate("Error"),
progress_dialog.translate("Output format is not checked."),
)
return
self.import_path = self.dlg.mQgsFileWidget_inputPath.filePath()
if not self.import_path:
QMessageBox.information(
None,
progress_dialog.translate("Error"),
progress_dialog.translate("Input DEM path is not defined."),
)
return
self.output_path = self.dlg.mQgsFileWidget_outputPath.filePath()
if do_GeoTiff and not self.output_path:
QMessageBox.information(
None,
progress_dialog.translate("Error"),
progress_dialog.translate("GeoTIFF output path is not defined."),
)
return
self.output_path_terrain = self.dlg.mQgsFileWidget_outputPathTerrain.filePath()
if do_TerrainRGB and not self.output_path_terrain:
QMessageBox.information(
None,
progress_dialog.translate("Error"),
progress_dialog.translate("Terrain RGB output path is not defined."),
)
return
self.output_epsg = (
self.dlg.mQgsProjectionSelectionWidget_outputCrs.crs().authid()
)
if not self.output_epsg:
QMessageBox.information(
None,
progress_dialog.translate("Error"),
progress_dialog.translate("CRS of output file is not defined."),
)
return
do_add_layer = self.dlg.checkBox_openLayers.isChecked()
try:
if do_GeoTiff and not self.process_interrupted:
# check if directory exists
directory = os.path.dirname(self.output_path)
if not os.path.isdir(directory):
QMessageBox.information(
None,
progress_dialog.translate("Error"),
progress_dialog.translate("Cannot find output folder.")
+ f"\n{directory}",
)
return
filename = os.path.basename(self.output_path)
# Add .tiff to output path if missing
if not filename.lower().endswith(".tiff"):
filename += ".tiff"
self.convert(
output_path=os.path.dirname(self.output_path),
filename=filename,
rgbify=False,
)
if do_add_layer and not self.process_interrupted:
self.add_layer(
os.path.dirname(self.output_path),
tiff_name=filename,
layer_name=os.path.splitext(filename)[0],
)
if do_TerrainRGB and not self.process_interrupted:
# check if directory exists
directory = os.path.dirname(self.output_path_terrain)
if not os.path.isdir(directory):
QMessageBox.information(
None,
progress_dialog.translate("Error"),
progress_dialog.translate("Cannot find output folder.")
+ f"\n{directory}",
)
return
filename = os.path.basename(self.output_path_terrain)
# Add .tiff to output path if missing
if not filename.lower().endswith(".tiff"):
filename += ".tiff"
self.convert(
os.path.dirname(self.output_path_terrain),
filename=filename,
rgbify=True,
)
if do_add_layer and not self.process_interrupted:
self.add_layer(
os.path.dirname(self.output_path_terrain),
tiff_name=filename,
layer_name=os.path.splitext(filename)[0],
)
except Exception as e:
QMessageBox.information(
None, progress_dialog.translate("Error"), progress_dialog.translate(e)
)
return
if not self.process_interrupted:
QMessageBox.information(
None,
progress_dialog.translate("Completed"),
progress_dialog.translate("Process completed."),
)
self.dlg.hide()
return True
def dlg_cancel(self):
self.dlg.hide()
def switch_input_type(self):
if self.dlg.radioButton_xmlzipfile.isChecked():
self.dlg.mQgsFileWidget_inputPath.setStorageMode(
QgsFileWidget.GetMultipleFiles
)
else:
self.dlg.mQgsFileWidget_inputPath.setStorageMode(QgsFileWidget.GetDirectory)
def set_terrain_path(self):
# set Terrain file path automatically if path is not defined and Geotiff path is defined
geotiff_path = self.dlg.mQgsFileWidget_outputPath.filePath()
if (
self.dlg.checkBox_outputTerrainRGB.isChecked()
and os.path.splitext(geotiff_path)[1] == ".tiff"
and not self.dlg.mQgsFileWidget_outputPathTerrain.filePath()
.lower()
.endswith(".tiff")
):
terrain_path = (
os.path.splitext(geotiff_path)[0]
+ f"_Terrain-RGB{os.path.splitext(os.path.basename(geotiff_path))[1]}"
)
self.dlg.mQgsFileWidget_outputPathTerrain.setFilePath(terrain_path)
def on_download_page_clicked(self):
webbrowser.open("https://fgd.gsi.go.jp/download/")
return
def on_abort_clicked(self, thread, progress_dialog: ProgressDialog) -> None:
if QMessageBox.Yes == QMessageBox.question(
None,
progress_dialog.translate("Aborting"),
progress_dialog.translate("Are you sure to cancel process?"),
QMessageBox.Yes,
QMessageBox.No,
):
self.set_interrupted()
thread.process_interrupted = True
self.abort_process(thread, progress_dialog)
def abort_process(self, thread, progress_dialog: ProgressDialog) -> None:
if self.process_interrupted:
thread.exit()
progress_dialog.abort_dialog()
self.dlg_cancel()
return
def set_interrupted(self):
self.process_interrupted = True
def handle_process_failed(self, error_message, thread, progress_dialog):
progress_dialog.close()
QMessageBox.information(
None,
progress_dialog.translate("Error"),
progress_dialog.translate(error_message),
)
self.set_interrupted()
self.abort_process(thread, progress_dialog)