Skip to content
Permalink
Browse files
Add method to retrieve all labels from QgsLabelingResults, instead of…
… just labels within a rect
  • Loading branch information
nyalldawson committed Jun 9, 2021
1 parent 8bfb959 commit 8e2fb49718a3bd00d930e2c9ba01237fcaae04d2
@@ -25,6 +25,13 @@ Class that stores computed placement from labeling engine.
~QgsLabelingResults();


QList< QgsLabelPosition > allLabels() const;
%Docstring
Returns a list of all labels generated by the labeling run.

.. versionadded:: 3.20
%End

QList<QgsLabelPosition> labelsAtPosition( const QgsPointXY &p ) const;
%Docstring
Returns the details of any labels placed at the specified point (in map coordinates).
@@ -41,6 +41,13 @@ Removes and deletes all the entries.



QList< QgsLabelPosition > allLabels() const;
%Docstring
Returns a list of all labels generated by the labeling run.

.. versionadded:: 3.20
%End




@@ -23,6 +23,12 @@ QgsLabelingResults::QgsLabelingResults()

QgsLabelingResults::~QgsLabelingResults() = default;


QList<QgsLabelPosition> QgsLabelingResults::allLabels() const
{
return mLabelSearchTree ? mLabelSearchTree->allLabels() : QList<QgsLabelPosition>();
}

QList<QgsLabelPosition> QgsLabelingResults::labelsAtPosition( const QgsPointXY &p ) const
{
QList<QgsLabelPosition> positions;
@@ -40,6 +40,13 @@ class CORE_EXPORT QgsLabelingResults
//! QgsLabelingResults cannot be copied.
QgsLabelingResults &operator=( const QgsLabelingResults &rh ) = delete;

/**
* Returns a list of all labels generated by the labeling run.
*
* \since QGIS 3.20
*/
QList< QgsLabelPosition > allLabels() const;

/**
* Returns the details of any labels placed at the specified point (in map coordinates).
*/
@@ -42,6 +42,17 @@ void QgsLabelSearchTree::label( const QgsPointXY &point, QList<QgsLabelPosition
}
}

QList<QgsLabelPosition> QgsLabelSearchTree::allLabels() const
{
QList<QgsLabelPosition> res;
res.reserve( mOwnedPositions.size() );
for ( const std::unique_ptr< QgsLabelPosition > &pos : mOwnedPositions )
{
res.append( * pos );
}
return res;
}

void QgsLabelSearchTree::labelsInRect( const QgsRectangle &r, QList<QgsLabelPosition *> &posList ) const
{
QList<QgsLabelPosition *> searchResults;
@@ -74,6 +74,13 @@ class CORE_EXPORT QgsLabelSearchTree

//TODO: why does this break bindings with QList<QgsLabelPosition>?

/**
* Returns a list of all labels generated by the labeling run.
*
* \since QGIS 3.20
*/
QList< QgsLabelPosition > allLabels() const;

/**
* Returns label position(s) in given rectangle. QgsLabelSearchTree keeps ownership, don't delete the LabelPositions
* \note not available in Python bindings
@@ -1961,7 +1961,17 @@ void TestQgsLabelingEngine::labelingResults()
QVERIFY( results );

// retrieve some labels
QList<QgsLabelPosition> labels = results->labelsAtPosition( QgsPointXY( -654732, 7003282 ) );
QList<QgsLabelPosition> labels = results->allLabels();
QCOMPARE( labels.count(), 3 );
std::sort( labels.begin(), labels.end(), []( const QgsLabelPosition & a, const QgsLabelPosition & b )
{
return a.labelText.compare( b.labelText );
} );
QCOMPARE( labels.at( 0 ).labelText, QStringLiteral( "1" ) );
QCOMPARE( labels.at( 1 ).labelText, QStringLiteral( "8888" ) );
QCOMPARE( labels.at( 2 ).labelText, QStringLiteral( "33333" ) );

labels = results->labelsAtPosition( QgsPointXY( -654732, 7003282 ) );
QCOMPARE( labels.count(), 1 );
QCOMPARE( labels.at( 0 ).featureId, 1 );
QCOMPARE( labels.at( 0 ).labelText, QStringLiteral( "1" ) );

0 comments on commit 8e2fb49

Please sign in to comment.