Skip to content
Permalink
Browse files

Fix broken $x_at, $y_at functions (fix #14462), add tests

(cherry-picked from 3b40e2b)
  • Loading branch information
nyalldawson committed Mar 12, 2016
1 parent 875a464 commit 35ceacc849a5b8b6bdd32b4452d77b34c745d482
Showing with 69 additions and 6 deletions.
  1. +11 −6 src/core/qgsexpression.cpp
  2. +3 −0 src/core/qgsexpressionlexer.ll
  3. +55 −0 tests/src/core/testqgsexpression.cpp
@@ -1597,17 +1597,22 @@ static QVariant pointAt( const QVariantList& values, const QgsExpressionContext*
{
FEAT_FROM_CONTEXT( context, f );
int idx = getIntValue( values.at( 0 ), parent );
ENSURE_GEOM_TYPE( f, g, QGis::Line );
QgsPolyline polyline = g->asPolyline();
if ( idx < 0 )
idx += polyline.count();
const QgsGeometry* g = f.constGeometry();
if ( !g || g->isEmpty() )
return QVariant();

if ( idx < 0 || idx >= polyline.count() )
if ( idx < 0 )
{
idx += g->geometry()->nCoordinates();
}
if ( idx < 0 || idx >= g->geometry()->nCoordinates() )
{
parent->setEvalErrorString( QObject::tr( "Index is out of range" ) );
return QVariant();
}
return QVariant( QPointF( polyline[idx].x(), polyline[idx].y() ) );

QgsPoint p = g->vertexAt( idx );
return QVariant( QPointF( p.x(), p.y() ) );
}

static QVariant fcnXat( const QVariantList& values, const QgsExpressionContext* f, QgsExpression* parent )
@@ -106,6 +106,7 @@ col_first [A-Za-z_]|{non_ascii}
col_next [A-Za-z0-9_]|{non_ascii}
column_ref {col_first}{col_next}*

deprecated_function "$"[xXyY]_?[aA][tT]
special_col "$"{column_ref}
variable "@"{column_ref}

@@ -193,6 +194,8 @@ string "'"{str_char}*"'"
{string} { TEXT_FILTER(stripText); return STRING; }
{deprecated_function} { TEXT; return FUNCTION; }
{special_col} { TEXT; return SPECIAL_COL; }
{variable} { TEXT; return VARIABLE; }
@@ -1243,10 +1243,65 @@ class TestQgsExpression: public QObject
vPerimeter = exp3.evaluate( &context );
QCOMPARE( vPerimeter.toDouble(), 26. );

QgsExpression deprecatedExpXAt( "$x_at(1)" );
context.setFeature( fPolygon );
QVariant xAt = deprecatedExpXAt.evaluate( &context );
QCOMPARE( xAt.toDouble(), 10.0 );
context.setFeature( fPolyline );
xAt = deprecatedExpXAt.evaluate( &context );
QCOMPARE( xAt.toDouble(), 10.0 );

QgsExpression deprecatedExpXAtNeg( "$x_at(-2)" );
context.setFeature( fPolygon );
xAt = deprecatedExpXAtNeg.evaluate( &context );
QCOMPARE( xAt.toDouble(), 2.0 );

QgsExpression deprecatedExpYAt( "$y_at(2)" );
context.setFeature( fPolygon );
QVariant yAt = deprecatedExpYAt.evaluate( &context );
QCOMPARE( yAt.toDouble(), 6.0 );
QgsExpression deprecatedExpYAt2( "$y_at(1)" );
context.setFeature( fPolyline );
yAt = deprecatedExpYAt2.evaluate( &context );
QCOMPARE( yAt.toDouble(), 0.0 );

QgsExpression deprecatedExpYAtNeg( "$y_at(-2)" );
context.setFeature( fPolygon );
yAt = deprecatedExpYAtNeg.evaluate( &context );
QCOMPARE( yAt.toDouble(), 6.0 );

QgsExpression expXAt( "x_at(1)" );
context.setFeature( fPolygon );
xAt = expXAt.evaluate( &context );
QCOMPARE( xAt.toDouble(), 10.0 );
context.setFeature( fPolyline );
xAt = expXAt.evaluate( &context );
QCOMPARE( xAt.toDouble(), 10.0 );

QgsExpression expXAtNeg( "x_at(-2)" );
context.setFeature( fPolygon );
xAt = expXAtNeg.evaluate( &context );
QCOMPARE( xAt.toDouble(), 2.0 );

QgsExpression expYAt( "y_at(2)" );
context.setFeature( fPolygon );
yAt = expYAt.evaluate( &context );
QCOMPARE( yAt.toDouble(), 6.0 );
QgsExpression expYAt2( "$y_at(1)" );
context.setFeature( fPolyline );
yAt = expYAt2.evaluate( &context );
QCOMPARE( yAt.toDouble(), 0.0 );

QgsExpression expYAtNeg( "y_at(-2)" );
context.setFeature( fPolygon );
yAt = expYAtNeg.evaluate( &context );
QCOMPARE( yAt.toDouble(), 6.0 );

QgsExpression exp4( "bounds_width($geometry)" );
QVariant vBoundsWidth = exp4.evaluate( &fPolygon );
QCOMPARE( vBoundsWidth.toDouble(), 8.0 );

context.setFeature( fPolygon );
vBoundsWidth = exp4.evaluate( &context );
QCOMPARE( vBoundsWidth.toDouble(), 8.0 );

0 comments on commit 35ceacc

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