Skip to content
Merged
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
10 changes: 7 additions & 3 deletions src/dhnx/gistools/geometry_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,11 @@ def check_double_points(gdf, radius=0.001, id_column=None):
return l_ids


def gdf_to_df(gdf):
def gdf_to_df(df):
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to rename this to something like drop_geometry_data? In particular, as the new parameter name suggests that that also plain DataFrames (not only GeoDataFrames) are okay.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At first my idea was to only write df = gdf.drop(columns=gdf.geometry.name). But I thought it would be saver to check if the given DataFrame is actually a GeoDataFrame. In fact, the previous version of that function actually works with DataFrames, because it would not change anything if no column named "geometry" is found.
Therefore plain DataFrames were ok before, and are ok now. I feel like renaming the input to "df" is only logical, in that sense.
I do not feel the need to rename the function, but I would not argue against it.

"""Converts a GeoDataFrame to a pandas.DataFrame by deleting the geometry
column."""

df = pd.DataFrame(gdf[[col for col in gdf.columns if col != "geometry"]])
if isinstance(df, gpd.GeoDataFrame):
df = df.drop(columns=df.geometry.name)

return df

Expand Down Expand Up @@ -364,6 +364,9 @@ def weld_segments(
gdf_line_net_new = _weld_segments(
gdf_line_net_new, gdf_line_gen, gdf_line_houses, debug_plotting
)
if len(gdf_line_net_new) == 0:
gdf_line_net_new = gdf_line_net_last
break
Comment on lines +367 to +369
Copy link
Member

Choose a reason for hiding this comment

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

Can you please add a comment when this might happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I remember correctly, I had a case where weld_segments would just delete all lines in a network. Adding that change did not solve my actual problem (because it only returns the previous state of an almost deleted network) but it simplified searching for the root of the problem by not breaking the following code (which would throw other errors with an empty network).
I must admit that I did not store the problematic input network and cannot remember the details. As I said, it was a rare edge case.

logger.info("Welding lines... done")
return gdf_line_net_new

Expand Down Expand Up @@ -586,6 +589,7 @@ def debug_plot(neighbours, color="red"):
# Merge the MultiLineString into a single object
merged_line = linemerge(multi_line)
gdf_merged = gpd.GeoDataFrame(geometry=[merged_line], crs=crs)
gdf_merged = gdf_merged.explode() # Explode Multi- into LineStrings
Copy link
Member

Choose a reason for hiding this comment

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

Why this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are other places in the code (e.g. specifically geometry_operations.split_multilinestr_to_linestr() called at the beginning of connect_points.process_geometry()) which make sure there are no MultiLineStrings in the network. I cannot tell where exactly, but this causes issues somewhere down the road.
Once again I sadly do not have a reproducible example of this, but this single line once fixed a problematic network I worked with.

debug_plot(neighbours) # Plot the segments before the merge
debug_plot(gdf_merged, color="orange") # ...and after the merge
gdf_line_net_new = pd.concat(
Expand Down