Skip to content
Permalink
Browse files
Support % and to-number and keys which are expressions
  • Loading branch information
mhugent authored and github-actions committed Dec 23, 2021
1 parent d2ff250 commit 9e1efa8c5a51af48aca40a44d776d16bd6ac890d
Showing with 33 additions and 9 deletions.
  1. +26 −8 src/core/vectortile/qgsmapboxglstyleconverter.cpp
  2. +1 −1 src/core/vectortile/qgsmapboxglstyleconverter.h
  3. +6 −0 tests/src/python/test_qgsmapboxglconverter.py
@@ -2712,6 +2712,14 @@ Qt::PenJoinStyle QgsMapBoxGlStyleConverter::parseJoinStyle( const QString &style
QString QgsMapBoxGlStyleConverter::parseExpression( const QVariantList &expression, QgsMapBoxGlStyleConversionContext &context, bool colorExpected )
{
QString op = expression.value( 0 ).toString();
if ( op == QLatin1String( "%" ) && expression.size() >= 3 )
{
return QStringLiteral( "%1 %2 %3" ).arg( parseValue( expression.value( 1 ), context ) ).arg( op ).arg( parseValue( expression.value( 2 ), context ) );
}
else if ( op == QLatin1String( "to-number" ) )
{
return QStringLiteral( "to_real(%1)" ).arg( parseValue( expression.value( 1 ), context ) );
}
if ( op == QLatin1String( "literal" ) )
{
return expression.value( 1 ).toString();
@@ -2749,7 +2757,7 @@ QString QgsMapBoxGlStyleConverter::parseExpression( const QVariantList &expressi
QVariantList contraJsonExpr = expression.value( 1 ).toList();
contraJsonExpr[0] = QString( op + contraJsonExpr[0].toString() );
// ['!', ['has', 'level']] -> ['!has', 'level']
return parseKey( contraJsonExpr );
return parseKey( contraJsonExpr, context );
}
else if ( op == QLatin1String( "==" )
|| op == QLatin1String( "!=" )
@@ -2763,20 +2771,20 @@ QString QgsMapBoxGlStyleConverter::parseExpression( const QVariantList &expressi
op = QStringLiteral( "IS" );
else if ( op == QLatin1String( "!=" ) )
op = QStringLiteral( "IS NOT" );
return QStringLiteral( "%1 %2 %3" ).arg( parseKey( expression.value( 1 ) ),
return QStringLiteral( "%1 %2 %3" ).arg( parseKey( expression.value( 1 ), context ),
op, parseValue( expression.value( 2 ), context ) );
}
else if ( op == QLatin1String( "has" ) )
{
return parseKey( expression.value( 1 ) ) + QStringLiteral( " IS NOT NULL" );
return parseKey( expression.value( 1 ), context ) + QStringLiteral( " IS NOT NULL" );
}
else if ( op == QLatin1String( "!has" ) )
{
return parseKey( expression.value( 1 ) ) + QStringLiteral( " IS NULL" );
return parseKey( expression.value( 1 ), context ) + QStringLiteral( " IS NULL" );
}
else if ( op == QLatin1String( "in" ) || op == QLatin1String( "!in" ) )
{
const QString key = parseKey( expression.value( 1 ) );
const QString key = parseKey( expression.value( 1 ), context );
QStringList parts;
for ( int i = 2; i < expression.size(); ++i )
{
@@ -2795,7 +2803,7 @@ QString QgsMapBoxGlStyleConverter::parseExpression( const QVariantList &expressi
}
else if ( op == QLatin1String( "get" ) )
{
return parseKey( expression.value( 1 ) );
return parseKey( expression.value( 1 ), context );
}
else if ( op == QLatin1String( "match" ) )
{
@@ -3125,11 +3133,17 @@ QString QgsMapBoxGlStyleConverter::parseValueCheckColor( const QVariant &value,
return parseValue( value, context );
}

QString QgsMapBoxGlStyleConverter::parseKey( const QVariant &value )
QString QgsMapBoxGlStyleConverter::parseKey( const QVariant &value, QgsMapBoxGlStyleConversionContext &context )
{
if ( value.toString() == QLatin1String( "$type" ) )
{
return QStringLiteral( "_geom_type" );
else if ( value.type() == QVariant::List || value.type() == QVariant::StringList )
}
if ( value.toString() == QLatin1String( "level" ) )
{
return QStringLiteral( "level" );
}
else if ( ( value.type() == QVariant::List && value.toList().size() == 1 ) || value.type() == QVariant::StringList )
{
if ( value.toList().size() > 1 )
return value.toList().at( 1 ).toString();
@@ -3143,6 +3157,10 @@ QString QgsMapBoxGlStyleConverter::parseKey( const QVariant &value )
return valueString;
}
}
else if ( value.type() == QVariant::List && value.toList().size() > 1 )
{
return parseExpression( value.toList(), context );
}
return QgsExpression::quotedColumnRef( value.toString() );
}

@@ -549,7 +549,7 @@ class CORE_EXPORT QgsMapBoxGlStyleConverter
* Checks if value is a color before calling parseValue
*/
static QString parseValueCheckColor( const QVariant &value, QgsMapBoxGlStyleConversionContext &context );
static QString parseKey( const QVariant &value );
static QString parseKey( const QVariant &value, QgsMapBoxGlStyleConversionContext &context );

/**
* Checks if interpolation bottom/top values are numeric values
@@ -349,6 +349,12 @@ def testParseExpression(self):
self.assertEqual(QgsMapBoxGlStyleConverter.parseExpression(["to-string", ["get", "name"]],
conversion_context),
'''to_string("name")''')
self.assertEqual(QgsMapBoxGlStyleConverter.parseExpression(["to-number", ["get", "elevation"]],
conversion_context),
'''to_real("elevation")''')
self.assertEqual(QgsMapBoxGlStyleConverter.parseExpression(["%", 100, 20],
conversion_context),
'''100 % 20''')

def testConvertLabels(self):
context = QgsMapBoxGlStyleConversionContext()

0 comments on commit 9e1efa8

Please sign in to comment.