0

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.

enter image description here

import geopandas as gpd

lines_gdf = gpd.read_file('input.geojson')
lines_union_gdf = gpd.overlay(lines_gdf, lines_gdf, how='union')

enter image description here

Example input file: input.geojson

Expected output file: output.geojson

  • Can your provide a small reproducible example (eg dataframe with 3 LineStrings) and the desired result for that? – joris Mar 14 '18 at 14:48
  • I added some sample data and sample code. I hope this makes it more clear. – ustroetz Mar 14 '18 at 16:43
  • In case someone is looking for the solution to the problem with PostGIS: SELECT (ST_Dump(ST_Node(ST_Collect(geom)))).geom as geom FROM table; – ustroetz Mar 24 '18 at 16:06
3

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
<shapely.geometry.multilinestring.MultiLineString object at 0x10cc20e10>
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)

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.