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

The issue where the index_col parameter of read_shapefile can only be index. #414

Closed
wants to merge 1 commit into from

Conversation

Anjieluo
Copy link

@Anjieluo Anjieluo commented Apr 9, 2024

In previous code, the index_col parameter of the read_shapefile function in wntr.network.io seemed to only accept index (this required the element name in my shapefile to be index only). I wanted to use a field from my shapefile, such as id, to replace this index, instead of being restricted to using index. Therefore, I made some modifications and have provided some test documentation.
test.zip

I hope to contribute to wntr, and I apologize if my idea is incorrect.

@@ -171,7 +171,7 @@ def _extract_geodataframe(df, crs=None, links_as_points=False):
df = df_links[df_links['link_type'] == 'Valve']
self.valves = _extract_geodataframe(df, crs, valves_as_points)

def _create_wn(self, append=None):
def _create_wn(self, append=None, index_col='index'):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding index_col as an additional keyword argument is unnecessary here because the index name can be extracted from the dataframes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your reply, help me to be able to use WNTR better

@@ -192,7 +192,7 @@ def _create_wn(self, append=None):
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.rename(columns={index_col:'name', 'geometry':'coordinates'}, inplace=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, rather than resetting the index and then renaming the new index column I think it is more straightforward to run df = element.reset_index(names="name") which will reset the index and rename the index column to name in one line.

@@ -217,7 +217,7 @@ def from_dict(d: dict, append=None):
link["start_node_name"],
link["end_node_name"],
pump_type=pump_type,
pump_parameter=link.setdefault("power")
pump_parameter=link.setdefault("power",50.0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems unrelated to the rest. Can you please remove this.

@@ -350,7 +350,7 @@ def from_gis(gis_data, append=None):
if isinstance(gis_data, dict):
gis_data = WaterNetworkGIS(gis_data)

wn = gis_data._create_wn(append=append)
wn = gis_data._create_wn(append=append,index_col=index_col)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change causes an error because index_col is not defined in this context. It is also no longer necessary given the earlier suggestions.

@kbonney
Copy link
Collaborator

kbonney commented Apr 15, 2024

Thanks for the PR @Anjieluo. Please address the above comments.

@kbonney
Copy link
Collaborator

kbonney commented Apr 15, 2024

These updates will make #395 unnecessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants