Cutting lines¶
Cutting and splitting line geometries.
- cut_lines(gdf, max_length, ignore_index=False)[source]
Cuts lines of a GeoDataFrame into pieces of a given length.
- Parameters:
gdf (
GeoDataFrame
) – GeoDataFrame.max_length (
int
) – The maximum length of the lines in the output GeoDataFrame.ignore_index (
bool
) – If True, the resulting axis will be labeled 0, 1, …, n - 1. Defaults to False.
- Return type:
GeoDataFrame
- Returns:
A GeoDataFrame with lines cut to the maximum distance.
Note
This method is time consuming for large networks and low ‘max_length’.
Examples:¶
>>> from sgis import read_parquet_url, cut_lines >>> roads = read_parquet_url("https://media.githubusercontent.com/media/statisticsnorway/ssb-sgis/main/tests/testdata/roads_oslo_2022.parquet") >>> roads.length.describe().round(1) count 93395.0 mean 41.2 std 78.5 min 0.2 25% 14.0 50% 27.7 75% 47.5 max 5213.7 dtype: float64 >>> roads = cut_lines(roads, max_length=100) >>> roads.length.describe().round(1) count 126304.0 mean 30.5 std 30.1 min 0.0 25% 5.7 50% 22.5 75% 44.7 max 100.0 dtype: float64
- cut_lines_once(gdf, distances, ignore_index=False)[source]
Cuts lines of a GeoDataFrame in two at the given distance or distances.
Takes a GeoDataFrame of lines and cuts each line in two. If distances is a number, all lines will be cut at the same length.
- Parameters:
gdf (
GeoDataFrame
) – GeoDataFrame.distances (
int
|float
|str
|Series
) – The distance from the start of the lines to cut at. Either a number, the name of a column or array-like of same length as the line GeoDataFrame.ignore_index (
bool
) – If True, the resulting axis will be labeled 0, 1, …, n - 1. Defaults to False.
- Return type:
GeoDataFrame
Examples:¶
>>> from sgis import cut_lines_once, to_gdf >>> import pandas as pd >>> from shapely.geometry import LineString >>> gdf = to_gdf(LineString([(0, 0), (1, 1), (2, 2)])) >>> gdf = pd.concat([gdf, gdf, gdf]) >>> gdf geometry 0 LINESTRING (0.00000 0.00000, 1.00000 1.00000, ... 0 LINESTRING (0.00000 0.00000, 1.00000 1.00000, ... 0 LINESTRING (0.00000 0.00000, 1.00000 1.00000, ...
Cut all lines at 1 unit from the start of the lines.
>>> cut_lines_once(gdf, 1) geometry 0 LINESTRING (0.00000 0.00000, 0.70711 0.70711) 1 LINESTRING (0.70711 0.70711, 1.00000 1.00000, ... 2 LINESTRING (0.00000 0.00000, 0.70711 0.70711) 3 LINESTRING (0.70711 0.70711, 1.00000 1.00000, ... 4 LINESTRING (0.00000 0.00000, 0.70711 0.70711) 5 LINESTRING (0.70711 0.70711, 1.00000 1.00000, ...
Cut distance as column.
>>> gdf["dist"] = [1, 2, 3] >>> cut_lines_once(gdf, "dist") geometry dist 0 LINESTRING (0.00000 0.00000, 0.70711 0.70711) 1 0 LINESTRING (0.70711 0.70711, 1.00000 1.00000, ... 1 0 LINESTRING (0.00000 0.00000, 1.00000 1.00000, ... 2 0 LINESTRING (1.41421 1.41421, 2.00000 2.00000) 2 0 LINESTRING (0.00000 0.00000, 1.00000 1.00000, ... 3
Cut distance as list (same result as above).
>>> cut_lines_once(gdf, [1, 2, 3]) geometry dist 0 LINESTRING (0.00000 0.00000, 0.70711 0.70711) 1 0 LINESTRING (0.70711 0.70711, 1.00000 1.00000, ... 1 0 LINESTRING (0.00000 0.00000, 1.00000 1.00000, ... 2 0 LINESTRING (1.41421 1.41421, 2.00000 2.00000) 2 0 LINESTRING (0.00000 0.00000, 1.00000 1.00000, ... 3
- split_lines_by_nearest_point(gdf, points, max_distance=None, splitted_col=None)[source]
Split lines that are closest to s point.
Snaps points to nearest lines and splits the lines in two at the snap point. The splitting is done pointwise, meaning each point splits one line in two. The line will not be split if the point is closest to the endpoint of the line.
This function is used in NetworkAnalysis if split_lines is set to True.
- Parameters:
gdf (
GeoDataFrame
) – GeoDataFrame of lines that will be split.points (
GeoDataFrame
) – GeoDataFrame of points to split the lines with.max_distance (
int
|float
|None
) – the maximum distance between the point and the line. Points further away than max_distance will not split any lines. Defaults to None.splitted_col (
str
|None
) – Optionally add a column
- Return type:
DataFrame
- Returns:
A GeoDataFrame with the same columns as the input lines, but with the lines split at the closest point to the points. If no lines are within ‘max_distance’ from any points, ‘gdf’ is returned as it came.
- Raises:
ValueError – If the crs of the input data differs.
Examples:¶
>>> from sgis import read_parquet_url, split_lines_by_nearest_point >>> roads = read_parquet_url("https://media.githubusercontent.com/media/statisticsnorway/ssb-sgis/main/tests/testdata/roads_oslo_2022.parquet") >>> points = read_parquet_url("https://media.githubusercontent.com/media/statisticsnorway/ssb-sgis/main/tests/testdata/points_oslo.parquet") >>> rows = len(roads) >>> rows 93395
Splitting lines for points closer than 10 meters from the lines.
>>> roads = split_lines_by_nearest_point(roads, points, max_distance=10) >>> print("number of lines that were split:", len(roads) - rows) number of lines that were split: 380
Splitting lines by all points.
>>> roads = split_lines_by_nearest_point(roads, points) >>> print("number of lines that were split:", len(roads) - rows) number of lines that were split: 848
Not all lines were split. That is because some points were closest to an endpoint of a line.