Skip to content

Commit

Permalink
Start moving some spatial index related functions to QgsSpatialIndexU…
Browse files Browse the repository at this point in the history
…tils
  • Loading branch information
nyalldawson committed Dec 10, 2019
1 parent 358e466 commit 69844ec
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -357,6 +357,7 @@ SET(QGIS_CORE_SRCS
qgssnappingutils.cpp
qgsspatialindex.cpp
qgsspatialindexkdbush.cpp
qgsspatialindexutils.cpp
qgssqlexpressioncompiler.cpp
qgssqliteexpressioncompiler.cpp
qgssqlstatement.cpp
Expand Down Expand Up @@ -858,6 +859,7 @@ SET(QGIS_CORE_HDRS
qgsspatialindex.h
qgsspatialindexkdbush.h
qgsspatialindexkdbushdata.h
qgsspatialindexutils.h
qgsspatialiteutils.h
qgssqlexpressioncompiler.h
qgssqliteutils.h
Expand Down
16 changes: 5 additions & 11 deletions src/core/qgsspatialindex.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgslogger.h"
#include "qgsfeaturesource.h"
#include "qgsfeedback.h"
#include "qgsspatialindexutils.h"

#include <spatialindex/SpatialIndex.h>
#include <QMutex>
Expand Down Expand Up @@ -355,20 +356,13 @@ QgsSpatialIndex &QgsSpatialIndex::operator=( const QgsSpatialIndex &other )
return *this;
}

SpatialIndex::Region QgsSpatialIndex::rectToRegion( const QgsRectangle &rect )
{
double pt1[2] = { rect.xMinimum(), rect.yMinimum() },
pt2[2] = { rect.xMaximum(), rect.yMaximum() };
return SpatialIndex::Region( pt1, pt2, 2 );
}

bool QgsSpatialIndex::featureInfo( const QgsFeature &f, SpatialIndex::Region &r, QgsFeatureId &id )
{
QgsRectangle rect;
if ( !featureInfo( f, rect, id ) )
return false;

r = rectToRegion( rect );
r = QgsSpatialIndexUtils::rectangleToRegion( rect );
return true;
}

Expand Down Expand Up @@ -429,7 +423,7 @@ bool QgsSpatialIndex::insertFeature( QgsFeatureId id, const QgsRectangle &bounds

bool QgsSpatialIndex::addFeature( QgsFeatureId id, const QgsRectangle &bounds )
{
SpatialIndex::Region r( rectToRegion( bounds ) );
SpatialIndex::Region r( QgsSpatialIndexUtils::rectangleToRegion( bounds ) );

QMutexLocker locker( &d->mMutex );

Expand Down Expand Up @@ -476,7 +470,7 @@ QList<QgsFeatureId> QgsSpatialIndex::intersects( const QgsRectangle &rect ) cons
QList<QgsFeatureId> list;
QgisVisitor visitor( list );

SpatialIndex::Region r = rectToRegion( rect );
SpatialIndex::Region r = QgsSpatialIndexUtils::rectangleToRegion( rect );

QMutexLocker locker( &d->mMutex );
d->mRTree->intersectsWithQuery( r, visitor );
Expand Down Expand Up @@ -515,7 +509,7 @@ QList<QgsFeatureId> QgsSpatialIndex::nearestNeighbor( const QgsGeometry &geometr
QList<QgsFeatureId> list;
QgisVisitor visitor( list );

SpatialIndex::Region r = rectToRegion( geometry.boundingBox() );
SpatialIndex::Region r = QgsSpatialIndexUtils::rectangleToRegion( geometry.boundingBox() );

QMutexLocker locker( &d->mMutex );
QgsNearestNeighborComparator nnc( d->mFlags & QgsSpatialIndex::FlagStoreFeatureGeometries ? &d->mGeometries : nullptr,
Expand Down
2 changes: 0 additions & 2 deletions src/core/qgsspatialindex.h
Expand Up @@ -255,8 +255,6 @@ class CORE_EXPORT QgsSpatialIndex : public QgsFeatureSink

private:

static SpatialIndex::Region rectToRegion( const QgsRectangle &rect );

/**
* Calculates feature info to insert into index.
* \param f input feature
Expand Down
25 changes: 25 additions & 0 deletions src/core/qgsspatialindexutils.cpp
@@ -0,0 +1,25 @@
/***************************************************************************
qgsspatialindexutils.cpp
------------------------
Date : December 2019
Copyright : (C) 2019 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsspatialindexutils.h"
#include "qgsrectangle.h"
#include <spatialindex/SpatialIndex.h>

SpatialIndex::Region QgsSpatialIndexUtils::rectangleToRegion( const QgsRectangle &rectangle )
{
double pt1[2] = { rectangle.xMinimum(), rectangle.yMinimum() };
double pt2[2] = { rectangle.xMaximum(), rectangle.yMaximum() };
return SpatialIndex::Region( pt1, pt2, 2 );
}
60 changes: 60 additions & 0 deletions src/core/qgsspatialindexutils.h
@@ -0,0 +1,60 @@
/***************************************************************************
qgsgenericspatialindex.h
------------------------
Date : December 2019
Copyright : (C) 2019 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSSPATIALINDEXUTILS_H
#define QGSSPATIALINDEXUTILS_H

#include "qgis_core.h"
#define SIP_NO_FILE

class QgsRectangle;

///@cond PRIVATE
// forward declaration
namespace SpatialIndex
{
class IStorageManager;
class ISpatialIndex;
class Region;
class Point;

namespace StorageManager
{
class IBuffer;
}
}
///@endcond

/**
* \ingroup core
* \class QgsSpatialIndex
*
* Contains utility functions for working with spatial indexes.
*
* \note Not available in Python bindings.
* \since QGIS 3.12
*/
class CORE_EXPORT QgsSpatialIndexUtils
{
public:

/**
* Converts a QGIS \a rectangle to a SpatialIndex region.
*/
static SpatialIndex::Region rectangleToRegion( const QgsRectangle &rectangle );

};

#endif // QGSSPATIALINDEXUTILS_H

0 comments on commit 69844ec

Please sign in to comment.