QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsspatialindexkdbush_p.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsspatialindexkdbush_p.h
3  -----------------
4  begin : July 2018
5  copyright : (C) 2018 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSSPATIALINDEXKDBUSH_PRIVATE_H
19 #define QGSSPATIALINDEXKDBUSH_PRIVATE_H
20 
21 #define SIP_NO_FILE
22 
24 
25 //
26 // W A R N I N G
27 // -------------
28 //
29 // This file is not part of the QGIS API. It exists purely as an
30 // implementation detail. This header file may change from version to
31 // version without notice, or even be removed.
32 //
33 
34 #include "qgsfeature.h"
36 #include "qgsfeatureiterator.h"
37 #include "qgsfeedback.h"
38 #include "qgsfeaturesource.h"
39 #include <memory>
40 #include <QList>
41 #include "kdbush.hpp"
42 
43 
44 class PointXYKDBush : public kdbush::KDBush< std::pair<double, double>, QgsSpatialIndexKDBushData, std::size_t >
45 {
46  public:
47 
48  explicit PointXYKDBush( QgsFeatureIterator &fi, QgsFeedback *feedback = nullptr )
49  {
50  fillFromIterator( fi, feedback );
51  }
52 
53  explicit PointXYKDBush( const QgsFeatureSource &source, QgsFeedback *feedback )
54  {
55  points.reserve( source.featureCount() );
56  QgsFeatureIterator it = source.getFeatures( QgsFeatureRequest().setNoAttributes() );
57  fillFromIterator( it, feedback );
58  }
59 
60  void fillFromIterator( QgsFeatureIterator &fi, QgsFeedback *feedback = nullptr )
61  {
62  std::size_t size = 0;
63 
64  QgsFeature f;
65  while ( fi.nextFeature( f ) )
66  {
67  if ( feedback && feedback->isCanceled() )
68  return;
69 
70  if ( !f.hasGeometry() )
71  continue;
72 
74  {
75  const QgsPoint *point = qgsgeometry_cast< const QgsPoint * >( f.geometry().constGet() );
76  points.emplace_back( QgsSpatialIndexKDBushData( f.id(), point->x(), point->y() ) );
77  }
78  else
79  {
80  // not a point
81  continue;
82  }
83 
84  size++;
85  }
86 
87  if ( size == 0 )
88  return;
89 
90  sortKD( 0, size - 1, 0 );
91  }
92 
93  std::size_t size() const
94  {
95  return points.size();
96  }
97 
98 };
99 
100 class QgsSpatialIndexKDBushPrivate
101 {
102  public:
103 
104  explicit QgsSpatialIndexKDBushPrivate( QgsFeatureIterator &fi, QgsFeedback *feedback = nullptr )
105  : index( qgis::make_unique < PointXYKDBush >( fi, feedback ) )
106  {}
107 
108  explicit QgsSpatialIndexKDBushPrivate( const QgsFeatureSource &source, QgsFeedback *feedback = nullptr )
109  : index( qgis::make_unique < PointXYKDBush >( source, feedback ) )
110  {}
111 
112  QAtomicInt ref = 1;
113  std::unique_ptr< PointXYKDBush > index;
114 };
115 
117 
118 #endif // QGSSPATIALINDEXKDBUSH_PRIVATE_H
QgsWkbTypes::Point
@ Point
Definition: qgswkbtypes.h:71
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
qgsfeatureiterator.h
qgsfeature.h
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsGeometry::wkbType
QgsWkbTypes::Type wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
Definition: qgsgeometry.cpp:345
QgsFeatureSource
Definition: qgsfeaturesource.h:37
qgsspatialindexkdbushdata.h
QgsFeature::id
QgsFeatureId id
Definition: qgsfeature.h:68
QgsPoint::y
double y
Definition: qgspoint.h:59
QgsFeatureRequest
Definition: qgsfeaturerequest.h:75
QgsFeedback
Definition: qgsfeedback.h:43
QgsFeatureSource::getFeatures
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const =0
Returns an iterator for the features in the source.
qgsfeaturesource.h
QgsGeometry::constGet
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Definition: qgsgeometry.cpp:128
QgsFeatureSource::featureCount
virtual long featureCount() const =0
Returns the number of features contained in the source, or -1 if the feature count is unknown.
QgsFeedback::isCanceled
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:66
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:373
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
QgsFeature
Definition: qgsfeature.h:55
QgsFeatureIterator
Definition: qgsfeatureiterator.h:263
qgsfeedback.h
QgsPoint::x
double x
Definition: qgspoint.h:58
QgsWkbTypes::flatType
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:701
QgsSpatialIndexKDBushData
Definition: qgsspatialindexkdbushdata.h:32