diff --git a/python/core/geometry/qgscurve.sip.in b/python/core/geometry/qgscurve.sip.in index 8e0d1a4e800..00b6516bd43 100644 --- a/python/core/geometry/qgscurve.sip.in +++ b/python/core/geometry/qgscurve.sip.in @@ -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: diff --git a/src/core/geometry/qgscurve.cpp b/src/core/geometry/qgscurve.cpp index 0ff86f01918..dd90a760e17 100644 --- a/src/core/geometry/qgscurve.cpp +++ b/src/core/geometry/qgscurve.cpp @@ -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::quiet_NaN(); + + return length() / d; +} + void QgsCurve::clearCache() const { mBoundingBox = QgsRectangle(); diff --git a/src/core/geometry/qgscurve.h b/src/core/geometry/qgscurve.h index ff84cfec680..e331c94b61c 100644 --- a/src/core/geometry/qgscurve.h +++ b/src/core/geometry/qgscurve.h @@ -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 /**