Skip to content

Commit

Permalink
Add convenience constructor to create QgsLineString between two points
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 23, 2018
1 parent fbf6b4a commit 3f3b951
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/core/geometry/qgslinestring.sip.in
Expand Up @@ -46,6 +46,13 @@ This constructor is more efficient then calling setPoints()
or repeatedly calling addVertex()

.. versionadded:: 3.0
%End

QgsLineString( const QgsPoint &p1, const QgsPoint &p2 );
%Docstring
Constructs a linestring with a single segment from ``p1`` to ``p2``.

.. versionadded:: 3.2
%End

QgsLineString( const QVector<QgsPointXY> &points );
Expand Down
25 changes: 25 additions & 0 deletions src/core/geometry/qgslinestring.cpp
Expand Up @@ -124,6 +124,31 @@ QgsLineString::QgsLineString( const QVector<double> &x, const QVector<double> &y
}
}

QgsLineString::QgsLineString( const QgsPoint &p1, const QgsPoint &p2 )
{
mWkbType = QgsWkbTypes::LineString;
mX.resize( 2 );
mX[ 0 ] = p1.x();
mX[ 1 ] = p2.x();
mY.resize( 2 );
mY[ 0 ] = p1.y();
mY[ 1 ] = p2.y();
if ( p1.is3D() )
{
mWkbType = QgsWkbTypes::addZ( mWkbType );
mZ.resize( 2 );
mZ[ 0 ] = p1.z();
mZ[ 1 ] = p2.z();
}
if ( p1.isMeasure() )
{
mWkbType = QgsWkbTypes::addM( mWkbType );
mM.resize( 2 );
mM[ 0 ] = p1.m();
mM[ 1 ] = p2.m();
}
}

QgsLineString::QgsLineString( const QVector<QgsPointXY> &points )
{
mWkbType = QgsWkbTypes::LineString;
Expand Down
6 changes: 6 additions & 0 deletions src/core/geometry/qgslinestring.h
Expand Up @@ -63,6 +63,12 @@ class CORE_EXPORT QgsLineString: public QgsCurve
const QVector<double> &z = QVector<double>(),
const QVector<double> &m = QVector<double>() );

/**
* Constructs a linestring with a single segment from \a p1 to \a p2.
* \since QGIS 3.2
*/
QgsLineString( const QgsPoint &p1, const QgsPoint &p2 );

/**
* Construct a linestring from list of points.
* This constructor is more efficient then calling setPoints()
Expand Down
38 changes: 38 additions & 0 deletions tests/src/core/testqgsgeometry.cpp
Expand Up @@ -2696,6 +2696,44 @@ void TestQgsGeometry::lineString()
QCOMPARE( fromVector3D.yAt( 1 ), 40.0 );
QCOMPARE( fromVector3D.zAt( 1 ), 200.0 );

// from 2 points
QgsLineString from2Pts( QgsPoint( 1, 2 ), QgsPoint( 21, 22 ) );
QCOMPARE( from2Pts.wkbType(), QgsWkbTypes::LineString );
QCOMPARE( from2Pts.numPoints(), 2 );
QCOMPARE( from2Pts.xAt( 0 ), 1.0 );
QCOMPARE( from2Pts.yAt( 0 ), 2.0 );
QCOMPARE( from2Pts.xAt( 1 ), 21.0 );
QCOMPARE( from2Pts.yAt( 1 ), 22.0 );
from2Pts = QgsLineString( QgsPoint( QgsWkbTypes::PointZ, 1, 2, 3 ), QgsPoint( QgsWkbTypes::PointZ, 21, 22, 23 ) );
QCOMPARE( from2Pts.wkbType(), QgsWkbTypes::LineStringZ );
QCOMPARE( from2Pts.numPoints(), 2 );
QCOMPARE( from2Pts.xAt( 0 ), 1.0 );
QCOMPARE( from2Pts.yAt( 0 ), 2.0 );
QCOMPARE( from2Pts.zAt( 0 ), 3.0 );
QCOMPARE( from2Pts.xAt( 1 ), 21.0 );
QCOMPARE( from2Pts.yAt( 1 ), 22.0 );
QCOMPARE( from2Pts.zAt( 1 ), 23.0 );
from2Pts = QgsLineString( QgsPoint( QgsWkbTypes::PointM, 1, 2, 0, 3 ), QgsPoint( QgsWkbTypes::PointM, 21, 22, 0, 23 ) );
QCOMPARE( from2Pts.wkbType(), QgsWkbTypes::LineStringM );
QCOMPARE( from2Pts.numPoints(), 2 );
QCOMPARE( from2Pts.xAt( 0 ), 1.0 );
QCOMPARE( from2Pts.yAt( 0 ), 2.0 );
QCOMPARE( from2Pts.mAt( 0 ), 3.0 );
QCOMPARE( from2Pts.xAt( 1 ), 21.0 );
QCOMPARE( from2Pts.yAt( 1 ), 22.0 );
QCOMPARE( from2Pts.mAt( 1 ), 23.0 );
from2Pts = QgsLineString( QgsPoint( QgsWkbTypes::PointZM, 1, 2, 3, 4 ), QgsPoint( QgsWkbTypes::PointZM, 21, 22, 23, 24 ) );
QCOMPARE( from2Pts.wkbType(), QgsWkbTypes::LineStringZM );
QCOMPARE( from2Pts.numPoints(), 2 );
QCOMPARE( from2Pts.xAt( 0 ), 1.0 );
QCOMPARE( from2Pts.yAt( 0 ), 2.0 );
QCOMPARE( from2Pts.zAt( 0 ), 3.0 );
QCOMPARE( from2Pts.mAt( 0 ), 4.0 );
QCOMPARE( from2Pts.xAt( 1 ), 21.0 );
QCOMPARE( from2Pts.yAt( 1 ), 22.0 );
QCOMPARE( from2Pts.zAt( 1 ), 23.0 );
QCOMPARE( from2Pts.mAt( 1 ), 24.0 );

//addVertex
QgsLineString l2;
l2.addVertex( QgsPoint( 1.0, 2.0 ) );
Expand Down

0 comments on commit 3f3b951

Please sign in to comment.