QGIS API Documentation 3.99.0-Master (0c964c3d988)
Loading...
Searching...
No Matches
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
23
24//
25// W A R N I N G
26// -------------
27//
28// This file is not part of the QGIS API. It exists purely as an
29// implementation detail. This header file may change from version to
30// version without notice, or even be removed.
31//
32
33#include <functional>
34#include <memory>
35
36#include "kdbush.hpp"
37#include "qgsfeature.h"
38#include "qgsfeatureiterator.h"
39#include "qgsfeaturesource.h"
40#include "qgsfeedback.h"
42
43#include <QList>
44
45#define SIP_NO_FILE
46
47class PointXYKDBush : public kdbush::KDBush< std::pair<double, double>, QgsSpatialIndexKDBushData, std::size_t >
48{
49 public:
50
51 explicit PointXYKDBush( QgsFeatureIterator &fi, QgsFeedback *feedback = nullptr, const std::function< bool( const QgsFeature & ) > *callback = nullptr )
52 {
53 fillFromIterator( fi, feedback, callback );
54 }
55
56 explicit PointXYKDBush( const QgsFeatureSource &source, QgsFeedback *feedback )
57 {
58 points.reserve( source.featureCount() );
59 QgsFeatureIterator it = source.getFeatures( QgsFeatureRequest().setNoAttributes() );
60 fillFromIterator( it, feedback, nullptr );
61 }
62
63 PointXYKDBush()
64 {
65 }
66
67 void fillFromIterator( QgsFeatureIterator &fi, QgsFeedback *feedback = nullptr, const std::function< bool( const QgsFeature & ) > *callback = nullptr )
68 {
69 std::size_t size = 0;
70
71 QgsFeature f;
72 while ( fi.nextFeature( f ) )
73 {
74 if ( feedback && feedback->isCanceled() )
75 return;
76
77 if ( callback && !( *callback )( f ) )
78 return;
79
80 if ( !f.hasGeometry() )
81 continue;
82
84 {
85 const QgsPoint *point = qgsgeometry_cast< const QgsPoint * >( f.geometry().constGet() );
86 points.emplace_back( QgsSpatialIndexKDBushData( f.id(), point->x(), point->y() ) );
87 }
88 else
89 {
90 // not a point
91 continue;
92 }
93
94 size++;
95 }
96
97 if ( size == 0 )
98 return;
99
100 sortKD( 0, size - 1, 0 );
101 finalized = true;
102 }
103
104 bool addFeature( QgsFeatureId id, const QgsPointXY point )
105 {
106 if ( finalized )
107 return false;
108 points.emplace_back( QgsSpatialIndexKDBushData( id, point.x(), point.y() ) );
109 return true;
110 }
111
112 void finalize()
113 {
114 if ( !finalized && !points.empty() )
115 {
116 sortKD( 0, points.size() - 1, 0 );
117 finalized = true;
118 }
119 }
120
121 std::size_t size() const
122 {
123 return points.size();
124 }
125
126 bool finalized = false;
127
128};
129
130class QgsSpatialIndexKDBushPrivate
131{
132 public:
133
134 explicit QgsSpatialIndexKDBushPrivate( QgsFeatureIterator &fi, QgsFeedback *feedback = nullptr )
135 : index( std::make_unique < PointXYKDBush >( fi, feedback ) )
136 {}
137
138 explicit QgsSpatialIndexKDBushPrivate( const QgsFeatureSource &source, QgsFeedback *feedback = nullptr )
139 : index( std::make_unique < PointXYKDBush >( source, feedback ) )
140 {}
141
142 explicit QgsSpatialIndexKDBushPrivate( QgsFeatureIterator &fi, const std::function< bool( const QgsFeature & ) > &callback, QgsFeedback *feedback = nullptr )
143 : index( std::make_unique < PointXYKDBush >( fi, feedback, &callback ) )
144 {}
145
146 explicit QgsSpatialIndexKDBushPrivate()
147 : index( std::make_unique < PointXYKDBush >() )
148 {}
149
150 QAtomicInt ref = 1;
151 std::unique_ptr< PointXYKDBush > index;
152};
153
155
156#endif // QGSSPATIALINDEXKDBUSH_PRIVATE_H
@ Point
Point.
Definition qgis.h:293
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const =0
Returns an iterator for the features in the source.
virtual long long featureCount() const =0
Returns the number of features contained in the source, or -1 if the feature count is unknown.
QgsFeatureId id
Definition qgsfeature.h:68
QgsGeometry geometry
Definition qgsfeature.h:71
bool hasGeometry() const
Returns true if the feature has an associated geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:55
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Qgis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.).
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
double x
Definition qgspoint.h:56
double y
Definition qgspoint.h:57
static Qgis::WkbType flatType(Qgis::WkbType type)
Returns the flat type for a WKB type.
T qgsgeometry_cast(QgsAbstractGeometry *geom)
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features