Skip to content

Commit adda744

Browse files
committedAug 5, 2017
Port Join Attributes alg to new API
Improvements: - don't fetch unused geometry for joined table
1 parent 572dada commit adda744

File tree

3 files changed

+59
-56
lines changed

3 files changed

+59
-56
lines changed
 

‎python/plugins/processing/algs/qgis/JoinAttributes.py

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,25 @@
3030

3131
from qgis.core import (QgsFeature,
3232
QgsFeatureSink,
33-
QgsApplication,
34-
QgsProcessingUtils)
33+
QgsFeatureRequest,
34+
QgsProcessingParameterFeatureSource,
35+
QgsProcessingUtils,
36+
QgsProcessingParameterField,
37+
QgsProcessingParameterFeatureSink)
3538

3639
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
37-
from processing.core.parameters import ParameterVector
38-
from processing.core.parameters import ParameterTable
39-
from processing.core.parameters import ParameterTableField
40-
from processing.core.outputs import OutputVector
4140
from processing.tools import vector
4241

4342
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
4443

4544

4645
class JoinAttributes(QgisAlgorithm):
4746

48-
OUTPUT_LAYER = 'OUTPUT_LAYER'
49-
INPUT_LAYER = 'INPUT_LAYER'
50-
INPUT_LAYER_2 = 'INPUT_LAYER_2'
51-
TABLE_FIELD = 'TABLE_FIELD'
52-
TABLE_FIELD_2 = 'TABLE_FIELD_2'
47+
OUTPUT = 'OUTPUT'
48+
INPUT = 'INPUT'
49+
INPUT_2 = 'INPUT_2'
50+
FIELD = 'FIELD'
51+
FIELD_2 = 'FIELD_2'
5352

5453
def group(self):
5554
return self.tr('Vector general tools')
@@ -58,16 +57,15 @@ def __init__(self):
5857
super().__init__()
5958

6059
def initAlgorithm(self, config=None):
61-
self.addParameter(ParameterVector(self.INPUT_LAYER,
62-
self.tr('Input layer')))
63-
self.addParameter(ParameterTable(self.INPUT_LAYER_2,
64-
self.tr('Input layer 2'), False))
65-
self.addParameter(ParameterTableField(self.TABLE_FIELD,
66-
self.tr('Table field'), self.INPUT_LAYER))
67-
self.addParameter(ParameterTableField(self.TABLE_FIELD_2,
68-
self.tr('Table field 2'), self.INPUT_LAYER_2))
69-
self.addOutput(OutputVector(self.OUTPUT_LAYER,
70-
self.tr('Joined layer')))
60+
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
61+
self.tr('Input layer')))
62+
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT_2,
63+
self.tr('Input layer 2')))
64+
self.addParameter(QgsProcessingParameterField(self.FIELD,
65+
self.tr('Table field'), parentLayerParameterName=self.INPUT))
66+
self.addParameter(QgsProcessingParameterField(self.FIELD_2,
67+
self.tr('Table field 2'), parentLayerParameterName=self.INPUT_2))
68+
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Joined layer')))
7169

7270
def name(self):
7371
return 'joinattributestable'
@@ -76,26 +74,27 @@ def displayName(self):
7674
return self.tr('Join attributes table')
7775

7876
def processAlgorithm(self, parameters, context, feedback):
79-
input = self.getParameterValue(self.INPUT_LAYER)
80-
input2 = self.getParameterValue(self.INPUT_LAYER_2)
81-
output = self.getOutputFromName(self.OUTPUT_LAYER)
82-
field = self.getParameterValue(self.TABLE_FIELD)
83-
field2 = self.getParameterValue(self.TABLE_FIELD_2)
77+
input = self.parameterAsSource(parameters, self.INPUT, context)
78+
input2 = self.parameterAsSource(parameters, self.INPUT_2, context)
79+
field = self.parameterAsString(parameters, self.FIELD, context)
80+
field2 = self.parameterAsString(parameters, self.FIELD_2, context)
8481

85-
layer = QgsProcessingUtils.mapLayerFromString(input, context)
86-
joinField1Index = layer.fields().lookupField(field)
82+
joinField1Index = input.fields().lookupField(field)
83+
joinField2Index = input2.fields().lookupField(field2)
8784

88-
layer2 = QgsProcessingUtils.mapLayerFromString(input2, context)
89-
joinField2Index = layer2.fields().lookupField(field2)
85+
outFields = vector.combineFields(input.fields(), input2.fields())
9086

