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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-09-27 19:51:06 +02:00
|
|
|
from builtins import next
|
2016-09-21 18:24:26 +02:00
|
|
|
from builtins import str
|
|
|
|
from builtins import range
|
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
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-04-25 17:53:32 +10:00
|
|
|
from qgis.core import QgsFeatureRequest, QgsDistanceArea, QgsProcessingUtils
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
from processing.algs.qgis import QgisAlgorithm
|
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
|
2017-05-02 14:47:58 +10:00
|
|
|
from processing.tools import dataobjects
|
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
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class PointDistance(QgisAlgorithm):
|
2012-08-04 19:48:25 +02:00
|
|
|
|
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
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2016-01-25 15:42:11 +02:00
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'matrix.png'))
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector analysis tools')
|
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
def name(self):
|
2017-03-29 17:09:39 +10:00
|
|
|
return 'distancematrix'
|
2017-03-29 12:51:59 +10:00
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Distance matrix')
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
def defineCharacteristics(self):
|
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,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input point layer'), [dataobjects.TYPE_VECTOR_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,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Target point layer'), dataobjects.TYPE_VECTOR_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
|
|
|
|
2017-04-25 14:55:38 +10:00
|
|
|
def processAlgorithm(self, context, feedback):
|
2017-05-02 13:40:49 +10:00
|
|
|
inLayer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)
|
2012-09-28 12:33:12 +03:00
|
|
|
inField = self.getParameterValue(self.INPUT_FIELD)
|
2017-05-02 13:40:49 +10:00
|
|
|
targetLayer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.TARGET_LAYER), context)
|
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:
|
2017-04-25 17:53:32 +10:00
|
|
|
nPoints = QgsProcessingUtils.featureCount(targetLayer, context)
|
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
|
2017-04-25 15:57:24 +10:00
|
|
|
self.linearMatrix(context, inLayer, inField, targetLayer, targetField,
|
2017-01-06 20:04:00 +10:00
|
|
|
matType, nPoints, feedback)
|
2013-10-01 20:52:22 +03:00
|
|
|
elif matType == 1:
|
2015-08-31 16:59:11 +02:00
|
|
|
# Standard distance matrix
|
2017-04-25 15:57:24 +10:00
|
|
|
self.regularMatrix(context, inLayer, inField, targetLayer, targetField,
|
2017-01-06 20:04:00 +10:00
|
|
|
nPoints, feedback)
|
2013-10-01 20:52:22 +03:00
|
|
|
elif matType == 2:
|
|
|
|
# Summary distance matrix
|
2017-04-25 15:57:24 +10:00
|
|
|
self.linearMatrix(context, inLayer, inField, targetLayer, targetField,
|
2017-01-06 20:04:00 +10:00
|
|
|
matType, nPoints, feedback)
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2017-04-25 15:57:24 +10:00
|
|
|
def linearMatrix(self, context, inLayer, inField, targetLayer, targetField,
|
2017-01-06 20:04:00 +10:00
|
|
|
matType, nPoints, feedback):
|
2013-10-01 20:52:22 +03:00
|
|
|
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
|
|
|
|
2017-05-02 13:39:36 +10:00
|
|
|
index = QgsProcessingUtils.createSpatialIndex(targetLayer, context)
|
2012-08-04 19:48:25 +02:00
|
|
|
|
2016-09-22 12:09:13 +02:00
|
|
|
inIdx = inLayer.fields().lookupField(inField)
|
|
|
|
outIdx = targetLayer.fields().lookupField(targetField)
|
2012-08-04 19:48:25 +02:00
|
|
|
|
|
|
|
distArea = QgsDistanceArea()
|
2013-02-07 01:09:39 +01:00
|
|
|
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(inLayer, context)
|
2017-04-26 07:41:42 +10:00
|
|
|
total = 100.0 / QgsProcessingUtils.featureCount(inLayer, context)
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, inFeat in enumerate(features):
|
2012-08-04 19:48:25 +02:00
|
|
|
inGeom = inFeat.geometry()
|
2016-09-21 18:24:26 +02:00
|
|
|
inID = str(inFeat.attributes()[inIdx])
|
2012-08-04 19:48:25 +02:00
|
|
|
featList = index.nearestNeighbor(inGeom.asPoint(), nPoints)
|
|
|
|
distList = []
|
|
|
|
vari = 0.0
|
2016-10-17 10:30:55 +10:00
|
|
|
request = QgsFeatureRequest().setFilterFids(featList).setSubsetOfAttributes([outIdx])
|
|
|
|
for outFeat in targetLayer.getFeatures(request):
|
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:
|
2016-09-21 18:24:26 +02:00
|
|
|
self.writer.addRecord([inID, str(outID), str(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))
|
2016-09-21 18:24:26 +02:00
|
|
|
self.writer.addRecord([inID, str(mean),
|
|
|
|
str(vari), str(min(distList)),
|
|
|
|
str(max(distList))])
|
2012-08-04 19:48:25 +02:00
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2012-08-04 19:48:25 +02:00
|
|
|
|
2017-04-25 15:57:24 +10:00
|
|
|
def regularMatrix(self, context, inLayer, inField, targetLayer, targetField,
|
2017-01-06 20:04:00 +10:00
|
|
|
nPoints, feedback):
|
2017-05-02 13:39:36 +10:00
|
|
|
index = QgsProcessingUtils.createSpatialIndex(targetLayer, context)
|
2013-02-07 01:09:39 +01:00
|
|
|
|
2016-09-22 12:09:13 +02:00
|
|
|
inIdx = inLayer.fields().lookupField(inField)
|
2012-08-04 19:48:25 +02:00
|
|
|
|
|
|
|
distArea = QgsDistanceArea()
|
|
|
|
|
|
|
|
first = True
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(inLayer, context)
|
2017-04-25 17:53:32 +10:00
|
|
|
total = 100.0 / QgsProcessingUtils.featureCount(inLayer, context)
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, inFeat in enumerate(features):
|
2012-08-04 19:48:25 +02:00
|
|
|
inGeom = inFeat.geometry()
|
2016-09-21 18:24:26 +02:00
|
|
|
inID = str(inFeat.attributes()[inIdx])
|
2014-08-11 14:56:38 +12:00
|
|
|
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)
|
2016-09-21 18:24:26 +02:00
|
|
|
outFeat = next(targetLayer.getFeatures(request))
|
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())
|
2016-09-21 18:24:26 +02:00
|
|
|
data.append(str(float(dist)))
|
2013-09-05 19:23:18 +03:00
|
|
|
self.writer.addRecord(data)
|
2012-08-04 19:48:25 +02:00
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|