1

I have a GeoDataFrame with data from Open Street Map. I want to split all intersecting streets but keep the columns from the original splitted way.

Example
I want to split the way with the id 4294200 into 2 parts.

original_id         highway         lanes   geometry
4294200             motorway_link   2       LINESTRING (6.01706 49.51282, 6.01664 49.51293...
628218213           motorway_link   2       LINESTRING (6.01446 49.51440, 6.01435 49.51451...

enter image description here

Expected result

original_id         highway         lanes   geometry
4294200             motorway_link   2       LINESTRING (6.01706 49.51282, 6.01664 49.51293...
4294200             motorway_link   2       LINESTRING (6.01706 49.51251, 6.01652 49.51279...
628218213           motorway_link   2       LINESTRING (6.01446 49.51440, 6.01435 49.51451...

enter image description here

Implementation

import geopandas as gpd
data = gpd.gpd.read_file('luxembourg.geojson')

# split ways -> working
unary = data.geometry.unary_union
geometries = [i for i in unary]
ids = [j for j in range(len(geometries))]
unary = gpd.GeoDataFrame({"id": ids, "geometry": geometries}, crs="EPSG:4326")

# join splitted ways with columns from original data frame (e.g. highway, lanes etc.) -> not working
joined = gpd.sjoin(unary, data, how="inner", op='within')

# this only contains 1 splitted way instead of 2
way_4294200=joined[joined['original_id'] == 4294200] 

Problem
The data frame way_4294200 only contains one splitted way (green), the red one was not mapped correct.

original_id         highway         lanes   geometry
4294200             motorway_link   2       LINESTRING (6.01701 49.51221, 6.01636...) 

enter image description here

What is the problem with the sjoin? Or is there a better way to split the geometries without losing the attributes?

1 Answer 1

2

Having gone through your code it appears it is splitting the GeoDataFrame into individual geometries, but then you are only joining the first split geometry with the data GeoDataFrame. Use a for loop to iterate over each split geometry and join it with the GeoDataFrame.

import geopandas as gpd
data = gpd.gpd.read_file('luxembourg.geojson')

# split ways
unary = data.geometry.unary_union
geometries = [i for i in unary]
ids = [j for j in range(len(geometries))]
unary = gpd.GeoDataFrame({"id": ids, "geometry": geometries}, crs="EPSG:4326")
results = [] #empty list to store the results
for i in range(len(unary)):
    joined = gpd.sjoin(unary.loc[[i]], data, how="inner", op='within')
    results.append(joined)
joined = pd.concat(results)
way_4294200 = joined[joined['original_id'] == 4294200]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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