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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Michael Minn'
|
|
|
|
__date__ = 'May 2010'
|
|
|
|
__copyright__ = '(C) 2010, Michael Minn'
|
|
|
|
|
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,
|
2017-08-03 01:34:28 +10:00
|
|
|
QgsSpatialIndex,
|
2017-03-29 10:42:42 +10:00
|
|
|
QgsWkbTypes,
|
2017-08-03 01:34:28 +10:00
|
|
|
QgsUnitTypes,
|
|
|
|
QgsProcessing,
|
|
|
|
QgsProcessingParameterFeatureSource,
|
|
|
|
QgsProcessingParameterField,
|
|
|
|
QgsProcessingParameterEnum,
|
|
|
|
QgsProcessingParameterFeatureSink,
|
|
|
|
QgsProcessingException)
|
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
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class HubDistancePoints(QgisAlgorithm):
|
2017-08-03 01:34:28 +10:00
|
|
|
INPUT = 'INPUT'
|
2014-10-03 10:37:16 +03:00
|
|
|
HUBS = 'HUBS'
|
|
|
|
FIELD = 'FIELD'
|
|
|
|
UNIT = 'UNIT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
2017-08-03 01:34:28 +10:00
|
|
|
LAYER_UNITS = 'LAYER_UNITS'
|
2014-10-03 10:37:16 +03:00
|
|
|
|
2017-08-03 01:34:28 +10:00
|
|
|
UNITS = [QgsUnitTypes.DistanceMeters,
|
|
|
|
QgsUnitTypes.DistanceFeet,
|
|
|
|
QgsUnitTypes.DistanceMiles,
|
|
|
|
QgsUnitTypes.DistanceKilometers,
|
|
|
|
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):
|
2017-08-22 23:36:42 +10:00
|
|
|
return self.tr('Vector analysis')
|
2017-03-29 12:04:09 +10:00
|
|
|
|
2017-12-14 14:03:56 +02:00
|
|
|
def groupId(self):
|
|
|
|
return 'vectoranalysis'
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-07-10 16:31:14 +10:00
|
|
|
|
|
|
|
def initAlgorithm(self, config=None):
|
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')]
|
|
|
|
|
2017-08-03 01:34:28 +10:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
|
|
|
|
self.tr('Source points layer')))
|
|
|
|
self.addParameter(QgsProcessingParameterFeatureSource(self.HUBS,
|
|
|
|
self.tr('Destination hubs layer')))
|
|
|
|
self.addParameter(QgsProcessingParameterField(self.FIELD,
|
|
|
|
self.tr('Hub layer name attribute'), parentLayerParameterName=self.HUBS))
|
|
|
|
self.addParameter(QgsProcessingParameterEnum(self.UNIT,
|
|
|
|
self.tr('Measurement unit'), self.units))
|
2014-10-03 10:37:16 +03:00
|
|
|
|
2017-08-03 01:34:28 +10:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Hub distance'), QgsProcessing.TypeVectorPoint))
|
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-08-03 01:34:28 +10:00
|
|
|
if parameters[self.INPUT] == parameters[self.HUBS]:
|
|
|
|
raise QgsProcessingException(
|
|
|
|
self.tr('Same layer given for both hubs and spokes'))
|
2014-10-03 10:37:16 +03:00
|
|
|
|
2017-08-03 01:34:28 +10:00
|
|
|
point_source = self.parameterAsSource(parameters, self.INPUT, context)
|
2018-04-27 12:31:56 +10:00
|
|
|
if point_source is None:
|
|
|
|
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
|
|
|
|
|
2017-08-03 01:34:28 +10:00
|
|
|
hub_source = self.parameterAsSource(parameters, self.HUBS, context)
|
2018-04-27 12:31:56 +10:00
|
|
|
if hub_source is None:
|
|
|
|
raise QgsProcessingException(self.invalidSourceError(parameters, self.HUBS))
|
|
|
|
|
2017-08-03 01:34:28 +10:00
|
|
|
fieldName = self.parameterAsString(parameters, self.FIELD, context)
|
2014-10-03 10:37:16 +03:00
|
|
|
|
2017-08-03 01:34:28 +10:00
|
|
|
units = self.UNITS[self.parameterAsEnum(parameters, self.UNIT, context)]
|
2014-10-03 10:37:16 +03:00
|
|
|
|
2017-08-03 01:34:28 +10:00
|
|
|
fields = point_source.fields()
|
2014-10-03 10:37:16 +03:00
|
|
|
fields.append(QgsField('HubName', QVariant.String))
|
|
|
|
fields.append(QgsField('HubDist', QVariant.Double))
|
|
|
|
|
2017-08-03 01:34:28 +10:00
|
|
|
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
|
|
|
fields, QgsWkbTypes.Point, point_source.sourceCrs())
|
2018-04-27 12:52:02 +10:00
|
|
|
if sink is None:
|
|
|
|
raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT))
|
2014-10-03 10:37:16 +03:00
|
|
|
|
2017-12-20 12:32:20 +10:00
|
|
|
index = QgsSpatialIndex(hub_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(point_source.sourceCrs(), context.transformContext())))
|
2014-10-03 10:37:16 +03:00
|
|
|
|
|
|
|
distance = QgsDistanceArea()
|
2017-12-20 15:08:14 +10:00
|
|
|
distance.setSourceCrs(point_source.sourceCrs(), context.transformContext())
|
2017-07-27 10:49:52 +10:00
|
|
|
distance.setEllipsoid(context.project().ellipsoid())
|
2014-10-03 10:37:16 +03:00
|
|
|
|
|
|
|
# Scan source points, find nearest hub, and write to output file
|
2017-08-03 01:34:28 +10:00
|
|
|
features = point_source.getFeatures()
|
|
|
|
total = 100.0 / point_source.featureCount() if point_source.featureCount() else 0
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, f in enumerate(features):
|
2017-08-03 01:34:28 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
|
|
|
|
if not f.hasGeometry():
|
|
|
|
sink.addFeature(f, QgsFeatureSink.FastInsert)
|
|
|
|
continue
|
|
|
|
|
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)
|
2017-12-20 12:32:20 +10:00
|
|
|
ft = next(hub_source.getFeatures(QgsFeatureRequest().setFilterFid(neighbors[0]).setSubsetOfAttributes([fieldName], hub_source.fields()).setDestinationCrs(point_source.sourceCrs(), context.transformContext())))
|
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
|
|
|
|
2017-08-03 01:34:28 +10:00
|
|
|
if units != self.LAYER_UNITS:
|
|
|
|
hub_dist_in_desired_units = distance.convertLengthMeasurement(hubDist, units)
|
|
|
|
else:
|
|
|
|
hub_dist_in_desired_units = hubDist
|
|
|
|
|
2014-10-03 10:37:16 +03:00
|
|
|
attributes = f.attributes()
|
2016-06-21 15:59:04 +03:00
|
|
|
attributes.append(ft[fieldName])
|
2017-08-03 01:34:28 +10:00
|
|
|
attributes.append(hub_dist_in_desired_units)
|
2014-10-03 10:37:16 +03:00
|
|
|
|
|
|
|
feat = QgsFeature()
|
|
|
|
feat.setAttributes(attributes)
|
|
|
|
|
2017-10-30 07:38:33 +01:00
|
|
|
feat.setGeometry(QgsGeometry.fromPointXY(src))
|
2014-10-03 10:37:16 +03:00
|
|
|
|
2017-08-03 01:34:28 +10:00
|
|
|
sink.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
|
|
|
|
2017-08-03 01:34:28 +10:00
|
|
|
return {self.OUTPUT: dest_id}
|