Skip to content
Permalink
Browse files

Even simpler filter strings in some circumstances

  • Loading branch information
nyalldawson committed Sep 6, 2020
1 parent b5ff62a commit d30186817388c1881d558639692502d46a01cab9
Showing with 49 additions and 17 deletions.
  1. +44 −12 src/core/vectortile/qgsmapboxglstyleconverter.cpp
  2. +5 −5 tests/src/python/test_qgsmapboxglconverter.py
@@ -1259,32 +1259,64 @@ QString QgsMapBoxGlStyleConverter::parseExpression( const QVariantList &expressi
{
const QString attribute = expression.value( 1 ).toList().value( 1 ).toString();

QString caseString = QStringLiteral( "CASE " );
for ( int i = 2; i < expression.size() - 2; i += 2 )
if ( expression.size() == 5
&& expression.at( 3 ).type() == QVariant::Bool && expression.at( 3 ).toBool() == true
&& expression.at( 4 ).type() == QVariant::Bool && expression.at( 4 ).toBool() == false )
{
if ( expression.at( i ).type() == QVariant::List || expression.at( i ).type() == QVariant::StringList )
// simple case, make a nice simple expression instead of a CASE statement
if ( expression.at( 2 ).type() == QVariant::List || expression.at( 2 ).type() == QVariant::StringList )
{
QStringList parts;
for ( const QVariant &p : expression.at( i ).toList() )
for ( const QVariant &p : expression.at( 2 ).toList() )
{
parts << QgsExpression::quotedValue( p );
}

if ( parts.size() > 1 )
caseString += QStringLiteral( "WHEN %1 IN (%2) " ).arg( QgsExpression::quotedColumnRef( attribute ), parts.join( ", " ) );
return QStringLiteral( "%1 IN (%2)" ).arg( QgsExpression::quotedColumnRef( attribute ), parts.join( ", " ) );
else
caseString += QStringLiteral( "WHEN %1 " ).arg( QgsExpression::createFieldEqualityExpression( attribute, expression.at( i ).toList().value( 0 ) ) );
return QgsExpression::createFieldEqualityExpression( attribute, expression.at( 2 ).toList().value( 0 ) );
}
else if ( expression.at( i ).type() == QVariant::String || expression.at( i ).type() == QVariant::Int
|| expression.at( i ).type() == QVariant::Double )
else if ( expression.at( 2 ).type() == QVariant::String || expression.at( 2 ).type() == QVariant::Int
|| expression.at( 2 ).type() == QVariant::Double )
{
caseString += QStringLiteral( "WHEN (%1) " ).arg( QgsExpression::createFieldEqualityExpression( attribute, expression.at( i ) ) );
return QgsExpression::createFieldEqualityExpression( attribute, expression.at( 2 ) );
}
else
{
context.pushWarning( QObject::tr( "Skipping non-supported expression" ) );
return QString();
}
}
else
{
QString caseString = QStringLiteral( "CASE " );
for ( int i = 2; i < expression.size() - 2; i += 2 )
{
if ( expression.at( i ).type() == QVariant::List || expression.at( i ).type() == QVariant::StringList )
{
QStringList parts;
for ( const QVariant &p : expression.at( i ).toList() )
{
parts << QgsExpression::quotedValue( p );
}

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 )
{
caseString += QStringLiteral( "WHEN (%1) " ).arg( QgsExpression::createFieldEqualityExpression( attribute, expression.at( i ) ) );
}

caseString += QStringLiteral( "THEN %1 " ).arg( QgsExpression::quotedValue( expression.at( i + 1 ) ) );
caseString += QStringLiteral( "THEN %1 " ).arg( QgsExpression::quotedValue( expression.at( i + 1 ) ) );
}
caseString += QStringLiteral( "ELSE %1 END" ).arg( QgsExpression::quotedValue( expression.last() ) );
return caseString;
}
caseString += QStringLiteral( "ELSE %1 END" ).arg( QgsExpression::quotedValue( expression.last() ) );
return caseString;
}
else
{
@@ -139,12 +139,12 @@ def testParseExpression(self):
["==", ["get", "level"], 0],
["match", ["get", "type"], ["Restricted"], True, False]
], conversion_context),
'''(level IS 0) AND (CASE WHEN "type" = 'Restricted' THEN TRUE ELSE FALSE END)''')
'''(level IS 0) AND ("type" = 'Restricted')''')

self.assertEqual(QgsMapBoxGlStyleConverter.parseExpression([
"match", ["get", "type"], ["Restricted"], True, False
], conversion_context),
'''CASE WHEN "type" = 'Restricted' THEN TRUE ELSE FALSE END''')
'''"type" = 'Restricted\'''')

self.assertEqual(QgsMapBoxGlStyleConverter.parseExpression([
"match", ["get", "type"], ["Restricted"], "r", ["Local"], "l", ["Secondary", "Main"], "m", "n"
@@ -156,19 +156,19 @@ def testParseExpression(self):
["==", ["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)''')
'''(level IS 0) AND ("type" IN ('Restricted', 'Temporary'))''')
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)''')
'''("level" = 1) OR ("type" = 'Local')''')
self.assertEqual(QgsMapBoxGlStyleConverter.parseExpression([
"none",
["match", ["get", "level"], [1], True, False],
["match", ["get", "type"], ["Local"], True, False]
], conversion_context),
'''NOT (CASE WHEN "level" = 1 THEN TRUE ELSE FALSE END) AND NOT (CASE WHEN "type" = 'Local' THEN TRUE ELSE FALSE END)''')
'''NOT ("level" = 1) AND NOT ("type" = 'Local')''')
self.assertEqual(QgsMapBoxGlStyleConverter.parseExpression([
"match",
["get", "type"],

0 comments on commit d301868

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