Skip to content

Commit

Permalink
[afs] Basic parsing capacity for label expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Dec 11, 2018
1 parent 9d5a221 commit 54f28df
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
21 changes: 19 additions & 2 deletions src/providers/arcgisrest/qgsarcgisrestutils.cpp
Expand Up @@ -893,8 +893,7 @@ QgsAbstractVectorLayerLabeling *QgsArcGisRestUtils::parseEsriLabeling( const QVa
if ( !exp.isValid() ) if ( !exp.isValid() )
where.clear(); where.clear();


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


QgsRuleBasedLabeling::Rule *child = new QgsRuleBasedLabeling::Rule( settings, maxScale, minScale, where, QObject::tr( "ASF label %1" ).arg( i++ ), false ); QgsRuleBasedLabeling::Rule *child = new QgsRuleBasedLabeling::Rule( settings, maxScale, minScale, where, QObject::tr( "ASF label %1" ).arg( i++ ), false );
Expand Down Expand Up @@ -983,6 +982,24 @@ QgsFeatureRenderer *QgsArcGisRestUtils::parseEsriRenderer( const QVariantMap &re
return nullptr; 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 ) QColor QgsArcGisRestUtils::parseEsriColorJson( const QVariant &colorData )
{ {
const QVariantList colorParts = colorData.toList(); const QVariantList colorParts = colorData.toList();
Expand Down
1 change: 1 addition & 0 deletions src/providers/arcgisrest/qgsarcgisrestutils.h
Expand Up @@ -61,6 +61,7 @@ class QgsArcGisRestUtils
static QgsFeatureRenderer *parseEsriRenderer( const QVariantMap &rendererData ); static QgsFeatureRenderer *parseEsriRenderer( const QVariantMap &rendererData );
static QgsAbstractVectorLayerLabeling *parseEsriLabeling( const QVariantList &labelingData ); static QgsAbstractVectorLayerLabeling *parseEsriLabeling( const QVariantList &labelingData );


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


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


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

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

0 comments on commit 54f28df

Please sign in to comment.