Skip to content
Permalink
Browse files

[FEATURE][processing] add discard non matching option to join attribu…

…te table alg
  • Loading branch information
nirvn committed Mar 2, 2018
1 parent 5576468 commit 1542b484bec3ac639695c9ebedcaf5732395bdc8
@@ -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>
@@ -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>
@@ -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:
Binary file not shown.
@@ -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
@@ -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" ) ) );
}
@@ -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
@@ -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 ) );
@@ -199,7 +201,7 @@ QVariantMap QgsJoinByAttributeAlgorithm::processAlgorithm( const QVariantMap &pa
sink->addFeature( feat, QgsFeatureSink::FastInsert );
}
}
else
else if ( !discardNonMatching )
{
sink->addFeature( feat, QgsFeatureSink::FastInsert );
}

0 comments on commit 1542b48

Please sign in to comment.
You can’t perform that action at this time.