Skip to main content
1 of 3
Leo
  • 972
  • 6
  • 23

Self split geopandas Linestring geodataframe in a fast way without loosing all attributes

I have geodataframe of many LineStrings. The LineStrings intersect but are not split at those intersections. My current solution to achieve this is from here:

network = gp.read_file(filenameNetwork)
newNetwork = gp.GeoDataFrame()
for splittedGeom in network.geometry.unary_union:
    part = gp.GeoDataFrame([[splittedGeom]], columns=['geometry'])
    newNetwork = newNetwork.append(part)

However I loose all columns with this solution. I also tried this, but it takes way too long:

shape = gp.read_file(filename)
headers = list(shape.columns)

finished = False
alreadysplit = 0
while not finished:
    finished = True
    for line in range(alreadysplit, len(shape)):
        linegeom = shape.at[line, 'geometry']
        for splitter in linelist:
            if splitter == linegeom:
                continue
            try:
                linegeomsplitted = ops.split(linegeom, splitter)
            except (TypeError, ValueError):
                continue
            if len(linegeomsplitted) > 1:
                shape.at[line, 'geometry'] = (list(linegeomsplitted.geoms))[0]
                for split in range(1, (len(list(linegeomsplitted.geoms)))):
                    splittedline = (list(linegeomsplitted.geoms))[split]
                    onenewshape = gp.GeoDataFrame([[splittedline, 1]], columns=['geometry', 'split'])
                    for head in headers:
                        if head == 'geometry':
                            continue
                        headValue = shape.at[line, head]
                        onenewshape.at[0, head] = headValue
                    shape = shape.append(onenewshape, sort=False)
                    shape = shape.reset_index(drop=True)
                finished = False
                shape = shape.reset_index(drop=True)
                break
        if not finished:
            alreadysplit = line
            break

any suggestions?

Leo
  • 972
  • 6
  • 23