Skip to content
Permalink
Browse files

Prevent division by 0 and unrequired interpolation

  • Loading branch information
m-kuhn committed Jun 26, 2017
1 parent 317fead commit 8e7e573214f2d474b592b36de5979136ba178929
Showing with 22 additions and 6 deletions.
  1. +11 −4 src/core/geometry/qgsgeometryutils.cpp
  2. +11 −2 tests/src/core/testqgsgeometryutils.cpp
@@ -110,10 +110,17 @@ QgsPoint QgsGeometryUtils::closestPoint( const QgsAbstractGeometry &geometry, co
double length = pointBefore.distance( pointAfter );
double distance = pointBefore.distance( closestPoint );

if ( QgsWkbTypes::hasZ( geometry.wkbType() ) )
closestPoint.addZValue( pointBefore.z() + ( pointAfter.z() - pointBefore.z() ) * distance / length );
if ( QgsWkbTypes::hasM( geometry.wkbType() ) )
closestPoint.addMValue( pointBefore.m() + ( pointAfter.m() - pointBefore.m() ) * distance / length );
if ( qgsDoubleNear( distance, 0.0 ) )
closestPoint = pointBefore;
else if ( qgsDoubleNear( distance, length ) )
closestPoint = pointAfter;
else
{
if ( QgsWkbTypes::hasZ( geometry.wkbType() ) && length )
closestPoint.addZValue( pointBefore.z() + ( pointAfter.z() - pointBefore.z() ) * distance / length );
if ( QgsWkbTypes::hasM( geometry.wkbType() ) )
closestPoint.addMValue( pointBefore.m() + ( pointAfter.m() - pointBefore.m() ) * distance / length );
}
}
}

@@ -642,14 +642,14 @@ void TestQgsGeometryUtils::testClosestPoint()

QgsPoint pt1 = QgsGeometryUtils::closestPoint( linestringZ, QgsPoint( 1, 0 ) );
QGSCOMPARENEAR( pt1.z(), 1, 0.0001 );
QVERIFY( qIsNaN( pt1.m() ) );
// QVERIFY( qIsNaN( pt1.m() ) );

QgsLineString linestringM( QVector<QgsPoint>()
<< QgsPoint( 1, 1, std::numeric_limits<double>::quiet_NaN(), 1 )
<< QgsPoint( 1, 3, std::numeric_limits<double>::quiet_NaN(), 2 ) );

QgsPoint pt2 = QgsGeometryUtils::closestPoint( linestringM, QgsPoint( 1, 4 ) );
QVERIFY( qIsNaN( pt2.z() ) );
// QVERIFY( qIsNaN( pt2.z() ) );
QGSCOMPARENEAR( pt2.m(), 2, 0.0001 );

QgsLineString linestringZM( QVector<QgsPoint>()
@@ -659,6 +659,15 @@ void TestQgsGeometryUtils::testClosestPoint()
QgsPoint pt3 = QgsGeometryUtils::closestPoint( linestringZM, QgsPoint( 2, 2 ) );
QGSCOMPARENEAR( pt3.z(), 1.5, 0.0001 );
QGSCOMPARENEAR( pt3.m(), 1.5, 0.0001 );

QgsLineString linestringDuplicatedPoint( QVector<QgsPoint>()
<< QgsPoint( 1, 1, 1, 1 )
<< QgsPoint( 1, 1, 1, 1 )
<< QgsPoint( 1, 3, 2, 2 ) );

QgsPoint pt4 = QgsGeometryUtils::closestPoint( linestringDuplicatedPoint, QgsPoint( 1, 0 ) );
QGSCOMPARENEAR( pt4.z(), 1, 0.0001 );
QGSCOMPARENEAR( pt4.m(), 1, 0.0001 );
}

QGSTEST_MAIN( TestQgsGeometryUtils )

0 comments on commit 8e7e573

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