wntr.network.io module

The wntr.network.io module includes functions that convert the water network model to other data formats, create a water network model from file, and write the water network model to a file.

Contents

to_dict(wn)

Convert a WaterNetworkModel into a dictionary

from_dict(d[, append])

Create or append a WaterNetworkModel from a dictionary

to_gis(wn[, crs, pumps_as_points, ...])

Convert a WaterNetworkModel into GeoDataFrames

from_gis(gis_data[, append])

Create or append a WaterNetworkModel from GeoDataFrames

to_graph(wn[, node_weight, link_weight, ...])

Convert a WaterNetworkModel into a networkx MultiDiGraph

write_json(wn, path_or_buf, **kw_json)

Write the WaterNetworkModel to a JSON file

read_json(path_or_buf[, append])

Create or append a WaterNetworkModel from a JSON file

write_inpfile(wn, filename[, units, ...])

Write the WaterNetworkModel to an EPANET INP file

read_inpfile(filename[, append])

Create or append a WaterNetworkModel from an EPANET INP file

write_geojson(wn, prefix[, crs, ...])

Write the WaterNetworkModel to a set of GeoJSON files, one file for each network element.

read_geojson(files[, index_col, append])

Create or append a WaterNetworkModel from GeoJSON files

write_shapefile(wn, prefix[, crs, ...])

Write the WaterNetworkModel to a set of Esri Shapefiles, one directory for each network element.

read_shapefile(files[, index_col, append])

Create or append a WaterNetworkModel from Esri Shapefiles

wntr.network.io.to_dict(wn) dict[source]

Convert a WaterNetworkModel into a dictionary

Parameters

wn (WaterNetworkModel) – Water network model

Returns

dict – Dictionary representation of the WaterNetworkModel

wntr.network.io.from_dict(d: dict, append=None)[source]

Create or append a WaterNetworkModel from a dictionary

Parameters
  • d (dict) – Dictionary representation of the water network model

  • append (WaterNetworkModel or None, optional) – Existing WaterNetworkModel to append. If None, a new WaterNetworkModel is created.

Returns

WaterNetworkModel

wntr.network.io.to_gis(wn, crs=None, pumps_as_points=False, valves_as_points=False)[source]

Convert a WaterNetworkModel into GeoDataFrames

Parameters
  • wn (WaterNetworkModel) – Water network model

  • crs (str, optional) – Coordinate reference system, by default None

  • pumps_as_points (bool, optional) – Represent pumps as points (True) or lines (False), by default False

  • valves_as_points (bool, optional) – Represent valves as points (True) or lines (False), by default False

Returns

WaterNetworkGIS object that contains GeoDataFrames

wntr.network.io.from_gis(gis_data, append=None)[source]

Create or append a WaterNetworkModel from GeoDataFrames

Parameters
  • gis_data (WaterNetworkGIS or dictionary of GeoDataFrames) – GeoDataFrames containing water network attributes. If gis_data is a dictionary, then the keys are junctions, tanks, reservoirs, pipes, pumps, and valves. If the pumps or valves are Points, they will be converted to Lines with the same start and end node location.

  • append (WaterNetworkModel or None, optional) – Existing WaterNetworkModel to append. If None, a new WaterNetworkModel is created.

Returns

WaterNetworkModel

wntr.network.io.to_graph(wn, node_weight=None, link_weight=None, modify_direction=False)[source]

Convert a WaterNetworkModel into a networkx MultiDiGraph

Parameters
  • node_weight (dict or pandas Series (optional)) – Node weights

  • link_weight (dict or pandas Series (optional)) – Link weights.

  • modify_direction (bool (optional)) – If True, than if the link weight is negative, the link start and end node are switched and the abs(weight) is assigned to the link (this is useful when weighting graphs by flowrate). If False, link direction and weight are not changed.

Returns

networkx MultiDiGraph

wntr.network.io.write_json(wn, path_or_buf, **kw_json)[source]

Write the WaterNetworkModel to a JSON file

Parameters
  • path_or_buf (str or IO stream) – Name of the file or file pointer

  • kw_json (keyword arguments) – Arguments to pass directly to json.dump

wntr.network.io.read_json(path_or_buf, append=None, **kw_json)[source]

Create or append a WaterNetworkModel from a JSON file

Parameters
  • f (str) – Name of the file or file pointer

  • append (WaterNetworkModel or None, optional) – Existing WaterNetworkModel to append. If None, a new WaterNetworkModel is created.

  • kw_json (keyword arguments) – Keyword arguments to pass to json.load

Returns

WaterNetworkModel

wntr.network.io.write_inpfile(wn, filename: str, units=None, version: float = 2.2, force_coordinates: bool = False)[source]

