QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 // forward declaration
23 namespace SpatialIndex SIP_SKIP
24 {
25  class IStorageManager;
26  class ISpatialIndex;
27  class Region;
28  class Point;
29 
30  namespace StorageManager
31  {
32  class IBuffer;
33  }
34 }
35 
36 class QgsFeedback;
37 class QgsFeature;
38 class QgsRectangle;
39 class QgsPointXY;
40 
41 #include "qgis_core.h"
42 #include "qgsfeaturesink.h"
43 #include <QList>
44 #include <QSharedDataPointer>
45 
46 #include "qgsfeature.h"
47 
48 class QgsSpatialIndexData;
49 class QgsFeatureIterator;
50 class QgsFeatureSource;
51 
67 class CORE_EXPORT QgsSpatialIndex : public QgsFeatureSink
68 {
69 
70  public:
71 
72  /* creation of spatial index */
73 
75  enum Flag
76  {
77  FlagStoreFeatureGeometries = 1 << 0,
78  };
79  Q_DECLARE_FLAGS( Flags, Flag )
80 
81 
84  QgsSpatialIndex( QgsSpatialIndex::Flags flags = QgsSpatialIndex::Flags() );
85 
96  explicit QgsSpatialIndex( const QgsFeatureIterator &fi, QgsFeedback *feedback = nullptr, QgsSpatialIndex::Flags flags = QgsSpatialIndex::Flags() );
97 
98 #ifndef SIP_RUN
99 
112  explicit QgsSpatialIndex( const QgsFeatureIterator &fi, const std::function< bool( const QgsFeature & ) > &callback, QgsSpatialIndex::Flags flags = QgsSpatialIndex::Flags() );
113 #endif
114 
126  explicit QgsSpatialIndex( const QgsFeatureSource &source, QgsFeedback *feedback = nullptr, QgsSpatialIndex::Flags flags = QgsSpatialIndex::Flags() );
127 
129  QgsSpatialIndex( const QgsSpatialIndex &other );
130 
132  ~QgsSpatialIndex() override;
133 
135  QgsSpatialIndex &operator=( const QgsSpatialIndex &other );
136 
137  /* operations */
138 
143  Q_DECL_DEPRECATED bool insertFeature( const QgsFeature &feature ) SIP_DEPRECATED;
144 
152  bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
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 
182 
183  /* queries */
184 
192  QList<QgsFeatureId> intersects( const QgsRectangle &rectangle ) const;
193 
210  QList<QgsFeatureId> nearestNeighbor( const QgsPointXY &point, int neighbors = 1, double maxDistance = 0 ) const;
211 
230  QList<QgsFeatureId> nearestNeighbor( const QgsGeometry &geometry, int neighbors = 1, double maxDistance = 0 ) const;
231 
232 #ifndef SIP_RUN
233 
241  QgsGeometry geometry( QgsFeatureId id ) const;
242 
243 #else
244 
253  SIP_PYOBJECT geometry( QgsFeatureId id ) const SIP_TYPEHINT( QgsGeometry );
254  % MethodCode
255  std::unique_ptr< QgsGeometry > g = qgis::make_unique< QgsGeometry >( sipCpp->geometry( a0 ) );
256  if ( g->isNull() )
257  {
258  PyErr_SetString( PyExc_KeyError, QStringLiteral( "No geometry with feature id %1 exists in the index." ).arg( a0 ).toUtf8().constData() );
259  sipIsErr = 1;
260  }
261  else
262  {
263  sipRes = sipConvertFromType( g.release(), sipType_QgsGeometry, Py_None );
264  }
265  % End
266 #endif
267 
268  /* debugging */
269 
271  QAtomicInt SIP_PYALTERNATIVETYPE( int ) refs() const;
272 
273  private:
274 
283  static bool featureInfo( const QgsFeature &f, SpatialIndex::Region &r, QgsFeatureId &id ) SIP_SKIP;
284 
294  static bool featureInfo( const QgsFeature &f, QgsRectangle &rect, QgsFeatureId &id );
295 
296  friend class QgsFeatureIteratorDataStream; // for access to featureInfo()
297 
298  private:
299 
300  QSharedDataPointer<QgsSpatialIndexData> d;
301 
302 };
303 
304 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsSpatialIndex::Flags )
305 
306 #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.
Flag
Flags controlling how features are added to a sink.
virtual bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())=0
Adds a list of features to the sink.
An interface for objects which provide features via a getFeatures method.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
A class to represent a 2D point.
Definition: qgspointxy.h:44
A rectangle specified with double values.
Definition: qgsrectangle.h:42
A spatial index for QgsFeature objects.
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.
#define SIP_TYPEHINT(type)
Definition: qgis_sip.h:218
#define SIP_PYALTERNATIVETYPE(type)
Definition: qgis_sip.h:136
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_SKIP
Definition: qgis_sip.h:126
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:614
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.