91-
outFields = vector.combineVectorFields(layer, layer2)
92-
writer = output.getVectorWriter(outFields, layer.wkbType(), layer.crs(), context)
87+
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
88+
outFields, input.wkbType(), input.sourceCrs())
9389

94-
# Cache attributes of Layer 2
90+
# Cache attributes of input2
9591
cache = {}
96-
features = QgsProcessingUtils.getFeatures(layer2, context)
97-
total = 100.0 / layer2.featureCount() if layer2.featureCount() else 0
92+
features = input2.getFeatures(QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry))
93+
total = 100.0 / input2.featureCount() if input2.featureCount() else 0
9894
for current, feat in enumerate(features):
95+
if feedback.isCanceled():
96+
break
97+
9998
attrs = feat.attributes()
10099
joinValue2 = str(attrs[joinField2Index])
101100
if joinValue2 not in cache:
@@ -104,14 +103,18 @@ def processAlgorithm(self, parameters, context, feedback):
104103

105104
# Create output vector layer with additional attribute
106105
outFeat = QgsFeature()
107-
features = QgsProcessingUtils.getFeatures(layer, context)
108-
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
106+
features = input.getFeatures()
107+
total = 100.0 / input.featureCount() if input.featureCount() else 0
109108
for current, feat in enumerate(features):
109+
if feedback.isCanceled():
110+
break
111+
110112
outFeat.setGeometry(feat.geometry())
111113
attrs = feat.attributes()
112114
joinValue1 = str(attrs[joinField1Index])
113115
attrs.extend(cache.get(joinValue1, []))
114116
outFeat.setAttributes(attrs)
115-
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
117+
sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
116118
feedback.setProgress(int(current * total))
117-
del writer
119+
120+
return {self.OUTPUT: dest_id}

‎python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
from .ImportIntoPostGIS import ImportIntoPostGIS
8383
from .ImportIntoSpatialite import ImportIntoSpatialite
8484
from .Intersection import Intersection
85+
from .JoinAttributes import JoinAttributes
8586
from .LinesIntersection import LinesIntersection
8687
from .LinesToPolygons import LinesToPolygons
8788
from .MeanCoords import MeanCoords
@@ -151,7 +152,6 @@
151152
# from .StatisticsByCategories import StatisticsByCategories
152153
# from .FieldsCalculator import FieldsCalculator
153154
# from .FieldPyculator import FieldsPyculator
154-
# from .JoinAttributes import JoinAttributes
155155
# from .PointsDisplacement import PointsDisplacement
156156
# from .PointsFromPolygons import PointsFromPolygons
157157
# from .PointsFromLines import PointsFromLines
@@ -189,7 +189,6 @@ def getAlgs(self):
189189
# ExtractByLocation(),
190190
# SpatialJoin(),
191191
# GeometryConvert(), FieldsCalculator(),
192-
# JoinAttributes(),
193192
# FieldsPyculator(),
194193
# StatisticsByCategories(),
195194
# RasterLayerStatistics(), PointsDisplacement(),
@@ -249,6 +248,7 @@ def getAlgs(self):
249248
ImportIntoPostGIS(),
250249
ImportIntoSpatialite(),
251250
Intersection(),
251+
JoinAttributes(),
252252
LinesIntersection(),
253253
LinesToPolygons(),
254254
MeanCoords(),

‎python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,21 +2265,21 @@ tests:
22652265
- expected/points_to_path_grouped2.gml
22662266
type: vector
22672267

2268-
# - algorithm: qgis:joinattributestable
2269-
# name: join the attribute table by common field
2270-
# params:
2271-
# INPUT_LAYER:
2272-
# name: points.gml
2273-
# type: vector
2274-
# INPUT_LAYER_2:
2275-
# name: table.dbf
2276-
# type: table
2277-
# TABLE_FIELD: id
2278-
# TABLE_FIELD_2: ID
2279-
# results:
2280-
# OUTPUT_LAYER:
2281-
# name: expected/join_attribute_table.gml
2282-
# type: vector
2268+
- algorithm: qgis:joinattributestable
2269+
name: join the attribute table by common field
2270+
params:
2271+
INPUT:
2272+
name: points.gml
2273+
type: vector
2274+
INPUT_2:
2275+
name: table.dbf
2276+
type: table
2277+
FIELD: id
2278+
FIELD_2: ID
2279+
results:
2280+
OUTPUT:
2281+
name: expected/join_attribute_table.gml
2282+
type: vector
22832283

22842284
- algorithm: qgis:convexhull
22852285
name: Simple convex hull

0 commit comments

Comments
 (0)
Please sign in to comment.