Make TradWeight a simple subclass and deprecate
[xapian.git] / xapian-core / geospatial / latlongcoord.cc
blobd1d74dabce48a50fbb2289758582baa9e4d49f95
1 /** @file
2 * @brief Latitude and longitude representations.
3 */
4 /* Copyright 2008 Lemur Consulting Ltd
5 * Copyright 2011 Richard Boulton
6 * Copyright 2024 Olly Betts
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21 * USA
24 #include <config.h>
26 #include "xapian/geospatial.h"
27 #include "xapian/error.h"
29 #include "geoencode.h"
30 #include "str.h"
32 #include <cmath>
33 #include <string_view>
35 using namespace Xapian;
36 using namespace std;
38 LatLongCoord::LatLongCoord(double latitude_, double longitude_)
39 : latitude(latitude_),
40 longitude(longitude_)
42 if (latitude < -90.0 || latitude > 90.0)
43 throw InvalidArgumentError("Latitude out-of-range");
44 longitude = fmod(longitude_, 360);
45 if (longitude < 0) longitude += 360;
48 void
49 LatLongCoord::unserialise(string_view serialised)
51 const char * ptr = serialised.data();
52 const char * end = ptr + serialised.size();
53 unserialise(&ptr, end);
54 if (ptr != end)
55 throw SerialisationError(
56 "Junk found at end of serialised LatLongCoord");
59 void
60 LatLongCoord::unserialise(const char ** ptr, const char * end)
62 size_t len = end - *ptr;
63 if (len < 2) {
64 latitude = 0;
65 longitude = 0;
66 return;
68 GeoEncode::decode(*ptr, end - *ptr, latitude, longitude);
69 if (len < 6) {
70 *ptr = end;
71 } else {
72 *ptr += 6;
76 string
77 LatLongCoord::serialise() const
79 string result;
80 GeoEncode::encode(latitude, longitude, result);
81 return result;
84 string
85 LatLongCoord::get_description() const
87 string res("Xapian::LatLongCoord(");
88 res += str(latitude);
89 res += ", ";
90 res += str(longitude);
91 res += ")";
92 return res;
95 void
96 LatLongCoords::unserialise(string_view serialised)
98 const char * ptr = serialised.data();
99 const char * end_ptr = ptr + serialised.size();
100 coords.clear();
101 while (ptr != end_ptr) {
102 coords.emplace_back();
103 coords.back().unserialise(&ptr, end_ptr);
107 string
108 LatLongCoords::serialise() const
110 string result;
111 for (auto&& coord : coords) {
112 GeoEncode::encode(coord.latitude, coord.longitude, result);
114 return result;
117 string
118 LatLongCoords::get_description() const
120 string res("Xapian::LatLongCoords(");
121 for (auto coord = coords.begin(); coord != coords.end(); ++coord) {
122 if (coord != coords.begin()) {
123 res += ", ";
125 res += "(";
126 res += str(coord->latitude);
127 res += ", ";
128 res += str(coord->longitude);
129 res += ")";
131 res += ")";
132 return res;