QGIS/python/plugins/processing/algs/qgis/HubDistancePoints.py

140 lines
5.5 KiB
Python
Raw Normal View History

2014-10-03 10:37:16 +03:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
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
from qgis.core import Qgis, QgsField, QgsGeometry, QgsDistanceArea, QgsFeature, QgsFeatureRequest, QgsWkbTypes
2014-10-03 10:37:16 +03:00
from processing.core.GeoAlgorithm import GeoAlgorithm
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
from processing.tools import dataobjects, vector
from math import sqrt
class HubDistancePoints(GeoAlgorithm):
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'
]
2014-10-03 10:37:16 +03:00
def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Distance to nearest hub (points)')
self.group, self.i18n_group = self.trAlgorithm('Vector analysis tools')
2014-10-03 10:37:16 +03: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,
self.tr('Source points layer')))
2014-10-03 10:37:16 +03:00
self.addParameter(ParameterVector(self.HUBS,
self.tr('Destination hubs layer')))
2015-01-15 20:41:15 +02:00
self.addParameter(ParameterTableField(self.FIELD,
self.tr('Hub layer name attribute'), self.HUBS))
2015-01-15 20:41:15 +02:00
self.addParameter(ParameterSelection(self.UNIT,
self.tr('Measurement unit'), self.units))
2014-10-03 10:37:16 +03:00
self.addOutput(OutputVector(self.OUTPUT, self.tr('Hub distance'), datatype=[dataobjects.TYPE_VECTOR_POINT]))
2014-10-03 10:37:16 +03:00
def processAlgorithm(self, progress):
layerPoints = dataobjects.getObjectFromUri(
self.getParameterValue(self.POINTS))
layerHubs = dataobjects.getObjectFromUri(
self.getParameterValue(self.HUBS))
fieldName = self.getParameterValue(self.FIELD)
addLines = self.getParameterValue(self.GEOMETRY)
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
fields = layerPoints.fields()
2014-10-03 10:37:16 +03:00
fields.append(QgsField('HubName', QVariant.String))
fields.append(QgsField('HubDist', QVariant.Double))
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
fields, QgsWkbTypes.Point, layerPoints.crs())
2014-10-03 10:37:16 +03:00
index = vector.spatialindex(layerHubs)
2014-10-03 10:37:16 +03:00
distance = QgsDistanceArea()
distance.setSourceCrs(layerPoints.crs().srsid())
distance.setEllipsoidalMode(True)
# Scan source points, find nearest hub, and write to output file
features = vector.features(layerPoints)
total = 100.0 / len(features)
for current, f in enumerate(features):
2014-10-03 10:37:16 +03:00
src = f.geometry().boundingBox().center()
neighbors = index.nearestNeighbor(src, 1)
ft = next(layerHubs.getFeatures(QgsFeatureRequest().setFilterFid(neighbors[0]).setSubsetOfAttributes([fieldName], layerHubs.fields())))
closest = ft.geometry().boundingBox().center()
hubDist = distance.measureLine(src, closest)
2014-10-03 10:37:16 +03:00
attributes = f.attributes()
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(
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)
feat.setGeometry(QgsGeometry.fromPoint(src))
2014-10-03 10:37:16 +03:00
writer.addFeature(feat)
progress.setPercentage(int(current * total))
2014-10-03 10:37:16 +03:00
del writer