Skip to content
Permalink
Browse files

Add some more line segment methods

  • Loading branch information
nyalldawson committed Apr 23, 2018
1 parent cd3b976 commit 70b67c6150bd04102883c8f69a833cbbb9feb765
@@ -7,6 +7,7 @@
************************************************************************/



class QgsLineSegment2D
{
%Docstring
@@ -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;
@@ -12,6 +12,7 @@




class QgsLineString: QgsCurve
{
%Docstring
@@ -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;
@@ -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.
@@ -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()
@@ -22,6 +22,7 @@
#include "qgsgeometryutils.h"
#include "qgsmaptopixel.h"
#include "qgswkbptr.h"
#include "qgslinesegment.h"

#include <cmath>
#include <memory>
@@ -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 );
@@ -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.
@@ -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;

/**
@@ -50,6 +50,7 @@
#include "qgsgeometryfactory.h"
#include "qgscurvepolygon.h"
#include "qgsproject.h"
#include "qgslinesegment.h"

//qgs unit test utility class
#include "qgsrenderchecker.h"
@@ -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 ) );
@@ -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))

0 comments on commit 70b67c6

Please sign in to comment.
You can’t perform that action at this time.