Skip to content

Commit 3b40e2b

Browse files
committedMar 12, 2016
Fix broken $x_at, $y_at functions (fix #14462), add tests
1 parent 51d6f5f commit 3b40e2b

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed
 

‎src/core/qgsexpression.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,17 +1597,22 @@ static QVariant pointAt( const QVariantList& values, const QgsExpressionContext*
15971597
{
15981598
FEAT_FROM_CONTEXT( context, f );
15991599
int idx = getIntValue( values.at( 0 ), parent );
1600-
ENSURE_GEOM_TYPE( f, g, QGis::Line );
1601-
QgsPolyline polyline = g->asPolyline();
1602-
if ( idx < 0 )
1603-
idx += polyline.count();
1600+
const QgsGeometry* g = f.constGeometry();
1601+
if ( !g || g->isEmpty() )
1602+
return QVariant();
16041603

1605-
if ( idx < 0 || idx >= polyline.count() )
1604+
if ( idx < 0 )
1605+
{
1606+
idx += g->geometry()->nCoordinates();
1607+
}
1608+
if ( idx < 0 || idx >= g->geometry()->nCoordinates() )
16061609
{
16071610
parent->setEvalErrorString( QObject::tr( "Index is out of range" ) );
16081611
return QVariant();
16091612
}
1610-
return QVariant( QPointF( polyline[idx].x(), polyline[idx].y() ) );
1613+
1614+
QgsPoint p = g->vertexAt( idx );
1615+
return QVariant( QPointF( p.x(), p.y() ) );
16111616
}
16121617

16131618
static QVariant fcnXat( const QVariantList& values, const QgsExpressionContext* f, QgsExpression* parent )

‎src/core/qgsexpressionlexer.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ col_first [A-Za-z_]|{non_ascii}
106106
col_next [A-Za-z0-9_]|{non_ascii}
107107
column_ref {col_first}{col_next}*
108108

109+
deprecated_function "$"[xXyY]_?[aA][tT]
109110
special_col "$"{column_ref}
110111
variable "@"{column_ref}
111112

@@ -193,6 +194,8 @@ string "'"{str_char}*"'"
193194
194195
{string} { TEXT_FILTER(stripText); return STRING; }
195196
197+
{deprecated_function} { TEXT; return FUNCTION; }
198+
196199
{special_col} { TEXT; return SPECIAL_COL; }
197200
198201
{variable} { TEXT; return VARIABLE; }

‎tests/src/core/testqgsexpression.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,10 +1243,65 @@ class TestQgsExpression: public QObject
12431243
vPerimeter = exp3.evaluate( &context );
12441244
QCOMPARE( vPerimeter.toDouble(), 26. );
12451245

1246+
QgsExpression deprecatedExpXAt( "$x_at(1)" );
1247+
context.setFeature( fPolygon );
1248+
QVariant xAt = deprecatedExpXAt.evaluate( &context );
1249+
QCOMPARE( xAt.toDouble(), 10.0 );
1250+
context.setFeature( fPolyline );
1251+
xAt = deprecatedExpXAt.evaluate( &context );
1252+
QCOMPARE( xAt.toDouble(), 10.0 );
1253+
1254+
QgsExpression deprecatedExpXAtNeg( "$x_at(-2)" );
1255+
context.setFeature( fPolygon );
1256+
xAt = deprecatedExpXAtNeg.evaluate( &context );
1257+
QCOMPARE( xAt.toDouble(), 2.0 );
1258+
1259+
QgsExpression deprecatedExpYAt( "$y_at(2)" );
1260+
context.setFeature( fPolygon );
1261+
QVariant yAt = deprecatedExpYAt.evaluate( &context );
1262+
QCOMPARE( yAt.toDouble(), 6.0 );
1263+
QgsExpression deprecatedExpYAt2( "$y_at(1)" );
1264+
context.setFeature( fPolyline );
1265+
yAt = deprecatedExpYAt2.evaluate( &context );
1266+
QCOMPARE( yAt.toDouble(), 0.0 );
1267+
1268+
QgsExpression deprecatedExpYAtNeg( "$y_at(-2)" );
1269+
context.setFeature( fPolygon );
1270+
yAt = deprecatedExpYAtNeg.evaluate( &context );
1271+
QCOMPARE( yAt.toDouble(), 6.0 );
1272+
1273+
QgsExpression expXAt( "x_at(1)" );
1274+
context.setFeature( fPolygon );
1275+
xAt = expXAt.evaluate( &context );
1276+
QCOMPARE( xAt.toDouble(), 10.0 );
1277+
context.setFeature( fPolyline );
1278+
xAt = expXAt.evaluate( &context );
1279+
QCOMPARE( xAt.toDouble(), 10.0 );
1280+
1281+
QgsExpression expXAtNeg( "x_at(-2)" );
1282+
context.setFeature( fPolygon );
1283+
xAt = expXAtNeg.evaluate( &context );
1284+
QCOMPARE( xAt.toDouble(), 2.0 );
1285+
1286+
QgsExpression expYAt( "y_at(2)" );
1287+
context.setFeature( fPolygon );
1288+
yAt = expYAt.evaluate( &context );
1289+
QCOMPARE( yAt.toDouble(), 6.0 );
1290+
QgsExpression expYAt2( "$y_at(1)" );
1291+
context.setFeature( fPolyline );
1292+
yAt = expYAt2.evaluate( &context );
1293+
QCOMPARE( yAt.toDouble(), 0.0 );
1294+
1295+
QgsExpression expYAtNeg( "y_at(-2)" );
1296+
context.setFeature( fPolygon );
1297+
yAt = expYAtNeg.evaluate( &context );
1298+
QCOMPARE( yAt.toDouble(), 6.0 );
1299+
12461300
QgsExpression exp4( "bounds_width($geometry)" );
12471301
QVariant vBoundsWidth = exp4.evaluate( &fPolygon );
12481302
QCOMPARE( vBoundsWidth.toDouble(), 8.0 );
12491303

1304+
context.setFeature( fPolygon );
12501305
vBoundsWidth = exp4.evaluate( &context );
12511306
QCOMPARE( vBoundsWidth.toDouble(), 8.0 );
12521307

0 commit comments

Comments
 (0)
Please sign in to comment.