2
\$\begingroup\$

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 path
Val='Value'                      # Enter the field name you would like to summarize
from 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 matches

and

for exs in CheckExist: # to prevent duplicates from being created, if there is a match

I would greatly appreciate any help to make it more efficient.

\$\endgroup\$
  • \$\begingroup\$ layer = QgsVectorLayer(path,'Lines', 'ogr') throws: NameError: name 'QgsVectorLayer' is not defined. Is it possible there's another import required? \$\endgroup\$ – Mast Jul 17 '16 at 19:50
  • \$\begingroup\$ After fixing my earlier comment with from qgis.core import *, it breaks again: AttributeError: 'NoneType' object has no attribute 'addAttributes'. I suspect your .shp file looks different than mine. I know this is an old question, but do you have the original inputfile around or something similar enough to work? \$\endgroup\$ – Mast Jul 17 '16 at 20:19

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.