Skip to content

Commit

Permalink
[FEATURE][processing] add discard non matching option to join attribu…
Browse files Browse the repository at this point in the history
…te table alg
  • Loading branch information
nirvn committed Mar 2, 2018
1 parent 5576468 commit 1542b48
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>join_attribute_table</Name>
<ElementPath>join_attribute_table</ElementPath>
<GeometryType>1</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>1</FeatureCount>
<ExtentXMin>0.00000</ExtentXMin>
<ExtentXMax>8.00000</ExtentXMax>
<ExtentYMin>-5.00000</ExtentYMin>
<ExtentYMax>3.00000</ExtentYMax>
</DatasetSpecificInfo>
<PropertyDefn>
<Name>id</Name>
<ElementPath>id</ElementPath>
<Type>Integer</Type>
</PropertyDefn>
<PropertyDefn>
<Name>id2</Name>
<ElementPath>id2</ElementPath>
<Type>Integer</Type>
</PropertyDefn>
<PropertyDefn>
<Name>id_2</Name>
<ElementPath>id_2</ElementPath>
<Type>Integer</Type>
</PropertyDefn>
<PropertyDefn>
<Name>NUM_A</Name>
<ElementPath>NUM_A</ElementPath>
<Type>Real</Type>
</PropertyDefn>
<PropertyDefn>
<Name>ST_A</Name>
<ElementPath>ST_A</ElementPath>
<Type>String</Type>
<Width>8</Width>
</PropertyDefn>
</GMLFeatureClass>
</GMLFeatureClassList>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=""
xmlns:ogr="http://ogr.maptools.org/"
xmlns:gml="http://www.opengis.net/gml">
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>0</gml:X><gml:Y>-5</gml:Y></gml:coord>
<gml:coord><gml:X>8</gml:X><gml:Y>3</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:join_attribute_table fid="points.0">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>1,1</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:id>1</ogr:id>
<ogr:id2>2</ogr:id2>
<ogr:id_2>1</ogr:id_2>
<ogr:NUM_A>1.100000</ogr:NUM_A>
<ogr:ST_A>string a</ogr:ST_A>
</ogr:join_attribute_table>
</gml:featureMember>
</ogr:FeatureCollection>
18 changes: 18 additions & 0 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2788,6 +2788,24 @@ tests:
name: expected/join_attribute_table.gml
type: vector

- algorithm: native:joinattributestable
name: join the attribute table by common field, discard non matching (one-to-one)
params:
METHOD: 0
INPUT:
name: points.gml
type: vector
INPUT_2:
name: table2.dbf
type: table
FIELD: id
FIELD_2: ID
DISCARD_NONMATCHING: true
results:
OUTPUT:
name: expected/join_attribute_table_discard_nonmatching.gml
type: vector

- algorithm: native:joinattributestable
name: join the attribute table by common field (one-to-many)
params:
Expand Down
Binary file added python/plugins/processing/tests/testdata/table2.dbf
Binary file not shown.
20 changes: 11 additions & 9 deletions src/analysis/processing/qgsalgorithmjoinbyattribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ QString QgsJoinByAttributeAlgorithm::name() const

QString QgsJoinByAttributeAlgorithm::displayName() const
{
return QObject::tr( "Join attributes table" );
return QObject::tr( "Join attributes by field value" );
}

QStringList QgsJoinByAttributeAlgorithm::tags() const
{
return QObject::tr( "join,connect,attributes,values,fields" ).split( ',' );
return QObject::tr( "join,connect,attributes,values,fields,tables" ).split( ',' );
}

QString QgsJoinByAttributeAlgorithm::group() const
Expand Down Expand Up @@ -65,10 +65,12 @@ void QgsJoinByAttributeAlgorithm::initAlgorithm( const QVariantMap & )
QVariant(), QStringLiteral( "INPUT_2" ), QgsProcessingParameterField::Any,
true, true ) );

addParameter( new QgsProcessingParameterEnum(
QStringLiteral( "METHOD" ),
QObject::tr( "Join type" ),
methods, false, 0 ) );
addParameter( new QgsProcessingParameterEnum( QStringLiteral( "METHOD" ),
QObject::tr( "Join type" ),
methods, false, 0 ) );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "DISCARD_NONMATCHING" ),
QObject::tr( "Discard records which could not be joined" ),
false ) );

addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Joined layer" ) ) );
}
Expand All @@ -78,8 +80,7 @@ QString QgsJoinByAttributeAlgorithm::shortHelpString() const
return QObject::tr( "This algorithm takes an input vector layer and creates a new vector layer that is an extended version of the "
"input one, with additional attributes in its attribute table.\n\n"
"The additional attributes and their values are taken from a second vector layer. An attribute is selected "
"in each of them to define the join criteria.\n\n"
"The algorithm will output one feature per matching row(s) from the second vector layer." );
"in each of them to define the join criteria." );
}

QgsJoinByAttributeAlgorithm *QgsJoinByAttributeAlgorithm::createInstance() const
Expand All @@ -90,6 +91,7 @@ QgsJoinByAttributeAlgorithm *QgsJoinByAttributeAlgorithm::createInstance() const
QVariantMap QgsJoinByAttributeAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
int joinMethod = parameterAsEnum( parameters, QStringLiteral( "METHOD" ), context );
bool discardNonMatching = parameterAsBool( parameters, QStringLiteral( "DISCARD_NONMATCHING" ), context );

std::unique_ptr< QgsFeatureSource > input( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
std::unique_ptr< QgsFeatureSource > input2( parameterAsSource( parameters, QStringLiteral( "INPUT_2" ), context ) );
Expand Down Expand Up @@ -199,7 +201,7 @@ QVariantMap QgsJoinByAttributeAlgorithm::processAlgorithm( const QVariantMap &pa
sink->addFeature( feat, QgsFeatureSink::FastInsert );
}
}
else
else if ( !discardNonMatching )
{
sink->addFeature( feat, QgsFeatureSink::FastInsert );
}
Expand Down

0 comments on commit 1542b48

Please sign in to comment.