Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base_demand, demand_pattern and demand_category at junctions go to/from gis #447

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions documentation/gis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ For example, the junctions GeoDataFrame contains the following information:
:skipif: gpd is None

>>> print(wn_gis.junctions.head())
node_type elevation initial_quality geometry
name
10 Junction 216.408 5.000e-04 POINT (20.00000 70.00000)
11 Junction 216.408 5.000e-04 POINT (30.00000 70.00000)
12 Junction 213.360 5.000e-04 POINT (50.00000 70.00000)
13 Junction 211.836 5.000e-04 POINT (70.00000 70.00000)
21 Junction 213.360 5.000e-04 POINT (30.00000 40.00000)
node_type base_demand demand_pattern elevation initial_quality geometry
name
10 Junction 0.000 1 216.408 5.000e-04 POINT (20.00000 70.00000)
11 Junction 0.009 1 216.408 5.000e-04 POINT (30.00000 70.00000)
12 Junction 0.009 1 213.360 5.000e-04 POINT (50.00000 70.00000)
13 Junction 0.006 1 211.836 5.000e-04 POINT (70.00000 70.00000)
21 Junction 0.009 1 213.360 5.000e-04 POINT (30.00000 40.00000)

Each GeoDataFrame contains attributes and geometry:

Expand Down Expand Up @@ -334,23 +334,23 @@ and then translates the GeoDataFrames coordinates to EPSG:3857.

>>> wn_gis = wntr.network.to_gis(wn, crs='EPSG:4326')
>>> print(wn_gis.junctions.head())
node_type elevation initial_quality geometry
name
10 Junction 216.408 5.000e-04 POINT (20.00000 70.00000)
11 Junction 216.408 5.000e-04 POINT (30.00000 70.00000)
12 Junction 213.360 5.000e-04 POINT (50.00000 70.00000)
13 Junction 211.836 5.000e-04 POINT (70.00000 70.00000)
21 Junction 213.360 5.000e-04 POINT (30.00000 40.00000)
node_type base_demand demand_pattern elevation initial_quality geometry
name
10 Junction 0.000 1 216.408 5.000e-04 POINT (20.00000 70.00000)
11 Junction 0.009 1 216.408 5.000e-04 POINT (30.00000 70.00000)
12 Junction 0.009 1 213.360 5.000e-04 POINT (50.00000 70.00000)
13 Junction 0.006 1 211.836 5.000e-04 POINT (70.00000 70.00000)
21 Junction 0.009 1 213.360 5.000e-04 POINT (30.00000 40.00000)

>>> wn_gis.to_crs('EPSG:3857')
>>> print(wn_gis.junctions.head())
node_type elevation initial_quality geometry
name
10 Junction 216.408 5.000e-04 POINT (2226389.816 11068715.659)
11 Junction 216.408 5.000e-04 POINT (3339584.724 11068715.659)
12 Junction 213.360 5.000e-04 POINT (5565974.540 11068715.659)
13 Junction 211.836 5.000e-04 POINT (7792364.356 11068715.659)
21 Junction 213.360 5.000e-04 POINT (3339584.724 4865942.280)
node_type base_demand demand_pattern elevation initial_quality geometry
name
10 Junction 0.000 1 216.408 5.000e-04 POINT (2226389.816 11068715.659)
11 Junction 0.009 1 216.408 5.000e-04 POINT (3339584.724 11068715.659)
12 Junction 0.009 1 213.360 5.000e-04 POINT (5565974.540 11068715.659)
13 Junction 0.006 1 211.836 5.000e-04 POINT (7792364.356 11068715.659)
21 Junction 0.009 1 213.360 5.000e-04 POINT (3339584.724 4865942.280)

Snap point geometries to the nearest point or line
----------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions documentation/model_io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ The following example returns valid base GeoJSON column names for junctions.

>>> geojson_column_names = wntr.network.io.valid_gis_names()
>>> print(geojson_column_names['junctions'])
['name', 'elevation', 'coordinates', 'emitter_coefficient', 'initial_quality', 'minimum_pressure', 'required_pressure', 'pressure_exponent', 'tag']
['name', 'elevation', 'coordinates', 'base_demand', 'demand_pattern', 'demand_category', 'emitter_coefficient', 'initial_quality', 'minimum_pressure', 'required_pressure', 'pressure_exponent', 'tag']

A minimal list of valid column names can also be obtained by setting ``complete_list`` to False.
Column names that are optional (i.e., ``initial_quality``) and not included in the GeoJSON file are defined using default values.
Expand Down Expand Up @@ -311,7 +311,7 @@ To use Esri Shapefiles in WNTR, several formatting requirements are enforced:

