Skip to content
Permalink
Browse files

[afs] Basic parsing capacity for label expressions

  • Loading branch information
nirvn committed Dec 11, 2018
1 parent 9d5a221 commit 54f28dfbb9b994803191de77468d5d5cc8459ff7
@@ -893,8 +893,7 @@ QgsAbstractVectorLayerLabeling *QgsArcGisRestUtils::parseEsriLabeling( const QVa
if ( !exp.isValid() )
where.clear();

QString expression = labeling.value( QStringLiteral( "labelExpression" ) ).toString();
settings->fieldName = expression.replace( '[', '"' ).replace( ']', '"' );
settings->fieldName = parseEsriLabelingExpression( labeling.value( QStringLiteral( "labelExpression" ) ).toString() );
settings->isExpression = true;

QgsRuleBasedLabeling::Rule *child = new QgsRuleBasedLabeling::Rule( settings, maxScale, minScale, where, QObject::tr( "ASF label %1" ).arg( i++ ), false );
@@ -983,6 +982,24 @@ QgsFeatureRenderer *QgsArcGisRestUtils::parseEsriRenderer( const QVariantMap &re
return nullptr;
}

QString QgsArcGisRestUtils::parseEsriLabelingExpression( const QString &string )
{
QString expression = string;

// Replace a few ArcGIS token to QGIS equivalents
expression = expression.replace( QRegularExpression( "(?=([^\"\\\\]*(\\\\.|\"([^\"\\\\]*\\\\.)*[^\"\\\\]*\"))*[^\"]*$)(\\s|^)CONCAT(\\s|$)" ), QStringLiteral( "\\4||\\5" ) );
expression = expression.replace( QRegularExpression( "(?=([^\"\\\\]*(\\\\.|\"([^\"\\\\]*\\\\.)*[^\"\\\\]*\"))*[^\"]*$)(\\s|^)NEWLINE(\\s|$)" ), QStringLiteral( "\\4'\\n'\\5" ) );

// ArcGIS's double quotes are single quotes in QGIS
expression = expression.replace( QRegularExpression( "\"(.*?(?<!\\\\))\"" ), QStringLiteral( "'\\1'" ) );
expression = expression.replace( QRegularExpression( "\\\\\"" ), QStringLiteral( "\"" ) );

// ArcGIS's square brakets are double quotes in QGIS
expression = expression.replace( QRegularExpression( "\\[([^]]*)\\]" ), QStringLiteral( "\"\\1\"" ) );

return expression;
}

QColor QgsArcGisRestUtils::parseEsriColorJson( const QVariant &colorData )
{
const QVariantList colorParts = colorData.toList();
@@ -61,6 +61,7 @@ class QgsArcGisRestUtils
static QgsFeatureRenderer *parseEsriRenderer( const QVariantMap &rendererData );
static QgsAbstractVectorLayerLabeling *parseEsriLabeling( const QVariantList &labelingData );

static QString parseEsriLabelingExpression( const QString &string );
static QColor parseEsriColorJson( const QVariant &colorData );
static Qt::PenStyle parseEsriLineStyle( const QString &style );
static Qt::BrushStyle parseEsriFillStyle( const QString &style );
@@ -497,7 +497,7 @@ void TestQgsArcGisRestUtils::testParseLabeling()
"},{"
"\"labelPlacement\": \"esriServerPointLabelPlacementAboveRight\","
"\"where\": \"1_testing broken where string\","
"\"labelExpression\": \"[Name]\","
"\"labelExpression\": \"\\\"Name: \\\" CONCAT [Name] CONCAT NEWLINE CONCAT [Size]\","
"\"useCodedValues\": true,"
"\"symbol\": {"
"\"type\": \"esriTS\","
@@ -556,6 +556,7 @@ void TestQgsArcGisRestUtils::testParseLabeling()
QVERIFY( settings );
QCOMPARE( settings->placement, QgsPalLayerSettings::OverPoint );
QCOMPARE( settings->quadOffset, QgsPalLayerSettings::QuadrantAboveRight );
QCOMPARE( settings->fieldName, QStringLiteral( "\"Name\"" ) );

QgsTextFormat textFormat = settings->format();
QCOMPARE( textFormat.color(), QColor( 255, 0, 0 ) );
@@ -564,6 +565,8 @@ void TestQgsArcGisRestUtils::testParseLabeling()

settings = children.at( 1 )->settings();
QVERIFY( settings );
QCOMPARE( settings->fieldName, QStringLiteral( "'Name: ' || \"Name\" || '\\n' || \"Size\"" ) );

textFormat = settings->format();
QCOMPARE( textFormat.buffer().enabled(), true );
QCOMPARE( textFormat.buffer().color(), QColor( 255, 255, 255 ) );

0 comments on commit 54f28df

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