Skip to content

Commit

Permalink
[processing] speedup Hub distance algorithm (fix #15012)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Jun 21, 2016
1 parent 2d9b2a3 commit e0c9733
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions python/plugins/processing/algs/qgis/HubDistance.py
Expand Up @@ -26,7 +26,7 @@
__revision__ = '$Format:%H$'

from qgis.PyQt.QtCore import QVariant
from qgis.core import QGis, QgsField, QgsGeometry, QgsDistanceArea, QgsFeature
from qgis.core import QGis, QgsField, QgsGeometry, QgsDistanceArea, QgsFeature, QgsFeatureRequest
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
Expand Down Expand Up @@ -106,12 +106,7 @@ def processAlgorithm(self, progress):
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
fields, geomType, layerPoints.crs())

# Create array of hubs in memory
hubs = []
features = vector.features(layerHubs)
for f in features:
hubs.append(Hub(f.geometry().boundingBox().center(),
unicode(f[fieldName])))
index = vector.spatialindex(layerHubs)

distance = QgsDistanceArea()
distance.setSourceCrs(layerPoints.crs().srsid())
Expand All @@ -123,17 +118,13 @@ def processAlgorithm(self, progress):
for current, f in enumerate(features):
src = f.geometry().boundingBox().center()

closest = hubs[0]
hubDist = distance.measureLine(src, closest.point)

for hub in hubs:
dist = distance.measureLine(src, hub.point)
if dist < hubDist:
closest = hub
hubDist = dist
neighbors = index.nearestNeighbor(src, 1)
ft = layerHubs.getFeatures(QgsFeatureRequest().setFilterFid(neighbors[0])).next()
closest = ft.geometry().boundingBox().center()
hubDist = distance.measureLine(src, closest)

attributes = f.attributes()
attributes.append(closest.name)
attributes.append(ft[fieldName])
if units == 'Feet':
attributes.append(hubDist * 3.2808399)
elif units == 'Miles':
Expand All @@ -142,8 +133,8 @@ def processAlgorithm(self, progress):
attributes.append(hubDist / 1000.0)
elif units != 'Meters':
attributes.append(sqrt(
pow(src.x() - closest.point.x(), 2.0) +
pow(src.y() - closest.point.y(), 2.0)))
pow(src.x() - closest.x(), 2.0) +
pow(src.y() - closest.y(), 2.0)))
else:
attributes.append(hubDist)

Expand All @@ -153,16 +144,9 @@ def processAlgorithm(self, progress):
if geomType == QGis.WKBPoint:
feat.setGeometry(QgsGeometry.fromPoint(src))
else:
feat.setGeometry(QgsGeometry.fromPolyline([src, closest.point]))
feat.setGeometry(QgsGeometry.fromPolyline([src, closest]))

writer.addFeature(feat)
progress.setPercentage(int(current * total))

del writer


class Hub:

def __init__(self, point, name):
self.point = point
self.name = name

0 comments on commit e0c9733

Please sign in to comment.