summaryrefslogtreecommitdiff
path: root/libs/geometry/test/algorithms/distance/distance_se_pl_pl.cpp
blob: 2e14e8892aa9bdfeb1787cd4dbef8677c2b94759 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Boost.Geometry (aka GGL, Generic Geometry Library)
// Unit Test

// Copyright (c) 2014, Oracle and/or its affiliates.

// Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle

// Licensed under the Boost Software License version 1.0.
// http://www.boost.org/users/license.html

#include <iostream>

#ifndef BOOST_TEST_MODULE
#define BOOST_TEST_MODULE test_distance_spherical_equatorial_pl_pl
#endif

#include <boost/range.hpp>

#include <boost/test/included/unit_test.hpp>
#include <boost/geometry/strategies/strategies.hpp>

#include "test_distance_se_common.hpp"


typedef bg::cs::spherical_equatorial<bg::degree> cs_type;
typedef bg::model::point<double, 2, cs_type> point_type;
typedef bg::model::multi_point<point_type> multi_point_type;

namespace distance = bg::strategy::distance;
namespace services = distance::services;
typedef bg::default_distance_result<point_type>::type return_type;

typedef distance::haversine<double> point_point_strategy;
typedef distance::comparable::haversine<double> comparable_point_point_strategy;

//===========================================================================

template <typename Strategy>
inline typename bg::distance_result<point_type, point_type, Strategy>::type
distance_from_wkt(std::string const& wkt1,
                  std::string const& wkt2,
                  Strategy const& strategy)
{
    point_type p1, p2;
    bg::read_wkt(wkt1, p1);
    bg::read_wkt(wkt2, p2);
    return bg::distance(p1, p2, strategy);
}

inline bg::default_comparable_distance_result<point_type>::type
comparable_distance_from_wkt(std::string const& wkt1,
                             std::string const& wkt2)
{
    point_type p1, p2;
    bg::read_wkt(wkt1, p1);
    bg::read_wkt(wkt2, p2);
    return bg::comparable_distance(p1, p2);
}

//===========================================================================

template <typename Strategy>
void test_distance_point_point(Strategy const& strategy,
                               bool is_comparable_strategy = false)
{
#ifdef BOOST_GEOMETRY_TEST_DEBUG
    std::cout << std::endl;
    std::cout << "point/point distance tests" << std::endl;
#endif
    typedef test_distance_of_geometries<point_type, point_type> tester;

    tester::apply("p-p-01",
                  "POINT(10 10)",
                  "POINT(0 0)",
                  (is_comparable_strategy
                   ? 0.0150768448035229
                   : (0.24619691677893202 * strategy.radius())),
                  0.0150768448035229,
                  strategy);
    tester::apply("p-p-02",
                  "POINT(10 10)",
                  "POINT(10 10)",
                  0,
                  strategy);

    // antipodal points
    tester::apply("p-p-03",
                  "POINT(0 10)",
                  "POINT(180 -10)",
                  (is_comparable_strategy
                   ? 1.0
                   : (180.0 * bg::math::d2r * strategy.radius())),
                  1.0,
                  strategy);
    tester::apply("p-p-04",
                  "POINT(0 0)",
                  "POINT(180 0)",
                  (is_comparable_strategy
                   ? 1.0
                   : (180.0 * bg::math::d2r * strategy.radius())),
                  1.0,
                  strategy);
}

//===========================================================================

template <typename Strategy>
void test_distance_point_multipoint(Strategy const& strategy)
{
#ifdef BOOST_GEOMETRY_TEST_DEBUG
    std::cout << std::endl;
    std::cout << "point/multipoint distance tests" << std::endl;
#endif
    typedef test_distance_of_geometries<point_type, multi_point_type> tester;

    tester::apply("p-mp-01",
                  "POINT(10 10)",
                  "MULTIPOINT(10 10,20 10,20 20,10 20)",
                  0,
                  strategy);
    tester::apply("p-mp-02",
                  "POINT(10 10)",
                  "MULTIPOINT(20 20,20 30,30 20,30 30)",
                  distance_from_wkt("POINT(10 10)", "POINT(20 20)", strategy),
                  comparable_distance_from_wkt("POINT(10 10)", "POINT(20 20)"),
                  strategy);
    tester::apply("p-mp-03",
                  "POINT(3 0)",
                  "MULTIPOINT(20 20,20 40,40 20,40 40)",
                  distance_from_wkt("POINT(3 0)", "POINT(20 20)", strategy),
                  comparable_distance_from_wkt("POINT(3 0)", "POINT(20 20)"),
                  strategy);

    // almost antipodal points
    tester::apply("p-mp-04",
                  "POINT(179 2)",
                  "MULTIPOINT(3 3,4 3,4 4,3 4)",
                  distance_from_wkt("POINT(179 2)", "POINT(4 4)", strategy),
                  comparable_distance_from_wkt("POINT(179 2)", "POINT(4 4)"),
                  strategy);

    // minimum distance across the dateline
    tester::apply("p-mp-05",
                  "POINT(355 5)",
                  "MULTIPOINT(10 10,20 10,20 20,10 20)",
                  distance_from_wkt("POINT(355 5)", "POINT(10 10)", strategy),
                  comparable_distance_from_wkt("POINT(355 5)", "POINT(10 10)"),
                  strategy);
    tester::apply("p-mp-06",
                  "POINT(-5 5)",
                  "MULTIPOINT(10 10,20 10,20 20,10 20)",
                  distance_from_wkt("POINT(-5 5)", "POINT(10 10)", strategy),
                  comparable_distance_from_wkt("POINT(-5 5)", "POINT(10 10)"),
                  strategy);
}

