QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 = nullptr );
85 
96  explicit QgsSpatialIndex( const QgsFeatureIterator &fi, QgsFeedback *feedback = nullptr, QgsSpatialIndex::Flags flags = nullptr );
97 
109  explicit QgsSpatialIndex( const QgsFeatureSource &source, QgsFeedback *feedback = nullptr, QgsSpatialIndex::Flags flags = nullptr );
110 
112  QgsSpatialIndex( const QgsSpatialIndex &other );
113 
115  ~QgsSpatialIndex() override;
116 
118  QgsSpatialIndex &operator=( const QgsSpatialIndex &other );
119 
120  /* operations */
121 
126  Q_DECL_DEPRECATED bool insertFeature( const QgsFeature &feature ) SIP_DEPRECATED;
127 
135  bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = nullptr ) override;
136 
144  bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = nullptr ) override;
145 
151  Q_DECL_DEPRECATED bool insertFeature( QgsFeatureId id, const QgsRectangle &bounds ) SIP_DEPRECATED;
152 
158  bool addFeature( QgsFeatureId id, const QgsRectangle &bounds );
159 
163  bool deleteFeature( const QgsFeature &feature );
164 
165 
166  /* queries */
167 
175  QList<QgsFeatureId> intersects( const QgsRectangle &rectangle ) const;
176 
193  QList<QgsFeatureId> nearestNeighbor( const QgsPointXY &point, int neighbors = 1, double maxDistance = 0 ) const;
194 
213  QList<QgsFeatureId> nearestNeighbor( const QgsGeometry &geometry, int neighbors = 1, double maxDistance = 0 ) const;
214 
215 #ifndef SIP_RUN
216 
224  QgsGeometry geometry( QgsFeatureId id ) const;
225 
226 #else
227 
236  SIP_PYOBJECT geometry( QgsFeatureId id ) const SIP_TYPEHINT( QgsGeometry );
237  % MethodCode
238  std::unique_ptr< QgsGeometry > g = qgis::make_unique< QgsGeometry >( sipCpp->geometry( a0 ) );
239  if ( g->isNull() )
240  {
241  PyErr_SetString( PyExc_KeyError, QStringLiteral( "No geometry with feature id %1 exists in the index." ).arg( a0 ).toUtf8().constData() );
242  sipIsErr = 1;
243  }
244  else
245  {
246  sipRes = sipConvertFromType( g.release(), sipType_QgsGeometry, Py_None );
247  }
248  % End
249 #endif
250 
251  /* debugging */
252 
254  QAtomicInt SIP_PYALTERNATIVETYPE( int ) refs() const;
255 
256  private:
257 
258  static SpatialIndex::Region rectToRegion( const QgsRectangle &rect );
259 
268  static bool featureInfo( const QgsFeature &f, SpatialIndex::Region &r, QgsFeatureId &id ) SIP_SKIP;
269 
279  static bool featureInfo( const QgsFeature &f, QgsRectangle &rect, QgsFeatureId &id );
280 
281  friend class QgsFeatureIteratorDataStream; // for access to featureInfo()
282 
283  private:
284 
285  QSharedDataPointer<QgsSpatialIndexData> d;
286 
287 };
288 
289 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsSpatialIndex::Flags )
290 
291 #endif //QGSSPATIALINDEX_H
Wrapper for iterator of features from vector data provider or vector layer.
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:571
A class to represent a 2D point.
Definition: qgspointxy.h:43
An interface for objects which accept features via addFeature(s) methods.
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
#define SIP_TYPEHINT(type)
Definition: qgis_sip.h:206
Base class for feedback objects to be used for cancellation of something running in a worker thread...
Definition: qgsfeedback.h:44
#define SIP_SKIP
Definition: qgis_sip.h:119
#define SIP_DEPRECATED
Definition: qgis_sip.h:99
A spatial index for QgsFeature objects.
An interface for objects which provide features via a getFeatures method.
Flag
Flags controlling how features are added to a sink.
#define SIP_PYALTERNATIVETYPE(type)
Definition: qgis_sip.h:129