Skip to content

Commit

Permalink
Fix broken $x_at, $y_at functions (fix #14462), add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 12, 2016
1 parent 51d6f5f commit 3b40e2b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/core/qgsexpression.cpp
Expand Up @@ -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 )
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsexpressionlexer.ll
Expand Up @@ -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}

Expand Down Expand Up @@ -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; }
Expand Down
55 changes: 55 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -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 );

Expand Down

0 comments on commit 3b40e2b

Please sign in to comment.