Skip to content

Commit 21dae49

Browse files
committed
Fix geometry tests for NaN
1 parent 7f17498 commit 21dae49

File tree

5 files changed

+56
-29
lines changed

5 files changed

+56
-29
lines changed

src/core/geometry/qgsabstractgeometry.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ bool QgsAbstractGeometry::convertTo( QgsWkbTypes::Type type )
234234
}
235235
else if ( !is3D() )
236236
{
237-
addZValue();
237+
addZValue( std::numeric_limits<double>::quiet_NaN() );
238238
}
239239

240240
if ( !needM )
@@ -243,7 +243,7 @@ bool QgsAbstractGeometry::convertTo( QgsWkbTypes::Type type )
243243
}
244244
else if ( !isMeasure() )
245245
{
246-
addMValue();
246+
addMValue( std::numeric_limits<double>::quiet_NaN() );
247247
}
248248

249249
return true;

src/core/geometry/qgslinestring.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,8 @@ void QgsLineString::append( const QgsLineString *line )
592592
}
593593
else
594594
{
595-
// if append line does not have z coordinates, fill with 0 to match number of points in final line
596-
mZ.insert( mZ.count(), mX.size() - mZ.size(), 0 );
595+
// if append line does not have z coordinates, fill with NaN to match number of points in final line
596+
mZ.insert( mZ.count(), mX.size() - mZ.size(), std::numeric_limits<double>::quiet_NaN() );
597597
}
598598
}
599599

@@ -605,8 +605,8 @@ void QgsLineString::append( const QgsLineString *line )
605605
}
606606
else
607607
{
608-
// if append line does not have m values, fill with 0 to match number of points in final line
609-
mM.insert( mM.count(), mX.size() - mM.size(), 0 );
608+
// if append line does not have m values, fill with NaN to match number of points in final line
609+
mM.insert( mM.count(), mX.size() - mM.size(), std::numeric_limits<double>::quiet_NaN() );
610610
}
611611
}
612612

@@ -1128,7 +1128,7 @@ bool QgsLineString::convertTo( QgsWkbTypes::Type type )
11281128
{
11291129
//special handling required for conversion to LineString25D
11301130
dropMValue();
1131-
addZValue();
1131+
addZValue( std::numeric_limits<double>::quiet_NaN() );
11321132
mWkbType = QgsWkbTypes::LineString25D;
11331133
return true;
11341134
}

src/core/geometry/qgspoint.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ bool QgsPoint::operator==( const QgsPoint &pt ) const
101101
equal &= qgsDoubleNear( pt.x(), mX, 1E-8 );
102102
equal &= qgsDoubleNear( pt.y(), mY, 1E-8 );
103103
if ( QgsWkbTypes::hasZ( type ) )
104-
equal &= qgsDoubleNear( pt.z(), mZ, 1E-8 );
104+
equal &= qgsDoubleNear( pt.z(), mZ, 1E-8 ) || ( qIsNaN( pt.z() ) && qIsNaN( mZ ) );
105105
if ( QgsWkbTypes::hasM( type ) )
106-
equal &= qgsDoubleNear( pt.m(), mM, 1E-8 );
106+
equal &= qgsDoubleNear( pt.m(), mM, 1E-8 ) || ( qIsNaN( pt.m() ) && qIsNaN( mM ) );
107107

108108
return equal;
109109
}
@@ -546,11 +546,15 @@ double QgsPoint::inclination( const QgsPoint &other ) const
546546

