Skip to content

Commit 9e736d1

Browse files
authored
Merge pull request #3580 from rldhont/ogcutils_propertyIsLike_attributs-master_2
Backport [BUGFIX] Support OGC PropertyIsLike attributs #3551
2 parents f36b403 + fddd56c commit 9e736d1

File tree

2 files changed

+263
-42
lines changed

2 files changed

+263
-42
lines changed

src/core/qgsogcutils.cpp

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,10 @@ static int binaryOperatorFromTagName( const QString& tagName )
16661666

16671667
static QString binaryOperatorToTagName( QgsExpression::BinaryOperator op )
16681668
{
1669+
if ( op == QgsExpression::boILike )
1670+
{
1671+
return "PropertyIsLike";
1672+
}
16691673
return binaryOperatorsTagNamesMap().key( op, QString() );
16701674
}
16711675

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

1759+
if ( op == QgsExpression::boLike && element.hasAttribute( "matchCase" ) && element.attribute( "matchCase" ) == "false" )
1760+
{
1761+
op = QgsExpression::boILike;
1762+
}
1763+
17551764
QDomElement operandElem = element.firstChildElement();
17561765
QgsExpression::Node *expr = nodeFromOgcFilter( operandElem, errorMessage ), *leftOp = expr;
17571766
if ( !expr )
@@ -1772,6 +1781,64 @@ QgsExpression::NodeBinaryOperator* QgsOgcUtils::nodeBinaryOperatorFromOgcFilter(
17721781
return nullptr;
17731782
}
17741783

1784+
if ( op == QgsExpression::boLike || op == QgsExpression::boILike )
1785+
{
1786+
QString wildCard;
1787+
if ( element.hasAttribute( "wildCard" ) )
1788+
{
1789+
wildCard = element.attribute( "wildCard" );
1790+
}
1791+
QString singleChar;
1792+
if ( element.hasAttribute( "singleChar" ) )
1793+
{
1794+
singleChar = element.attribute( "singleChar" );
1795+
}
1796+
QString escape = "\\";
1797+
if ( element.hasAttribute( "escape" ) )
1798+
{
1799+
escape = element.attribute( "escape" );
1800+
}
1801+
// replace
1802+
QString oprValue = static_cast<const QgsExpression::NodeLiteral*>( opRight )->value().toString();
1803+
if ( !wildCard.isEmpty() && wildCard != "%" )
1804+
{
1805+
oprValue.replace( '%', "\\%" );
1806+
if ( oprValue.startsWith( wildCard ) )
1807+
{
1808+
oprValue.replace( 0, 1, "%" );
1809+
}
1810+
QRegExp rx( "[^" + QRegExp::escape( escape ) + "](" + QRegExp::escape( wildCard ) + ")" );
1811+
int pos = 0;
1812+
while (( pos = rx.indexIn( oprValue, pos ) ) != -1 )
1813+
{
1814+
oprValue.replace( pos + 1, 1, "%" );
1815+
pos += 1;
1816+
}
1817+
oprValue.replace( escape + wildCard, wildCard );
1818+
}
1819+
if ( !singleChar.isEmpty() && singleChar != "_" )
1820+
{
1821+
oprValue.replace( '_', "\\_" );
1822+
if ( oprValue.startsWith( singleChar ) )
1823+
{
1824+
oprValue.replace( 0, 1, "_" );
1825+
}
1826+
QRegExp rx( "[^" + QRegExp::escape( escape ) + "](" + QRegExp::escape( singleChar ) + ")" );
1827+
int pos = 0;
1828+
while (( pos = rx.indexIn( oprValue, pos ) ) != -1 )
1829+
{
1830+
oprValue.replace( pos + 1, 1, "_" );
1831+
pos += 1;
1832+
}
1833+
oprValue.replace( escape + singleChar, singleChar );
1834+
}
1835+
if ( !escape.isEmpty() && escape != "\\" )
1836+
{
1837+
oprValue.replace( escape + escape, escape );
1838+
}
1839+
opRight = new QgsExpression::NodeLiteral( oprValue );
1840+
}
1841+
17751842
expr = new QgsExpression::NodeBinaryOperator( static_cast< QgsExpression::BinaryOperator >( op ), expr, opRight );
17761843
}
17771844

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

2292-
// setup wildcards to <ogc:PropertyIsLike>
2359+
// setup wildCards to <ogc:PropertyIsLike>
22932360
boElem.setAttribute( "wildCard", "%" );
2294-
boElem.setAttribute( "singleChar", "?" );
2361+
boElem.setAttribute( "singleChar", "_" );
22952362
if ( mFilterVersion == QgsOgcUtils::FILTER_OGC_1_0 )
2296-
boElem.setAttribute( "escape", "!" );
2363+
boElem.setAttribute( "escape", "\\" );
22972364
else
2298-
boElem.setAttribute( "escapeChar", "!" );
2365+
boElem.setAttribute( "escapeChar", "\\" );
22992366
}
23002367

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

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

2734-
// setup wildcards to <ogc:PropertyIsLike>
2803+
// setup wildCards to <ogc:PropertyIsLike>
27352804
boElem.setAttribute( "wildCard", "%" );
2736-
boElem.setAttribute( "singleChar", "?" );
2805+
boElem.setAttribute( "singleChar", "_" );
27372806
if ( mFilterVersion == QgsOgcUtils::FILTER_OGC_1_0 )
2738-
boElem.setAttribute( "escape", "!" );
2807+
boElem.setAttribute( "escape", "\\" );
27392808
else
2740-
boElem.setAttribute( "escapeChar", "!" );
2809+
boElem.setAttribute( "escapeChar", "\\" );
27412810
}
27422811

27432812
boElem.appendChild( leftElem );

0 commit comments

Comments
 (0)