Skip to content
Permalink
Browse files

[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.
  • Loading branch information
nyalldawson committed Apr 4, 2018
1 parent 8aa9a82 commit f9783ea5600247441dec998ed8a3929eac890208
Showing with 15 additions and 0 deletions.
  1. +12 −0 src/core/expression/qgsexpressionfunction.cpp
  2. +3 −0 tests/src/core/testqgsexpression.cpp
@@ -2376,6 +2376,17 @@ static QVariant fcnYMax( const QVariantList &values, const QgsExpressionContext
return QVariant::fromValue( geom.boundingBox().yMaximum() );
}

static QVariant fcnFlipCoordinates( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QgsGeometry geom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
if ( geom.isNull() )
return QVariant();

std::unique_ptr< QgsAbstractGeometry > flipped( geom.constGet()->clone() );
flipped->swapXy();
return QVariant::fromValue( QgsGeometry( std::move( flipped ) ) );
}

static QVariant fcnIsClosed( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
@@ -4228,6 +4239,7 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
<< new QgsStaticExpressionFunction( QStringLiteral( "y_max" ), 1, fcnYMax, QStringLiteral( "GeometryGroup" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "ymax" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "geom_from_wkt" ), 1, fcnGeomFromWKT, QStringLiteral( "GeometryGroup" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "geomFromWKT" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "geom_from_gml" ), 1, fcnGeomFromGML, QStringLiteral( "GeometryGroup" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "geomFromGML" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "flip_coordinates" ), 1, fcnFlipCoordinates, QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "relate" ), -1, fcnRelate, QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "intersects_bbox" ), 2, fcnBbox, QStringLiteral( "GeometryGroup" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "bbox" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "disjoint" ), 2, fcnDisjoint, QStringLiteral( "GeometryGroup" ) )
@@ -953,6 +953,9 @@ class TestQgsExpression: public QObject
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 );
QTest::newRow( "hausdorff not geom 1" ) << " hausdorff_distance( 'a',geom_from_wkt('LINESTRING (0 0, 2 0)'))" << true << QVariant();
QTest::newRow( "hausdorff not geom 2" ) << " hausdorff_distance( geom_from_wkt('LINESTRING (0 0, 2 0)'), 'b')" << true << QVariant();
QTest::newRow( "flip_coordinates not geom" ) << "flip_coordinates('g')" << true << QVariant();
QTest::newRow( "flip_coordinates null" ) << "flip_coordinates(NULL)" << false << QVariant();
QTest::newRow( "flip_coordinates point" ) << "geom_to_wkt(flip_coordinates(geom_from_wkt('POINT(1 2)')))" << false << QVariant( "Point (2 1)" );

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

0 comments on commit f9783ea

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