2012-12-24 13:00:31 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
JoinAttributes.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail 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
|
2012-12-24 13:00:31 +01:00
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-12-24 13:00:31 +01:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-12-24 13:00:31 +01:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
from qgis.core import (QgsFeature,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-08-05 05:41:15 +10:00
|
|
|
QgsFeatureRequest,
|
|
|
|
QgsProcessingParameterFeatureSource,
|
|
|
|
QgsProcessingUtils,
|
|
|
|
QgsProcessingParameterField,
|
|
|
|
QgsProcessingParameterFeatureSink)
|
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
|
|
|
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2017-05-02 14:47:58 +10:00
|
|
|
from processing.tools import vector
|
2012-12-24 13:00:31 +01:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class JoinAttributes(QgisAlgorithm):
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2017-08-05 05:41:15 +10:00
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
INPUT = 'INPUT'
|
|
|
|
INPUT_2 = 'INPUT_2'
|
|
|
|
FIELD = 'FIELD'
|
|
|
|
FIELD_2 = 'FIELD_2'
|
2012-12-24 13:00:31 +01:00
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
2017-08-22 23:36:42 +10:00
|
|
|
return self.tr('Vector general')
|
2017-03-29 12:04:09 +10:00
|
|
|
|
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):
|
2017-08-05 05:41:15 +10:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
|
|
|
|
self.tr('Input layer')))
|
|
|
|
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT_2,
|
|
|
|
self.tr('Input layer 2')))
|
|
|
|
self.addParameter(QgsProcessingParameterField(self.FIELD,
|
|
|
|
self.tr('Table field'), parentLayerParameterName=self.INPUT))
|
|
|
|
self.addParameter(QgsProcessingParameterField(self.FIELD_2,
|
|
|
|
self.tr('Table field 2'), parentLayerParameterName=self.INPUT_2))
|
|
|
|
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Joined layer')))
|
2012-12-24 13:00:31 +01:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'joinattributestable'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Join attributes table')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-08-05 05:41:15 +10:00
|
|
|
input = self.parameterAsSource(parameters, self.INPUT, context)
|
|
|
|
input2 = self.parameterAsSource(parameters, self.INPUT_2, context)
|
|
|
|
field = self.parameterAsString(parameters, self.FIELD, context)
|
|
|
|
field2 = self.parameterAsString(parameters, self.FIELD_2, context)
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2017-08-05 05:41:15 +10:00
|
|
|
joinField1Index = input.fields().lookupField(field)
|
|
|
|
joinField2Index = input2.fields().lookupField(field2)
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2017-08-05 05:41:15 +10:00
|
|
|
outFields = vector.combineFields(input.fields(), input2.fields())
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2017-08-05 05:41:15 +10:00
|
|
|
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
|
|
|
outFields, input.wkbType(), input.sourceCrs())
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2017-08-05 05:41:15 +10:00
|
|
|
# Cache attributes of input2
|
2014-07-05 13:37:37 +09:00
|
|
|
cache = {}
|
2017-08-05 05:41:15 +10:00
|
|
|
features = input2.getFeatures(QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry))
|
|
|
|
total = 100.0 / input2.featureCount() if input2.featureCount() else 0
|
2016-04-04 11:45:16 +03:00
|
|
|
for current, feat in enumerate(features):
|
2017-08-05 05:41:15 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
|
2016-02-17 09:36:59 +02:00
|
|
|
attrs = feat.attributes()
|
2016-09-21 18:24:26 +02:00
|
|
|
joinValue2 = str(attrs[joinField2Index])
|
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
|
|
|
if joinValue2 not in cache:
|
2016-02-17 09:36:59 +02:00
|
|
|
cache[joinValue2] = attrs
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2014-07-05 13:37:37 +09:00
|
|
|
|
2012-12-24 13:00:31 +01:00
|
|
|
# Create output vector layer with additional attribute
|
2016-02-17 09:36:59 +02:00
|
|
|
outFeat = QgsFeature()
|
2017-08-05 05:41:15 +10:00
|
|
|
features = input.getFeatures()
|
|
|
|
total = 100.0 / input.featureCount() if input.featureCount() else 0
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, feat in enumerate(features):
|
2017-08-05 05:41:15 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
|
2016-02-17 09:36:59 +02:00
|
|
|
outFeat.setGeometry(feat.geometry())
|
|
|
|
attrs = feat.attributes()
|
2016-09-21 18:24:26 +02:00
|
|
|
joinValue1 = str(attrs[joinField1Index])
|
2014-07-05 13:37:37 +09:00
|
|
|
attrs.extend(cache.get(joinValue1, []))
|
2013-02-07 23:40:43 +01:00
|
|
|
outFeat.setAttributes(attrs)
|
2017-08-05 05:41:15 +10:00
|
|
|
sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2017-08-05 05:41:15 +10:00
|
|
|
|
|
|
|
return {self.OUTPUT: dest_id}
|