//===========================================================================

template <typename Strategy>
void test_distance_multipoint_multipoint(Strategy const& strategy)
{
#ifdef BOOST_GEOMETRY_TEST_DEBUG
    std::cout << std::endl;
    std::cout << "multipoint/multipoint distance tests" << std::endl;
#endif
    typedef test_distance_of_geometries
        <
            multi_point_type, multi_point_type
        > tester;

    tester::apply("mp-mp-01",
                  "MULTIPOINT(10 10,11 10,10 11,11 11)",
                  "MULTIPOINT(11 11,12 11,12 12,11 12)",
                  0,
                  strategy);
    tester::apply("mp-mp-02",
                  "MULTIPOINT(10 10,11 10,10 11,11 11)",
                  "MULTIPOINT(12 12,12 13,13 12,13 13)",
                  distance_from_wkt("POINT(11 11)", "POINT(12 12)", strategy),
                  comparable_distance_from_wkt("POINT(11 11)", "POINT(12 12)"),
                  strategy);

    // example with many points in each multi-point so that the r-tree
    // does some splitting.
    tester::apply("mp-mp-03",
                  "MULTIPOINT(1 1,1 2,1 3,1 4,1 5,1 6,1 7,1 8,1 9,1 10,\
                  2 1,2 2,2 3,2 4,2 5,2 6,2 7,2 8,2 9,2 10,\
                  3 1,3 2,3 3,3 4,3 5,3 6,3 7,3 8,3 9,3 10,\
                  10 1,10 10)",
                  "MULTIPOINT(11 11,11 12,11 13,11 14,11 15,\
                  11 16,11 17,11 18,11 19,11 20,\
                  12 11,12 12,12 13,12 24,12 15,\
                  12 16,12 17,12 18,12 29,12 20,\
                  13 11,13 12,13 13,13 24,13 15,\
                  13 16,13 17,13 18,13 29,13 20,\
                  20 11,20 20)",
                  distance_from_wkt("POINT(10 10)", "POINT(11 11)", strategy),
                  comparable_distance_from_wkt("POINT(10 10)", "POINT(11 11)"),
                  strategy);

}

//===========================================================================

template <typename Point, typename Strategy>
void test_more_empty_input_pointlike_pointlike(Strategy const& strategy)
{
#ifdef BOOST_GEOMETRY_TEST_DEBUG
    std::cout << std::endl;
    std::cout << "testing on empty inputs... " << std::flush;
#endif
    bg::model::multi_point<Point> multipoint_empty;

    Point point = from_wkt<Point>("POINT(0 0)");

    // 1st geometry is empty
    test_empty_input(multipoint_empty, point, strategy);

    // 2nd geometry is empty
    test_empty_input(point, multipoint_empty, strategy);

    // both geometries are empty
    test_empty_input(multipoint_empty, multipoint_empty, strategy);

#ifdef BOOST_GEOMETRY_TEST_DEBUG
    std::cout << "done!" << std::endl;
#endif
}

//===========================================================================

BOOST_AUTO_TEST_CASE( test_all_point_point )
{
    test_distance_point_point(point_point_strategy());
    test_distance_point_point(point_point_strategy(earth_radius_km));
    test_distance_point_point(point_point_strategy(earth_radius_miles));

    test_distance_point_point(comparable_point_point_strategy(), true);
    test_distance_point_point
        (comparable_point_point_strategy(earth_radius_km), true);
    test_distance_point_point
        (comparable_point_point_strategy(earth_radius_miles), true);
}

BOOST_AUTO_TEST_CASE( test_all_point_multipoint )
{
    test_distance_point_multipoint(point_point_strategy());
    test_distance_point_multipoint(point_point_strategy(earth_radius_km));
    test_distance_point_multipoint(point_point_strategy(earth_radius_miles));

    test_distance_point_multipoint(comparable_point_point_strategy());
    test_distance_point_multipoint
        (comparable_point_point_strategy(earth_radius_km));
    test_distance_point_multipoint
        (comparable_point_point_strategy(earth_radius_miles));
}

BOOST_AUTO_TEST_CASE( test_all_multipoint_multipoint )
{
    test_distance_multipoint_multipoint(point_point_strategy());
    test_distance_multipoint_multipoint(point_point_strategy(earth_radius_km));
    test_distance_multipoint_multipoint
        (point_point_strategy(earth_radius_miles));

    test_distance_multipoint_multipoint(comparable_point_point_strategy());
    test_distance_multipoint_multipoint
        (comparable_point_point_strategy(earth_radius_km));
    test_distance_multipoint_multipoint
        (comparable_point_point_strategy(earth_radius_miles));
}

BOOST_AUTO_TEST_CASE( test_all_empty_input_pointlike_pointlike )
{
    test_more_empty_input_pointlike_pointlike
        <
            point_type
        >(point_point_strategy());

    test_more_empty_input_pointlike_pointlike
        <
            point_type
        >(point_point_strategy(earth_radius_km));

    test_more_empty_input_pointlike_pointlike
        <
            point_type
        >(point_point_strategy(earth_radius_miles));

    test_more_empty_input_pointlike_pointlike
        <
            point_type
        >(comparable_point_point_strategy());

    test_more_empty_input_pointlike_pointlike
        <
            point_type
        >(comparable_point_point_strategy(earth_radius_km));

    test_more_empty_input_pointlike_pointlike
        <
            point_type
        >(comparable_point_point_strategy(earth_radius_miles));
}