Skip to content

Commit

Permalink
[mvt] support rotations for labels (#58881)
Browse files Browse the repository at this point in the history
* [mvt] support rotations for labels

* fix code style

* Update src/core/vectortile/qgsmapboxglstyleconverter.cpp

Co-authored-by: Even Rouault <even.rouault@spatialys.com>

---------

Co-authored-by: Even Rouault <even.rouault@spatialys.com>
  • Loading branch information
3nids and rouault authored Sep 27, 2024
1 parent 27e1ec9 commit 9302008
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/core/vectortile/qgsmapboxglstyleconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1681,6 +1681,32 @@ void QgsMapBoxGlStyleConverter::parseSymbolLayer( const QVariantMap &jsonLayer,
}
}

if ( jsonLayout.contains( QStringLiteral( "text-rotate" ) ) )
{
const QVariant jsonTextRotate = jsonLayout.value( QStringLiteral( "text-rotate" ) );
switch ( jsonTextRotate.userType() )
{
case QMetaType::Type::Double:
case QMetaType::Type::Int:
{
labelSettings.angleOffset = jsonTextRotate.toDouble();
break;
}

case QMetaType::Type::QVariantList:
case QMetaType::Type::QStringList:
{
const QgsProperty property = parseValueList( jsonTextRotate.toList(), PropertyType::Numeric, context );
ddLabelProperties.setProperty( QgsPalLayerSettings::Property::LabelRotation, property );
break;
}

default:
context.pushWarning( QObject::tr( "%1: Skipping unsupported text-rotate type (%2)" ).arg( context.layerId(), QMetaType::typeName( static_cast<QMetaType::Type>( jsonTextRotate.userType() ) ) ) );
break;
}
}

if ( jsonLayout.contains( QStringLiteral( "text-transform" ) ) )
{
const QString textTransform = jsonLayout.value( QStringLiteral( "text-transform" ) ).toString();
Expand Down
35 changes: 35 additions & 0 deletions tests/src/python/test_qgsmapboxglconverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,41 @@ def testLabelWithField(self):
self.assertFalse(labeling_style.labelSettings().isExpression)
self.assertEqual(labeling_style.labelSettings().fieldName, 'substance')

def testLabelRotation(self):
context = QgsMapBoxGlStyleConversionContext()
style = {
"layout": {
"visibility": "visible",
"text-field": "{substance}",
"text-rotate": 123
},
"paint": {
"text-color": "rgba(47, 47, 47, 1)",
},
"type": "symbol"
}
rendererStyle, has_renderer, labeling_style, has_labeling = QgsMapBoxGlStyleConverter.parseSymbolLayer(style, context)
self.assertTrue(has_labeling)
self.assertEqual(labeling_style.labelSettings().angleOffset, 123)

context = QgsMapBoxGlStyleConversionContext()
style = {
"layout": {
"visibility": "visible",
"text-field": "{substance}",
"text-rotate": ["get", "direction"]
},
"paint": {
"text-color": "rgba(47, 47, 47, 1)",
},
"type": "symbol"
}
rendererStyle, has_renderer, labeling_style, has_labeling = QgsMapBoxGlStyleConverter.parseSymbolLayer(style, context)
self.assertTrue(has_labeling)
ls = labeling_style.labelSettings()
ddp = ls.dataDefinedProperties()
self.assertEqual(ddp.property(QgsPalLayerSettings.Property.LabelRotation).asExpression(), '"direction"')

def test_parse_zoom_levels(self):
context = QgsMapBoxGlStyleConversionContext()
style = {
Expand Down

0 comments on commit 9302008

Please sign in to comment.