3

I have a layer containing line segments which are connected by their endpoints (e.g. as within a street network). I'd like to merge (union) polylines within this layer into one feature until following restrictions are met:

  • total length of the merged feature is greater than a minimal length (e.g. 100m)
  • only line segments can be merged which are neighbors

The length of the lines are defined within attributes. It shall be possible to remember which lines are merged (mapping to the original feature ids). Merge Segments

The idea is to iterate through all features with length < minlength and intersect with other features to find the neigbours. Then the neighbour with minimal length will be merged (union) with this feature. The importat part would be not to iterate through features already merged (maybe by recursion).

Pseudo Code:

Program Mergelength
define minLength
define mergedFeatureList = empty
iterate through all features of featureClass where length < minLength
   if feature not in mergedFeatureList
      select features from featureclass which intersect (touch) feature
      find feature with minimum length
      union feature geometries
      update feature length
      insert feature into mergedFeatureList
      do (repeat) until feature length > minLength

Incomplete code (qgis python):

from PyQt4.QtCore import *
import math
##################################################################################################
minLength = 50

inlayer = qgis.utils.iface.activeLayer()
lenidx = inlayer.fieldNameIndex('REALLENGTH')
lididx = inlayer.fieldNameIndex('RSN')

# Function definition is here
def intersection(feature, layer, idIdx, lenIdx, alreadyMergedSegements):
    intersections = {}
    for ifeature in layer.getFeatures():
        if feature.geometry().intersects(ifeature.geometry()) and ifeature.attributes()[idIdx] not in alreadyMergedSegements:
            intersections[ifeature.attributes()[idIdx]] = ifeature.attributes()[lenIdx]
    return intersections

# Function definition is here
def mergeSegements(lineFeature, lenidx, lididx, minLength, inLayer):
    alreadyMergedSegements = []
    myIntersections = intersection(lineFeature, inLayer, lididx, lenidx, alreadyMergedSegements)
    minRsn = min(myIntersections, key=lambda k: myIntersections[k]) 
    request = QgsFeatureRequest().setFilterExpression( u'"RSN" = ' + str(minRsn))
    for feature in inLayer.getFeatures(request):
        fet = QgsFeature()
        unionGeom = lineFeature.geometry().combine(feature.geometry())
        mergedLength = float(feature.attributes()[lenidx]) + float(lineFeature.attributes()[lenidx])
        alreadyMergedSegements.append(minRsn)

    return -1



print lenidx
iter = inlayer.getFeatures()
for feature in iter:
    length =  feature.attributes()[lenidx]
    if length < minLength:
        feat = mergeSegements(feature, lenidx, lididx, minLength, inlayer)
    else:
        print "no merge needed."
  • Welcome to gis.stackexchange! Please note that a good question on this site is expected to show some degree of research on your part, i.e. what you have tried and - if applicable - code so far. For more info, you can check our faq. – underdark Aug 31 '15 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.