Skip to content

Commit 70b67c6

Browse files
committedApr 23, 2018
Add some more line segment methods
1 parent cd3b976 commit 70b67c6

File tree

7 files changed

+59
-0
lines changed

7 files changed

+59
-0
lines changed
 

‎python/core/geometry/qgslinesegment.sip.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
************************************************************************/
88

99

10+
1011
class QgsLineSegment2D
1112
{
1213
%Docstring
@@ -24,6 +25,12 @@ Represents a single 2D line segment, consisting of a 2D start and end vertex onl
2425
%Docstring
2526
Constructor for a QgsLineSegment2D from the specified ``start`` point to
2627
the ``end`` point.
28+
%End
29+
30+
QgsLineSegment2D( double x1, double y1, double x2, double y2 );
31+
%Docstring
32+
Constructor for a QgsLineSegment2D from the point (``x1``, ``y2``) to
33+
(``x2``, ``y2``).
2734
%End
2835

2936
double length() const;

‎python/core/geometry/qgslinestring.sip.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313

1414

15+
1516
class QgsLineString: QgsCurve
1617
{
1718
%Docstring
@@ -62,6 +63,13 @@ This constructor is more efficient then calling setPoints()
6263
or repeatedly calling addVertex()
6364

6465
.. versionadded:: 3.0
66+
%End
67+
68+
explicit QgsLineString( const QgsLineSegment2D &segment );
69+
%Docstring
70+
Construct a linestring from a single 2d line segment.
71+
72+
.. versionadded:: 3.2
6573
%End
6674

6775
virtual bool equals( const QgsCurve &other ) const;

‎src/core/geometry/qgslinesegment.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "qgis_core.h"
1212
#include "qgspointxy.h"
1313

14+
class QgsLineString;
15+
1416
/**
1517
* \ingroup core
1618
* Represents a single 2D line segment, consisting of a 2D start and end vertex only.
@@ -30,6 +32,15 @@ class CORE_EXPORT QgsLineSegment2D
3032
, mEnd( end )
3133
{}
3234

35+
/**
36+
* Constructor for a QgsLineSegment2D from the point (\a x1, \a y2) to
37+
* (\a x2, \a y2).
38+
*/
39+
QgsLineSegment2D( double x1, double y1, double x2, double y2 )
40+
: mStart( QgsPointXY( x1, y1 ) )
41+
, mEnd( QgsPointXY( x2, y2 ) )
42+
{}
43+
3344
/**
3445
* Returns the length of the segment.
3546
* \see lengthSquared()

‎src/core/geometry/qgslinestring.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "qgsgeometryutils.h"
2323
#include "qgsmaptopixel.h"
2424
#include "qgswkbptr.h"
25+
#include "qgslinesegment.h"
2526

2627
#include <cmath>
2728
#include <memory>
@@ -161,6 +162,17 @@ QgsLineString::QgsLineString( const QVector<QgsPointXY> &points )
161162
}
162163
}
163164

165+
QgsLineString::QgsLineString( const QgsLineSegment2D &segment )
166+
{
167+
mWkbType = QgsWkbTypes::LineString;
168+
mX.resize( 2 );
169+
mY.resize( 2 );
170+
mX[0] = segment.startX();
171+
mX[1] = segment.endX();
172+
mY[0] = segment.startY();
173+
mY[1] = segment.endY();
174+
}
175+
164176
bool QgsLineString::equals( const QgsCurve &other ) const
165177
{
166178
const QgsLineString *otherLine = qgsgeometry_cast< const QgsLineString * >( &other );

‎src/core/geometry/qgslinestring.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include "qgscurve.h"
2727
#include "qgscompoundcurve.h"
2828

29+
class QgsLineSegment2D;
30+
2931
/***************************************************************************
3032
* This class is considered CRITICAL and any change MUST be accompanied with
3133
* full unit tests in testqgsgeometry.cpp.
@@ -77,6 +79,12 @@ class CORE_EXPORT QgsLineString: public QgsCurve
7779
*/
7880
QgsLineString( const QVector<QgsPointXY> &points );
7981

82+
/**
83+
* Construct a linestring from a single 2d line segment.
84+
* \since QGIS 3.2
85+
*/
86+
explicit QgsLineString( const QgsLineSegment2D &segment );
87+
8088
bool equals( const QgsCurve &other ) const override;
8189

8290
/**

‎tests/src/core/testqgsgeometry.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "qgsgeometryfactory.h"
5151
#include "qgscurvepolygon.h"
5252
#include "qgsproject.h"
53+
#include "qgslinesegment.h"
5354

5455
//qgs unit test utility class
5556
#include "qgsrenderchecker.h"
@@ -2849,6 +2850,15 @@ void TestQgsGeometry::lineString()
28492850
QCOMPARE( from2Pts.zAt( 1 ), 23.0 );
28502851
QCOMPARE( from2Pts.mAt( 1 ), 24.0 );
28512852

2853+
// from lineSegment
2854+
QgsLineString fromSegment( QgsLineSegment2D( QgsPointXY( 1, 2 ), QgsPointXY( 3, 4 ) ) );
2855+
QCOMPARE( fromSegment.wkbType(), QgsWkbTypes::LineString );
2856+
QCOMPARE( fromSegment.numPoints(), 2 );
2857+
QCOMPARE( fromSegment.xAt( 0 ), 1.0 );
2858+
QCOMPARE( fromSegment.yAt( 0 ), 2.0 );
2859+
QCOMPARE( fromSegment.xAt( 1 ), 3.0 );
2860+
QCOMPARE( fromSegment.yAt( 1 ), 4.0 );
2861+
28522862
//addVertex
28532863
QgsLineString l2;
28542864
l2.addVertex( QgsPoint( 1.0, 2.0 ) );

‎tests/src/python/test_qgslinesegment.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ def testConstruct(self):
2727
segment = QgsLineSegment2D(QgsPointXY(1, 2), QgsPointXY(3, 4))
2828
self.assertEqual(segment.start(), QgsPointXY(1, 2))
2929
self.assertEqual(segment.end(), QgsPointXY(3, 4))
30+
segment = QgsLineSegment2D(1, 2, 3, 4)
31+
self.assertEqual(segment.start(), QgsPointXY(1, 2))
32+
self.assertEqual(segment.end(), QgsPointXY(3, 4))
3033

3134
def testGettersSetters(self):
3235
segment = QgsLineSegment2D(QgsPointXY(1, 2), QgsPointXY(3, 4))

0 commit comments

Comments
 (0)
Please sign in to comment.