QGIS API Documentation 3.99.0-Master (357b655ed83)
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#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 <functional>
35#include <memory>
36
37#include "kdbush.hpp"
38#include "qgsfeature.h"
39#include "qgsfeatureiterator.h"
40#include "qgsfeaturesource.h"
41#include "qgsfeedback.h"
43
44#include <QList>
45
46class PointXYKDBush : public kdbush::KDBush< std::pair<double, double>, QgsSpatialIndexKDBushData, std::size_t >
47{
48 public:
49
50 explicit PointXYKDBush( QgsFeatureIterator &fi, QgsFeedback *feedback = nullptr, const std::function< bool( const QgsFeature & ) > *callback = nullptr )
51 {
52 fillFromIterator( fi, feedback, callback );
53 }
54
55 explicit PointXYKDBush( const QgsFeatureSource &source, QgsFeedback *feedback )
56 {
57 points.reserve( source.featureCount() );
58 QgsFeatureIterator it = source.getFeatures( QgsFeatureRequest().setNoAttributes() );
59 fillFromIterator( it, feedback, nullptr );
60 }
61
62 PointXYKDBush()
63 {
64 }
65
66 void fillFromIterator( QgsFeatureIterator &fi, QgsFeedback *feedback = nullptr, const std::function< bool( const QgsFeature & ) > *callback = nullptr )
67 {
68 std::size_t size = 0;
69
70 QgsFeature f;
71 while ( fi.nextFeature( f ) )
72 {
73 if ( feedback && feedback->isCanceled() )
74 return;
75
76 if ( callback && !( *callback )( f ) )
77 return;
78
79 if ( !f.hasGeometry() )
80 continue;
81
83 {
85 points.emplace_back( QgsSpatialIndexKDBushData( f.id(), point->x(), point->y() ) );
86 }
87 else
88 {
89 // not a point
90 continue;
91 }
92
93 size++;
94 }
95
96 if ( size == 0 )
97 return;
98
99 sortKD( 0, size - 1, 0 );
100 finalized = true;
101 }
102
103 bool addFeature( QgsFeatureId id, const QgsPointXY point )
104 {
105 if ( finalized )
106 return false;
107 points.emplace_back( QgsSpatialIndexKDBushData( id, point.x(), point.y() ) );
108 return true;
109 }
110
111 void finalize()
112 {
113 if ( !finalized && !points.empty() )
114 {
115 sortKD( 0, points.size() - 1, 0 );
116 finalized = true;
117 }
118 }
119
120 std::size_t size() const
121 {
122 return points.size();
123 }
124
125 bool finalized = false;
126
127};
128
129class QgsSpatialIndexKDBushPrivate
130{
131 public:
132
133 explicit QgsSpatialIndexKDBushPrivate( QgsFeatureIterator &fi, QgsFeedback *feedback = nullptr )
134 : index( std::make_unique < PointXYKDBush >( fi, feedback ) )
135 {}
136
137 explicit QgsSpatialIndexKDBushPrivate( const QgsFeatureSource &source, QgsFeedback *feedback = nullptr )
138 : index( std::make_unique < PointXYKDBush >( source, feedback ) )
139 {}
140
141 explicit QgsSpatialIndexKDBushPrivate( QgsFeatureIterator &fi, const std::function< bool( const QgsFeature & ) > &callback, QgsFeedback *feedback = nullptr )
142 : index( std::make_unique < PointXYKDBush >( fi, feedback, &callback ) )
143 {}
144
145 explicit QgsSpatialIndexKDBushPrivate()
146 : index( std::make_unique < PointXYKDBush >() )
147 {}
148
149 QAtomicInt ref = 1;
150 std::unique_ptr< PointXYKDBush > index;
151};
152
154
155#endif // QGSSPATIALINDEXKDBUSH_PRIVATE_H
@ Point
Point.
Definition qgis.h:282
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
An interface for objects which provide features via a getFeatures method.
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.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsFeatureId id
Definition qgsfeature.h:68
QgsGeometry geometry
Definition qgsfeature.h:71
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
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.).
Represents a 2D point.
Definition qgspointxy.h:62
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
double x
Definition qgspoint.h:56
double y
Definition qgspoint.h:57
A container for data stored inside a QgsSpatialIndexKDBush index.
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