Skip to content
Permalink
Browse files

Allow converting more types of MapBox GL text-field configuration

  • Loading branch information
nyalldawson committed Sep 7, 2020
1 parent 1d1e1cc commit c4b344d47f7db06155ab172244b2ba757d804d0a
Showing with 48 additions and 3 deletions.
  1. +12 −3 src/core/vectortile/qgsmapboxglstyleconverter.cpp
  2. +36 −0 tests/src/python/test_qgsmapboxglconverter.py
@@ -827,7 +827,7 @@ void QgsMapBoxGlStyleConverter::parseSymbolLayer( const QVariantMap &jsonLayer,
* "bar", { "font-scale": 0.8 }
* ]
*/
if ( textFieldList.size() > 2 )
if ( textFieldList.size() > 2 && textFieldList.at( 0 ).toString() == QLatin1String( "format" ) )
{
QStringList parts;
for ( int i = 1; i < textFieldList.size(); ++i )
@@ -846,7 +846,12 @@ void QgsMapBoxGlStyleConverter::parseSymbolLayer( const QVariantMap &jsonLayer,
}
else
{
labelSettings.fieldName = processLabelField( textFieldList.value( 1 ).toList().value( 0 ).toString(), labelSettings.isExpression );
/*
* e.g.
* "text-field": ["to-string", ["get", "name"]]
*/
labelSettings.fieldName = parseExpression( textFieldList, context );
labelSettings.isExpression = true;
}
break;
}
@@ -1361,7 +1366,7 @@ QString QgsMapBoxGlStyleConverter::parseExpression( const QVariantList &expressi
}
else if ( op == QLatin1String( "get" ) )
{
return parseKey( expression.value( 1 ).toList().value( 1 ) );
return parseKey( expression.value( 1 ) );
}
else if ( op == QLatin1String( "match" ) )
{
@@ -1426,6 +1431,10 @@ QString QgsMapBoxGlStyleConverter::parseExpression( const QVariantList &expressi
return caseString;
}
}
else if ( op == QLatin1String( "to-string" ) )
{
return QStringLiteral( "to_string(%1)" ).arg( parseExpression( expression.value( 1 ).toList(), context ) );
}
else
{
context.pushWarning( QObject::tr( "Skipping non-supported expression" ) );
@@ -213,6 +213,14 @@ def testParseExpression(self):
conversion_context),
'''("_symbol" IS 8) AND (("Viz" IS NULL OR "Viz" NOT IN (3)))''')

self.assertEqual(QgsMapBoxGlStyleConverter.parseExpression(["get", "name"],
conversion_context),
'''"name"''')

self.assertEqual(QgsMapBoxGlStyleConverter.parseExpression(["to-string", ["get", "name"]],
conversion_context),
'''to_string("name")''')

def testConvertLabels(self):
context = QgsMapBoxGlStyleConversionContext()
style = {
@@ -360,6 +368,34 @@ def testConvertLabels(self):
self.assertEqual(labeling.labelSettings().fieldName, '''concat(concat("name_en",' - ',"name_fr"),"bar")''')
self.assertTrue(labeling.labelSettings().isExpression)

style = {
"layout": {
"text-field": ["to-string", ["get", "name"]],
"text-font": [
"Open Sans Semibold",
"Arial Unicode MS Bold"
],
"text-max-width": 8,
"text-anchor": "top",
"text-size": 11,
"icon-size": 1
},
"type": "symbol",
"id": "poi_label",
"paint": {
"text-color": "#666",
"text-halo-width": 1.5,
"text-halo-color": "rgba(255,255,255,0.95)",
"text-halo-blur": 1
},
"source-layer": "poi_label"
}
renderer, has_renderer, labeling, has_labeling = QgsMapBoxGlStyleConverter.parseSymbolLayer(style, context)
self.assertFalse(has_renderer)
self.assertTrue(has_labeling)
self.assertEqual(labeling.labelSettings().fieldName, '''to_string("name")''')
self.assertTrue(labeling.labelSettings().isExpression)

# text-transform

style = {

0 comments on commit c4b344d

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