Skip to content
Permalink
Browse files
Make QGIS Line Intersections algorithm able to keep all attributes.
  • Loading branch information
gacarrillor authored and nyalldawson committed Nov 24, 2016
1 parent d44bc11 commit 193d400d2957769021a411f4defbcb3c4425b811
Showing with 25 additions and 8 deletions.
  1. +25 −8 python/plugins/processing/algs/qgis/LinesIntersection.py
@@ -30,7 +30,8 @@

from qgis.PyQt.QtGui import QIcon

from qgis.core import Qgis, QgsFeatureRequest, QgsFeature, QgsGeometry, QgsWkbTypes
from qgis.core import (Qgis, QgsFeatureRequest, QgsFeature, QgsGeometry,
QgsWkbTypes, QgsFields)

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
@@ -63,12 +64,12 @@ def defineCharacteristics(self):
self.tr('Intersect layer'), [dataobjects.TYPE_VECTOR_LINE]))
self.addParameter(ParameterTableField(
self.FIELD_A,
self.tr('Input unique ID field'),
self.tr('Input field to keep (leave as [not set] to keep all fields)'),
self.INPUT_A,
optional=True))
self.addParameter(ParameterTableField(
self.FIELD_B,
self.tr('Intersect unique ID field'),
self.tr('Intersect field to keep (leave as [not set] to keep all fields)'),
self.INPUT_B,
optional=True))

@@ -83,10 +84,22 @@ def processAlgorithm(self, progress):
idxA = layerA.fields().lookupField(fieldA)
idxB = layerB.fields().lookupField(fieldB)

fieldList = [layerA.fields()[idxA],
layerB.fields()[idxB]]
if idxA != -1:
fieldListA = QgsFields()
fieldListA.append(layerA.fields()[idxA])
else:
fieldListA = layerA.fields()

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList,
if idxB != -1:
fieldListB = QgsFields()
fieldListB.append(layerB.fields()[idxB])
else:
fieldListB = layerB.fields()

fieldListB = vector.testForUniqueness(fieldListA, fieldListB)
fieldListA.extend(fieldListB)

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldListA,
QgsWkbTypes.Point, layerA.crs())

spatialIndex = vector.spatialindex(layerB)
@@ -115,7 +128,11 @@ def processAlgorithm(self, progress):

points = []
attrsA = inFeatA.attributes()
if idxA != -1:
attrsA = [attrsA[idxA]]
attrsB = inFeatB.attributes()
if idxB != -1:
attrsB = [attrsB[idxB]]

if engine.intersects(tmpGeom.geometry()):
tempGeom = inGeom.intersection(tmpGeom)
@@ -127,8 +144,8 @@ def processAlgorithm(self, progress):

for j in points:
outFeat.setGeometry(tempGeom.fromPoint(j))
outFeat.setAttributes([attrsA[idxA],
attrsB[idxB]])
attrsA.extend(attrsB)
outFeat.setAttributes(attrsA)
writer.addFeature(outFeat)

progress.setPercentage(int(current * total))

0 comments on commit 193d400

Please sign in to comment.