Skip to content

Commit

Permalink
Add some more line segment methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 23, 2018
1 parent cd3b976 commit 70b67c6
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/core/geometry/qgslinesegment.sip.in
Expand Up @@ -7,6 +7,7 @@
************************************************************************/



class QgsLineSegment2D
{
%Docstring
Expand All @@ -24,6 +25,12 @@ Represents a single 2D line segment, consisting of a 2D start and end vertex onl
%Docstring
Constructor for a QgsLineSegment2D from the specified ``start`` point to
the ``end`` point.
%End

QgsLineSegment2D( double x1, double y1, double x2, double y2 );
%Docstring
Constructor for a QgsLineSegment2D from the point (``x1``, ``y2``) to
(``x2``, ``y2``).
%End

double length() const;
Expand Down
8 changes: 8 additions & 0 deletions python/core/geometry/qgslinestring.sip.in
Expand Up @@ -12,6 +12,7 @@




class QgsLineString: QgsCurve
{
%Docstring
Expand Down Expand Up @@ -62,6 +63,13 @@ This constructor is more efficient then calling setPoints()
or repeatedly calling addVertex()

.. versionadded:: 3.0
%End

explicit QgsLineString( const QgsLineSegment2D &segment );
%Docstring
Construct a linestring from a single 2d line segment.

.. versionadded:: 3.2
%End

virtual bool equals( const QgsCurve &other ) const;
Expand Down
11 changes: 11 additions & 0 deletions src/core/geometry/qgslinesegment.h
Expand Up @@ -11,6 +11,8 @@
#include "qgis_core.h"
#include "qgspointxy.h"

class QgsLineString;

/**
* \ingroup core
* Represents a single 2D line segment, consisting of a 2D start and end vertex only.
Expand All @@ -30,6 +32,15 @@ class CORE_EXPORT QgsLineSegment2D
, mEnd( end )
{}

/**
* Constructor for a QgsLineSegment2D from the point (\a x1, \a y2) to
* (\a x2, \a y2).
*/
QgsLineSegment2D( double x1, double y1, double x2, double y2 )
: mStart( QgsPointXY( x1, y1 ) )
, mEnd( QgsPointXY( x2, y2 ) )
{}

/**
* Returns the length of the segment.
* \see lengthSquared()
Expand Down
12 changes: 12 additions & 0 deletions src/core/geometry/qgslinestring.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgsgeometryutils.h"
#include "qgsmaptopixel.h"
#include "qgswkbptr.h"
#include "qgslinesegment.h"

#include <cmath>
#include <memory>
Expand Down Expand Up @@ -161,6 +162,17 @@ QgsLineString::QgsLineString( const QVector<QgsPointXY> &points )
}
}

QgsLineString::QgsLineString( const QgsLineSegment2D &segment )
{
mWkbType = QgsWkbTypes::LineString;
mX.resize( 2 );
mY.resize( 2 );
mX[0] = segment.startX();
mX[1] = segment.endX();
mY[0] = segment.startY();
mY[1] = segment.endY();
}

bool QgsLineString::equals( const QgsCurve &other ) const
{
const QgsLineString *otherLine = qgsgeometry_cast< const QgsLineString * >( &other );
Expand Down
8 changes: 8 additions & 0 deletions src/core/geometry/qgslinestring.h
Expand Up @@ -26,6 +26,8 @@
#include "qgscurve.h"
#include "qgscompoundcurve.h"

class QgsLineSegment2D;

/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests in testqgsgeometry.cpp.
Expand Down Expand Up @@ -77,6 +79,12 @@ class CORE_EXPORT QgsLineString: public QgsCurve
*/
QgsLineString( const QVector<QgsPointXY> &points );

/**
* Construct a linestring from a single 2d line segment.
* \since QGIS 3.2
*/
explicit QgsLineString( const QgsLineSegment2D &segment );

bool equals( const QgsCurve &other ) const override;

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/src/core/testqgsgeometry.cpp
Expand Up @@ -50,6 +50,7 @@
#include "qgsgeometryfactory.h"
#include "qgscurvepolygon.h"
#include "qgsproject.h"
#include "qgslinesegment.h"

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

// from lineSegment
QgsLineString fromSegment( QgsLineSegment2D( QgsPointXY( 1, 2 ), QgsPointXY( 3, 4 ) ) );
QCOMPARE( fromSegment.wkbType(), QgsWkbTypes::LineString );
QCOMPARE( fromSegment.numPoints(), 2 );
QCOMPARE( fromSegment.xAt( 0 ), 1.0 );
QCOMPARE( fromSegment.yAt( 0 ), 2.0 );
QCOMPARE( fromSegment.xAt( 1 ), 3.0 );
QCOMPARE( fromSegment.yAt( 1 ), 4.0 );

//addVertex
QgsLineString l2;
l2.addVertex( QgsPoint( 1.0, 2.0 ) );
Expand Down
3 changes: 3 additions & 0 deletions tests/src/python/test_qgslinesegment.py
Expand Up @@ -27,6 +27,9 @@ def testConstruct(self):
segment = QgsLineSegment2D(QgsPointXY(1, 2), QgsPointXY(3, 4))
self.assertEqual(segment.start(), QgsPointXY(1, 2))
self.assertEqual(segment.end(), QgsPointXY(3, 4))
segment = QgsLineSegment2D(1, 2, 3, 4)
self.assertEqual(segment.start(), QgsPointXY(1, 2))
self.assertEqual(segment.end(), QgsPointXY(3, 4))

def testGettersSetters(self):
segment = QgsLineSegment2D(QgsPointXY(1, 2), QgsPointXY(3, 4))
Expand Down

0 comments on commit 70b67c6

Please sign in to comment.