2 * @brief Latitude and longitude representations.
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
26 #include "xapian/geospatial.h"
27 #include "xapian/error.h"
29 #include "geoencode.h"
33 #include <string_view>
35 using namespace Xapian
;
38 LatLongCoord::LatLongCoord(double latitude_
, double longitude_
)
39 : latitude(latitude_
),
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;
49 LatLongCoord::unserialise(string_view serialised
)
51 const char * ptr
= serialised
.data();
52 const char * end
= ptr
+ serialised
.size();
53 unserialise(&ptr
, end
);
55 throw SerialisationError(
56 "Junk found at end of serialised LatLongCoord");
60 LatLongCoord::unserialise(const char ** ptr
, const char * end
)
62 size_t len
= end
- *ptr
;
68 GeoEncode::decode(*ptr
, end
- *ptr
, latitude
, longitude
);
77 LatLongCoord::serialise() const
80 GeoEncode::encode(latitude
, longitude
, result
);
85 LatLongCoord::get_description() const
87 string
res("Xapian::LatLongCoord(");
90 res
+= str(longitude
);
96 LatLongCoords::unserialise(string_view serialised
)
98 const char * ptr
= serialised
.data();
99 const char * end_ptr
= ptr
+ serialised
.size();
101 while (ptr
!= end_ptr
) {
102 coords
.emplace_back();
103 coords
.back().unserialise(&ptr
, end_ptr
);
108 LatLongCoords::serialise() const
111 for (auto&& coord
: coords
) {
112 GeoEncode::encode(coord
.latitude
, coord
.longitude
, result
);
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()) {
126 res
+= str(coord
->latitude
);
128 res
+= str(coord
->longitude
);