Function to Truncate LineStrings within a Polygon
Function to Truncate LineStrings within a Polygon
By: Jason Kronemeyer
This function takes two GeoDataFrames: one containing LineStrings and the other containing a Polygon. The function will truncate each LineString where it crosses outside the Polygon and remove the part of the LineString that is outside the Polygon.
import geopandas as gpd
from shapely.geometry import LineString, Polygon
from shapely.ops import split
def truncate_lines_within_polygon(lines_gdf, polygon_gdf):
# Ensure the polygon GeoDataFrame contains only one polygon
if len(polygon_gdf) != 1:
raise ValueError("Polygon GeoDataFrame should contain exactly one polygon.")
# Extract the polygon
polygon = polygon_gdf.geometry.iloc[0]
# Function to truncate a single LineString
def truncate_line(line):
intersection = line.intersection(polygon)
split_lines = split(line, intersection)
for segment in split_lines:
if segment.within(polygon):
return segment
return None
# Apply the truncation function to each LineString in the GeoDataFrame
truncated_lines = lines_gdf.geometry.apply(truncate_line)
# Create a new GeoDataFrame with the truncated lines
truncated_gdf = gpd.GeoDataFrame(geometry=truncated_lines)
return truncated_gdf
# Example usage
# Create example GeoDataFrames
lines_gdf = gpd.GeoDataFrame(geometry=[LineString([(0, 0), (2, 2), (4, 4), (6, 6)])])
polygon_gdf = gpd.GeoDataFrame(geometry=[Polygon([(1, 1), (5, 1), (5, 5), (1, 5)])])
# Truncate the lines
truncated_gdf = truncate_lines_within_polygon(lines_gdf, polygon_gdf)
print(truncated_gdf)
This function will: Ensure the polygon GeoDataFrame contains exactly one polygon. Extract the polygon from the GeoDataFrame. Define a helper function to truncate a single LineString. Apply this helper function to each LineString in the lines GeoDataFrame. Return a new GeoDataFrame containing the truncated LineStrings.
Why Truncate?
Truncating a line string involves shortening or cutting down a line string geometry, which is typically used in spatial or GIS (Geographic Information Systems) applications. A line string represents a sequence of connected points, forming a path or shape.
When you truncate a line string, you remove or “trim” parts of it—either from one or both ends—while keeping the remaining portion intact. This process is often used to adjust the length of a line string to fit specific requirements or constraints, such as a desired distance or spatial boundary.
For example, if you have a line string that represents a road or path, truncating it might involve cutting off the first 10 meters or the last 20 meters, depending on the context or desired result.