PyQgis API#

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

This API will allow you to use features in PyQGIS scripts and within the python console in QGIS.

Usage#

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

>>> import wntrqgis
>>> import wntr

We will use one of the example .inp files provided

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

We can load the example file into QGIS

>>> layers = wntrqgis.to_qgis(wntrqgis.examples['KY10'], crs='EPSG:3089', units='LPS')
>>> 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 = wntrqgis.from_qgis(layers, units='LPS', headloss='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 = wntrqgis.to_qgis(wn, results, crs='EPSG:3089', units='lps')
>>> result_layers
{'NODES': <QgsVectorLayer: 'Nodes' (memory)>, 'LINKS': <QgsVectorLayer: 'Links' (memory)>}

Reference#

wntrqgis.from_qgis(layers, units, headloss=None, wn=None, project=None, crs=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', 'CFS', 'GPM', 'MGD', 'IMGD', 'AFD', 'SI']) – The flow unit set that the layers being read use.

  • headloss (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 WaterNetworkModel that the layers will be read into. Will create a new model if None.

  • project (QgsProject | None) – QgsProject instance, if None the current QgsProject.instance() will be used.

  • crs (QgsCoordinateReferenceSystem | str | None) – All geometry will be transformed into this coordinate reference system. If not set the geometry of the first layer will be used.

Return type:

WaterNetworkModel

wntrqgis.to_qgis(wn, results=None, crs=None, units=None)#

Write from WNTR network model to QGIS Layers

Parameters:
Return type:

dict[str, QgsVectorLayer]