I have a GeoPandas dataframe with lines all originating in the same location. I want to partition the lines into segments and merge them to get a dataframe containing all the unique segments.
I tried overlay with union, which seems to solve my described problem. However, this only supports polygons. I am looking for a solution lines.
import geopandas as gpd
lines_gdf = gpd.read_file('input.geojson')
lines_union_gdf = gpd.overlay(lines_gdf, lines_gdf, how='union')
Example input file: input.geojson
Expected output file: output.geojson
Answer
For the geometry you can use the unary_union (Shapely unary_union) predicate. The method will split all self-intersection geometries (Planar Graph)
# unary union of all the geometries of the GeoDataFrame
lines_gdf.geometry.unary_union
lines_gdf.geometry.unary_union.wkt
'MULTILINESTRING ((13.39825630187988 52.50709252547882, 13.40160369873047 52.50617828126911), (13.40160369873047 52.50617828126911, 13.40518712997437 52.50513340745842), (13.40518712997437 52.50513340745842, 13.40799808502197 52.50440198101236), (13.40518712997437 52.50513340745842, 13.40608835220337 52.50564278654393), (13.40160369873047 52.50617828126911, 13.40445756912231 52.50790226875552))'
for geom in lines_gdf.geometry.unary_union:
print(geom)
LINESTRING (13.39825630187988 52.50709252547882, 13.40160369873047 52.50617828126911)
LINESTRING (13.40160369873047 52.50617828126911, 13.40518712997437 52.50513340745842)
LINESTRING (13.40518712997437 52.50513340745842, 13.40799808502197 52.50440198101236)
LINESTRING (13.40518712997437 52.50513340745842, 13.40608835220337 52.50564278654393)
LINESTRING (13.40160369873047 52.50617828126911, 13.40445756912231 52.50790226875552)
No comments:
Post a Comment