-
Notifications
You must be signed in to change notification settings - Fork 4
/
wnt_network_from_epanet.py
261 lines (226 loc) · 8.63 KB
/
wnt_network_from_epanet.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
WaterNetworkTools
A QGIS plugin
Water Network Modelling Utilities
-------------------
begin : 2019-07-19
copyright : (C) 2019 by Andrés García Martínez
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__ = 'Andrés García Martínez'
__date__ = '2019-07-19'
__copyright__ = '(C) 2019 by Andrés García Martínez'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from qgis.PyQt.QtCore import QCoreApplication, QVariant
from qgis.core import (QgsProcessingAlgorithm,
QgsFields,
QgsField,
QgsFeature,
QgsProcessingParameterFile,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterCrs,
QgsGeometry,
QgsWkbTypes
)
from . import utils_core as tools
class NetworkFromEpanetAlgorithm(QgsProcessingAlgorithm):
"""
Built a network from an epanet file.
"""
# DEFINE CONSTANTS
INPUT = 'INPUT'
NODE_OUTPUT = 'NODE_OUTPUT'
LINK_OUTPUT = 'LINK_OUTPUT'
CRS = 'CRS'
def tr(self, string):
"""
Returns a translatable string with the self.tr() function.
"""
return QCoreApplication.translate('Processing', string)
def createInstance(self):
"""
createInstance must return a new copy of algorithm.
"""
return NetworkFromEpanetAlgorithm()
def name(self):
"""
Returns the unique algorithm name, used for identifying the algorithm.
"""
return 'network_from_epanet'
def displayName(self):
"""
Returns the translated algorithm name.
"""
return self.tr('Network from epanet file')
def group(self):
"""
Returns the name of the group this algorithm belongs to.
"""
return self.tr('Import')
def groupId(self):
"""
Returns the unique ID of the group this algorithm belongs to.
"""
return 'import'
def shortHelpString(self):
"""
Returns a localised short help string for the algorithm.
"""
return self.tr('''Import an epanet inp file generating a network.
The epanet data imported is:
Nodes: *id *type (JUNCTIONS/RESERVOIRS/TANK) *elevation
Links: *id *start *end *type (PIPE/CVPIPE/PUMP/PRV/PSV/PBV/FCV/TCV/GPV
*length (if type is PIPE or CVPIPE) *diameter *roughness
===
Importa un archivo epanet inp y genera una red.
Los datos importados de epanet son:
Nodes: *id *type (JUNCTIONS/RESERVOIRS/TANK) *elevation
Links: *id *start *end *type (PIPE/CVPIPE/PUMP/PRV/PSV/PBV/FCV/TCV/GPV
*length (if type is PIPE or CVPIPE) *diameter *roughness
''')
def initAlgorithm(self, config=None):
"""
Define the inputs and outputs of the algorithm.
"""
# ADD INPUT FILE AND CRS SELECTOR
self.addParameter(
QgsProcessingParameterFile(
self.INPUT,
self.tr('Epanet file'),
extension='inp'
)
)
self.addParameter(
QgsProcessingParameterCrs(
self.CRS,
self.tr('Coordinate reference system (CRS)')
)
)
# ADD NODE AND LINK SINKS
self.addParameter(
QgsProcessingParameterFeatureSink(
self.NODE_OUTPUT,
self.tr('Epanet nodes')
)
)
self.addParameter(
QgsProcessingParameterFeatureSink(
self.LINK_OUTPUT,
self.tr('Epanet links'),
)
)
def processAlgorithm(self, parameters, context, feedback):
"""
RUN PROCESS
"""
# INPUT
epanetf = self.parameterAsFile(parameters, self.INPUT, context)
crs = self.parameterAsCrs(parameters, self.CRS, context)
# SHOW INFO
feedback.pushInfo('='*40)
feedback.pushInfo('CRS is {}'.format(crs.authid()))
# READ NETWORK
network = tools.WntNetwork()
network.from_epanet(epanetf)
nodes = network.nodes()
links = network.links()
# GENERATE NODES LAYER
newfields = QgsFields()
newfields.append(QgsField("id", QVariant.String))
newfields.append(QgsField("type", QVariant.String))
newfields.append(QgsField("elevation", QVariant.Double))
(node_sink, node_id) = self.parameterAsSink(
parameters,
self.NODE_OUTPUT,
context,
newfields,
QgsWkbTypes.Point,
crs
)
# ADD NODES
ncnt = 0
ntot = len(nodes)
for node in nodes:
#add feature to sink
ncnt += 1
f = QgsFeature()
f.setGeometry(QgsGeometry.fromWkt(node.to_wkt()))
f.setAttributes([node.name(), node.get_type(), node.get_elevation()])
# ADD NODE
node_sink.addFeature(f)
# SHOW PROGRESS
if ncnt % 100 == 0:
feedback.setProgress(50*ncnt/ntot) # Update the progress bar
# GENERATE LINKS LAYER
newfields = QgsFields()
newfields.append(QgsField("id", QVariant.String))
newfields.append(QgsField("start", QVariant.String))
newfields.append(QgsField("end", QVariant.String))
newfields.append(QgsField("type", QVariant.String))
newfields.append(QgsField("length", QVariant.Double))
newfields.append(QgsField("diameter", QVariant.Double))
newfields.append(QgsField("roughness", QVariant.Double))
(link_sink, link_id) = self.parameterAsSink(
parameters,
self.LINK_OUTPUT,
context,
newfields,
QgsWkbTypes.LineString,
crs
)
# ADD LINKS
lcnt = 0
ltot = len(links)
for link in links:
lcnt += 1
f = QgsFeature()
f.setGeometry(QgsGeometry.fromWkt(link.to_wkt()))
if link.get_type() in ['PIPE', 'CVPIPE']:
f.setAttributes([link.name(),
link.start(),
link.end(),
link.get_type(),
link.epanet['length'],
link.epanet['diameter'],
link.epanet['roughness']]
)
else:
f.setAttributes(
[link.name(),
link.start(),
link.end(),
link.get_type(),
None,
None,
None]
)
# ADD LINK
link_sink.addFeature(f)
# SHOW PROGRESS
if lcnt % 100 == 0:
feedback.setProgress(50+50*lcnt/ltot) # Update the progress bar
# SHOW INFO
msg = 'Epanet model Loaded successfully.'
feedback.pushInfo(msg)
msg = 'Added: {} nodes.'.format(ncnt)
feedback.pushInfo(msg)
msg = 'Added: {} links.'.format(lcnt)
feedback.pushInfo(msg)
feedback.pushInfo('='*40)
# PROCCES CANCELED
if feedback.isCanceled():
return {}
# OUTPUT
return {self.NODE_OUTPUT: node_id, self.LINK_OUTPUT: link_id}