Skip to content

Commit

Permalink
Add list constructor for QgsMultiLineString too
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 1, 2024
1 parent b6cabe7 commit 2c30087
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ Multi line string geometry collection.
Constructor for an empty multilinestring geometry.
%End

QgsMultiLineString( const QList< QgsLineString > &linestrings ) /HoldGIL/;
%Docstring
Constructor for a multilinestring containing the specified ``linestrings``.

The ``linestrings`` will be internally cloned.

.. versionadded:: 3.38
%End

QgsMultiLineString( const QList< QgsLineString * > &linestrings /Transfer/ ) /HoldGIL/;
%Docstring
Constructor for a multilinestring containing the specified ``linestrings``.

Ownership of the ``linestrings`` will be transferred to the multilinestring.

.. versionadded:: 3.38
%End


SIP_PYOBJECT lineStringN( int index ) /TypeHint="QgsLineString"/;
Expand Down
17 changes: 17 additions & 0 deletions python/core/auto_generated/geometry/qgsmultilinestring.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ Multi line string geometry collection.
Constructor for an empty multilinestring geometry.
%End

QgsMultiLineString( const QList< QgsLineString > &linestrings ) /HoldGIL/;
%Docstring
Constructor for a multilinestring containing the specified ``linestrings``.

The ``linestrings`` will be internally cloned.

.. versionadded:: 3.38
%End

QgsMultiLineString( const QList< QgsLineString * > &linestrings /Transfer/ ) /HoldGIL/;
%Docstring
Constructor for a multilinestring containing the specified ``linestrings``.

Ownership of the ``linestrings`` will be transferred to the multilinestring.

.. versionadded:: 3.38
%End


SIP_PYOBJECT lineStringN( int index ) /TypeHint="QgsLineString"/;
Expand Down
28 changes: 28 additions & 0 deletions src/core/geometry/qgsmultilinestring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,34 @@ QgsMultiLineString::QgsMultiLineString()
mWkbType = Qgis::WkbType::MultiLineString;
}

QgsMultiLineString::QgsMultiLineString( const QList<QgsLineString> &linestrings )
{
if ( linestrings.empty() )
return;

mGeometries.reserve( linestrings.size() );
for ( const QgsLineString &line : linestrings )
{
mGeometries.append( line.clone() );
}

setZMTypeFromSubGeometry( &linestrings.at( 0 ), Qgis::WkbType::MultiLineString );
}

QgsMultiLineString::QgsMultiLineString( const QList<QgsLineString *> &linestrings )
{
if ( linestrings.empty() )
return;

mGeometries.reserve( linestrings.size() );
for ( QgsLineString *line : linestrings )
{
mGeometries.append( line );
}

setZMTypeFromSubGeometry( linestrings.at( 0 ), Qgis::WkbType::MultiLineString );
}

QgsLineString *QgsMultiLineString::lineStringN( int index )
{
return qgsgeometry_cast< QgsLineString * >( geometryN( index ) );
Expand Down
17 changes: 17 additions & 0 deletions src/core/geometry/qgsmultilinestring.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ class CORE_EXPORT QgsMultiLineString: public QgsMultiCurve
*/
QgsMultiLineString() SIP_HOLDGIL;

/**
* Constructor for a multilinestring containing the specified \a linestrings.
*
* The \a linestrings will be internally cloned.
*
* \since QGIS 3.38
*/
QgsMultiLineString( const QList< QgsLineString > &linestrings ) SIP_HOLDGIL;

/**
* Constructor for a multilinestring containing the specified \a linestrings.
*
* Ownership of the \a linestrings will be transferred to the multilinestring.
*
* \since QGIS 3.38
*/
QgsMultiLineString( const QList< QgsLineString * > &linestrings SIP_TRANSFER ) SIP_HOLDGIL;

#ifndef SIP_RUN

Expand Down
26 changes: 26 additions & 0 deletions tests/src/python/test_qgsmultilinestring.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@ def testConstruct(self):
self.assertEqual(line.startPoint(), QgsPoint(1, 2))
self.assertEqual(line.endPoint(), QgsPoint(3, 4))

p = QgsMultiLineString([])
self.assertTrue(p.isEmpty())

value = QgsLineString([[1, 2], [10, 2], [10, 10]])
p = QgsMultiLineString([value])
self.assertEqual(p.asWkt(), 'MultiLineString ((1 2, 10 2, 10 10))')
# constructor should have made internal copy
del value
self.assertEqual(p.asWkt(), 'MultiLineString ((1 2, 10 2, 10 10))')

p = QgsMultiLineString([QgsLineString([[1, 2], [10, 2], [10, 10], [1, 2]]),
QgsLineString([[100, 2], [110, 2], [110, 10], [100, 2]])])
self.assertEqual(p.asWkt(), 'MultiLineString ((1 2, 10 2, 10 10, 1 2),(100 2, 110 2, 110 10, 100 2))')

# with z
p = QgsMultiLineString([QgsLineString([[1, 2, 3], [10, 2, 3], [10, 10, 3], [1, 2, 3]]),
QgsLineString([[100, 2, 4], [110, 2, 4], [110, 10, 4], [100, 2, 4]])])
self.assertEqual(p.asWkt(),
'MultiLineStringZ ((1 2 3, 10 2 3, 10 10 3, 1 2 3),(100 2 4, 110 2 4, 110 10 4, 100 2 4))')

# with zm
p = QgsMultiLineString([QgsLineString([[1, 2, 3, 5], [10, 2, 3, 5], [10, 10, 3, 5], [1, 2, 3, 5]]),
QgsLineString([[100, 2, 4, 6], [110, 2, 4, 6], [110, 10, 4, 6], [100, 2, 4, 6]])])
self.assertEqual(p.asWkt(),
'MultiLineStringZM ((1 2 3 5, 10 2 3 5, 10 10 3 5, 1 2 3 5),(100 2 4 6, 110 2 4 6, 110 10 4 6, 100 2 4 6))')

def testMeasureLine(self):
multiline = QgsMultiLineString()
m_line = multiline.measuredLine(10, 20)
Expand Down

0 comments on commit 2c30087

Please sign in to comment.