Skip to content

Commit

Permalink
[processing] keep column names in standard distance matrix (fix #17150)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Oct 24, 2017
1 parent bb21395 commit c64f03d
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions python/plugins/processing/algs/qgis/PointDistance.py
Expand Up @@ -16,9 +16,6 @@
* *
***************************************************************************
"""
from builtins import next
from builtins import str
from builtins import range

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
Expand Down Expand Up @@ -207,14 +204,15 @@ def linearMatrix(self, parameters, context, source, inField, target_source, targ

def regularMatrix(self, parameters, context, source, inField, target_source, targetField,
nPoints, feedback):

index = QgsSpatialIndex(target_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(source.sourceCrs())), feedback)
inIdx = source.fields().lookupField(inField)

distArea = QgsDistanceArea()
distArea.setSourceCrs(source.sourceCrs())
distArea.setEllipsoid(context.project().ellipsoid())

inIdx = source.fields().lookupField(inField)
targetIdx = target_source.fields().lookupField(targetField)

index = QgsSpatialIndex(target_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(source.sourceCrs())), feedback)

first = True
sink = None
dest_id = None
Expand All @@ -225,27 +223,28 @@ def regularMatrix(self, parameters, context, source, inField, target_source, tar
break

inGeom = inFeat.geometry()
inID = str(inFeat.attributes()[inIdx])
featList = index.nearestNeighbor(inGeom.asPoint(), nPoints)
if first:
featList = index.nearestNeighbor(inGeom.asPoint(), nPoints)
first = False
fields = QgsFields()
input_id_field = source.fields()[inIdx]
input_id_field.setName('ID')
fields.append(input_id_field)
for i in range(len(featList)):
fields.append(QgsField('DIST_{0}'.format(i + 1), QVariant.Double))
for f in target_source.getFeatures(QgsFeatureRequest().setFilterFids(featList).setSubsetOfAttributes([targetIdx]).setDestinationCrs(source.sourceCrs())):
fields.append(QgsField(str(f[targetField]), QVariant.Double))

(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, source.wkbType(), source.sourceCrs())

data = [inID]
data = [inFeat[inField]]
for target in target_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setFilterFids(featList).setDestinationCrs(source.sourceCrs())):
if feedback.isCanceled():
break
outGeom = target.geometry()
dist = distArea.measureLine(inGeom.asPoint(),
outGeom.asPoint())
data.append(float(dist))
data.append(dist)

out_feature = QgsFeature()
out_feature.setGeometry(inGeom)
out_feature.setAttributes(data)
Expand Down

0 comments on commit c64f03d

Please sign in to comment.