Skip to content
Permalink
Browse files

[processing] keep column names in standard distance matrix (fix #17150)

  • Loading branch information
alexbruy committed Oct 24, 2017
1 parent bb21395 commit c64f03d0a43822c091e3f60bd4b8b3538d000c1e
Showing with 12 additions and 13 deletions.
  1. +12 −13 python/plugins/processing/algs/qgis/PointDistance.py
@@ -16,9 +16,6 @@
* *
***************************************************************************
"""
from builtins import next
from builtins import str
from builtins import range

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
@@ -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
@@ -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)

0 comments on commit c64f03d

Please sign in to comment.
You can’t perform that action at this time.