-
Notifications
You must be signed in to change notification settings - Fork 1
/
ltop_test_dem.py
274 lines (226 loc) · 8.73 KB
/
ltop_test_dem.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
LTOrographicPrecipitation
A QGIS plugin
Implements the Smith & Barstad (2004) LT model
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2018-05-02
copyright : (C) 2018-2020 by Andy Aschwanden and Constantine Khrulev
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. *
* *
***************************************************************************/
"""
__author__ = "Andy Aschwanden and Constantine Khrulev"
__date__ = "2018-05-02"
__copyright__ = "(C) 2018-2020 by Andy Aschwanden and Constantine Khrulev"
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = "$Format:%H$"
import os
from PyQt5.QtCore import QCoreApplication
from qgis.core import (
Qgis,
QgsProcessingAlgorithm,
QgsProcessingParameterCrs,
QgsProcessingParameterDefinition,
QgsProcessingParameterExtent,
QgsProcessingParameterNumber,
QgsProcessingParameterPoint,
QgsProcessingParameterRasterDestination,
QgsRasterFileWriter,
)
try:
import numpy as np
from .linear_orog_precip import gaussian_bump
has_numpy = True
except:
has_numpy = False
class LTOrographicPrecipitationTestInput(QgsProcessingAlgorithm):
"""
Creates the gaussian bump field for testing.
"""
# Constants used to refer to parameters and outputs. They will be
# used when calling the algorithm from another algorithm, or when
# calling from the QGIS console.
OUTPUT = "OUTPUT"
TARGET_CRS = "TARGET_CRS"
EXTENT = "EXTENT"
DX = "DX"
DY = "DY"
SIGMA_X = "SIGMA_X"
SIGMA_Y = "SIGMA_Y"
CENTER = "CENTER"
def initAlgorithm(self, config):
x_min = -100e3
x_max = 200e3
y_min = -150e3
y_max = 150e3
dx = 750
dy = 750
x0 = -25e3
y0 = 0.0
sigma_x = 15e3
sigma_y = 15e3
self.addParameter(
QgsProcessingParameterCrs(
self.TARGET_CRS, self.tr("Target CRS"), "ProjectCrs"
)
)
self.addParameter(
QgsProcessingParameterExtent(
self.EXTENT,
self.tr("Extent"),
"{}, {}, {}, {}".format(x_min, x_max, y_min, y_max),
)
)
self.addParameter(
QgsProcessingParameterNumber(
self.DX,
self.tr("Grid spacing (dx, meters)"),
QgsProcessingParameterNumber.Double,
defaultValue=dx,
minValue=0.0,
)
)
self.addParameter(
QgsProcessingParameterNumber(
self.DY,
self.tr("Grid spacing (dy, meters)"),
QgsProcessingParameterNumber.Double,
defaultValue=dy,
minValue=0.0,
)
)
self.addParameter(
QgsProcessingParameterPoint(
self.CENTER, self.tr("Center"), "{}, {}".format(x0, y0)
)
)
s_x = QgsProcessingParameterNumber(
self.SIGMA_X,
self.tr("Spread in the X direction (sigma_x)"),
QgsProcessingParameterNumber.Double,
defaultValue=sigma_x,
minValue=0.0,
)
s_x.setFlags(s_x.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(s_x)
s_y = QgsProcessingParameterNumber(
self.SIGMA_Y,
self.tr("Spread in the Y direction (sigma_y)"),
QgsProcessingParameterNumber.Double,
defaultValue=sigma_y,
minValue=0.0,
)
s_y.setFlags(s_y.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(s_y)
self.addParameter(
QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr("Testing DEM"))
)
def prepareAlgorithm(self, parameters, context, feedback):
"Prepare and validate parameter values."
self.crs = self.parameterAsCrs(parameters, self.TARGET_CRS, context)
self.extent = self.parameterAsExtent(parameters, self.EXTENT, context, self.crs)
self.center = self.parameterAsPoint(parameters, self.CENTER, context, self.crs)
self.dx = self.parameterAsDouble(parameters, self.DX, context)
self.dy = self.parameterAsDouble(parameters, self.DY, context)
self.sigma_x = self.parameterAsDouble(parameters, self.SIGMA_X, context)
self.sigma_y = self.parameterAsDouble(parameters, self.SIGMA_Y, context)
if self.dx > self.extent.width():
feedback.reportError(
self.tr("Grid spacing cannot exceed grid extent (x direction).")
)
return False
if self.dy > self.extent.height():
feedback.reportError(
self.tr("Grid spacing cannot exceed grid extent (y direction).")
)
return False
self.outputFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
return True
def processAlgorithm(self, parameters, context, feedback):
"Create the test DEM."
extent = self.extent
x_min = extent.xMinimum()
x_max = extent.xMaximum()
y_min = extent.yMinimum()
y_max = extent.yMaximum()
x0 = self.center.x()
y0 = self.center.y()
outputFormat = QgsRasterFileWriter.driverForExtension(
os.path.splitext(self.outputFile)[1]
)
rows = max([np.ceil(extent.height() / self.dy), 1.0])
cols = max([np.ceil(extent.width() / self.dx), 1.0])
writer = QgsRasterFileWriter(self.outputFile)
writer.setOutputProviderKey("gdal")
writer.setOutputFormat(outputFormat)
provider = writer.createOneBandRaster(
Qgis.Float64, cols, rows, extent, self.crs
)
provider.setNoDataValue(1, -9999)
_, _, data = gaussian_bump(
x_min,
x_max,
y_min,
y_max,
self.dx,
self.dy,
x0=x0,
y0=y0,
sigma_x=self.sigma_x,
sigma_y=self.sigma_y,
)
provider.write(
bytes(data.data), 1, cols, rows, 0, 0 # band # width # height
) # offset
provider.setEditable(False)
return {self.OUTPUT: self.outputFile}
def name(self):
"""
Returns the algorithm name, used for identifying the algorithm. This
string should be fixed for the algorithm, and must not be localised.
The name should be unique within each provider. Names should contain
lowercase alphanumeric characters only and no spaces or other
formatting characters.
"""
return "bump"
def displayName(self):
"""
Returns the translated algorithm name, which should be used for any
user-visible display of the algorithm name.
"""
return self.tr("Isolated Gaussian hill")
def group(self):
"""
Returns the name of the group this algorithm belongs to. This string
should be localised.
"""
return self.tr("Testing")
def groupId(self):
"""
Returns the unique ID of the group this algorithm belongs to. This
string should be fixed for the algorithm, and must not be localised.
The group id should be unique within each provider. Group id should
contain lowercase alphanumeric characters only and no spaces or other
formatting characters.
"""
return "testing"
def tr(self, string):
return QCoreApplication.translate("Processing", string)
def createInstance(self):
return LTOrographicPrecipitationTestInput()
def shortHelpString(self):
return """
Create a raster layer containing a Gaussian "bump" orography that can be used to test the LT model.
Default parameter values create the field needed to reproduce Figure 4 in Smith and Barstad (2004)."""
def canExecute(self):
return has_numpy, "NumPy is not installed"