Write the WaterNetworkModel to an EPANET INP file

Note

By default, WNTR now uses EPANET version 2.2 for the EPANET simulator engine. Thus, The WaterNetworkModel will also write an EPANET 2.2 formatted INP file by default as well. Because the PDD analysis options will break EPANET 2.0, the version option will allow the user to force EPANET 2.0 compatibility at the expense of pressured-dependent analysis options being turned off.

Parameters
  • wn (wntr WaterNetworkModel) – Water network model

  • filename (string) – Name of the inp file.

  • units (str, int or FlowUnits) – Name of the units being written to the inp file.

  • version (float, {2.0, 2.2}) – Optionally specify forcing EPANET 2.0 compatibility.

  • force_coordinates (bool) – This only applies if self.options.graphics.map_filename is not None, and will force the COORDINATES section to be written even if a MAP file is provided. False by default, but coordinates are written by default since the MAP file is None by default.

wntr.network.io.read_inpfile(filename, append=None)[source]

Create or append a WaterNetworkModel from an EPANET INP file

Parameters
  • filename (string) – Name of the INP file.

  • append (WaterNetworkModel or None, optional) – Existing WaterNetworkModel to append. If None, a new WaterNetworkModel is created.

Returns

WaterNetworkModel

wntr.network.io.write_geojson(wn, prefix: str, crs=None, pumps_as_points=True, valves_as_points=True)[source]

Write the WaterNetworkModel to a set of GeoJSON files, one file for each network element.

The GeoJSON only includes information from the water network model. To add results of a simulation or analysis, do:

wn_gis = wn.to_gis()
wn_gis.add_node_attributes(some_data_to_add, 'name_of_attribute')
wn_gis.write_geojson(...)
Parameters
  • wn (wntr WaterNetworkModel) – Water network model

  • prefix (str) – File prefix

  • crs (str, optional) – Coordinate reference system, by default None

  • pumps_as_points (bool, optional) – Represent pumps as points (True) or lines (False), by default False

  • valves_as_points (bool, optional) – Represent valves as points (True) or lines (False), by default False

wntr.network.io.read_geojson(files, index_col='index', append=None)[source]

Create or append a WaterNetworkModel from GeoJSON files

Parameters
  • files (dictionary) – Dictionary of GeoJSON filenames, where the keys are in the set (‘junction’, ‘tanks’, ‘reservoirs’, ‘pipes’, ‘pumps’, ‘valves’) and values are the corresponding GeoJSON filename

  • index_col (str, optional) – Column that contains the element name

  • append (WaterNetworkModel or None, optional) – Existing WaterNetworkModel to append. If None, a new WaterNetworkModel is created.

Returns

WaterNetworkModel

wntr.network.io.write_shapefile(wn, prefix: str, crs=None, pumps_as_points=True, valves_as_points=True)[source]

Write the WaterNetworkModel to a set of Esri Shapefiles, one directory for each network element.

The Shapefiles only includes information from the water network model. To add results of a simulation or analysis, do:

wn_gis = wn.to_gis()
wn_gis.add_node_attributes(some_data_to_add, 'name_of_attribute')
wn_gis.write_shapefile(...)
Parameters
  • wn (wntr WaterNetworkModel) – Water network model

  • prefix (str) – File and directory prefix

  • crs (str, optional) – Coordinate reference system, by default None

  • pumps_as_points (bool, optional) – Represent pumps as points (True) or lines (False), by default False

  • valves_as_points (bool, optional) – Represent valves as points (True) or lines (False), by default False

wntr.network.io.read_shapefile(files, index_col='index', append=None)[source]

Create or append a WaterNetworkModel from Esri Shapefiles

Parameters
  • files (dictionary) – Dictionary of Shapefile file or directory names, where the keys are in the set (‘junction’, ‘tanks’, ‘reservoirs’, ‘pipes’, ‘pumps’, ‘valves’) and values are the corresponding Shapefile filenames or directories

  • index_col (str, optional) – Column that contains the element name

  • append (WaterNetworkModel or None, optional) – Existing WaterNetworkModel to append. If None, a new WaterNetworkModel is created.

Returns

WaterNetworkModel

wntr.network.io.valid_gis_names(complete_list=True, truncate_names=None)[source]

Valid column/field names for GeoJSON or Shapefiles

Note that Shapefile field names are truncated to 10 characters (set truncate=10)

Parameters
  • complete_list (bool) – Include a complete list of column/field names (beyond basic attributes)

  • truncate_names (None or int) – Truncate column/field names to specified number of characters, set truncate=10 for Shapefiles. None indicates no truncation.

Returns

dict (Dictionary of valid GeoJSON or Shapefile column/field names)