Skip to content

Commit

Permalink
Merge pull request #38831 from pblottiere/fcngeomz_null
Browse files Browse the repository at this point in the history
Check if a point is 3D before returning its z value
  • Loading branch information
pblottiere committed Sep 30, 2020
2 parents 8351e06 + 1aee462 commit 9cf95ff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -2349,6 +2349,9 @@ static QVariant fcnGeomZ( const QVariantList &values, const QgsExpressionContext
if ( geom.isNull() )
return QVariant(); //or 0?

if ( !geom.constGet()->is3D() )
return QVariant();

//if single point, return the point's z coordinate
if ( geom.type() == QgsWkbTypes::PointGeometry && !geom.isMultipart() )
{
Expand Down Expand Up @@ -2377,6 +2380,9 @@ static QVariant fcnGeomM( const QVariantList &values, const QgsExpressionContext
if ( geom.isNull() )
return QVariant(); //or 0?

if ( !geom.constGet()->isMeasure() )
return QVariant();

//if single point, return the point's m value
if ( geom.type() == QgsWkbTypes::PointGeometry && !geom.isMultipart() )
{
Expand Down
2 changes: 2 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -1159,8 +1159,10 @@ class TestQgsExpression: public QObject
QTest::newRow( "y point" ) << "y(make_point(2.2,4.4))" << false << QVariant( 4.4 );
QTest::newRow( "z point" ) << "z(make_point(2.2,4.4,6.6))" << false << QVariant( 6.6 );
QTest::newRow( "z not point" ) << "z(geom_from_wkt('LINESTRING(2 0,2 2, 3 2, 3 0)'))" << false << QVariant();
QTest::newRow( "z no z coord" ) << "z( geom_from_wkt( 'POINT ( 0 0 )' ) )" << false << QVariant();
QTest::newRow( "m point" ) << "m(make_point_m(2.2,4.4,7.7))" << false << QVariant( 7.7 );
QTest::newRow( "m not point" ) << "m(geom_from_wkt('LINESTRING(2 0,2 2, 3 2, 3 0)'))" << false << QVariant();
QTest::newRow( "m no m coord" ) << "m( geom_from_wkt( 'POINT ( 0 0 )' ) )" << false << QVariant();
QTest::newRow( "x line" ) << "x(geom_from_wkt('LINESTRING(2 0,2 2, 3 2, 3 0)'))" << false << QVariant( 2.5 );
QTest::newRow( "x line" ) << "y(geom_from_wkt('LINESTRING(2 0,2 2, 3 2, 3 0)'))" << false << QVariant( 1.2 );
QTest::newRow( "x polygon" ) << "x(geom_from_wkt('POLYGON((2 0,2 2, 3 2, 3 0, 2 0))'))" << false << QVariant( 2.5 );
Expand Down

0 comments on commit 9cf95ff

Please sign in to comment.