Skip to content

Commit

Permalink
[processing] fix Standard (N x T) distance matrix (ref #17150)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Oct 6, 2017
1 parent 9a8c07e commit 54031ea
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions python/plugins/processing/algs/qgis/PointDistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,15 @@ def processAlgorithm(self, progress):
nPoints = self.getParameterValue(self.NEAREST_POINTS)

outputFile = self.getOutputFromName(self.DISTANCE_MATRIX)
self.writer = outputFile.getTableWriter([])

if inLayer.id() == targetLayer.id():
if nPoints > 0:
nPoints += 1

if nPoints < 1:
nPoints = len(vector.features(targetLayer))

self.writer = outputFile.getTableWriter([])

if matType == 0:
# Linear distance matrix
self.linearMatrix(inLayer, inField, targetLayer, targetField,
Expand Down Expand Up @@ -158,33 +161,30 @@ def linearMatrix(self, inLayer, inField, targetLayer, targetField,

def regularMatrix(self, inLayer, inField, targetLayer, targetField,
nPoints, progress):
index = vector.spatialindex(targetLayer)
features = vector.features(inLayer)
total = 100.0 / len(features) if len(features) > 0 else 1

inIdx = inLayer.fieldNameIndex(inField)
targetIdx = targetLayer.fieldNameIndex(targetField)

first = True
distArea = QgsDistanceArea()
index = vector.spatialindex(targetLayer)

first = True
features = vector.features(inLayer)
total = 100.0 / len(features) if len(features) > 0 else 1
for current, inFeat in enumerate(features):
inGeom = inFeat.geometry()
inID = unicode(inFeat.attributes()[inIdx])
featList = index.nearestNeighbor(inGeom.asPoint(), nPoints)
if first:
featList = index.nearestNeighbor(inGeom.asPoint(), nPoints)
first = False
data = ['ID']
for i in range(len(featList)):
data.append('DIST_{0}'.format(i + 1))
request = QgsFeatureRequest().setFilterFids(featList).setSubsetOfAttributes([targetIdx])
for f in targetLayer.getFeatures(request):
data.append(unicode(f[targetField]))
self.writer.addRecord(data)

data = [inID]
for i in featList:
request = QgsFeatureRequest().setFilterFid(i)
outFeat = targetLayer.getFeatures(request).next()
outGeom = outFeat.geometry()
dist = distArea.measureLine(inGeom.asPoint(),
outGeom.asPoint())
data = [unicode(inFeat[inField])]
for f in targetLayer.getFeatures(request):
outGeom = f.geometry()
dist = distArea.measureLine(inGeom.asPoint(), outGeom.asPoint())
data.append(unicode(float(dist)))
self.writer.addRecord(data)

Expand Down

0 comments on commit 54031ea

Please sign in to comment.