Skip to content

Commit 2beb736

Browse files
author
Fred Thomas
committed
delegate split_multilinestrings to geopandas explode
1 parent 5e050fe commit 2beb736

File tree

1 file changed

+13
-39
lines changed

1 file changed

+13
-39
lines changed

src/snkit/network.py

+13-39
Original file line numberDiff line numberDiff line change
@@ -186,50 +186,24 @@ def _set_precision(geom):
186186

187187

188188
def split_multilinestrings(network):
189-
"""Create multiple edges from any MultiLineString edge
190-
191-
Ensures that edge geometries are all LineStrings, duplicates attributes over any
192-
created multi-edges.
193189
"""
194-
simple_edge_attrs = []
195-
simple_edge_geoms = []
196-
edges = network.edges
197-
198-
# # Commented out idea about dealing with multi-part MultiLineStrings separately
199-
# is_multi_single_part = edges.geometry.apply(lambda geom: geom.geom_type == 'MultiLineString' and len(geom) == 1)
200-
# is_multi_multi_part = edges.geometry.apply(lambda geom: geom.geom_type == 'MultiLineString' and len(geom) > 1)
201-
# is_not_multi = edges.geometry.apply(lambda geom: geom.geom_type != 'MultiLineString')
202-
203-
# not_multi_edges = edges[is_not_multi].copy()
204-
# multi_single_part_edges = edges[is_multi_single_part].copy()
205-
# multi_single_part_edges.geometry = multi_single_part_edges.geometry.apply(lambda geom: next(geom))
206-
# edges = edges[is_multi_multi_part].copy()
207-
208-
for edge in tqdm(
209-
edges.itertuples(index=False), desc="split_multi", total=len(edges)
210-
):
211-
if edge.geometry.geom_type == "MultiLineString":
212-
edge_parts = list(edge.geometry)
213-
else:
214-
edge_parts = [edge.geometry]
215-
216-
for part in edge_parts:
217-
simple_edge_geoms.append(part)
190+
Create multiple edges from any MultiLineString edge
218191
219-
attrs = GeoDataFrame([edge] * len(edge_parts))
220-
simple_edge_attrs.append(attrs)
192+
Ensures that edge geometries are all LineStrings, duplicates attributes
193+
over any created multi-edges.
194+
"""
221195

222-
simple_edge_geoms = GeoDataFrame(simple_edge_geoms, columns=["geometry"])
223-
edges = (
224-
pandas.concat(simple_edge_attrs, axis=0)
225-
.reset_index(drop=True)
226-
.drop("geometry", axis=1)
227-
)
228-
edges = pandas.concat([edges, simple_edge_geoms], axis=1)
196+
edges = network.edges
197+
geom_col: str = geometry_column_name(edges)
198+
split_edges = edges.explode(column=geom_col, ignore_index=True)
229199

230-
# edges = pandas.concat([edges, multi_single_part_edges, not_multi_edges], axis=0).reset_index(drop=True)
200+
geo_types = set(split_edges.geom_type)
201+
if geo_types != {'LineString'}:
202+
raise ValueError(
203+
f"exploded edges -> not only LineStrings {geo_types=}"
204+
)
231205

232-
return Network(nodes=network.nodes, edges=edges)
206+
return Network(nodes=network.nodes, edges=split_edges)
233207

234208

235209
def merge_multilinestring(geom):

0 commit comments

Comments
 (0)