Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow converting more types of MapBox GL text-field configuration
  • Loading branch information
nyalldawson committed Sep 7, 2020
1 parent 1d1e1cc commit c4b344d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/core/vectortile/qgsmapboxglstyleconverter.cpp
Expand Up @@ -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 )
Expand All @@ -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;
}
Expand Down Expand Up @@ -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" ) )
{
Expand Down Expand Up @@ -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" ) );
Expand Down
36 changes: 36 additions & 0 deletions tests/src/python/test_qgsmapboxglconverter.py
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit c4b344d

Please sign in to comment.