Skip to content

Commit

Permalink
Merge pull request #1531 from ccrook/DistanceMatrixFix
Browse files Browse the repository at this point in the history
[processing] Distance matrix fixes
  • Loading branch information
alexbruy committed Sep 19, 2014
2 parents 9ae648b + c945eec commit 18c84af
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions python/plugins/processing/algs/qgis/ftools/PointDistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def linearMatrix(self, inLayer, inField, targetLayer, targetField,
index = vector.spatialindex(targetLayer)

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

outFeat = QgsFeature()
inGeom = QgsGeometry()
Expand All @@ -125,7 +125,7 @@ def linearMatrix(self, inLayer, inField, targetLayer, targetField,
total = 100.0 / float(len(features))
for inFeat in features:
inGeom = inFeat.geometry()
inID = inFeat.attributes()[inIdx]
inID = unicode(inFeat.attributes()[inIdx])
featList = index.nearestNeighbor(inGeom.asPoint(), nPoints)
distList = []
vari = 0.0
Expand All @@ -137,17 +137,16 @@ def linearMatrix(self, inLayer, inField, targetLayer, targetField,
dist = distArea.measureLine(inGeom.asPoint(),
outGeom.asPoint())
if matType == 0:
self.writer.addRecord([unicode(inID), unicode(outID),
unicode(dist)])
self.writer.addRecord([inID,unicode(outID),unicode(dist)])
else:
distList.append(float(dist))

if matType == 2:
if matType != 0:
mean = sum(distList) / len(distList)
for i in distList:
vari += (i - mean) * (i - mean)
vari = math.sqrt(vari / len(distList))
self.writer.addRecord([unicode(inID), unicode(mean),
self.writer.addRecord([inID, unicode(mean),
unicode(vari), unicode(min(distList)),
unicode(max(distList))])

Expand All @@ -173,18 +172,16 @@ def regularMatrix(self, inLayer, inField, targetLayer, targetField,

for inFeat in features:
inGeom = inFeat.geometry()
inID = inFeat.attributes()[inIdx]
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 featList:
request = QgsFeatureRequest().setFilterFid(i)
outFeat = targetLayer.getFeatures(request).next()
data.append(unicode(outFeat.attributes[outIdx]))
for i in range(len(featList)):
data.append('DIST_{0}'.format(i+1))
self.writer.addRecord(data)

data = [unicode(inID)]
data = [inID]
for i in featList:
request = QgsFeatureRequest().setFilterFid(i)
outFeat = targetLayer.getFeatures(request).next()
Expand Down

0 comments on commit 18c84af

Please sign in to comment.