mirror of
				https://github.com/qgis/QGIS.git
				synced 2025-11-04 00:04:25 -05:00 
			
		
		
		
	A very fast static spatial index for 2D points based on a flat KD-tree, using https://github.com/mourner/kdbush.hpp Compared to QgsSpatialIndex, this index: - supports single point features only (no multipoints) - is static (features cannot be added or removed from the index after construction) - is much faster! - supports true "distance based" searches, i.e. return all points within a radius from a search point
		
			
				
	
	
		
			34 lines
		
	
	
		
			842 B
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			34 lines
		
	
	
		
			842 B
		
	
	
	
		
			C++
		
	
	
	
	
	
#include "include/kdbush.hpp"
 | 
						|
 | 
						|
#include <chrono>
 | 
						|
#include <iostream>
 | 
						|
#include <random>
 | 
						|
#include <vector>
 | 
						|
 | 
						|
int main() {
 | 
						|
    std::mt19937 gen(0);
 | 
						|
    std::uniform_int_distribution<> dis(-10000, 10000);
 | 
						|
 | 
						|
    using Point = std::pair<int, int>;
 | 
						|
 | 
						|
    const std::size_t num_points = 1000000;
 | 
						|
 | 
						|
    std::vector<Point> points;
 | 
						|
    points.reserve(num_points);
 | 
						|
 | 
						|
    for (std::size_t i = 0; i < num_points; i++) {
 | 
						|
        points.emplace_back(dis(gen), dis(gen));
 | 
						|
    }
 | 
						|
 | 
						|
    const auto started = std::chrono::high_resolution_clock::now();
 | 
						|
    kdbush::KDBush<Point> index(points);
 | 
						|
    const auto finished = std::chrono::high_resolution_clock::now();
 | 
						|
 | 
						|
    const auto duration =
 | 
						|
        std::chrono::duration_cast<std::chrono::nanoseconds>(finished - started).count();
 | 
						|
 | 
						|
    std::cerr << "indexed 1M points in " << (duration / 1e6) << "ms\n";
 | 
						|
 | 
						|
    return 0;
 | 
						|
}
 |