Skip to main content
Notice removed Draw attention by CommunityBot
Bounty Ended with gene's answer chosen by CommunityBot
edited body
Source Link
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404

anyAny suggestions?

any suggestions?

Any suggestions?

Tweeted twitter.com/StackGIS/status/1332429004532539403
added 34 characters in body
Source Link
Leo
  • 972
  • 6
  • 23
shapefrom shapely import ops

streets = gpstreets.read_filereset_index(filenamedrop=True)
streets = streets[['geometry', 'costs']]
headers = list(shapestreets.columns)
 
finished = False
alreadysplitindex = 0
while not finished:
    finishednewStreets = True
   gp.GeoDataFrame( columns=['geometry'])
for line in range(alreadysplit, len(shapestreets)-1):
    print(line, len(streets))
    linegeom = shapestreets.at[line, 'geometry']
    isNotSplitted = True
    for splitterline2 in linelistrange(len(streets)):
        if line2 == line:
 if splitter == linegeom:        continue
        linegeom2 = streets.at[line2, 'geometry']
      continue  if linegeom2.crosses(linegeom):
            try:
                linegeomsplitted = ops.split(linegeom, splitterlinegeom2)
            except (TypeError, ValueError):
                continue
            if len(linegeomsplitted) > 1:
                shape.at[line, 'geometry']isNotSplitted = (list(linegeomsplitted.geoms))[0]False
                for split in range(1, (len(list(linegeomsplitted.geoms)))):
                    splittedline = (list(linegeomsplitted.geoms))[split]
                    onenewshape = gp.GeoDataFrame([[splittedline,for 1]],head columns=['geometry',in 'split'])headers:
                    forif head in== headers'geometry':
                        if headheadValue === 'geometry':splittedline
                            continueelse:
                        headValue = shapestreets.at[line, head]
                        onenewshapenewStreets.at[0at[index, head] = headValue
                    shape =index shape.append(onenewshape,+= sort=False)1
          if isNotSplitted:
        for shapehead =in shape.reset_index(drop=True)headers:
                finishedheadValue = False
  streets.at[line, head]
            newStreets.at[index, shapehead] = shape.reset_index(drop=True)headValue
            index += 1
streets = breaknewStreets
streets = streets.drop_duplicates(subset=['geometry'],
      if not finished:
            alreadysplit = line
            breakkeep='first')
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
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')
Notice added Draw attention by Leo
Bounty Started worth 50 reputation by Leo
Source Link
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?