Skip to content

Commit

Permalink
Fix geometry tests for NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Jun 14, 2017
1 parent 7f17498 commit 21dae49
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/core/geometry/qgsabstractgeometry.cpp
Expand Up @@ -234,7 +234,7 @@ bool QgsAbstractGeometry::convertTo( QgsWkbTypes::Type type )
}
else if ( !is3D() )
{
addZValue();
addZValue( std::numeric_limits<double>::quiet_NaN() );
}

if ( !needM )
Expand All @@ -243,7 +243,7 @@ bool QgsAbstractGeometry::convertTo( QgsWkbTypes::Type type )
}
else if ( !isMeasure() )
{
addMValue();
addMValue( std::numeric_limits<double>::quiet_NaN() );
}

return true;
Expand Down
10 changes: 5 additions & 5 deletions src/core/geometry/qgslinestring.cpp
Expand Up @@ -592,8 +592,8 @@ void QgsLineString::append( const QgsLineString *line )
}
else
{
// if append line does not have z coordinates, fill with 0 to match number of points in final line
mZ.insert( mZ.count(), mX.size() - mZ.size(), 0 );
// if append line does not have z coordinates, fill with NaN to match number of points in final line
mZ.insert( mZ.count(), mX.size() - mZ.size(), std::numeric_limits<double>::quiet_NaN() );
}
}

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

Expand Down Expand Up @@ -1128,7 +1128,7 @@ bool QgsLineString::convertTo( QgsWkbTypes::Type type )
{
//special handling required for conversion to LineString25D
dropMValue();
addZValue();
addZValue( std::numeric_limits<double>::quiet_NaN() );
mWkbType = QgsWkbTypes::LineString25D;
return true;
}
Expand Down
10 changes: 7 additions & 3 deletions src/core/geometry/qgspoint.cpp
Expand Up @@ -101,9 +101,9 @@ bool QgsPoint::operator==( const QgsPoint &pt ) const
equal &= qgsDoubleNear( pt.x(), mX, 1E-8 );
equal &= qgsDoubleNear( pt.y(), mY, 1E-8 );
if ( QgsWkbTypes::hasZ( type ) )
equal &= qgsDoubleNear( pt.z(), mZ, 1E-8 );
equal &= qgsDoubleNear( pt.z(), mZ, 1E-8 ) || ( qIsNaN( pt.z() ) && qIsNaN( mZ ) );
if ( QgsWkbTypes::hasM( type ) )
equal &= qgsDoubleNear( pt.m(), mM, 1E-8 );
equal &= qgsDoubleNear( pt.m(), mM, 1E-8 ) || ( qIsNaN( pt.m() ) && qIsNaN( mM ) );

return equal;
}
Expand Down Expand Up @@ -546,11 +546,15 @@ double QgsPoint::inclination( const QgsPoint &other ) const

