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
|
|
|
|
|
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 qgis.core import QgsFeature
|
|
|
|
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
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
|
2013-09-12 13:19:00 +02:00
|
|
|
from processing.tools import dataobjects, 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
|
|
|
|
2012-12-24 13:00:31 +01:00
|
|
|
class JoinAttributes(GeoAlgorithm):
|
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
|
|
|
|
|
|
|
def defineCharacteristics(self):
|
2015-07-26 03:48:27 +02:00
|
|
|
self.name, self.i18n_name = self.trAlgorithm('Join attributes table')
|
|
|
|
self.group, self.i18n_group = self.trAlgorithm('Vector general tools')
|
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
|
|
|
|
|
|
|
def processAlgorithm(self, progress):
|
|
|
|
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)
|
|
|
|
|
2013-09-12 13:19:00 +02:00
|
|
|
layer = dataobjects.getObjectFromUri(input)
|
2016-09-22 12:09:13 +02:00
|
|
|
joinField1Index = layer.fields().lookupField(field)
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-09-12 13:19:00 +02:00
|
|
|
layer2 = dataobjects.getObjectFromUri(input2)
|
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)
|
2016-08-04 07:33:49 +10:00
|
|
|
writer = output.getVectorWriter(outFields, layer.wkbType(),
|
2015-08-22 14:29:41 +02:00
|
|
|
layer.crs())
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2014-07-05 13:37:37 +09:00
|
|
|
# Cache attributes of Layer 2
|
|
|
|
cache = {}
|
2016-02-17 09:36:59 +02:00
|
|
|
features = vector.features(layer2)
|
|
|
|
total = 100.0 / len(features)
|
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
|
|
|
|
progress.setPercentage(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()
|
2013-10-01 20:52:22 +03:00
|
|
|
features = vector.features(layer)
|
2016-02-17 09:36:59 +02:00
|
|
|
total = 100.0 / len(features)
|
|
|
|
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)
|
2013-02-04 00:14:39 +01:00
|
|
|
writer.addFeature(outFeat)
|
2016-02-17 09:36:59 +02:00
|
|
|
progress.setPercentage(int(current * total))
|
2013-01-01 23:52:00 +01:00
|
|
|
del writer
|