547547
QgsPoint QgsPoint::project( double distance, double azimuth, double inclination ) const
548548
{
549+
QgsWkbTypes::Type pType = mWkbType;
549550
double radsXy = azimuth * M_PI / 180.0;
550551
double dx = 0.0, dy = 0.0, dz = 0.0;
551552

552553
inclination = fmod( inclination, 360.0 );
553554

555+
if ( !qgsDoubleNear( inclination, 90.0 ) )
556+
pType = QgsWkbTypes::addZ( pType );
557+
554558
if ( !is3D() && qgsDoubleNear( inclination, 90.0 ) )
555559
{
556560
dx = distance * sin( radsXy );
@@ -564,5 +568,5 @@ QgsPoint QgsPoint::project( double distance, double azimuth, double inclination
564568
dz = distance * cos( radsZ );
565569
}
566570

567-
return QgsPoint( mX + dx, mY + dy, mZ + dz, mM, mWkbType );
571+
return QgsPoint( mX + dx, mY + dy, mZ + dz, mM, pType );
568572
}

src/core/geometry/qgspoint.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class CORE_EXPORT QgsPoint: public QgsAbstractGeometry
112112
*
113113
* \note Not available in Python bindings
114114
*/
115-
explicit QgsPoint( QgsWkbTypes::Type wkbType, double x, double y, double z = std::numeric_limits<double>::quiet_NaN(), double m = std::numeric_limits<double>::quiet_NaN() ) SIP_SKIP;
115+
explicit QgsPoint( QgsWkbTypes::Type wkbType, double x = 0.0, double y = 0.0, double z = std::numeric_limits<double>::quiet_NaN(), double m = std::numeric_limits<double>::quiet_NaN() ) SIP_SKIP;
116116

117117
bool operator==( const QgsPoint &pt ) const;
118118
bool operator!=( const QgsPoint &pt ) const;

tests/src/core/testqgsgeometry.cpp

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,22 @@ void TestQgsGeometry::point()
485485
QVERIFY( !p8.isMeasure() );
486486
QCOMPARE( p8.wkbType(), QgsWkbTypes::Point25D );
487487

488+
QgsPoint pp( QgsWkbTypes::Point );
489+
QVERIFY( !pp.is3D() );
490+
QVERIFY( !pp.isMeasure() );
491+
492+
QgsPoint ppz( QgsWkbTypes::PointZ );
493+
QVERIFY( ppz.is3D() );
494+
QVERIFY( !ppz.isMeasure() );
495+
496+
QgsPoint ppm( QgsWkbTypes::PointM );
497+
QVERIFY( !ppm.is3D() );
498+
QVERIFY( ppm.isMeasure() );
499+
500+
QgsPoint ppzm( QgsWkbTypes::PointZM );
501+
QVERIFY( ppzm.is3D() );
502+
QVERIFY( ppzm.isMeasure() );
503+
488504
#if 0 //should trigger an assert
489505
//try creating a point with a nonsense WKB type
490506
QgsPoint p9( QgsWkbTypes::PolygonZM, 11.0, 13.0, 9.0, 17.0 );
@@ -525,6 +541,7 @@ void TestQgsGeometry::point()
525541
QCOMPARE( p10.y(), 3.0 );
526542
//z
527543
p10.setZ( 17.0 );
544+
QCOMPARE( p10.is3D(), true );
528545
QCOMPARE( p10.z(), 17.0 );
529546
QCOMPARE( p10.rz(), 17.0 );
530547
p10.rz() = 13.0;
@@ -796,12 +813,12 @@ void TestQgsGeometry::point()
796813
p29.setM( 9.0 );
797814
QVERIFY( p29.convertTo( QgsWkbTypes::PointM ) );
798815
QCOMPARE( p29.wkbType(), QgsWkbTypes::PointM );
799-
QCOMPARE( p29.z(), 0.0 );
816+
QVERIFY( qIsNaN( p29.z() ) );
800817
QCOMPARE( p29.m(), 9.0 );
801818
QVERIFY( p29.convertTo( QgsWkbTypes::Point ) );
802819
QCOMPARE( p29.wkbType(), QgsWkbTypes::Point );
803-
QCOMPARE( p29.z(), 0.0 );
804-
QCOMPARE( p29.m(), 0.0 );
820+
QVERIFY( qIsNaN( p29.z() ) );
821+
QVERIFY( qIsNaN( p29.m() ) );
805822
QVERIFY( !p29.convertTo( QgsWkbTypes::Polygon ) );
806823

807824
//boundary
@@ -833,12 +850,12 @@ void TestQgsGeometry::point()
833850

834851
// distance 3D
835852
QCOMPARE( QgsPoint( 0, 0 ).distanceSquared3D( QgsPoint( 1, 1 ) ), 2.0 );
836-
QCOMPARE( QgsPoint( 0, 0 ).distanceSquared3D( 1, 1, 0 ), 2.0 );
837-
QCOMPARE( QgsPoint( 0, 0 ).distanceSquared3D( QgsPoint( QgsWkbTypes::PointZ, 2, 2, 2, 0 ) ), 12.0 );
838-
QCOMPARE( QgsPoint( 0, 0 ).distanceSquared3D( 2, 2, 2 ), 12.0 );
839-
QCOMPARE( QgsPoint( QgsWkbTypes::PointZ, 2, 2, 2, 0 ).distanceSquared3D( QgsPoint( 1, 1 ) ), 6.0 );
853+
QVERIFY( qIsNaN( QgsPoint( 0, 0 ).distanceSquared3D( 1, 1, 0 ) ) );
854+
QVERIFY( qIsNaN( QgsPoint( 0, 0 ).distanceSquared3D( QgsPoint( QgsWkbTypes::PointZ, 2, 2, 2, 0 ) ) ) );
855+
QVERIFY( qIsNaN( QgsPoint( 0, 0 ).distanceSquared3D( 2, 2, 2 ) ) );
856+
QVERIFY( qIsNaN( QgsPoint( QgsWkbTypes::PointZ, 2, 2, 2, 0 ).distanceSquared3D( QgsPoint( 1, 1 ) ) ) );
840857
QCOMPARE( QgsPoint( QgsWkbTypes::PointZ, 2, 2, 2, 0 ).distanceSquared3D( 1, 1, 0 ), 6.0 );
841-
QCOMPARE( QgsPoint( QgsWkbTypes::PointZ, -2, -2, -2, 0 ).distanceSquared3D( QgsPoint( 0, 0 ) ), 12.0 );
858+
QVERIFY( qIsNaN( QgsPoint( QgsWkbTypes::PointZ, -2, -2, -2, 0 ).distanceSquared3D( QgsPoint( 0, 0 ) ) ) );
842859
QCOMPARE( QgsPoint( QgsWkbTypes::PointZ, -2, -2, -2, 0 ).distanceSquared3D( 0, 0, 0 ), 12.0 );
843860
QCOMPARE( QgsPoint( QgsWkbTypes::PointZ, -2, -2, -2, 0 ).distanceSquared3D( QgsPoint( QgsWkbTypes::PointZ, 2, 2, 2, 0 ) ), 48.0 );
844861
QCOMPARE( QgsPoint( QgsWkbTypes::PointZ, -2, -2, -2, 0 ).distanceSquared3D( 2, 2, 2 ), 48.0 );
@@ -877,27 +894,33 @@ void TestQgsGeometry::point()
877894
// 2D
878895
QgsPoint p33 = QgsPoint( 1, 2 );
879896
QCOMPARE( p33.project( 1, 0 ), QgsPoint( 1, 3 ) );
880-
QCOMPARE( p33.project( 1, 0, 0 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2, 1 ) );
897+
QCOMPARE( p33.project( 1, 0, 0 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2 ) );
881898
QCOMPARE( p33.project( 1.5, 90 ), QgsPoint( 2.5, 2 ) );
882899
QCOMPARE( p33.project( 1.5, 90, 90 ), QgsPoint( 2.5, 2 ) ); // stay QgsWkbTypes::Point
883900
QCOMPARE( p33.project( 2, 180 ), QgsPoint( 1, 0 ) );
884-
QCOMPARE( p33.project( 2, 180, 180 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2, -2 ) );
885901
QCOMPARE( p33.project( 5, 270 ), QgsPoint( -4, 2 ) );
886-
QCOMPARE( p33.project( 5, 270, 270 ), QgsPoint( QgsWkbTypes::PointZ, 6, 2, 0 ) );
887902
QCOMPARE( p33.project( 6, 360 ), QgsPoint( 1, 8 ) );
888-
QCOMPARE( p33.project( 6, 360, 360 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2, 6 ) );
889903
QCOMPARE( p33.project( 5, 450 ), QgsPoint( 6, 2 ) );
890904
QCOMPARE( p33.project( 5, 450, 450 ), QgsPoint( 6, 2 ) ); // stay QgsWkbTypes::Point
891905
QCOMPARE( p33.project( -1, 0 ), QgsPoint( 1, 1 ) );
892-
QCOMPARE( p33.project( -1, 0, 0 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2, -1 ) );
893906
QCOMPARE( p33.project( 1.5, -90 ), QgsPoint( -0.5, 2 ) );
907+
p33.addZValue( 0 );
908+
QCOMPARE( p33.project( 1, 0, 0 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2, 1 ) );
909+
QCOMPARE( p33.project( 2, 180, 180 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2, -2 ) );
910+
QCOMPARE( p33.project( 5, 270, 270 ), QgsPoint( QgsWkbTypes::PointZ, 6, 2, 0 ) );
911+
QCOMPARE( p33.project( 6, 360, 360 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2, 6 ) );
912+
QCOMPARE( p33.project( -1, 0, 0 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2, -1 ) );
894913
QCOMPARE( p33.project( 1.5, -90, -90 ), QgsPoint( QgsWkbTypes::PointZ, 2.5, 2, 0 ) );
914+
895915
// PointM
916+
p33.dropZValue();
896917
p33.addMValue( 5.0 );
897918
QCOMPARE( p33.project( 1, 0 ), QgsPoint( QgsWkbTypes::PointM, 1, 3, 0, 5 ) );
898-
QCOMPARE( p33.project( 1, 0, 0 ), QgsPoint( QgsWkbTypes::PointZM, 1, 2, 1, 5 ) );
899919
QCOMPARE( p33.project( 5, 450, 450 ), QgsPoint( QgsWkbTypes::PointM, 6, 2, 0, 5 ) );
900920

921+
p33.addZValue( 0 );
922+
QCOMPARE( p33.project( 1, 0, 0 ), QgsPoint( QgsWkbTypes::PointZM, 1, 2, 1, 5 ) );
923+
901924
// 3D
902925
QgsPoint p34 = QgsPoint( QgsWkbTypes::PointZ, 1, 2, 2 );
903926
QCOMPARE( p34.project( 1, 0 ), QgsPoint( QgsWkbTypes::PointZ, 1, 3, 2 ) );
@@ -1171,7 +1194,7 @@ void TestQgsGeometry::lineString()
11711194
QCOMPARE( l7.wkbType(), QgsWkbTypes::LineStringZ );
11721195
l7.addVertex( QgsPoint( QgsWkbTypes::Point, 11.0, 12.0 ) ); //add 2d point
11731196
QCOMPARE( l7.wkbType(), QgsWkbTypes::LineStringZ ); //should still be 3d
1174-
QCOMPARE( l7.pointN( 1 ), QgsPoint( QgsWkbTypes::PointZ, 11.0, 12.0, 0.0 ) );
1197+
QCOMPARE( l7.pointN( 1 ), QgsPoint( QgsWkbTypes::PointZ, 11.0, 12.0 ) );
11751198
QVERIFY( l7.is3D() );
11761199
QCOMPARE( l7.numPoints(), 2 );
11771200
QCOMPARE( l7.vertexCount(), 2 );
@@ -1902,7 +1925,7 @@ void TestQgsGeometry::lineString()
19021925
QVERIFY( l24.insertVertex( QgsVertexId( 0, 0, 1 ), QgsPoint( 101, 102 ) ) );
19031926
QCOMPARE( l24.numPoints(), 5 );
19041927
QCOMPARE( l24.wkbType(), QgsWkbTypes::LineStringZM );
1905-
QCOMPARE( l24.pointN( 1 ), QgsPoint( QgsWkbTypes::PointZM, 101, 102, 0, 0 ) );
1928+
QCOMPARE( l24.pointN( 1 ), QgsPoint( QgsWkbTypes::PointZM, 101, 102 ) );
19061929

19071930
//insert 4d vertex in 2d line
19081931
l24.setPoints( QgsPointSequence() << QgsPoint( 1, 2 )
@@ -2133,7 +2156,7 @@ void TestQgsGeometry::lineString()
21332156
QCOMPARE( l28d.wkbType(), QgsWkbTypes::LineString );
21342157
QVERIFY( l28d.convertTo( QgsWkbTypes::LineStringZ ) );
21352158
QCOMPARE( l28d.wkbType(), QgsWkbTypes::LineStringZ );
2136-
QCOMPARE( l28d.pointN( 0 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2, 0.0 ) );
2159+
QCOMPARE( l28d.pointN( 0 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2 ) );
21372160
l28d.setZAt( 0, 5.0 );
21382161
QVERIFY( l28d.convertTo( QgsWkbTypes::LineString25D ) );
21392162
QCOMPARE( l28d.wkbType(), QgsWkbTypes::LineString25D );
@@ -2764,7 +2787,7 @@ void TestQgsGeometry::polygon()
27642787
QVERIFY( p6c.interiorRing( 1 )->is3D() );
27652788
QVERIFY( !p6c.interiorRing( 1 )->isMeasure() );
27662789
QCOMPARE( p6c.interiorRing( 1 )->wkbType(), QgsWkbTypes::LineString25D );
2767-
QCOMPARE( p6c.interiorRing( 1 )->vertexAt( QgsVertexId( 0, 0, 0 ) ), QgsPoint( QgsWkbTypes::Point25D, 0.1, 0.1, 0, 0 ) );
2790+
QCOMPARE( p6c.interiorRing( 1 )->vertexAt( QgsVertexId( 0, 0, 0 ) ), QgsPoint( QgsWkbTypes::Point25D, 0.1, 0.1 ) );
27682791

27692792
//add curved ring to polygon
27702793
circularRing = new QgsCircularString();

0 commit comments

Comments
 (0)