QgsPoint QgsPoint::project( double distance, double azimuth, double inclination ) const
{
QgsWkbTypes::Type pType = mWkbType;
double radsXy = azimuth * M_PI / 180.0;
double dx = 0.0, dy = 0.0, dz = 0.0;

inclination = fmod( inclination, 360.0 );

if ( !qgsDoubleNear( inclination, 90.0 ) )
pType = QgsWkbTypes::addZ( pType );

if ( !is3D() && qgsDoubleNear( inclination, 90.0 ) )
{
dx = distance * sin( radsXy );
Expand All @@ -564,5 +568,5 @@ QgsPoint QgsPoint::project( double distance, double azimuth, double inclination
dz = distance * cos( radsZ );
}

return QgsPoint( mX + dx, mY + dy, mZ + dz, mM, mWkbType );
return QgsPoint( mX + dx, mY + dy, mZ + dz, mM, pType );
}
2 changes: 1 addition & 1 deletion src/core/geometry/qgspoint.h
Expand Up @@ -112,7 +112,7 @@ class CORE_EXPORT QgsPoint: public QgsAbstractGeometry
*
* \note Not available in Python bindings
*/
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;
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;

bool operator==( const QgsPoint &pt ) const;
bool operator!=( const QgsPoint &pt ) const;
Expand Down
59 changes: 41 additions & 18 deletions tests/src/core/testqgsgeometry.cpp
Expand Up @@ -485,6 +485,22 @@ void TestQgsGeometry::point()
QVERIFY( !p8.isMeasure() );
QCOMPARE( p8.wkbType(), QgsWkbTypes::Point25D );

QgsPoint pp( QgsWkbTypes::Point );
QVERIFY( !pp.is3D() );
QVERIFY( !pp.isMeasure() );

QgsPoint ppz( QgsWkbTypes::PointZ );
QVERIFY( ppz.is3D() );
QVERIFY( !ppz.isMeasure() );

QgsPoint ppm( QgsWkbTypes::PointM );
QVERIFY( !ppm.is3D() );
QVERIFY( ppm.isMeasure() );

QgsPoint ppzm( QgsWkbTypes::PointZM );
QVERIFY( ppzm.is3D() );
QVERIFY( ppzm.isMeasure() );

#if 0 //should trigger an assert
//try creating a point with a nonsense WKB type
QgsPoint p9( QgsWkbTypes::PolygonZM, 11.0, 13.0, 9.0, 17.0 );
Expand Down Expand Up @@ -525,6 +541,7 @@ void TestQgsGeometry::point()
QCOMPARE( p10.y(), 3.0 );
//z
p10.setZ( 17.0 );
QCOMPARE( p10.is3D(), true );
QCOMPARE( p10.z(), 17.0 );
QCOMPARE( p10.rz(), 17.0 );
p10.rz() = 13.0;
Expand Down Expand Up @@ -796,12 +813,12 @@ void TestQgsGeometry::point()
p29.setM( 9.0 );
QVERIFY( p29.convertTo( QgsWkbTypes::PointM ) );
QCOMPARE( p29.wkbType(), QgsWkbTypes::PointM );
QCOMPARE( p29.z(), 0.0 );
QVERIFY( qIsNaN( p29.z() ) );
QCOMPARE( p29.m(), 9.0 );
QVERIFY( p29.convertTo( QgsWkbTypes::Point ) );
QCOMPARE( p29.wkbType(), QgsWkbTypes::Point );
QCOMPARE( p29.z(), 0.0 );
QCOMPARE( p29.m(), 0.0 );
QVERIFY( qIsNaN( p29.z() ) );
QVERIFY( qIsNaN( p29.m() ) );
QVERIFY( !p29.convertTo( QgsWkbTypes::Polygon ) );

//boundary
Expand Down Expand Up @@ -833,12 +850,12 @@ void TestQgsGeometry::point()

// distance 3D
QCOMPARE( QgsPoint( 0, 0 ).distanceSquared3D( QgsPoint( 1, 1 ) ), 2.0 );
QCOMPARE( QgsPoint( 0, 0 ).distanceSquared3D( 1, 1, 0 ), 2.0 );
QCOMPARE( QgsPoint( 0, 0 ).distanceSquared3D( QgsPoint( QgsWkbTypes::PointZ, 2, 2, 2, 0 ) ), 12.0 );
QCOMPARE( QgsPoint( 0, 0 ).distanceSquared3D( 2, 2, 2 ), 12.0 );
QCOMPARE( QgsPoint( QgsWkbTypes::PointZ, 2, 2, 2, 0 ).distanceSquared3D( QgsPoint( 1, 1 ) ), 6.0 );
QVERIFY( qIsNaN( QgsPoint( 0, 0 ).distanceSquared3D( 1, 1, 0 ) ) );
QVERIFY( qIsNaN( QgsPoint( 0, 0 ).distanceSquared3D( QgsPoint( QgsWkbTypes::PointZ, 2, 2, 2, 0 ) ) ) );
QVERIFY( qIsNaN( QgsPoint( 0, 0 ).distanceSquared3D( 2, 2, 2 ) ) );
QVERIFY( qIsNaN( QgsPoint( QgsWkbTypes::PointZ, 2, 2, 2, 0 ).distanceSquared3D( QgsPoint( 1, 1 ) ) ) );
QCOMPARE( QgsPoint( QgsWkbTypes::PointZ, 2, 2, 2, 0 ).distanceSquared3D( 1, 1, 0 ), 6.0 );
QCOMPARE( QgsPoint( QgsWkbTypes::PointZ, -2, -2, -2, 0 ).distanceSquared3D( QgsPoint( 0, 0 ) ), 12.0 );
QVERIFY( qIsNaN( QgsPoint( QgsWkbTypes::PointZ, -2, -2, -2, 0 ).distanceSquared3D( QgsPoint( 0, 0 ) ) ) );
QCOMPARE( QgsPoint( QgsWkbTypes::PointZ, -2, -2, -2, 0 ).distanceSquared3D( 0, 0, 0 ), 12.0 );
QCOMPARE( QgsPoint( QgsWkbTypes::PointZ, -2, -2, -2, 0 ).distanceSquared3D( QgsPoint( QgsWkbTypes::PointZ, 2, 2, 2, 0 ) ), 48.0 );
QCOMPARE( QgsPoint( QgsWkbTypes::PointZ, -2, -2, -2, 0 ).distanceSquared3D( 2, 2, 2 ), 48.0 );
Expand Down Expand Up @@ -877,27 +894,33 @@ void TestQgsGeometry::point()
// 2D
QgsPoint p33 = QgsPoint( 1, 2 );
QCOMPARE( p33.project( 1, 0 ), QgsPoint( 1, 3 ) );
QCOMPARE( p33.project( 1, 0, 0 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2, 1 ) );
QCOMPARE( p33.project( 1, 0, 0 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2 ) );
QCOMPARE( p33.project( 1.5, 90 ), QgsPoint( 2.5, 2 ) );
QCOMPARE( p33.project( 1.5, 90, 90 ), QgsPoint( 2.5, 2 ) ); // stay QgsWkbTypes::Point
QCOMPARE( p33.project( 2, 180 ), QgsPoint( 1, 0 ) );
QCOMPARE( p33.project( 2, 180, 180 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2, -2 ) );
QCOMPARE( p33.project( 5, 270 ), QgsPoint( -4, 2 ) );
QCOMPARE( p33.project( 5, 270, 270 ), QgsPoint( QgsWkbTypes::PointZ, 6, 2, 0 ) );
QCOMPARE( p33.project( 6, 360 ), QgsPoint( 1, 8 ) );
QCOMPARE( p33.project( 6, 360, 360 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2, 6 ) );
QCOMPARE( p33.project( 5, 450 ), QgsPoint( 6, 2 ) );
QCOMPARE( p33.project( 5, 450, 450 ), QgsPoint( 6, 2 ) ); // stay QgsWkbTypes::Point
QCOMPARE( p33.project( -1, 0 ), QgsPoint( 1, 1 ) );
QCOMPARE( p33.project( -1, 0, 0 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2, -1 ) );
QCOMPARE( p33.project( 1.5, -90 ), QgsPoint( -0.5, 2 ) );
p33.addZValue( 0 );
QCOMPARE( p33.project( 1, 0, 0 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2, 1 ) );
QCOMPARE( p33.project( 2, 180, 180 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2, -2 ) );
QCOMPARE( p33.project( 5, 270, 270 ), QgsPoint( QgsWkbTypes::PointZ, 6, 2, 0 ) );
QCOMPARE( p33.project( 6, 360, 360 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2, 6 ) );
QCOMPARE( p33.project( -1, 0, 0 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2, -1 ) );
QCOMPARE( p33.project( 1.5, -90, -90 ), QgsPoint( QgsWkbTypes::PointZ, 2.5, 2, 0 ) );

// PointM
p33.dropZValue();
p33.addMValue( 5.0 );
QCOMPARE( p33.project( 1, 0 ), QgsPoint( QgsWkbTypes::PointM, 1, 3, 0, 5 ) );
QCOMPARE( p33.project( 1, 0, 0 ), QgsPoint( QgsWkbTypes::PointZM, 1, 2, 1, 5 ) );
QCOMPARE( p33.project( 5, 450, 450 ), QgsPoint( QgsWkbTypes::PointM, 6, 2, 0, 5 ) );

p33.addZValue( 0 );
QCOMPARE( p33.project( 1, 0, 0 ), QgsPoint( QgsWkbTypes::PointZM, 1, 2, 1, 5 ) );

// 3D
QgsPoint p34 = QgsPoint( QgsWkbTypes::PointZ, 1, 2, 2 );
QCOMPARE( p34.project( 1, 0 ), QgsPoint( QgsWkbTypes::PointZ, 1, 3, 2 ) );
Expand Down Expand Up @@ -1171,7 +1194,7 @@ void TestQgsGeometry::lineString()
QCOMPARE( l7.wkbType(), QgsWkbTypes::LineStringZ );
l7.addVertex( QgsPoint( QgsWkbTypes::Point, 11.0, 12.0 ) ); //add 2d point
QCOMPARE( l7.wkbType(), QgsWkbTypes::LineStringZ ); //should still be 3d
QCOMPARE( l7.pointN( 1 ), QgsPoint( QgsWkbTypes::PointZ, 11.0, 12.0, 0.0 ) );
QCOMPARE( l7.pointN( 1 ), QgsPoint( QgsWkbTypes::PointZ, 11.0, 12.0 ) );
QVERIFY( l7.is3D() );
QCOMPARE( l7.numPoints(), 2 );
QCOMPARE( l7.vertexCount(), 2 );
Expand Down Expand Up @@ -1902,7 +1925,7 @@ void TestQgsGeometry::lineString()
QVERIFY( l24.insertVertex( QgsVertexId( 0, 0, 1 ), QgsPoint( 101, 102 ) ) );
QCOMPARE( l24.numPoints(), 5 );
QCOMPARE( l24.wkbType(), QgsWkbTypes::LineStringZM );
QCOMPARE( l24.pointN( 1 ), QgsPoint( QgsWkbTypes::PointZM, 101, 102, 0, 0 ) );
QCOMPARE( l24.pointN( 1 ), QgsPoint( QgsWkbTypes::PointZM, 101, 102 ) );

//insert 4d vertex in 2d line
l24.setPoints( QgsPointSequence() << QgsPoint( 1, 2 )
Expand Down Expand Up @@ -2133,7 +2156,7 @@ void TestQgsGeometry::lineString()
QCOMPARE( l28d.wkbType(), QgsWkbTypes::LineString );
QVERIFY( l28d.convertTo( QgsWkbTypes::LineStringZ ) );
QCOMPARE( l28d.wkbType(), QgsWkbTypes::LineStringZ );
QCOMPARE( l28d.pointN( 0 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2, 0.0 ) );
QCOMPARE( l28d.pointN( 0 ), QgsPoint( QgsWkbTypes::PointZ, 1, 2 ) );
l28d.setZAt( 0, 5.0 );
QVERIFY( l28d.convertTo( QgsWkbTypes::LineString25D ) );
QCOMPARE( l28d.wkbType(), QgsWkbTypes::LineString25D );
Expand Down Expand Up @@ -2764,7 +2787,7 @@ void TestQgsGeometry::polygon()
QVERIFY( p6c.interiorRing( 1 )->is3D() );
QVERIFY( !p6c.interiorRing( 1 )->isMeasure() );
QCOMPARE( p6c.interiorRing( 1 )->wkbType(), QgsWkbTypes::LineString25D );
QCOMPARE( p6c.interiorRing( 1 )->vertexAt( QgsVertexId( 0, 0, 0 ) ), QgsPoint( QgsWkbTypes::Point25D, 0.1, 0.1, 0, 0 ) );
QCOMPARE( p6c.interiorRing( 1 )->vertexAt( QgsVertexId( 0, 0, 0 ) ), QgsPoint( QgsWkbTypes::Point25D, 0.1, 0.1 ) );

//add curved ring to polygon
circularRing = new QgsCircularString();
Expand Down

0 comments on commit 21dae49

Please sign in to comment.