Sunday 20 January 2019

python - How to self union a GeoDataFrame?


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



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)


enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...