Skip to content

Commit

Permalink
Remove last use of pal::RTree
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 10, 2019
1 parent 727a4da commit f9a3d05
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 47 deletions.
7 changes: 5 additions & 2 deletions python/core/auto_generated/qgslabelsearchtree.sip.in
Expand Up @@ -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


Expand Down
5 changes: 4 additions & 1 deletion src/core/qgsgenericspatialindex.cpp
Expand Up @@ -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 );
Expand All @@ -148,3 +148,6 @@ namespace pal

template class QgsGenericSpatialIndex<pal::FeaturePart>;
template class QgsGenericSpatialIndex<pal::LabelPosition>;

class QgsLabelPosition;
template class QgsGenericSpatialIndex<QgsLabelPosition>;
4 changes: 2 additions & 2 deletions src/core/qgsgenericspatialindex.h
Expand Up @@ -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;
Expand Down
52 changes: 16 additions & 36 deletions src/core/qgslabelsearchtree.cpp
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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() )
Expand Down Expand Up @@ -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();
}
14 changes: 8 additions & 6 deletions src/core/qgslabelsearchtree.h
Expand Up @@ -24,7 +24,7 @@
#include <QList>
#include <QVector>
#include "qgspallabeling.h"
#include "rtree.hpp"
#include "qgsgenericspatialindex.h"
#include "qgsmapsettings.h"

class QgsPointXY;
Expand All @@ -49,16 +49,19 @@ class CORE_EXPORT QgsLabelSearchTree
/**
* Constructor for QgsLabelSearchTree.
*/
QgsLabelSearchTree() = default;
QgsLabelSearchTree();
~QgsLabelSearchTree();

//! QgsLabelSearchTree cannot be copied.
QgsLabelSearchTree( const QgsLabelSearchTree &rh ) = delete;
//! 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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit f9a3d05

Please sign in to comment.