Skip to content

Commit

Permalink
Make QGIS Line Intersections algorithm able to keep all attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
gacarrillor authored and nyalldawson committed Nov 24, 2016
1 parent d44bc11 commit 193d400
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions python/plugins/processing/algs/qgis/LinesIntersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down

0 comments on commit 193d400

Please sign in to comment.