diff --git a/python/plugins/processing/tests/testdata/expected/join_attributes_subset_ordered.gml b/python/plugins/processing/tests/testdata/expected/join_attributes_subset_ordered.gml new file mode 100644 index 00000000000..4f871d1e09c --- /dev/null +++ b/python/plugins/processing/tests/testdata/expected/join_attributes_subset_ordered.gml @@ -0,0 +1,127 @@ + + + -5 03 8 + + + + 1 11 1 + 1 1 + points.0 + 1 + 2 + airports.0 + 18 + 78 + NOATAK + + + + + 3 33 3 + 3 3 + points.1 + 2 + 1 + airports.1 + 18 + 264 + AMBLER + + + + + 2 22 2 + 2 2 + points.2 + 3 + 0 + airports.2 + 26 + 585 + BETTLES + + + + + 2 52 5 + 2 5 + points.3 + 4 + 2 + airports.3 + 18 + 9 + RALPH WIEN MEM + + + + + 1 41 4 + 1 4 + points.4 + 5 + 1 + airports.4 + 18 + 21 + SELAWIK + + + + + -5 0-5 0 + -5 0 + points.5 + 6 + 0 + airports.5 + 26 + 1113 + INDIAN MOUNTAIN LRRS + + + + + -1 8-1 8 + -1 8 + points.6 + 7 + 0 + airports.6 + 18 + 21 + BUCKLAND + + + + + -1 7-1 7 + -1 7 + points.7 + 8 + 0 + airports.7 + 16 + 243 + TIN CITY LRRS + + + + + -1 0-1 0 + -1 0 + points.8 + 9 + 0 + airports.8 + 16 + 1329 + GRANITE MOUNTAIN AFS + + + diff --git a/python/plugins/processing/tests/testdata/expected/join_attributes_subset_ordered.xsd b/python/plugins/processing/tests/testdata/expected/join_attributes_subset_ordered.xsd new file mode 100644 index 00000000000..df8801c753d --- /dev/null +++ b/python/plugins/processing/tests/testdata/expected/join_attributes_subset_ordered.xsd @@ -0,0 +1,93 @@ + + + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/python/plugins/processing/tests/testdata/expected/join_attributes_subset_unordered.gml b/python/plugins/processing/tests/testdata/expected/join_attributes_subset_unordered.gml new file mode 100644 index 00000000000..9a34aa08c1c --- /dev/null +++ b/python/plugins/processing/tests/testdata/expected/join_attributes_subset_unordered.gml @@ -0,0 +1,127 @@ + + + -5 03 8 + + + + 1 11 1 + 1 1 + points.0 + 1 + 2 + 78 + NOATAK + 18 + airports.0 + + + + + 3 33 3 + 3 3 + points.1 + 2 + 1 + 264 + AMBLER + 18 + airports.1 + + + + + 2 22 2 + 2 2 + points.2 + 3 + 0 + 585 + BETTLES + 26 + airports.2 + + + + + 2 52 5 + 2 5 + points.3 + 4 + 2 + 9 + RALPH WIEN MEM + 18 + airports.3 + + + + + 1 41 4 + 1 4 + points.4 + 5 + 1 + 21 + SELAWIK + 18 + airports.4 + + + + + -5 0-5 0 + -5 0 + points.5 + 6 + 0 + 1113 + INDIAN MOUNTAIN LRRS + 26 + airports.5 + + + + + -1 8-1 8 + -1 8 + points.6 + 7 + 0 + 21 + BUCKLAND + 18 + airports.6 + + + + + -1 7-1 7 + -1 7 + points.7 + 8 + 0 + 243 + TIN CITY LRRS + 16 + airports.7 + + + + + -1 0-1 0 + -1 0 + points.8 + 9 + 0 + 1329 + GRANITE MOUNTAIN AFS + 16 + airports.8 + + + diff --git a/python/plugins/processing/tests/testdata/expected/join_attributes_subset_unordered.qmd b/python/plugins/processing/tests/testdata/expected/join_attributes_subset_unordered.qmd new file mode 100644 index 00000000000..3ae1c9faa92 --- /dev/null +++ b/python/plugins/processing/tests/testdata/expected/join_attributes_subset_unordered.qmd @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + 0 + 0 + + + + + false + + + + diff --git a/python/plugins/processing/tests/testdata/expected/join_attributes_subset_unordered.xsd b/python/plugins/processing/tests/testdata/expected/join_attributes_subset_unordered.xsd new file mode 100644 index 00000000000..3bb1a2b9fcd --- /dev/null +++ b/python/plugins/processing/tests/testdata/expected/join_attributes_subset_unordered.xsd @@ -0,0 +1,93 @@ + + + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/python/plugins/processing/tests/testdata/expected/nearest_field_subset_unordered.gml b/python/plugins/processing/tests/testdata/expected/nearest_field_subset_unordered.gml new file mode 100644 index 00000000000..657957fda92 --- /dev/null +++ b/python/plugins/processing/tests/testdata/expected/nearest_field_subset_unordered.gml @@ -0,0 +1,181 @@ + + + -5 03 8 + + + + 1 11 1 + 1 1 + points.0 + 1 + 2 + Other + ANNETTE ISLAND + 19 + 108 + 1 + 143.164179420743 + 1 + 1 + -131.572235108872 + 55.0424346921242 + + + + + 3 33 3 + 3 3 + points.1 + 2 + 1 + Other + ANNETTE ISLAND + 19 + 108 + 1 + 144.284792930099 + 3 + 3 + -131.572235108872 + 55.0424346921242 + + + + + 2 22 2 + 2 2 + points.2 + 3 + 0 + Other + ANNETTE ISLAND + 19 + 108 + 1 + 143.718620470863 + 2 + 2 + -131.572235108872 + 55.0424346921242 + + + + + 2 52 5 + 2 5 + points.3 + 4 + 2 + Other + ANNETTE ISLAND + 19 + 108 + 1 + 146.511007370441 + 5 + 2 + -131.572235108872 + 55.0424346921242 + + + + + 1 41 4 + 1 4 + points.4 + 5 + 1 + Other + ANNETTE ISLAND + 19 + 108 + 1 + 145.946619282078 + 4 + 1 + -131.572235108872 + 55.0424346921242 + + + + + -5 0-5 0 + -5 0 + points.5 + 6 + 0 + Other + ANNETTE ISLAND + 19 + 108 + 1 + 144.624849231736 + 0 + -5 + -131.572235108872 + 55.0424346921242 + + + + + -1 8-1 8 + -1 8 + points.6 + 7 + 0 + Other + ANNETTE ISLAND + 19 + 108 + 1 + 150.403335400207 + 8 + -1 + -131.572235108872 + 55.0424346921242 + + + + + -1 7-1 7 + -1 7 + points.7 + 8 + 0 + Other + ANNETTE ISLAND + 19 + 108 + 1 + 149.4758135261 + 7 + -1 + -131.572235108872 + 55.0424346921242 + + + + + -1 0-1 0 + -1 0 + points.8 + 9 + 0 + Other + ANNETTE ISLAND + 19 + 108 + 1 + 143.010515479685 + 0 + -1 + -131.572235108872 + 55.0424346921242 + + + diff --git a/python/plugins/processing/tests/testdata/expected/nearest_field_subset_unordered.xsd b/python/plugins/processing/tests/testdata/expected/nearest_field_subset_unordered.xsd new file mode 100644 index 00000000000..106458d5da7 --- /dev/null +++ b/python/plugins/processing/tests/testdata/expected/nearest_field_subset_unordered.xsd @@ -0,0 +1,131 @@ + + + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/python/plugins/processing/tests/testdata/qgis_algorithm_tests4.yaml b/python/plugins/processing/tests/testdata/qgis_algorithm_tests4.yaml index 6a6f31e5950..b83be70c24f 100644 --- a/python/plugins/processing/tests/testdata/qgis_algorithm_tests4.yaml +++ b/python/plugins/processing/tests/testdata/qgis_algorithm_tests4.yaml @@ -2554,6 +2554,27 @@ tests: name: expected/join_to_nearest_no_matches.gml type: vector + - algorithm: native:joinbynearest + name: Join by nearest field unordered + params: + INPUT: + name: points.gml|layername=points + type: vector + INPUT_2: + name: airports.gml|layername=airports + type: vector + NEIGHBORS: 1 + PREFIX: '' + FIELDS_TO_COPY: + - USE + - NAME + - fk_region + - ELEV + results: + OUTPUT: + name: expected/nearest_field_subset_unordered.gml + type: vector + - name: Generate XYZ tiles (Directory) algorithm: qgis:tilesxyzdirectory project: ../../../../../tests/testdata/xyztiles.qgs @@ -3273,4 +3294,52 @@ tests: name: expected/join_attributes_table_issue_47650.gml type: vector + - algorithm: native:joinbyattributes + name: Join by nearest ordered field subet + params: + DISCARD_NONMATCHING: true + FIELD: id + FIELDS_TO_COPY: + - fid + - fk_region + - ELEV + - NAME + FIELD_2: ID + INPUT: + name: points.gml|layername=points + type: vector + INPUT_2: + name: airports.gml|layername=airports + type: vector + METHOD: 1 + PREFIX: '' + results: + OUTPUT: + name: expected/join_attributes_subset_ordered.gml + type: vector + + + - algorithm: native:joinbyattributes + name: Join by nearest unordered field subset + params: + DISCARD_NONMATCHING: true + FIELD: id + FIELDS_TO_COPY: + - ELEV + - NAME + - fk_region + - fid + FIELD_2: ID + INPUT: + name: points.gml|layername=points + type: vector + INPUT_2: + name: airports.gml|layername=airports + type: vector + METHOD: 1 + PREFIX: '' + results: + OUTPUT: + name: expected/join_attributes_subset_unordered.gml + type: vector # See ../README.md for a description of the file format diff --git a/src/analysis/processing/qgsalgorithmjoinbyattribute.cpp b/src/analysis/processing/qgsalgorithmjoinbyattribute.cpp index a1427d6a2f3..578ffa00a38 100644 --- a/src/analysis/processing/qgsalgorithmjoinbyattribute.cpp +++ b/src/analysis/processing/qgsalgorithmjoinbyattribute.cpp @@ -197,12 +197,10 @@ QVariantMap QgsJoinByAttributeAlgorithm::processAlgorithm( const QVariantMap &pa // only keep selected attributes QgsAttributes attributes; - const int attributeCount = feat.attributeCount(); - for ( int j = 0; j < attributeCount; ++j ) + QList::const_iterator fieldItr; + for ( fieldItr = fields2Indices.constBegin(); fieldItr != fields2Indices.constEnd(); ++fieldItr ) { - if ( !fields2Indices.contains( j ) ) - continue; - attributes << feat.attribute( j ); + attributes << feat.attribute( *fieldItr ); } input2AttributeCache.insert( feat.attribute( joinField2Index ), attributes ); diff --git a/src/analysis/processing/qgsalgorithmjoinbynearest.cpp b/src/analysis/processing/qgsalgorithmjoinbynearest.cpp index e4ccc17a0aa..477c71881fc 100644 --- a/src/analysis/processing/qgsalgorithmjoinbynearest.cpp +++ b/src/analysis/processing/qgsalgorithmjoinbynearest.cpp @@ -202,12 +202,10 @@ QVariantMap QgsJoinByNearestAlgorithm::processAlgorithm( const QVariantMap ¶ // only keep selected attributes QgsAttributes attributes; - const int attributeCount = f.attributeCount(); - for ( int j = 0; j < attributeCount; ++j ) + QList::const_iterator fieldItr; + for ( fieldItr = fields2Indices.constBegin(); fieldItr != fields2Indices.constEnd(); ++fieldItr ) { - if ( ! fields2Indices.contains( j ) ) - continue; - attributes << f.attribute( j ); + attributes << f.attribute( *fieldItr ); } input2AttributeCache.insert( f.id(), attributes );