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-04-25 17:53:32 +10:00
|
|
|
QgsApplication,
|
|
|
|
QgsProcessingUtils)
|
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
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterTable
|
|
|
|
from processing.core.parameters import ParameterTableField
|
|
|
|
from processing.core.outputs import OutputVector
|
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
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
OUTPUT_LAYER = 'OUTPUT_LAYER'
|
|
|
|
INPUT_LAYER = 'INPUT_LAYER'
|
|
|
|
INPUT_LAYER_2 = 'INPUT_LAYER_2'
|
|
|
|
TABLE_FIELD = 'TABLE_FIELD'
|
|
|
|
TABLE_FIELD_2 = 'TABLE_FIELD_2'
|
2012-12-24 13:00:31 +01:00
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector general tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT_LAYER,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input layer')))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterTable(self.INPUT_LAYER_2,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Input layer 2'), False))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterTableField(self.TABLE_FIELD,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Table field'), self.INPUT_LAYER))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterTableField(self.TABLE_FIELD_2,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Table field 2'), self.INPUT_LAYER_2))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT_LAYER,
|
2015-08-22 14:29:41 +02:00
|
|
|
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):
|
2012-12-24 13:00:31 +01:00
|
|
|
input = self.getParameterValue(self.INPUT_LAYER)
|
|
|
|
input2 = self.getParameterValue(self.INPUT_LAYER_2)
|
|
|
|
output = self.getOutputFromName(self.OUTPUT_LAYER)
|
|
|
|
field = self.getParameterValue(self.TABLE_FIELD)
|
2013-01-12 23:36:00 +01:00
|
|
|
field2 = self.getParameterValue(self.TABLE_FIELD_2)
|
|
|
|
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(input, context)
|
2016-09-22 12:09:13 +02:00
|
|
|
joinField1Index = layer.fields().lookupField(field)
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2017-05-02 13:40:49 +10:00
|
|
|
layer2 = QgsProcessingUtils.mapLayerFromString(input2, context)
|
2016-09-22 12:09:13 +02:00
|
|
|
joinField2Index = layer2.fields().lookupField(field2)
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
outFields = vector.combineVectorFields(layer, layer2)
|
2017-04-26 14:33:53 +10:00
|
|
|
writer = output.getVectorWriter(outFields, layer.wkbType(), layer.crs(), context)
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2014-07-05 13:37:37 +09:00
|
|
|
# Cache attributes of Layer 2
|
|
|
|
cache = {}
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layer2, context)
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / layer2.featureCount() if layer2.featureCount() else 0
|
2016-04-04 11:45:16 +03:00
|
|
|
for current, feat in enumerate(features):
|
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-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, feat in enumerate(features):
|
|
|
|
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-06-23 14:34:38 +10:00
|
|
|
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2013-01-01 23:52:00 +01:00
|
|
|
del writer
|