-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsrtm_downloader_dialog_base.py
More file actions
247 lines (214 loc) · 8.61 KB
/
Copy pathsrtm_downloader_dialog_base.py
File metadata and controls
247 lines (214 loc) · 8.61 KB
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
/***************************************************************************
SrtmDownloader
A QGIS plugin
Downloads SRTM Tiles from NASA Server
-------------------
begin : 2017-12-30
git sha : $Format:%H$
copyright : (C) 2017 by Dr. Horst Duester
email : horst.duester@kappasys.ch
***************************************************************************/
/***************************************************************************
* *
* 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 math
import os
import tempfile
from os.path import expanduser
from qgis.PyQt import uic
from qgis.PyQt.QtCore import pyqtSlot, QSettings
from qgis.PyQt.QtWidgets import (
QDialog,
QMessageBox,
QFileDialog,
QDialogButtonBox,
)
from qgis.core import (
QgsCoordinateReferenceSystem,
QgsCoordinateTransform,
QgsLayerTreeLayer,
QgsProject,
QgsRasterLayer,
)
from .about.do_about import About
from .about.metadata import Metadata
from .downloader import Downloader
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'srtm_downloader_dialog_base.ui'))
class SrtmDownloaderDialogBase(QDialog, FORM_CLASS):
"""
Class documentation goes here.
"""
def __init__(self, iface, parent=None):
"""
Constructor
@param parent reference to the parent widget
@type QWidget
"""
super(SrtmDownloaderDialogBase, self).__init__(parent)
self.setupUi(self)
self.iface = iface
self.username = None
self.password = None
self.success = False
self.cancelled = False
self.dir = tempfile.gettempdir()
self.btn_download.setEnabled(False)
self.request_is_aborted = False
self.is_error = None
self.spb_east.valueChanged.connect(self.coordinates_valid)
self.spb_west.valueChanged.connect(self.coordinates_valid)
self.spb_north.valueChanged.connect(self.coordinates_valid)
self.spb_south.valueChanged.connect(self.coordinates_valid)
self.setWindowTitle("SRTM-Downloader %s" % (Metadata().version()))
self.lne_SRTM_path.setText(tempfile.gettempdir())
self.min_tile = ''
self.max_tile = ''
self.n_tiles = 0
self.button_box.button(QDialogButtonBox.StandardButton.Close).setEnabled(True)
self.button_box.button(QDialogButtonBox.StandardButton.Abort).setEnabled(False)
self.settings = QSettings()
self.init_gui()
self.downloader = Downloader(self)
def init_gui(self):
dem_dict = {
"SRTMGL3": "SRTM GL3 90m",
"SRTMGL1": "SRTM GL1 30m",
"SRTMGL1_E": "SRTM GL1 Ellipsoidal 30m",
"AW3D30": "ALOS World 3D 30m",
"AW3D30_E": "ALOS World 3D Ellipsoidal, 30m",
"SRTM15Plus": "Global Bathymetry SRTM15+ V2.1 500m",
"NASADEM": "NASADEM Global DEM",
"COP30": "Copernicus Global DSM 30m",
"COP90": "Copernicus Global DSM 90m",
"EU_DTM": "DTM 30m",
"GEDI_L3": "DTM 1000m",
"GEBCOIceTopo": "Global Bathymetry 500m",
"GEBCOSubIceTopo": "Global Bathymetry 500m",
"CA_MRDEM_DSM": "DSM 30m",
"CA_MRDEM_DTM": "DTM 30m",
}
self.cmb_demtype.clear()
for key, desc in dem_dict.items():
self.cmb_demtype.addItem(f"{key} ({desc})", key)
index = self.cmb_demtype.findData(self.settings.value('/SRTM-Downloader/dem'))
if index >= 0:
self.cmb_demtype.setCurrentIndex(index)
self.lne_api_key.setText(self.settings.value('/SRTM-Downloader/api_key'))
@pyqtSlot()
def on_button_box_rejected(self):
"""
Slot documentation goes here.
"""
selected_dem = self.cmb_demtype.currentData()
self.settings.setValue('/SRTM-Downloader/dem', selected_dem)
self.settings.setValue('/SRTM-Downloader/api_key', self.lne_api_key.text())
self.reject()
@pyqtSlot()
def on_btn_extent_clicked(self):
"""
Slot documentation goes here.
"""
crs_dest = QgsCoordinateReferenceSystem(4326) # WGS84
crs_src = self.iface.mapCanvas().mapSettings().destinationCrs()
xform = QgsCoordinateTransform()
xform.setSourceCrs(crs_src)
xform.setDestinationCrs(crs_dest)
extent = xform.transform(self.iface.mapCanvas().extent())
self.spb_west.setValue(math.floor(extent.xMinimum()))
self.spb_east.setValue(math.ceil(extent.xMaximum()))
self.spb_south.setValue(math.floor(extent.yMinimum()))
self.spb_north.setValue(math.ceil(extent.yMaximum()))
def coordinates_valid(self, text):
if self.spb_north.value() < -56 and self.spb_south.value() < -56:
QMessageBox.warning(
None,
self.tr("Box out of covered area"),
self.tr("The area you have defined is completely outside the area covered by the SRTM tiles."),
QMessageBox.StandardButtons(
QMessageBox.StandardButton.Cancel))
self.btn_download.setEnabled(False)
elif self.spb_north.value() > 59 or self.spb_south.value() < -56 and self.spb_north.value() != 0:
res = QMessageBox.warning(
None,
self.tr("Box out of covered area"),
self.tr(
"The area you have defined is partly outside the area covered by the SRTM tiles. "
"Do you like to continue?"
),
QMessageBox.StandardButtons(
QMessageBox.StandardButton.No |
QMessageBox.StandardButton.Yes))
if res == QMessageBox.StandardButton.Yes:
self.btn_download.setEnabled(True)
else:
self.btn_download.setEnabled(False)
else:
self.btn_download.setEnabled(True)
def get_tiles(self):
product = self.cmb_demtype.currentData()
out_path = '{}/{}.tiff'.format(self.lne_SRTM_path.text(), product)
image = self.downloader.download_opentopo_globaldem(
product,
self.spb_south.value(),
self.spb_north.value(),
self.spb_west.value(),
self.spb_east.value(),
out_path,
)
self.load_image_to_canvas(image)
self.button_box.button(QDialogButtonBox.StandardButton.Close).setEnabled(True)
return True
def load_image_to_canvas(self, image_path=None):
rlayer = QgsRasterLayer(image_path, "DEM")
QgsProject.instance().addMapLayer(rlayer, False)
layer_tree = self.iface.layerTreeCanvasBridge().rootGroup()
layer_tree.insertChildNode(0, QgsLayerTreeLayer(rlayer))
if not rlayer.isValid():
print("Layer failed to load!")
@pyqtSlot()
def on_btn_download_clicked(self):
"""
Slot documentation goes here.
"""
self.min_tile = ''
self.max_tile = ''
self.button_box.setEnabled(True)
self.button_box.button(QDialogButtonBox.StandardButton.Close).setEnabled(False)
self.button_box.button(QDialogButtonBox.StandardButton.Abort).setEnabled(True)
self.get_tiles()
@pyqtSlot()
def on_btn_file_dialog_clicked(self):
"""
Slot documentation goes here.
"""
home = expanduser("~")
self.dir = QFileDialog.getExistingDirectory(
None, self.tr("Open Directory"),
home,
QFileDialog.Option.ShowDirsOnly | QFileDialog.Option.DontResolveSymlinks)
self.lne_SRTM_path.setText(self.dir)
@pyqtSlot()
def on_btn_about_clicked(self):
"""
Slot documentation goes here.
"""
self.about = About()
self.about.exec()
@pyqtSlot(str)
def on_lne_api_key_textChanged(self, p0):
"""
Slot documentation goes here.
@param p0 DESCRIPTION
@type str
"""
self.settings.setValue('/SRTM-Downloader/api_key', p0)