Ticket #10421: test_boost_geo2_index.cc

File test_boost_geo2_index.cc, 4.4 KB (added by awulkiew, 8 years ago)

The R-tree test exposing the bug.

Line 
1// Original version: Ola Martin Lykkja
2// Tweaks: Adam Wulkiewicz
3
4// This application tests different ways of searching for nearest points in spherical_equatorial CS
5// 1. std::sort()
6// 2. rtree.insert()
7// 3. rtree pack-create
8// The method 1 is considered as producing the correct results
9
10#include <boost/geometry.hpp>
11#include <boost/geometry/geometries/point.hpp>
12#include <boost/geometry/index/rtree.hpp>
13#include <boost/foreach.hpp>
14namespace bg = boost::geometry;
15namespace bgi = boost::geometry::index;
16
17// Standard C++ includes
18#include <vector>
19#include <algorithm>
20#include <iostream>
21
22typedef struct pos_s {
23 double lat;
24 double lon;
25} pos_t;
26
27typedef struct poi_s {
28 int id;
29 const char *name;
30 double lat;
31 double lon;
32} poi_t;
33
34// This includefile contains a lot of data points.
35#include "test_boost_geo2_data.h"
36
37// Calculate distance on earth surface
38template <typename Pt>
39double geo_calculate_distance(Pt const& from, Pt const& to)
40{
41 double res = bg::comparable_distance(from, to);
42 /*return bg::distance(geo_point(from->lon, from->lat),
43 geo_point(to->lon, to->lat),
44 bg::strategy::distance::haversine<double>(6371.0 * 1000.0));*/
45
46 return res * 100000000.0;
47}
48
49template <typename Pt>
50class sort_geo_poi_on_distance
51{
52private:
53 const Pt &zero;
54public:
55 sort_geo_poi_on_distance(const Pt &z) : zero(z) { }
56
57 template <typename V>
58 bool operator()(V const & a, V const & b) const
59 {
60 double ad = geo_calculate_distance(zero, a.first);
61 double bd = geo_calculate_distance(zero, b.first);
62 return ad < bd;
63 }
64};
65
66// Print result list
67template<typename T>
68void print(const bg::model::point<double, 2, T> &target,
69 const std::vector< std::pair< bg::model::point<double, 2, T>, const poi_t *> > & res,
70 const std::vector< std::pair< bg::model::point<double, 2, T>, const poi_t *> > & expected)
71{
72 typedef bg::model::point<double, 2, T> geo_point;
73 typedef std::pair<geo_point, const poi_t *> geo_value;
74
75 std::cout << "t:" << bg::get<0>(target) << "," << bg::get<1>(target) << "\n";
76
77 // Display results
78 unsigned int idx = 0;
79 BOOST_FOREACH(geo_value const& v, res)
80 {
81 pos_t poi_pos; poi_pos.lat=bg::get<1>(v.first); poi_pos.lon=bg::get<0>(v.first);
82 double dist = geo_calculate_distance(target, v.first);
83 bool correct = (expected.size() > idx && expected[idx].second->id == v.second->id);
84
85 printf(" %9.6f,%9.6f %6d %-40s %8.2f", poi_pos.lon, poi_pos.lat, v.second->id, v.second->name, dist);
86 if (!expected.empty())
87 printf(correct ? " Ok" : " Wrong");
88 printf("\n");
89 idx++;
90 }
91}
92
93
94
95int main()
96{
97 typedef bg::model::point<double, 2, bg::cs::spherical_equatorial<bg::degree> > geo_point;
98 typedef bg::model::box<geo_point> geo_box;
99 typedef std::pair<geo_point, const poi_t *> geo_poi_value;
100 typedef bgi::rtree< geo_poi_value, bgi::dynamic_rstar > poi_rtree;
101
102 size_t knn = 20;
103 size_t max = 30000;
104
105 std::vector<geo_poi_value> values;
106 poi_rtree rtree(bgi::dynamic_rstar(32));
107 size_t i = 0;
108 for (const poi_t *poi = get_poi_list(); poi->id && i < max; poi++, ++i)
109 {
110 geo_point pt(poi->lon, poi->lat);
111 values.push_back(std::make_pair(pt, poi));
112 rtree.insert(std::make_pair(pt, poi));
113 }
114 poi_rtree pack_rtree(values.begin(), values.end(), bgi::dynamic_rstar(32));
115
116 const poi_t *poi = get_poi_list();
117 geo_point target(poi->lon, poi->lat);
118
119 std::sort(values.begin(), values.end(), sort_geo_poi_on_distance<geo_point>(target));
120 values.resize(knn);
121
122 std::vector<geo_poi_value> result;
123 rtree.query(bgi::nearest(target, knn), std::back_inserter(result));
124 std::sort(result.begin(), result.end(), sort_geo_poi_on_distance<geo_point>(target));
125
126 std::vector<geo_poi_value> result2;
127 pack_rtree.query(bgi::nearest(target, knn), std::back_inserter(result2));
128 std::sort(result2.begin(), result2.end(), sort_geo_poi_on_distance<geo_point>(target));
129
130 std::cout << "------------------------------------\n\n";
131 print(target, values, values);
132 std::cout << "------------------------------------\n\n";
133 print(target, result, values);
134 std::cout << "------------------------------------\n\n";
135 print(target, result2, values);
136 std::cout << "------------------------------------\n\n";
137
138 return 0;
139}
140