File: latlong_metrics.cc

package info (click to toggle)
xapian-core 1.4.3-2%2Bdeb9u3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 21,412 kB
  • sloc: cpp: 113,868; ansic: 8,723; sh: 4,433; perl: 836; makefile: 566; tcl: 317; python: 40
file content (166 lines) | stat: -rw-r--r-- 4,078 bytes parent folder | download | duplicates (2)
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
/** @file latlong_metrics.cc
 * @brief Geospatial distance metrics.
 */
/* Copyright 2008 Lemur Consulting Ltd
 * Copyright 2011 Richard Boulton
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include <config.h>

#include "xapian/geospatial.h"
#include "xapian/error.h"
#include "serialise-double.h"

#include <cmath>

using namespace Xapian;
using namespace std;

/** Quadratic mean radius of the Earth in metres.
 */
#define QUAD_EARTH_RADIUS_METRES 6372797.6

/** Set M_PI if it's not already set.
 */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

LatLongMetric::~LatLongMetric()
{
}

double
LatLongMetric::operator()(const LatLongCoords & a,
			  const LatLongCoords &b) const
{
    if (a.empty() || b.empty()) {
	throw InvalidArgumentError("Empty coordinate list supplied to LatLongMetric::operator()().");
    }
    double min_dist = 0.0;
    bool have_min = false;
    for (LatLongCoordsIterator a_iter = a.begin();
	 a_iter != a.end();
	 ++a_iter)
    {
	for (LatLongCoordsIterator b_iter = b.begin();
	     b_iter != b.end();
	     ++b_iter)
	{
	    double dist = pointwise_distance(*a_iter, *b_iter);
	    if (!have_min) {
		min_dist = dist;
		have_min = true;
	    } else if (dist < min_dist) {
		min_dist = dist;
	    }
	}
    }
    return min_dist;
}

double
LatLongMetric::operator()(const LatLongCoords & a,
			  const char * b_ptr, size_t b_len) const
{
    if (a.empty() || b_len == 0) {
	throw InvalidArgumentError("Empty coordinate list supplied to LatLongMetric::operator()().");
    }
    double min_dist = 0.0;
    bool have_min = false;
    LatLongCoord b;
    const char * b_end = b_ptr + b_len;
    while (b_ptr != b_end) {
	b.unserialise(&b_ptr, b_end);
	for (LatLongCoordsIterator a_iter = a.begin();
	     a_iter != a.end();
	     ++a_iter)
	{
	    double dist = pointwise_distance(*a_iter, b);
	    if (!have_min) {
		min_dist = dist;
		have_min = true;
	    } else if (dist < min_dist) {
		min_dist = dist;
	    }
	}
    }
    return min_dist;
}


GreatCircleMetric::GreatCircleMetric()
	: radius(QUAD_EARTH_RADIUS_METRES)
{}

GreatCircleMetric::GreatCircleMetric(double radius_)
	: radius(radius_)
{}

double
GreatCircleMetric::pointwise_distance(const LatLongCoord & a,
				      const LatLongCoord & b) const
{
    double lata = a.latitude * (M_PI / 180.0);
    double latb = b.latitude * (M_PI / 180.0);

    double latdiff = lata - latb;
    double longdiff = (a.longitude - b.longitude) * (M_PI / 180.0);

    double sin_half_lat = sin(latdiff / 2);
    double sin_half_long = sin(longdiff / 2);
    double h = sin_half_lat * sin_half_lat +
	    sin_half_long * sin_half_long * cos(lata) * cos(latb);
    if (rare(h > 1.0)) {
	// Clamp to 1.0, asin(1.0) = M_PI / 2.0.
	return radius * M_PI;
    }
    return 2 * radius * asin(sqrt(h));
}

LatLongMetric *
GreatCircleMetric::clone() const
{
    return new GreatCircleMetric(radius);
}

string
GreatCircleMetric::name() const
{
    return "Xapian::GreatCircleMetric";
}

string
GreatCircleMetric::serialise() const
{
    return serialise_double(radius);
}

LatLongMetric *
GreatCircleMetric::unserialise(const string & s) const
{
    const char * p = s.data();
    const char * end = p + s.size();

    double new_radius = unserialise_double(&p, end);
    if (p != end) {
	throw Xapian::NetworkError("Bad serialised GreatCircleMetric - junk at end");
    }

    return new GreatCircleMetric(new_radius);
}