2014-10-03 10:52:48 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
Gridify.py
|
|
|
|
---------------------
|
|
|
|
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-21 18:24:26 +02:00
|
|
|
from builtins import str
|
2014-10-03 10:52:48 +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$'
|
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
from qgis.core import (QgsFeature,
|
|
|
|
QgsGeometry,
|
2017-06-01 12:18:43 +02:00
|
|
|
QgsPointXY,
|
2017-03-29 10:42:42 +10:00
|
|
|
QgsWkbTypes,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsApplication,
|
|
|
|
QgsProcessingUtils)
|
2017-05-19 11:27:16 +10:00
|
|
|
from processing.algs.qgis 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:52:48 +03:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterTableField
|
|
|
|
from processing.core.outputs import OutputVector
|
|
|
|
|
2017-05-02 14:47:58 +10:00
|
|
|
from processing.tools import dataobjects
|
2014-10-03 10:52:48 +03:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class HubLines(QgisAlgorithm):
|
2014-10-03 10:52:48 +03:00
|
|
|
HUBS = 'HUBS'
|
|
|
|
HUB_FIELD = 'HUB_FIELD'
|
|
|
|
SPOKES = 'SPOKES'
|
|
|
|
SPOKE_FIELD = 'SPOKE_FIELD'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
|
|
|
return QgsApplication.getThemeIcon("/providerQgis.svg")
|
|
|
|
|
|
|
|
def svgIconPath(self):
|
|
|
|
return QgsApplication.iconPath("providerQgis.svg")
|
|
|
|
|
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 'hublines'
|
2017-03-29 12:51:59 +10:00
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Hub lines')
|
2014-10-03 10:52:48 +03:00
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
def defineCharacteristics(self):
|
2014-10-03 10:52:48 +03:00
|
|
|
self.addParameter(ParameterVector(self.HUBS,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Hub layer')))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterTableField(self.HUB_FIELD,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Hub ID field'), self.HUBS))
|
2014-10-03 10:52:48 +03:00
|
|
|
self.addParameter(ParameterVector(self.SPOKES,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Spoke layer')))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterTableField(self.SPOKE_FIELD,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Spoke ID field'), self.SPOKES))
|
2014-10-03 10:52:48 +03:00
|
|
|
|
2016-08-23 19:33:42 +03:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Hub lines'), datatype=[dataobjects.TYPE_VECTOR_LINE]))
|
2014-10-03 10:52:48 +03:00
|
|
|
|
2017-04-25 14:55:38 +10:00
|
|
|
def processAlgorithm(self, context, feedback):
|
2017-05-02 13:40:49 +10:00
|
|
|
layerHub = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.HUBS), context)
|
|
|
|
layerSpoke = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.SPOKES), context)
|
2014-10-03 10:52:48 +03:00
|
|
|
|
|
|
|
fieldHub = self.getParameterValue(self.HUB_FIELD)
|
|
|
|
fieldSpoke = self.getParameterValue(self.SPOKE_FIELD)
|
|
|
|
|
|
|
|
if layerHub.source() == layerSpoke.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:52:48 +03:00
|
|
|
|
2017-04-26 14:33:53 +10:00
|
|
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layerSpoke.fields(), QgsWkbTypes.LineString,
|
|
|
|
layerSpoke.crs(), context)
|
2014-10-03 10:52:48 +03:00
|
|
|
|
2017-04-25 17:59:35 +10:00
|
|
|
spokes = QgsProcessingUtils.getFeatures(layerSpoke, context)
|
|
|
|
hubs = QgsProcessingUtils.getFeatures(layerHub, context)
|
2017-04-25 17:53:32 +10:00
|
|
|
total = 100.0 / QgsProcessingUtils.featureCount(layerSpoke, context)
|
2014-10-03 10:52:48 +03:00
|
|
|
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, spokepoint in enumerate(spokes):
|
2014-10-03 10:52:48 +03:00
|
|
|
p = spokepoint.geometry().boundingBox().center()
|
|
|
|
spokeX = p.x()
|
|
|
|
spokeY = p.y()
|
2016-09-21 18:24:26 +02:00
|
|
|
spokeId = str(spokepoint[fieldSpoke])
|
2014-10-03 10:52:48 +03:00
|
|
|
|
|
|
|
for hubpoint in hubs:
|
2016-09-21 18:24:26 +02:00
|
|
|
hubId = str(hubpoint[fieldHub])
|
2014-10-03 10:52:48 +03:00
|
|
|
if hubId == spokeId:
|
|
|
|
p = hubpoint.geometry().boundingBox().center()
|
|
|
|
hubX = p.x()
|
|
|
|
hubY = p.y()
|
|
|
|
|
|
|
|
f = QgsFeature()
|
|
|
|
f.setAttributes(spokepoint.attributes())
|
|
|
|
f.setGeometry(QgsGeometry.fromPolyline(
|
2017-06-01 12:18:43 +02:00
|
|
|
[QgsPointXY(spokeX, spokeY), QgsPointXY(hubX, hubY)]))
|
2014-10-03 10:52:48 +03:00
|
|
|
writer.addFeature(f)
|
|
|
|
|
|
|
|
break
|
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2014-10-03 10:52:48 +03:00
|
|
|
|
|
|
|
del writer
|