From 5b5aa576677bf9d001751d65c4aa0a52826cc1cc Mon Sep 17 00:00:00 2001 From: Morten Letnes Date: Thu, 12 Oct 2023 20:43:08 +0200 Subject: [PATCH] vectorize --- src/sgis/networkanalysis/cutting_lines.py | 74 ----------------------- 1 file changed, 74 deletions(-) diff --git a/src/sgis/networkanalysis/cutting_lines.py b/src/sgis/networkanalysis/cutting_lines.py index ec0ed56e..0f1d2151 100644 --- a/src/sgis/networkanalysis/cutting_lines.py +++ b/src/sgis/networkanalysis/cutting_lines.py @@ -218,80 +218,6 @@ def change_line_endpoint( return gdf - for idx in dists.index: - line = gdf.loc[idx, "geometry"] - coordslist = list(line.coords) - coordslist[i] = pointmapper[idx] - gdf.loc[idx, "geometry"] = LineString(coordslist) - return gdf - - -def split_lines2( - gdf: GeoDataFrame, points: GeoDataFrame, max_distance: int | float -) -> GeoDataFrame: - if not (gdf.geom_type == "LineString").all(): - raise ValueError() - - geom_col = gdf._geometry_column_name - - snapped = snap_within_distance(points, lines, max_distance=max_distance) - - line_segments = get_line_segments(gdf) - - multipoints = gdf.assign(**{geom_col == extract_unique_points(gdf[geom_col])}) - multipoints["_split_idx"] = range(len(multipoints)) - points = multipoints.explode(index_parts=False) - - snap_points_df = ( - GeoDataFrame( - {"geometry": extract_unique_points(get_parts(line_merge(snap_to)))} - ).explode(index_parts=False) - # .pipe(remove_points_on_straight_lines) - ) - - -def add_nearest_points_as_line_vertices( - lines: np.ndarray, - points: np.ndarray, - max_distance: int | float | None = None, -) -> np.ndarray: - PRECISION = 0.000001 - - lines["_line_index"] = range(len(lines)) - - snapped = snap_within_distance(points, lines, max_distance=max_distance) - snapped.index = snapped.geometry - - line_segments = ( - get_line_segments(lines) - .reset_index(drop=True) - .sjoin(snapped.pipe(buff, PRECISION)) - ) - - line_segments["endpoints"] = line_segments.geometry.boundary - - agged = line_segments.groupby(level=0).apply(sorted_unary_union) - line_segments = line_segments.drop_duplicates("_line_index") - line_segments.geometry = agged - - # line_segments = sfilter_inverse(line_segments, geoms) - - merged = line_segments.groupby("_line_index", as_index=False)["geometry"].agg( - unary_union - ) - - merged = merged.join(lines.drop(columns="geometry")) - - merged.geometry = line_merge(merged.geometry) - - is_not_merged = merged.geom_type == "MultiLineString" - - merged.loc[is_not_merged, "geometry"] = merged.loc[is_not_merged, "geometry"].apply( - line_merge_by_force - ) - - return merged - def cut_lines(gdf: GeoDataFrame, max_length: int, ignore_index=False) -> GeoDataFrame: """Cuts lines of a GeoDataFrame into pieces of a given length.