Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add straightDistance2d and sinuosity measures to QgsCurve
  • Loading branch information
nyalldawson committed Mar 12, 2018
1 parent 05fb8f7 commit 6c17905
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
20 changes: 20 additions & 0 deletions python/core/geometry/qgscurve.sip.in
Expand Up @@ -186,6 +186,26 @@ Returns the y-coordinate of the specified node in the line string.
Returns a QPolygonF representing the points.
%End

double straightDistance2d() const;
%Docstring
Returns the straight distance of the curve, i.e. the direct/euclidean distance
between the first and last vertex of the curve. (Also known as
"as the crow flies" distance).

.. versionadded:: 3.2
%End

double sinuosity() const;
%Docstring
Returns the curve sinuosity, which is the ratio of the curve length() to curve
straightDistance2d(). Larger numbers indicate a more "sinuous" curve (i.e. more
"bendy"). The minimum value returned of 1.0 indicates a perfectly straight curve.

If a curve isClosed(), it has infinite sinuosity and will return NaN.

.. versionadded:: 3.2
%End



protected:
Expand Down
14 changes: 14 additions & 0 deletions src/core/geometry/qgscurve.cpp
Expand Up @@ -200,6 +200,20 @@ QPolygonF QgsCurve::asQPolygonF() const
return points;
}

double QgsCurve::straightDistance2d() const
{
return startPoint().distance( endPoint() );
}

double QgsCurve::sinuosity() const
{
double d = straightDistance2d();
if ( qgsDoubleNear( d, 0.0 ) )
return std::numeric_limits<double>::quiet_NaN();

return length() / d;
}

void QgsCurve::clearCache() const
{
mBoundingBox = QgsRectangle();
Expand Down
20 changes: 20 additions & 0 deletions src/core/geometry/qgscurve.h
Expand Up @@ -168,6 +168,26 @@ class CORE_EXPORT QgsCurve: public QgsAbstractGeometry
*/
QPolygonF asQPolygonF() const;

/**
* Returns the straight distance of the curve, i.e. the direct/euclidean distance
* between the first and last vertex of the curve. (Also known as
* "as the crow flies" distance).
*
* \since QGIS 3.2
*/
double straightDistance2d() const;

/**
* Returns the curve sinuosity, which is the ratio of the curve length() to curve
* straightDistance2d(). Larger numbers indicate a more "sinuous" curve (i.e. more
* "bendy"). The minimum value returned of 1.0 indicates a perfectly straight curve.
*
* If a curve isClosed(), it has infinite sinuosity and will return NaN.
*
* \since QGIS 3.2
*/
double sinuosity() const;

#ifndef SIP_RUN

/**
Expand Down

0 comments on commit 6c17905

Please sign in to comment.