QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsspatialindex.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsspatialindex.h - wrapper class for spatial index library
3 ----------------------
4 begin : December 2006
5 copyright : (C) 2006 by Martin Dobias
6 email : wonder.sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSSPATIALINDEX_H
17#define QGSSPATIALINDEX_H
18
19
20#include "qgis_sip.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
26// forward declaration
27namespace SpatialIndex SIP_SKIP
28{
29 class IStorageManager;
30 class ISpatialIndex;
31 class Region;
32 class Point;
33
35 {
36 class IBuffer;
37 }
38}
39
40class QgsFeedback;
41class QgsFeature;
42class QgsRectangle;
43class QgsPointXY;
44
45#include "qgis_core.h"
46#include "qgsfeaturesink.h"
47#include <QList>
48#include <QSharedDataPointer>
49
50#include "qgsfeature.h"
51
52class QgsSpatialIndexData;
55
71class CORE_EXPORT QgsSpatialIndex : public QgsFeatureSink
72{
73
74 public:
75
76 /* creation of spatial index */
77
79 enum Flag SIP_ENUM_BASETYPE( IntFlag )
80 {
82 };
83 Q_DECLARE_FLAGS( Flags, Flag )
84
85
89
99 explicit QgsSpatialIndex( const QgsFeatureIterator &fi, QgsFeedback *feedback = nullptr, QgsSpatialIndex::Flags flags = QgsSpatialIndex::Flags() );
100
101#ifndef SIP_RUN
102
114 explicit QgsSpatialIndex( const QgsFeatureIterator &fi, const std::function< bool( const QgsFeature & ) > &callback, QgsSpatialIndex::Flags flags = QgsSpatialIndex::Flags() );
115#endif
116
127 explicit QgsSpatialIndex( const QgsFeatureSource &source, QgsFeedback *feedback = nullptr, QgsSpatialIndex::Flags flags = QgsSpatialIndex::Flags() );
128
129 QgsSpatialIndex( const QgsSpatialIndex &other );
130
132 ~QgsSpatialIndex() override;
133
135
136 /* operations */
137
142 Q_DECL_DEPRECATED bool insertFeature( const QgsFeature &feature ) SIP_DEPRECATED;
143
151 bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
152
154
162 bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
163
169 Q_DECL_DEPRECATED bool insertFeature( QgsFeatureId id, const QgsRectangle &bounds ) SIP_DEPRECATED;
170
176 bool addFeature( QgsFeatureId id, const QgsRectangle &bounds );
177
181 bool deleteFeature( const QgsFeature &feature );
182
188 bool deleteFeature( QgsFeatureId id, const QgsRectangle &bounds );
189
190 /* queries */
191
199 QList<QgsFeatureId> intersects( const QgsRectangle &rectangle ) const;
200
217 QList<QgsFeatureId> nearestNeighbor( const QgsPointXY &point, int neighbors = 1, double maxDistance = 0 ) const;
218
237 QList<QgsFeatureId> nearestNeighbor( const QgsGeometry &geometry, int neighbors = 1, double maxDistance = 0 ) const;
238
239#ifndef SIP_RUN
240
249
250#else
251
261 SIP_PYOBJECT geometry( QgsFeatureId id ) const SIP_TYPEHINT( QgsGeometry );
262 % MethodCode
263 auto g = std::make_unique< QgsGeometry >( sipCpp->geometry( a0 ) );
264 if ( g->isNull() )
265 {
266 PyErr_SetString( PyExc_KeyError, u"No geometry with feature id %1 exists in the index."_s.arg( a0 ).toUtf8().constData() );
267 sipIsErr = 1;
268 }
269 else
270 {
271 sipRes = sipConvertFromType( g.release(), sipType_QgsGeometry, Py_None );
272 }
273 % End
274#endif
275
276 /* debugging */
277
279 QAtomicInt SIP_PYALTERNATIVETYPE( int ) refs() const;
280
281 private:
282
291 static bool featureInfo( const QgsFeature &f, SpatialIndex::Region &r, QgsFeatureId &id ) SIP_SKIP;
292
301 static bool featureInfo( const QgsFeature &f, QgsRectangle &rect, QgsFeatureId &id );
302
303 friend class QgsFeatureIteratorDataStream; // for access to featureInfo()
304
305 private:
306
307 QSharedDataPointer<QgsSpatialIndexData> d;
308
309};
310
312
313#endif //QGSSPATIALINDEX_H
Wrapper for iterator of features from vector data provider or vector layer.
An interface for objects which accept features via addFeature(s) methods.
virtual bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())
Adds a single feature to the sink.
virtual bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())=0
Adds a list of features to the sink.
QFlags< Flag > Flags
An interface for objects which provide features via a getFeatures method.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
A geometry is the spatial representation of a feature.
Represents a 2D point.
Definition qgspointxy.h:62
A rectangle specified with double values.
Flag
Flags controlling index behavior.
@ FlagStoreFeatureGeometries
Indicates that the spatial index should also store feature geometries. This requires more memory,...
QgsSpatialIndex & operator=(const QgsSpatialIndex &other)
QgsSpatialIndex(QgsSpatialIndex::Flags flags=QgsSpatialIndex::Flags())
Constructor for QgsSpatialIndex.
QList< QgsFeatureId > nearestNeighbor(const QgsPointXY &point, int neighbors=1, double maxDistance=0) const
Returns nearest neighbors to a point.
QList< QgsFeatureId > intersects(const QgsRectangle &rectangle) const
Returns a list of features with a bounding box which intersects the specified rectangle.
friend class QgsFeatureIteratorDataStream
QFlags< Flag > Flags
Q_DECL_DEPRECATED bool insertFeature(const QgsFeature &feature)
Adds a feature to the index.
QgsGeometry geometry(QgsFeatureId id) const
Returns the stored geometry for the indexed feature with matching id.
QgsSpatialIndex(const QgsFeatureIterator &fi, const std::function< bool(const QgsFeature &) > &callback, QgsSpatialIndex::Flags flags=QgsSpatialIndex::Flags())
Constructor - creates R-tree and bulk loads it with features from the iterator.
bool deleteFeature(const QgsFeature &feature)
Removes a feature from the index.
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:240
#define SIP_PYALTERNATIVETYPE(type)
Definition qgis_sip.h:149
#define SIP_DEPRECATED
Definition qgis_sip.h:114
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:275
#define SIP_SKIP
Definition qgis_sip.h:134
QList< QgsFeature > QgsFeatureList
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)