Skip to content
Permalink
Browse files

Cleaner filter expressions

  • Loading branch information
nyalldawson committed Sep 6, 2020
1 parent c32733b commit 2fa8cf3f6087987a3dfa012eaaf6b63f403b2e73
Showing with 34 additions and 36 deletions.
  1. +19 −33 src/core/vectortile/qgsmapboxglstyleconverter.cpp
  2. +15 −3 tests/src/python/test_qgsmapboxglconverter.py
@@ -1174,7 +1174,9 @@ Qt::PenJoinStyle QgsMapBoxGlStyleConverter::parseJoinStyle( const QString &style
QString QgsMapBoxGlStyleConverter::parseExpression( const QVariantList &expression, QgsMapBoxGlStyleConversionContext &context )
{
QString op = expression.value( 0 ).toString();
if ( op == QLatin1String( "all" ) )
if ( op == QLatin1String( "all" )
|| op == QLatin1String( "any" )
|| op == QLatin1String( "none" ) )
{
QStringList parts;
for ( int i = 1; i < expression.size(); ++i )
@@ -1187,37 +1189,17 @@ QString QgsMapBoxGlStyleConverter::parseExpression( const QVariantList &expressi
}
parts << part;
}
return QStringLiteral( "(%1)" ).arg( parts.join( QStringLiteral( ") AND (" ) ) );
}
else if ( op == QLatin1String( "any" ) )
{
QStringList parts;
for ( int i = 1; i < expression.size(); ++i )
{
QString part = parseValue( expression.at( i ), context );
if ( part.isEmpty() )
{
context.pushWarning( QObject::tr( "Skipping unsupported expression" ) );
return QString();
}
parts << part;
}
return QStringLiteral( "(%1)" ).arg( parts.join( QStringLiteral( ") OR (" ) ) );
}
else if ( op == QLatin1String( "none" ) )
{
QStringList parts;
for ( int i = 1; i < expression.size(); ++i )
{
QString part = parseValue( expression.at( i ), context );
if ( part.isEmpty() )
{
context.pushWarning( QObject::tr( "Skipping unsupported expression" ) );
return QString();
}
parts << part;
}
return QStringLiteral( "NOT (%1)" ).arg( parts.join( QStringLiteral( ") AND NOT (" ) ) );

if ( op == QLatin1String( "none" ) )
return QStringLiteral( "NOT (%1)" ).arg( parts.join( QStringLiteral( ") AND NOT (" ) ) );

QString operatorString;
if ( op == QLatin1String( "all" ) )
operatorString = QStringLiteral( ") AND (" );
else if ( op == QLatin1String( "any" ) )
operatorString = QStringLiteral( ") OR (" );

return QStringLiteral( "(%1)" ).arg( parts.join( operatorString ) );
}
else if ( op == '!' )
{
@@ -1286,7 +1268,11 @@ QString QgsMapBoxGlStyleConverter::parseExpression( const QVariantList &expressi
{
parts << QgsExpression::quotedValue( p );
}
caseString += QStringLiteral( "WHEN (%1 IN (%2)) " ).arg( QgsExpression::quotedColumnRef( attribute ), parts.join( ", " ) );

if ( parts.size() > 1 )
caseString += QStringLiteral( "WHEN %1 IN (%2) " ).arg( QgsExpression::quotedColumnRef( attribute ), parts.join( ", " ) );
else
caseString += QStringLiteral( "WHEN %1 " ).arg( QgsExpression::createFieldEqualityExpression( attribute, expression.at( i ).toList().value( 0 ) ) );
}
else if ( expression.at( i ).type() == QVariant::String || expression.at( i ).type() == QVariant::Int
|| expression.at( i ).type() == QVariant::Double )
@@ -139,21 +139,33 @@ def testParseExpression(self):
["==", ["get", "level"], 0],
["match", ["get", "type"], ["Restricted"], True, False]
], conversion_context),
'''(level IS 0) AND (CASE WHEN ("type" IN ('Restricted')) THEN true ELSE false END)''')
'''(level IS 0) AND (CASE WHEN "type" = 'Restricted' THEN true ELSE false END)''')
self.assertEqual(QgsMapBoxGlStyleConverter.parseExpression([
"all",
["==", ["get", "level"], 0],
["match", ["get", "type"], ["Restricted", "Temporary"], True, False]
], conversion_context),
'''(level IS 0) AND (CASE WHEN "type" IN ('Restricted', 'Temporary') THEN true ELSE false END)''')
self.assertEqual(QgsMapBoxGlStyleConverter.parseExpression([
"any",
["match", ["get", "level"], [1], True, False],
["match", ["get", "type"], ["Local"], True, False]
], conversion_context),
'''(CASE WHEN "level" = 1 THEN true ELSE false END) OR (CASE WHEN "type" = 'Local' THEN true ELSE false END)''')
self.assertEqual(QgsMapBoxGlStyleConverter.parseExpression([
"none",
["match", ["get", "level"], [1], True, False],
["match", ["get", "type"], ["Local"], True, False]
], conversion_context),
'''(CASE WHEN ("level" IN (1)) THEN true ELSE false END) AND (CASE WHEN ("type" IN ('Local')) THEN true ELSE false END)''')
'''NOT (CASE WHEN "level" = 1 THEN true ELSE false END) AND NOT (CASE WHEN "type" = 'Local' THEN true ELSE false END)''')
self.assertEqual(QgsMapBoxGlStyleConverter.parseExpression([
"match",
["get", "type"],
["Primary", "Motorway"],
False,
True
], conversion_context),
'''CASE WHEN ("type" IN ('Primary', 'Motorway')) THEN false ELSE true END''')
'''CASE WHEN "type" IN ('Primary', 'Motorway') THEN false ELSE true END''')
self.assertEqual(QgsMapBoxGlStyleConverter.parseExpression(["==", "_symbol", 0], conversion_context),
'''"_symbol" IS 0''')

0 comments on commit 2fa8cf3

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