Skip to content

Commit 6c17905

Browse files
committed
Add straightDistance2d and sinuosity measures to QgsCurve
1 parent 05fb8f7 commit 6c17905

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

python/core/geometry/qgscurve.sip.in

+20
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,26 @@ Returns the y-coordinate of the specified node in the line string.
186186
Returns a QPolygonF representing the points.
187187
%End
188188

189+
double straightDistance2d() const;
190+
%Docstring
191+
Returns the straight distance of the curve, i.e. the direct/euclidean distance
192+
between the first and last vertex of the curve. (Also known as
193+
"as the crow flies" distance).
194+
195+
.. versionadded:: 3.2
196+
%End
197+
198+
double sinuosity() const;
199+
%Docstring
200+
Returns the curve sinuosity, which is the ratio of the curve length() to curve
201+
straightDistance2d(). Larger numbers indicate a more "sinuous" curve (i.e. more
202+
"bendy"). The minimum value returned of 1.0 indicates a perfectly straight curve.
203+
204+
If a curve isClosed(), it has infinite sinuosity and will return NaN.
205+
206+
.. versionadded:: 3.2
207+
%End
208+
189209

190210

191211
protected:

src/core/geometry/qgscurve.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,20 @@ QPolygonF QgsCurve::asQPolygonF() const
200200
return points;
201201
}
202202

203+
double QgsCurve::straightDistance2d() const
204+
{
205+
return startPoint().distance( endPoint() );
206+
}
207+
208+
double QgsCurve::sinuosity() const
209+
{
210+
double d = straightDistance2d();
211+
if ( qgsDoubleNear( d, 0.0 ) )
212+
return std::numeric_limits<double>::quiet_NaN();
213+
214+
return length() / d;
215+
}
216+
203217
void QgsCurve::clearCache() const
204218
{
205219
mBoundingBox = QgsRectangle();

src/core/geometry/qgscurve.h

+20
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,26 @@ class CORE_EXPORT QgsCurve: public QgsAbstractGeometry
168168
*/
169169
QPolygonF asQPolygonF() const;
170170

171+
/**
172+
* Returns the straight distance of the curve, i.e. the direct/euclidean distance
173+
* between the first and last vertex of the curve. (Also known as
174+
* "as the crow flies" distance).
175+
*
176+
* \since QGIS 3.2
177+
*/
178+
double straightDistance2d() const;
179+
180+
/**
181+
* Returns the curve sinuosity, which is the ratio of the curve length() to curve
182+
* straightDistance2d(). Larger numbers indicate a more "sinuous" curve (i.e. more
183+
* "bendy"). The minimum value returned of 1.0 indicates a perfectly straight curve.
184+
*
185+
* If a curve isClosed(), it has infinite sinuosity and will return NaN.
186+
*
187+
* \since QGIS 3.2
188+
*/
189+
double sinuosity() const;
190+
171191
#ifndef SIP_RUN
172192

173193
/**

0 commit comments

Comments
 (0)