QGIS API Documentation 4.1.0-Master (60fea48833c)
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} //namespace SpatialIndex
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 public:
74 /* creation of spatial index */
75
77 enum Flag SIP_ENUM_BASETYPE( IntFlag )
78 {
80 = 1 << 0,
81 };
82 Q_DECLARE_FLAGS( Flags, Flag )
83
84
88
98 explicit QgsSpatialIndex( const QgsFeatureIterator &fi, QgsFeedback *feedback = nullptr, QgsSpatialIndex::Flags flags = QgsSpatialIndex::Flags() );
99
100#ifndef SIP_RUN
101
113 explicit QgsSpatialIndex( const QgsFeatureIterator &fi, const std::function< bool( const QgsFeature & ) > &callback, QgsSpatialIndex::Flags flags = QgsSpatialIndex::Flags() );
114#endif
115
126 explicit QgsSpatialIndex( const QgsFeatureSource &source, QgsFeedback *feedback = nullptr, QgsSpatialIndex::Flags flags = QgsSpatialIndex::Flags() );
127
128 QgsSpatialIndex( const QgsSpatialIndex &other );
129
131 ~QgsSpatialIndex() override;
132
134
135 /* operations */
136
141 Q_DECL_DEPRECATED bool insertFeature( const QgsFeature &feature ) SIP_DEPRECATED;
142
150 bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
151
153
161 bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
162
168 Q_DECL_DEPRECATED bool insertFeature( QgsFeatureId id, const QgsRectangle &bounds ) SIP_DEPRECATED;
169
175 bool addFeature( QgsFeatureId id, const QgsRectangle &bounds );
176
180 bool deleteFeature( const QgsFeature &feature );
181
187 bool deleteFeature( QgsFeatureId id, const QgsRectangle &bounds );
188
189 /* queries */
190
198 QList<QgsFeatureId> intersects( const QgsRectangle &rectangle ) const;
199
216 QList<QgsFeatureId> nearestNeighbor( const QgsPointXY &point, int neighbors = 1, double maxDistance = 0 ) const;
217
236 QList<QgsFeatureId> nearestNeighbor( const QgsGeometry &geometry, int neighbors = 1, double maxDistance = 0 ) const;
237
238#ifndef SIP_RUN
239
248
249#else
250 // clang-format off
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// clang-format on
275#endif
276
277 /* debugging */
278
280 QAtomicInt SIP_PYALTERNATIVETYPE( int ) refs() const;
281
282 private:
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 QSharedDataPointer<QgsSpatialIndexData> d;
307};
308
310
311#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:239
#define SIP_PYALTERNATIVETYPE(type)
Definition qgis_sip.h:148
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:274
#define SIP_SKIP
Definition qgis_sip.h:133
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)