PyQGIS API#

The API lets you import and export to and from the WNTR python package.

You will need to have WNTR installed in the same python environment as QGIS.

This is the fledgling documentation for the work-in-progress API. The API is likely to change at every release.

Usage#

Once installe, the easiest way to use the api is within the QGIS python console.

First import gusnet and wntr. Note that this is not necessary from the QGIS console - they are already imported.

>>> import gusnet
>>> import wntr

We will use one of the example .inp files provided

>>> gusnet.examples
{'KY1': '...ky1.inp', 'KY10': '...ky10.inp', ...}

We can load the example file into QGIS

>>> layers = gusnet.from_inp(gusnet.examples['KY10'], crs='EPSG:3089')
>>> layers
{'JUNCTIONS': <QgsVectorLayer: 'Junctions' (memory)>, 'RESERVOIRS': ..., 'TANKS': ..., 'PIPES': ..., 'PUMPS': ..., 'VALVES': ...}

The layers will now have been added to QGIS. You can make edits to them and create a WaterNetworkModel when done.

>>> wn = gusnet.to_wntr(layers, units='GPM', headloss_formula='H-W')
>>> wn
<wntr.network.model.WaterNetworkModel object ...>

We can run a simulation and load the results back into QGIS.

>>> sim = wntr.sim.EpanetSimulator(wn)
>>> results = sim.run_sim()
>>> result_layers = gusnet.from_wntr(wn, results, crs='EPSG:3089')
>>> result_layers
{'NODES': <QgsVectorLayer: 'Nodes' (memory)>, 'LINKS': <QgsVectorLayer: 'Links' (memory)>}

Reference#

gusnet.from_inp(inp_path, crs=None)#

Create QGIS Layers from an Epanet input (.inp) model file.

Parameters:
  • inp_path (PathLike | str) – path to the epanet input (.inp) file

  • crs (QgsCoordinateReferenceSystem | str | None) – The Coordinate Reference System of the coordinates in the inp file. If not set the layers will not have any projection associated.

Return type:

dict[str, QgsVectorLayer]

gusnet.from_wntr(wn, results=None, crs=None, units=None)#

Create QGIS layers from a WNTR WaterNetworkModel.

Optionally, specify WNTR simulation results to create qgis layers of the results.

Parameters:
  • wn (WaterNetworkModel) – the water network model

  • results (SimulationResults | None) – simulation results, if any.

  • crs (QgsCoordinateReferenceSystem | str | None) – The Coordinate Reference System of the coordinates in the wntr model. E.g. ‘EPSG:4326’. If not set the layers will not have any projection associated.

  • units (Optional[Literal['LPS', 'LPM', 'MLD', 'CMH', 'CMD', 'CFS', 'GPM', 'MGD', 'IMGD', 'AFD']]) – WNTR stores all values as SI units. Gusnet does not use pure SI units, so you must choose which unit set you would like to use. If not set, will use the units specified in wntr.options.hydraulic.inp_units

Return type:

dict[str, QgsVectorLayer]

gusnet.to_wntr(layers, units, headloss_formula=None, wn=None)#

Read from QGIS layers or feature sources to a WNTR WaterNetworkModel

Parameters:
  • layers (dict[Literal['JUNCTIONS', 'RESERVOIRS', 'TANKS', 'PIPES', 'VALVES', 'PUMPS'], QgsFeatureSource]) – layers to read from

  • units (Literal['LPS', 'LPM', 'MLD', 'CMH', 'CMD', 'CFS', 'GPM', 'MGD', 'IMGD', 'AFD']) – The flow unit set that the layers being read use.

  • headloss_formula (Optional[Literal['H-W', 'D-W', 'C-M']]) – the headloss formula to use (H-W for Hazen Williams, D-W for Darcy Weisbach, or C-M for Chezy-Manning). Must be set if there is no wn. If wn is provided, headloss in wn.options.hydraulic.headloss will be used instead.

  • wn (WaterNetworkModel | None) – The model that the layers will be read into. Will create a new model if None.

Return type:

WaterNetworkModel