Remove last use of pal::RTree

This commit is contained in:
Nyall Dawson 2019-12-09 11:42:51 +10:00
parent 727a4da01f
commit f9a3d0589a
5 changed files with 35 additions and 47 deletions

View File

@ -30,9 +30,12 @@ Constructor for QgsLabelSearchTree.
~QgsLabelSearchTree();
void clear();
void clear() /Deprecated/;
%Docstring
Removes and deletes all the entries
Removes and deletes all the entries.
.. deprecated::
has no effect since QGIS 3.12
%End

View File

@ -130,7 +130,7 @@ bool QgsGenericSpatialIndex<T>::deleteData( const T *data, const QgsRectangle &b
}
template<typename T>
bool QgsGenericSpatialIndex<T>::intersects( const QgsRectangle &rectangle, const std::function<bool ( const T * )> &callback )
bool QgsGenericSpatialIndex<T>::intersects( const QgsRectangle &rectangle, const std::function<bool ( const T * )> &callback ) const
{
GenericIndexVisitor<T> visitor( callback, mIdToData );
SpatialIndex::Region r = QgsSpatialIndexUtils::rectangleToRegion( rectangle );
@ -148,3 +148,6 @@ namespace pal
template class QgsGenericSpatialIndex<pal::FeaturePart>;
template class QgsGenericSpatialIndex<pal::LabelPosition>;
class QgsLabelPosition;
template class QgsGenericSpatialIndex<QgsLabelPosition>;

View File

@ -40,14 +40,14 @@ class CORE_EXPORT QgsGenericSpatialIndex
bool insertData( const T *data, const QgsRectangle &bounds );
bool deleteData( const T *data, const QgsRectangle &bounds );
bool intersects( const QgsRectangle &rectangle, const std::function< bool( const T *data )> &callback );
bool intersects( const QgsRectangle &rectangle, const std::function< bool( const T *data )> &callback ) const;
private:
std::unique_ptr< SpatialIndex::IStorageManager > mStorageManager;
std::unique_ptr< SpatialIndex::ISpatialIndex > mRTree;
QMutex mMutex;
mutable QMutex mMutex;
qint64 mNextId = 1;
QHash< qint64, const T * > mIdToData;

View File

@ -15,30 +15,20 @@
#include "qgslabelsearchtree.h"
#include "labelposition.h"
bool searchCallback( QgsLabelPosition *pos, void *context )
{
QList<QgsLabelPosition *> *list = static_cast< QList<QgsLabelPosition *>* >( context );
list->push_back( pos );
return true;
}
QgsLabelSearchTree::QgsLabelSearchTree() = default;
QgsLabelSearchTree::~QgsLabelSearchTree()
{
clear();
}
QgsLabelSearchTree::~QgsLabelSearchTree() = default;
void QgsLabelSearchTree::label( const QgsPointXY &point, QList<QgsLabelPosition *> &posList ) const
{
QgsPointXY p( point );
double c_min[2];
c_min[0] = p.x() - 0.1;
c_min[1] = p.y() - 0.1;
double c_max[2];
c_max[0] = p.x() + 0.1;
c_max[1] = p.y() + 0.1;
QList<QgsLabelPosition *> searchResults;
mSpatialIndex.Search( c_min, c_max, searchCallback, &searchResults );
mSpatialIndex.intersects( QgsRectangle( p.x() - 0.1, p.y() - 0.1, p.x() + 0.1, p.y() + 0.1 ), [&searchResults]( const QgsLabelPosition * pos ) -> bool
{
searchResults.push_back( const_cast< QgsLabelPosition * >( pos ) );
return true;
} );
//tolerance +-0.1 could be high in case of degree crs, so check if p is really contained in the results
posList.clear();
@ -54,15 +44,12 @@ void QgsLabelSearchTree::label( const QgsPointXY &point, QList<QgsLabelPosition
void QgsLabelSearchTree::labelsInRect( const QgsRectangle &r, QList<QgsLabelPosition *> &posList ) const
{
double c_min[2];
c_min[0] = r.xMinimum();
c_min[1] = r.yMinimum();
double c_max[2];
c_max[0] = r.xMaximum();
c_max[1] = r.yMaximum();
QList<QgsLabelPosition *> searchResults;
mSpatialIndex.Search( c_min, c_max, searchCallback, &searchResults );
mSpatialIndex.intersects( r, [&searchResults]( const QgsLabelPosition * pos )->bool
{
searchResults.push_back( const_cast< QgsLabelPosition * >( pos ) );
return true;
} );
posList.clear();
QList<QgsLabelPosition *>::const_iterator resultIt = searchResults.constBegin();
@ -98,17 +85,12 @@ bool QgsLabelSearchTree::insertLabel( pal::LabelPosition *labelPos, QgsFeatureId
yMin = std::min( yMin, res.y() );
yMax = std::max( yMax, res.y() );
}
double c_min[2];
double c_max[2];
c_min[0] = xMin;
c_min[1] = yMin;
c_max[0] = xMax;
c_max[1] = yMax;
const QgsRectangle bounds( xMin, yMin, xMax, yMax );
QgsGeometry labelGeometry( QgsGeometry::fromPolygonXY( QVector<QgsPolylineXY>() << cornerPoints ) );
std::unique_ptr< QgsLabelPosition > newEntry = qgis::make_unique< QgsLabelPosition >( featureId, labelPos->getAlpha() + mMapSettings.rotation(), cornerPoints, QgsRectangle( c_min[0], c_min[1], c_max[0], c_max[1] ),
std::unique_ptr< QgsLabelPosition > newEntry = qgis::make_unique< QgsLabelPosition >( featureId, labelPos->getAlpha() + mMapSettings.rotation(), cornerPoints, bounds,
labelPos->getWidth(), labelPos->getHeight(), layerName, labeltext, labelfont, labelPos->getUpsideDown(), diagram, pinned, providerId, labelGeometry, isUnplaced );
mSpatialIndex.Insert( c_min, c_max, newEntry.get() );
mSpatialIndex.insertData( newEntry.get(), bounds );
mOwnedPositions.emplace_back( std::move( newEntry ) );
if ( pal::LabelPosition *next = labelPos->getNextPart() )
@ -136,10 +118,8 @@ void QgsLabelSearchTree::setMapSettings( const QgsMapSettings &settings )
}
}
void QgsLabelSearchTree::clear()
{
mSpatialIndex.RemoveAll();
//PAL rtree iterator is buggy and doesn't iterate over all items, so we can't iterate through the tree to delete positions
mOwnedPositions.clear();
}

View File

@ -24,7 +24,7 @@
#include <QList>
#include <QVector>
#include "qgspallabeling.h"
#include "rtree.hpp"
#include "qgsgenericspatialindex.h"
#include "qgsmapsettings.h"
class QgsPointXY;
@ -49,7 +49,7 @@ class CORE_EXPORT QgsLabelSearchTree
/**
* Constructor for QgsLabelSearchTree.
*/
QgsLabelSearchTree() = default;
QgsLabelSearchTree();
~QgsLabelSearchTree();
//! QgsLabelSearchTree cannot be copied.
@ -57,8 +57,11 @@ class CORE_EXPORT QgsLabelSearchTree
//! QgsLabelSearchTree cannot be copied.
QgsLabelSearchTree &operator=( const QgsLabelSearchTree &rh ) = delete;
//! Removes and deletes all the entries
void clear();
/**
* Removes and deletes all the entries.
* \deprecated has no effect since QGIS 3.12
*/
Q_DECL_DEPRECATED void clear() SIP_DEPRECATED;
/**
* Returns label position(s) at a given point. QgsLabelSearchTree keeps ownership, don't delete the LabelPositions
@ -88,8 +91,7 @@ class CORE_EXPORT QgsLabelSearchTree
void setMapSettings( const QgsMapSettings &settings );
private:
// set as mutable because RTree template is not const-correct
mutable pal::RTree<QgsLabelPosition *, double, 2, double> mSpatialIndex;
QgsGenericSpatialIndex< QgsLabelPosition > mSpatialIndex;
std::vector< std::unique_ptr< QgsLabelPosition > > mOwnedPositions;
QgsMapSettings mMapSettings;
QTransform mTransform;