2014-10-03 10:37:16 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
2016-08-08 15:40:33 +03:00
|
|
|
HubDistancePoints.py
|
2014-10-03 10:37:16 +03:00
|
|
|
---------------------
|
|
|
|
Date : May 2010
|
|
|
|
Copyright : (C) 2010 by Michael Minn
|
|
|
|
Email : pyqgis at michaelminn 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
|
2014-10-03 10:37:16 +03:00
|
|
|
|
|
|
|
__author__ = 'Michael Minn'
|
|
|
|
__date__ = 'May 2010'
|
|
|
|
__copyright__ = '(C) 2010, Michael Minn'
|
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtCore import QVariant
|
2017-03-29 10:42:42 +10:00
|
|
|
from qgis.core import (QgsField,
|
|
|
|
QgsGeometry,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-03-29 10:42:42 +10:00
|
|
|
QgsDistanceArea,
|
|
|
|
QgsFeature,
|
|
|
|
QgsFeatureRequest,
|
|
|
|
QgsWkbTypes,
|
2017-04-18 08:11:51 +10:00
|
|
|
QgsApplication,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsProject,
|
|
|
|
QgsProcessingUtils)
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
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
|
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
2014-10-03 10:37:16 +03:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterTableField
|
|
|
|
from processing.core.parameters import ParameterSelection
|
|
|
|
from processing.core.outputs import OutputVector
|
|
|
|
|
2017-05-02 14:47:58 +10:00
|
|
|
from processing.tools import dataobjects
|
2014-10-03 10:37:16 +03:00
|
|
|
|
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
|
|
|
from math import sqrt
|
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class HubDistancePoints(QgisAlgorithm):
|
2014-10-03 10:37:16 +03:00
|
|
|
POINTS = 'POINTS'
|
|
|
|
HUBS = 'HUBS'
|
|
|
|
FIELD = 'FIELD'
|
|
|
|
UNIT = 'UNIT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
|
|
|
|
UNITS = ['Meters',
|
|
|
|
'Feet',
|
|
|
|
'Miles',
|
|
|
|
'Kilometers',
|
|
|
|
'Layer units'
|
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
|
|
|
]
|
2014-10-03 10:37:16 +03:00
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector analysis tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2015-08-31 16:59:11 +02:00
|
|
|
self.units = [self.tr('Meters'),
|
|
|
|
self.tr('Feet'),
|
|
|
|
self.tr('Miles'),
|
|
|
|
self.tr('Kilometers'),
|
|
|
|
self.tr('Layer units')]
|
|
|
|
|
2014-10-03 10:37:16 +03:00
|
|
|
self.addParameter(ParameterVector(self.POINTS,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Source points layer')))
|
2014-10-03 10:37:16 +03:00
|
|
|
self.addParameter(ParameterVector(self.HUBS,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Destination hubs layer')))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterTableField(self.FIELD,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Hub layer name attribute'), self.HUBS))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterSelection(self.UNIT,
|
2015-08-31 16:59:11 +02:00
|
|
|
self.tr('Measurement unit'), self.units))
|
2014-10-03 10:37:16 +03:00
|
|
|
|
2016-08-23 19:33:42 +03:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Hub distance'), datatype=[dataobjects.TYPE_VECTOR_POINT]))
|
2014-10-03 10:37:16 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'distancetonearesthubpoints'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Distance to nearest hub (points)')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-05-02 13:40:49 +10:00
|
|
|
layerPoints = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.POINTS), context)
|
|
|
|
layerHubs = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.HUBS), context)
|
2014-10-03 10:37:16 +03:00
|
|
|
fieldName = self.getParameterValue(self.FIELD)
|
|
|
|
|
|
|
|
units = self.UNITS[self.getParameterValue(self.UNIT)]
|
|
|
|
|
|
|
|
if layerPoints.source() == layerHubs.source():
|
|
|
|
raise GeoAlgorithmExecutionException(
|
2015-01-15 20:41:15 +02:00
|
|
|
self.tr('Same layer given for both hubs and spokes'))
|
2014-10-03 10:37:16 +03:00
|
|
|
|
2016-08-04 07:37:22 +10:00
|
|
|
fields = layerPoints.fields()
|
2014-10-03 10:37:16 +03:00
|
|
|
fields.append(QgsField('HubName', QVariant.String))
|
|
|
|
fields.append(QgsField('HubDist', QVariant.Double))
|
|
|
|
|
2017-04-26 14:33:53 +10:00
|
|
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, QgsWkbTypes.Point, layerPoints.crs(),
|
|
|
|
context)
|
2014-10-03 10:37:16 +03:00
|
|
|
|
2017-05-02 13:39:36 +10:00
|
|
|
index = QgsProcessingUtils.createSpatialIndex(layerHubs, context)
|
2014-10-03 10:37:16 +03:00
|
|
|
|
|
|
|
distance = QgsDistanceArea()
|
2017-02-23 18:23:52 +01:00
|
|
|
distance.setSourceCrs(layerPoints.crs())
|
2017-04-18 08:11:51 +10:00
|
|
|
distance.setEllipsoid(QgsProject.instance().ellipsoid())
|
2014-10-03 10:37:16 +03:00
|
|
|
|
|
|
|
# Scan source points, find nearest hub, and write to output file
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layerPoints, context)
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / layerPoints.featureCount() if layerPoints.featureCount() else 0
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, f in enumerate(features):
|
2014-10-03 10:37:16 +03:00
|
|
|
src = f.geometry().boundingBox().center()
|
|
|
|
|
2016-06-21 15:59:04 +03:00
|
|
|
neighbors = index.nearestNeighbor(src, 1)
|
2016-10-17 10:30:55 +10:00
|
|
|
ft = next(layerHubs.getFeatures(QgsFeatureRequest().setFilterFid(neighbors[0]).setSubsetOfAttributes([fieldName], layerHubs.fields())))
|
2016-06-21 15:59:04 +03:00
|
|
|
closest = ft.geometry().boundingBox().center()
|
|
|
|
hubDist = distance.measureLine(src, closest)
|
2014-10-03 10:37:16 +03:00
|
|
|
|
|
|
|
attributes = f.attributes()
|
2016-06-21 15:59:04 +03:00
|
|
|
attributes.append(ft[fieldName])
|
2014-10-03 10:37:16 +03:00
|
|
|
if units == 'Feet':
|
|
|
|
attributes.append(hubDist * 3.2808399)
|
|
|
|
elif units == 'Miles':
|
|
|
|
attributes.append(hubDist * 0.000621371192)
|
|
|
|
elif units == 'Kilometers':
|
|
|
|
attributes.append(hubDist / 1000.0)
|
|
|
|
elif units != 'Meters':
|
|
|
|
attributes.append(sqrt(
|
2016-06-21 15:59:04 +03:00
|
|
|
pow(src.x() - closest.x(), 2.0) +
|
|
|
|
pow(src.y() - closest.y(), 2.0)))
|
2014-10-03 10:37:16 +03:00
|
|
|
else:
|
|
|
|
attributes.append(hubDist)
|
|
|
|
|
|
|
|
feat = QgsFeature()
|
|
|
|
feat.setAttributes(attributes)
|
|
|
|
|
2016-08-08 15:40:33 +03:00
|
|
|
feat.setGeometry(QgsGeometry.fromPoint(src))
|
2014-10-03 10:37:16 +03:00
|
|
|
|
2017-06-23 14:34:38 +10:00
|
|
|
writer.addFeature(feat, QgsFeatureSink.FastInsert)
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2014-10-03 10:37:16 +03:00
|
|
|
|
|
|
|
del writer
|