Skip to content

Commit

Permalink
Merge pull request #395 from kbonney/gis_io
Browse files Browse the repository at this point in the history
Fix bug on GIS IO caused by varying index names
  • Loading branch information
kaklise authored Jun 13, 2024
2 parents cd9290c + b4162af commit 9173b56
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
7 changes: 3 additions & 4 deletions wntr/gis/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ def _create_wn(self, append=None):
for element in [self.junctions, self.tanks, self.reservoirs]:
if element.shape[0] > 0:
assert (element['geometry'].geom_type).isin(['Point']).all()
df = element.reset_index()
df.rename(columns={'index':'name', 'geometry':'coordinates'}, inplace=True)
df = element.reset_index(names="name")
df.rename(columns={'geometry':'coordinates'}, inplace=True)
df['coordinates'] = [[x,y] for x,y in zip(df['coordinates'].x,
df['coordinates'].y)]
wn_dict['nodes'].extend(df.to_dict('records'))
Expand All @@ -201,8 +201,7 @@ def _create_wn(self, append=None):
if element.shape[0] > 0:
assert 'start_node_name' in element.columns
assert 'end_node_name' in element.columns
df = element.reset_index()
df.rename(columns={'index':'name'}, inplace=True)
df = element.reset_index(names="name")
df['vertices'] = df.apply(lambda row: list(row.geometry.coords)[1:-1], axis=1)
df.drop(columns=['geometry'], inplace=True)
wn_dict['links'].extend(df.to_dict('records'))
Expand Down
8 changes: 8 additions & 0 deletions wntr/tests/test_gis.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ def setUpClass(self):
def tearDownClass(self):
pass

def test_gis_index(self):
# Tests that WN can be made using dataframes with customized index names
wn_gis = self.wn.to_gis()
wn_gis.junctions.index.name = "my_index"
wn_gis.pipes.index.name = "my_index"
wn2 = wntr.network.from_gis(wn_gis)
self.wn == wn2

def test_wn_to_gis(self):
# Check type
isinstance(self.gis_data.junctions, gpd.GeoDataFrame)
Expand Down

0 comments on commit 9173b56

Please sign in to comment.