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:
from shapely import ops
streets = streets.reset_index(drop=True)
streets = streets[['geometry', 'costs']]
headers = list(streets.columns)
index = 0
newStreets = gp.GeoDataFrame( columns=['geometry'])
for line in range(len(streets)-1):
print(line, len(streets))
linegeom = streets.at[line, 'geometry']
isNotSplitted = True
for line2 in range(len(streets)):
if line2 == line:
continue
linegeom2 = streets.at[line2, 'geometry']
if linegeom2.crosses(linegeom):
try:
linegeomsplitted = ops.split(linegeom, linegeom2)
except:
continue
isNotSplitted = False
for split in range((len(list(linegeomsplitted.geoms)))):
splittedline = (list(linegeomsplitted.geoms))[split]
for head in headers:
if head == 'geometry':
headValue = splittedline
else:
headValue = streets.at[line, head]
newStreets.at[index, head] = headValue
index += 1
if isNotSplitted:
for head in headers:
headValue = streets.at[line, head]
newStreets.at[index, head] = headValue
index += 1
streets = newStreets
streets = streets.drop_duplicates(subset=['geometry'],
keep='first')
Any suggestions?