Skip to content
Permalink
Browse files

Merge pull request #3580 from rldhont/ogcutils_propertyIsLike_attribu…

…ts-master_2

Backport [BUGFIX] Support OGC PropertyIsLike attributs #3551
  • Loading branch information
rldhont committed Oct 6, 2016
2 parents f36b403 + fddd56c commit 9e736d19f980223656bd2d7e0b20dde09df007f6
Showing with 263 additions and 42 deletions.
  1. +77 −8 src/core/qgsogcutils.cpp
  2. +186 −34 tests/src/core/testqgsogcutils.cpp
@@ -1666,6 +1666,10 @@ static int binaryOperatorFromTagName( const QString& tagName )

static QString binaryOperatorToTagName( QgsExpression::BinaryOperator op )
{
if ( op == QgsExpression::boILike )
{
return "PropertyIsLike";
}
return binaryOperatorsTagNamesMap().key( op, QString() );
}

@@ -1752,6 +1756,11 @@ QgsExpression::NodeBinaryOperator* QgsOgcUtils::nodeBinaryOperatorFromOgcFilter(
return nullptr;
}

if ( op == QgsExpression::boLike && element.hasAttribute( "matchCase" ) && element.attribute( "matchCase" ) == "false" )
{
op = QgsExpression::boILike;
}

QDomElement operandElem = element.firstChildElement();
QgsExpression::Node *expr = nodeFromOgcFilter( operandElem, errorMessage ), *leftOp = expr;
if ( !expr )
@@ -1772,6 +1781,64 @@ QgsExpression::NodeBinaryOperator* QgsOgcUtils::nodeBinaryOperatorFromOgcFilter(
return nullptr;
}

if ( op == QgsExpression::boLike || op == QgsExpression::boILike )
{
QString wildCard;
if ( element.hasAttribute( "wildCard" ) )
{
wildCard = element.attribute( "wildCard" );
}
QString singleChar;
if ( element.hasAttribute( "singleChar" ) )
{
singleChar = element.attribute( "singleChar" );
}
QString escape = "\\";
if ( element.hasAttribute( "escape" ) )
{
escape = element.attribute( "escape" );
}
// replace
QString oprValue = static_cast<const QgsExpression::NodeLiteral*>( opRight )->value().toString();
if ( !wildCard.isEmpty() && wildCard != "%" )
{
oprValue.replace( '%', "\\%" );
if ( oprValue.startsWith( wildCard ) )
{
oprValue.replace( 0, 1, "%" );
}
QRegExp rx( "[^" + QRegExp::escape( escape ) + "](" + QRegExp::escape( wildCard ) + ")" );
int pos = 0;
while (( pos = rx.indexIn( oprValue, pos ) ) != -1 )
{
oprValue.replace( pos + 1, 1, "%" );
pos += 1;
}
oprValue.replace( escape + wildCard, wildCard );
}
if ( !singleChar.isEmpty() && singleChar != "_" )
{
oprValue.replace( '_', "\\_" );
if ( oprValue.startsWith( singleChar ) )
{
oprValue.replace( 0, 1, "_" );
}
QRegExp rx( "[^" + QRegExp::escape( escape ) + "](" + QRegExp::escape( singleChar ) + ")" );
int pos = 0;
while (( pos = rx.indexIn( oprValue, pos ) ) != -1 )
{
oprValue.replace( pos + 1, 1, "_" );
pos += 1;
}
oprValue.replace( escape + singleChar, singleChar );
}
if ( !escape.isEmpty() && escape != "\\" )
{
oprValue.replace( escape + escape, escape );
}
opRight = new QgsExpression::NodeLiteral( oprValue );
}

expr = new QgsExpression::NodeBinaryOperator( static_cast< QgsExpression::BinaryOperator >( op ), expr, opRight );
}

@@ -2289,13 +2356,13 @@ QDomElement QgsOgcUtilsExprToFilter::expressionBinaryOperatorToOgcFilter( const
if ( op == QgsExpression::boILike )
boElem.setAttribute( "matchCase", "false" );

// setup wildcards to <ogc:PropertyIsLike>
// setup wildCards to <ogc:PropertyIsLike>
boElem.setAttribute( "wildCard", "%" );
boElem.setAttribute( "singleChar", "?" );
boElem.setAttribute( "singleChar", "_" );
if ( mFilterVersion == QgsOgcUtils::FILTER_OGC_1_0 )
boElem.setAttribute( "escape", "!" );
boElem.setAttribute( "escape", "\\" );
else
boElem.setAttribute( "escapeChar", "!" );
boElem.setAttribute( "escapeChar", "\\" );
}

boElem.appendChild( leftElem );
@@ -2716,6 +2783,8 @@ QDomElement QgsOgcUtilsSQLStatementToFilter::toOgcFilter( const QgsSQLStatement:
opText = "PropertyIsGreaterThan";
else if ( op == QgsSQLStatement::boLike )
opText = "PropertyIsLike";
else if ( op == QgsSQLStatement::boILike )
opText = "PropertyIsLike";

if ( opText.isEmpty() )
{
@@ -2731,13 +2800,13 @@ QDomElement QgsOgcUtilsSQLStatementToFilter::toOgcFilter( const QgsSQLStatement:
if ( op == QgsSQLStatement::boILike )
boElem.setAttribute( "matchCase", "false" );

// setup wildcards to <ogc:PropertyIsLike>
// setup wildCards to <ogc:PropertyIsLike>
boElem.setAttribute( "wildCard", "%" );
boElem.setAttribute( "singleChar", "?" );
boElem.setAttribute( "singleChar", "_" );
if ( mFilterVersion == QgsOgcUtils::FILTER_OGC_1_0 )
boElem.setAttribute( "escape", "!" );
boElem.setAttribute( "escape", "\\" );
else
boElem.setAttribute( "escapeChar", "!" );
boElem.setAttribute( "escapeChar", "\\" );
}

boElem.appendChild( leftElem );

0 comments on commit 9e736d1

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