2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
PointDistance.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
2012-08-04 19:48:25 +02:00
|
|
|
import math
|
2016-01-25 15:42:11 +02:00
|
|
|
|
|
|
|
from PyQt4.QtGui import QIcon
|
|
|
|
|
2016-03-15 11:54:34 +01:00
|
|
|
from qgis.core import QgsFeatureRequest, QgsDistanceArea
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterNumber
|
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterSelection
|
|
|
|
from processing.core.parameters import ParameterTableField
|
|
|
|
from processing.core.outputs import OutputTable
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.tools import dataobjects, vector
|
2012-08-04 19:48:25 +02:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2013-02-28 21:43:59 +04:00
|
|
|
|
2012-08-04 19:48:25 +02:00
|
|
|
class PointDistance(GeoAlgorithm):
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT_LAYER = 'INPUT_LAYER'
|
|
|
|
INPUT_FIELD = 'INPUT_FIELD'
|
|
|
|
TARGET_LAYER = 'TARGET_LAYER'
|
|
|
|
TARGET_FIELD = 'TARGET_FIELD'
|
|
|
|
MATRIX_TYPE = 'MATRIX_TYPE'
|
|
|
|
NEAREST_POINTS = 'NEAREST_POINTS'
|
|
|
|
DISTANCE_MATRIX = 'DISTANCE_MATRIX'
|
2012-08-04 19:48:25 +02:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
def getIcon(self):
|
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'matrix.png'))
|
|
|
|
|
2012-08-04 19:48:25 +02:00
|
|
|
def defineCharacteristics(self):
|
2015-07-26 03:48:27 +02:00
|
|
|
self.name, self.i18n_name = self.trAlgorithm('Distance matrix')
|
|
|
|
self.group, self.i18n_group = self.trAlgorithm('Vector analysis tools')
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2015-08-31 16:59:11 +02:00
|
|
|
self.mat_types = [self.tr('Linear (N*k x 3) distance matrix'),
|
|
|
|
self.tr('Standard (N x T) distance matrix'),
|
|
|
|
self.tr('Summary distance matrix (mean, std. dev., min, max)')]
|
|
|
|
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT_LAYER,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Input point layer'), [ParameterVector.VECTOR_TYPE_POINT]))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterTableField(self.INPUT_FIELD,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Input unique ID field'), self.INPUT_LAYER,
|
|
|
|
ParameterTableField.DATA_TYPE_ANY))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterVector(self.TARGET_LAYER,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Target point layer'), ParameterVector.VECTOR_TYPE_POINT))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterTableField(self.TARGET_FIELD,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Target unique ID field'), self.TARGET_LAYER,
|
|
|
|
ParameterTableField.DATA_TYPE_ANY))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterSelection(self.MATRIX_TYPE,
|
2015-08-31 16:59:11 +02:00
|
|
|
self.tr('Output matrix type'), self.mat_types, 0))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterNumber(self.NEAREST_POINTS,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Use only the nearest (k) target points'), 0, 9999, 0))
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addOutput(OutputTable(self.DISTANCE_MATRIX, self.tr('Distance matrix')))
|
2012-08-04 19:48:25 +02:00
|
|
|
|
|
|
|
def processAlgorithm(self, progress):
|
2013-10-01 20:52:22 +03:00
|
|
|
inLayer = dataobjects.getObjectFromUri(
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
self.getParameterValue(self.INPUT_LAYER))
|
2012-09-28 12:33:12 +03:00
|
|
|
inField = self.getParameterValue(self.INPUT_FIELD)
|
2013-10-01 20:52:22 +03:00
|
|
|
targetLayer = dataobjects.getObjectFromUri(
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
self.getParameterValue(self.TARGET_LAYER))
|
2012-09-28 12:33:12 +03:00
|
|
|
targetField = self.getParameterValue(self.TARGET_FIELD)
|
|
|
|
matType = self.getParameterValue(self.MATRIX_TYPE)
|
|
|
|
nPoints = self.getParameterValue(self.NEAREST_POINTS)
|
2012-08-04 19:48:25 +02:00
|
|
|
|
2013-09-05 19:23:18 +03:00
|
|
|
outputFile = self.getOutputFromName(self.DISTANCE_MATRIX)
|
2012-08-04 19:48:25 +02:00
|
|
|
|
|
|
|
if nPoints < 1:
|
2013-09-11 19:32:38 +02:00
|
|
|
nPoints = len(vector.features(targetLayer))
|
2012-08-04 19:48:25 +02:00
|
|
|
|
2013-09-05 19:23:18 +03:00
|
|
|
self.writer = outputFile.getTableWriter([])
|
2012-08-04 19:48:25 +02:00
|
|
|
|
|
|
|
if matType == 0:
|
2013-10-01 20:52:22 +03:00
|
|
|
# Linear distance matrix
|
|
|
|
self.linearMatrix(inLayer, inField, targetLayer, targetField,
|
2015-08-22 14:29:41 +02:00
|
|
|
matType, nPoints, progress)
|
2013-10-01 20:52:22 +03:00
|
|
|
elif matType == 1:
|
2015-08-31 16:59:11 +02:00
|
|
|
# Standard distance matrix
|
2013-10-01 20:52:22 +03:00
|
|
|
self.regularMatrix(inLayer, inField, targetLayer, targetField,
|
2015-08-22 14:29:41 +02:00
|
|
|
nPoints, progress)
|
2013-10-01 20:52:22 +03:00
|
|
|
elif matType == 2:
|
|
|
|
# Summary distance matrix
|
|
|
|
self.linearMatrix(inLayer, inField, targetLayer, targetField,
|
2015-08-22 14:29:41 +02:00
|
|
|
matType, nPoints, progress)
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
|
|
def linearMatrix(self, inLayer, inField, targetLayer, targetField,
|
|
|
|
matType, nPoints, progress):
|
|
|
|
if matType == 0:
|
|
|
|
self.writer.addRecord(['InputID', 'TargetID', 'Distance'])
|
2012-08-04 19:48:25 +02:00
|
|
|
else:
|
2013-10-01 20:52:22 +03:00
|
|
|
self.writer.addRecord(['InputID', 'MEAN', 'STDDEV', 'MIN', 'MAX'])
|
2013-02-07 01:09:39 +01:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
index = vector.spatialindex(targetLayer)
|
2012-08-04 19:48:25 +02:00
|
|
|
|
|
|
|
inIdx = inLayer.fieldNameIndex(inField)
|
2014-08-11 11:43:49 +12:00
|
|
|
outIdx = targetLayer.fieldNameIndex(targetField)
|
2012-08-04 19:48:25 +02:00
|
|
|
|
|
|
|
distArea = QgsDistanceArea()
|
2013-02-07 01:09:39 +01:00
|
|
|
|
2013-09-11 19:32:38 +02:00
|
|
|
features = vector.features(inLayer)
|
2016-02-17 09:36:59 +02:00
|
|
|
total = 100.0 / len(features)
|
|
|
|
for current, inFeat in enumerate(features):
|
2012-08-04 19:48:25 +02:00
|
|
|
inGeom = inFeat.geometry()
|
2014-08-11 14:56:38 +12:00
|
|
|
inID = unicode(inFeat.attributes()[inIdx])
|
2012-08-04 19:48:25 +02:00
|
|
|
featList = index.nearestNeighbor(inGeom.asPoint(), nPoints)
|
|
|
|
distList = []
|
|
|
|
vari = 0.0
|
|
|
|
for i in featList:
|
2013-02-28 21:43:59 +04:00
|
|
|
request = QgsFeatureRequest().setFilterFid(i)
|
|
|
|
outFeat = targetLayer.getFeatures(request).next()
|
2013-06-03 21:25:22 +02:00
|
|
|
outID = outFeat.attributes()[outIdx]
|
2012-08-04 19:48:25 +02:00
|
|
|
outGeom = outFeat.geometry()
|
2013-10-01 20:52:22 +03:00
|
|
|
dist = distArea.measureLine(inGeom.asPoint(),
|
2014-03-24 19:03:31 +02:00
|
|
|
outGeom.asPoint())
|
2012-08-04 19:48:25 +02:00
|
|
|
if matType == 0:
|
2015-08-22 14:29:41 +02:00
|
|
|
self.writer.addRecord([inID, unicode(outID), unicode(dist)])
|
2012-08-04 19:48:25 +02:00
|
|
|
else:
|
|
|
|
distList.append(float(dist))
|
|
|
|
|
2014-08-11 14:56:38 +12:00
|
|
|
if matType != 0:
|
2012-08-04 19:48:25 +02:00
|
|
|
mean = sum(distList) / len(distList)
|
|
|
|
for i in distList:
|
|
|
|
vari += (i - mean) * (i - mean)
|
|
|
|
vari = math.sqrt(vari / len(distList))
|
2014-08-11 14:56:38 +12:00
|
|
|
self.writer.addRecord([inID, unicode(mean),
|
2016-01-30 09:33:24 +11:00
|
|
|
unicode(vari), unicode(min(distList)),
|
|
|
|
unicode(max(distList))])
|
2012-08-04 19:48:25 +02:00
|
|
|
|
2012-09-28 12:33:12 +03:00
|
|
|
progress.setPercentage(int(current * total))
|
2012-08-04 19:48:25 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
def regularMatrix(self, inLayer, inField, targetLayer, targetField,
|
|
|
|
nPoints, progress):
|
2013-09-11 19:32:38 +02:00
|
|
|
index = vector.spatialindex(targetLayer)
|
2013-02-07 01:09:39 +01:00
|
|
|
|
|
|
|
inIdx = inLayer.fieldNameIndex(inField)
|
2012-08-04 19:48:25 +02:00
|
|
|
|
|
|
|
distArea = QgsDistanceArea()
|
|
|
|
|
|
|
|
first = True
|
2013-09-11 19:32:38 +02:00
|
|
|
features = vector.features(inLayer)
|
2016-02-17 09:36:59 +02:00
|
|
|
total = 100.0 / len(features)
|
|
|
|
for current, inFeat in enumerate(features):
|
2012-08-04 19:48:25 +02:00
|
|
|
inGeom = inFeat.geometry()
|
2014-08-11 14:56:38 +12:00
|
|
|
inID = unicode(inFeat.attributes()[inIdx])
|
|
|
|
featList = index.nearestNeighbor(inGeom.asPoint(), nPoints)
|
2012-08-04 19:48:25 +02:00
|
|
|
if first:
|
|
|
|
first = False
|
2013-10-01 20:52:22 +03:00
|
|
|
data = ['ID']
|
2014-08-11 14:56:38 +12:00
|
|
|
for i in range(len(featList)):
|
2015-08-22 14:29:41 +02:00
|
|
|
data.append('DIST_{0}'.format(i + 1))
|
2013-09-05 19:23:18 +03:00
|
|
|
self.writer.addRecord(data)
|
2012-08-04 19:48:25 +02:00
|
|
|
|
2014-08-11 14:56:38 +12:00
|
|
|
data = [inID]
|
2012-08-04 19:48:25 +02:00
|
|
|
for i in featList:
|
2013-02-28 21:43:59 +04:00
|
|
|
request = QgsFeatureRequest().setFilterFid(i)
|
|
|
|
outFeat = targetLayer.getFeatures(request).next()
|
2012-08-04 19:48:25 +02:00
|
|
|
outGeom = outFeat.geometry()
|
2013-10-01 20:52:22 +03:00
|
|
|
dist = distArea.measureLine(inGeom.asPoint(),
|
2014-03-24 19:03:31 +02:00
|
|
|
outGeom.asPoint())
|
2012-08-04 19:48:25 +02:00
|
|
|
data.append(unicode(float(dist)))
|
2013-09-05 19:23:18 +03:00
|
|
|
self.writer.addRecord(data)
|
2012-08-04 19:48:25 +02:00
|
|
|
|
2012-09-28 12:33:12 +03:00
|
|
|
progress.setPercentage(int(current * total))
|