I'm using Python 2.7.5 in a GIS environment (namely: QGIS 2.8.1, or PyQGIS).sys.version_info(major=2, minor=7, micro=5, releaselevel='final', serial=0)The aim is to identify spatially identical Polylines (i.e. overlapping) and sum a value from a selected field. For other polylines with no matches, the code just replicates those with their original field values.path='D:/Documents/qgis/lines_same/Lines.shp'  # Enter your SHP file pathVal='Value'                      # Enter the field name you would like to summarizefrom PyQt4.QtCore import *layer = QgsVectorLayer(path,'Lines', 'ogr')tmp = QgsVectorLayer(LineString, tmp_output, memory)dp = tmp.dataProvider()tmp.startEditing()dp.addAttributes ( [ QgsField(ID, QVariant.Int),        QgsField(IDsub, QVariant.Int),        QgsField(ValueSum, QVariant.Int) ] )iter = layer.getFeatures()for feature in iter:    CreateVerify = 0    geom = feature.geometry()    geom = geom.asPolyline()    ID = feature.id()    Value = feature[Val]    iterSub = layer.getFeatures()    for featureSub in iterSub:        geomSub = featureSub.geometry()        geomSub = geomSub.asPolyline()        IDsub = featureSub.id()        ValueSub = featureSub[Val]        if (geom==geomSub and ID!=IDsub):            CheckExist = tmp.getFeatures()            Exist = 0            for exs in CheckExist:                if (exs.geometry().asPolyline() == geom):                    Exist = Exist + 1            if (CreateVerify == 0 and Exist == 0):                CreateVerify = 1                New = QgsFeature()                New.setGeometry (QgsGeometry.fromPolyline(geom))                New.setAttributes([ID, IDsub, Value + ValueSub])                dp.addFeatures([ New ])                tmp.commitChanges()            elif (Exist > 0):                CreateVerify = 1                                if (CreateVerify == 0):        New = QgsFeature()        New.setGeometry (QgsGeometry.fromPolyline(geom))        New.setAttributes([ID,-99, Value])        dp.addFeatures([ New ])        tmp.commitChanges()    QgsMapLayerRegistry.instance().addMapLayer(tmp) I used many for loops to iterate through features and it seems too brute to me. mainly those two nested for loops:for featureSub in iterSub: # to compare geometries and search for matchesandfor exs in CheckExist: # to prevent duplicates from being created, if there is a matchI would greatly appreciate any help to make it more efficient.
Identifying Polylines and operating on field values
python;python 2.7;geospatial