28using namespace Qt::StringLiterals;
32std::vector<QgsDistanceUtils::NeighborResult> QgsDistanceUtils::nearestNeighbors(
36 if ( targetPoints.empty() )
41 std::vector<QgsDistanceUtils::NeighborResult> result;
44 if ( k <= 0 ||
static_cast<size_t>( k ) >= targetPoints.size() )
46 result.reserve( targetPoints.size() );
47 for (
const auto &target : targetPoints )
54 double dist = da.
measureLine( sourcePoint, target.second );
55 result.push_back( { target.first, dist, target.second } );
57 std::sort( result.begin(), result.end(), [](
const NeighborResult &a,
const NeighborResult &b ) { return a.distance < b.distance; } );
62 auto cmp = [](
const NeighborResult &a,
const NeighborResult &b ) {
return a.distance < b.distance; };
63 std::priority_queue<NeighborResult, std::vector<NeighborResult>,
decltype( cmp )> points_queue( cmp );
65 for (
const auto &target : targetPoints )
72 double dist = da.
measureLine( sourcePoint, target.second );
74 if ( points_queue.size() <
static_cast<size_t>( k ) )
76 points_queue.push( { target.first, dist, target.second } );
78 else if ( dist < points_queue.top().distance )
81 points_queue.push( { target.first, dist, target.second } );
85 result.reserve( points_queue.size() );
86 while ( !points_queue.empty() )
88 result.push_back( points_queue.top() );
93 std::reverse( result.begin(), result.end() );
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
double measureLine(const QVector< QgsPointXY > &points) const
Measures the length of a line with multiple segments.
bool isCanceled() const
Tells whether the operation has been canceled already.
Base class for providing feedback from a processing algorithm.