>>> shapefile_field_names = wntr.network.io.valid_gis_names(truncate_names=10)
>>> print(shapefile_field_names['junctions'])
['name', 'elevation', 'coordinate', 'emitter_co', 'initial_qu', 'minimum_pr', 'required_p', 'pressure_e', 'tag']
['name', 'elevation', 'coordinate', 'base_deman', 'demand_pat', 'demand_cat', 'emitter_co', 'initial_qu', 'minimum_pr', 'required_p', 'pressure_e', 'tag']

A minimal list of valid field names can also be obtained by setting ``complete_list`` to False.
Field names that are optional (i.e., ``initial_quality``) and not included in the Shapefile are defined using default values.
Expand Down
2 changes: 1 addition & 1 deletion wntr/network/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def to_dict(self):
d['node_type'] = self.node_type
for k in dir(self):
if not k.startswith('_') and \
k not in ['demand', 'base_demand', 'head', 'leak_area', 'leak_demand',
k not in ['demand', 'head', 'leak_area', 'leak_demand',
'leak_discharge_coeff', 'leak_status', 'level', 'pressure', 'quality', 'vol_curve', 'head_timeseries']:
try:
val = getattr(self, k)
Expand Down
31 changes: 30 additions & 1 deletion wntr/network/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ class Junction(Node):
_base_attributes = ["name",
"elevation",
"coordinates"]
_optional_attributes = ["emitter_coefficient",
_optional_attributes = ["base_demand",
"demand_pattern",
"demand_category",
"emitter_coefficient",
"initial_quality",
"minimum_pressure",
"required_pressure",
Expand Down Expand Up @@ -216,6 +219,32 @@ def base_demand(self):
def base_demand(self, value):
raise RuntimeWarning('The base_demand property is read-only. Please modify using demand_timeseries_list[0].base_value.')

@property
def demand_pattern(self):
"""Get the pattern_name of the first demand in the demand_timeseries_list.

This is a read-only property.
"""
if len(self.demand_timeseries_list) > 0:
return self.demand_timeseries_list[0].pattern_name
return None
@demand_pattern.setter
def demand_pattern(self, value):
raise RuntimeWarning('The demand_pattern property is read-only. Please modify using demand_timeseries_list[0].pattern_name')

@property
def demand_category(self):
"""Get the category of the first demand in the demand_timeseries_list.

This is a read-only property.
"""
if len(self.demand_timeseries_list) > 0:
return self.demand_timeseries_list[0].category
return None
@demand_category.setter
def demand_category(self, value):
raise RuntimeWarning('The demand_category property is read-only. Please modify using demand_timeseries_list[0].category.')

def add_leak(self, wn, area, discharge_coeff=0.75, start_time=None, end_time=None):
"""
Add a leak control to the water network model
Expand Down
10 changes: 5 additions & 5 deletions wntr/network/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,18 @@ def from_dict(d: dict, append=None):
if dl is not None and len(dl) > 0:
base_demand = dl[0].setdefault("base_val", 0.0)
pattern_name = dl[0].setdefault("pattern_name")
category = dl[0].setdefault("category")
demand_category = dl[0].setdefault("category")
else:
base_demand = 0.0
pattern_name = None
category = None
base_demand = node.setdefault('base_demand',0.0)
pattern_name = node.setdefault('pattern_name')
demand_category = node.setdefault('demand_category')
wn.add_junction(
name=name,
base_demand=base_demand,
demand_pattern=pattern_name,
elevation=node.setdefault("elevation"),
coordinates=node.setdefault("coordinates", list()),
demand_category=category,
demand_category=demand_category,
)
j = wn.get_node(name)
j.emitter_coefficient = node.setdefault("emitter_coefficient")
Expand Down
3 changes: 3 additions & 0 deletions wntr/tests/test_gis.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ def test_wn_to_gis(self):
assert set(['link_type', 'start_node_name', 'end_node_name', 'geometry']).issubset(self.gis_data.pipes.columns)
assert set(['link_type', 'start_node_name', 'end_node_name', 'geometry']).issubset(self.gis_data.pumps.columns)
#assert set(['link_type', 'start_node_name', 'end_node_name', 'geometry']).issubset(self.gis_data.valves.columns) # Net1 has no valves

#check base_demand and demand_pattern attrivutes
assert set(['base_demand','demand_pattern']).issubset(self.gis_data.junctions.columns)

def test_gis_to_wn(self):

Expand Down
Loading