Skip to content

Commit f9783ea

Browse files
committed
[FEATURE] Add flip_coordinates expression function
Returns a copy of the geometry with the x and y coordinates swapped. Useful for repairing geometries which have had their latitude and longitude values reversed.
1 parent 8aa9a82 commit f9783ea

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/core/expression/qgsexpressionfunction.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,6 +2376,17 @@ static QVariant fcnYMax( const QVariantList &values, const QgsExpressionContext
23762376
return QVariant::fromValue( geom.boundingBox().yMaximum() );
23772377
}
23782378

2379+
static QVariant fcnFlipCoordinates( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
2380+
{
2381+
QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2382+
if ( geom.isNull() )
2383+
return QVariant();
2384+
2385+
std::unique_ptr< QgsAbstractGeometry > flipped( geom.constGet()->clone() );
2386+
flipped->swapXy();
2387+
return QVariant::fromValue( QgsGeometry( std::move( flipped ) ) );
2388+
}
2389+
23792390
static QVariant fcnIsClosed( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
23802391
{
23812392
QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
@@ -4228,6 +4239,7 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
42284239
<< new QgsStaticExpressionFunction( QStringLiteral( "y_max" ), 1, fcnYMax, QStringLiteral( "GeometryGroup" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "ymax" ) )
42294240
<< new QgsStaticExpressionFunction( QStringLiteral( "geom_from_wkt" ), 1, fcnGeomFromWKT, QStringLiteral( "GeometryGroup" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "geomFromWKT" ) )
42304241
<< new QgsStaticExpressionFunction( QStringLiteral( "geom_from_gml" ), 1, fcnGeomFromGML, QStringLiteral( "GeometryGroup" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "geomFromGML" ) )
4242+
<< new QgsStaticExpressionFunction( QStringLiteral( "flip_coordinates" ), 1, fcnFlipCoordinates, QStringLiteral( "GeometryGroup" ) )
42314243
<< new QgsStaticExpressionFunction( QStringLiteral( "relate" ), -1, fcnRelate, QStringLiteral( "GeometryGroup" ) )
42324244
<< new QgsStaticExpressionFunction( QStringLiteral( "intersects_bbox" ), 2, fcnBbox, QStringLiteral( "GeometryGroup" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "bbox" ) )
42334245
<< new QgsStaticExpressionFunction( QStringLiteral( "disjoint" ), 2, fcnDisjoint, QStringLiteral( "GeometryGroup" ) )

tests/src/core/testqgsexpression.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,9 @@ class TestQgsExpression: public QObject
953953
QTest::newRow( "hausdorff line to line densify" ) << " round(hausdorff_distance( geom_from_wkt('LINESTRING (130 0, 0 0, 0 150)'),geom_from_wkt('LINESTRING (10 10, 10 150, 130 10)'),0.5))" << false << QVariant( 70 );
954954
QTest::newRow( "hausdorff not geom 1" ) << " hausdorff_distance( 'a',geom_from_wkt('LINESTRING (0 0, 2 0)'))" << true << QVariant();
955955
QTest::newRow( "hausdorff not geom 2" ) << " hausdorff_distance( geom_from_wkt('LINESTRING (0 0, 2 0)'), 'b')" << true << QVariant();
956+
QTest::newRow( "flip_coordinates not geom" ) << "flip_coordinates('g')" << true << QVariant();
957+
QTest::newRow( "flip_coordinates null" ) << "flip_coordinates(NULL)" << false << QVariant();
958+
QTest::newRow( "flip_coordinates point" ) << "geom_to_wkt(flip_coordinates(geom_from_wkt('POINT(1 2)')))" << false << QVariant( "Point (2 1)" );
956959

957960
// string functions
958961
QTest::newRow( "lower" ) << "lower('HeLLo')" << false << QVariant( "hello" );

0 commit comments

Comments
 (0)