xapian-core  2.0.0
latlongcoord.cc
Go to the documentation of this file.
1 
4 /* Copyright 2008 Lemur Consulting Ltd
5  * Copyright 2011 Richard Boulton
6  * Copyright 2024 Olly Betts
7  *
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.
12  *
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.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, see
20  * <https://www.gnu.org/licenses/>.
21  */
22 
23 #include <config.h>
24 
25 #include "xapian/geospatial.h"
26 #include "xapian/error.h"
27 
28 #include "geoencode.h"
29 #include "str.h"
30 
31 #include <cmath>
32 #include <string_view>
33 
34 using namespace Xapian;
35 using namespace std;
36 
37 LatLongCoord::LatLongCoord(double latitude_, double longitude_)
38  : latitude(latitude_),
39  longitude(longitude_)
40 {
41  if (latitude < -90.0 || latitude > 90.0)
42  throw InvalidArgumentError("Latitude out-of-range");
43  longitude = fmod(longitude_, 360);
44  if (longitude < 0) longitude += 360;
45 }
46 
47 void
48 LatLongCoord::unserialise(string_view serialised)
49 {
50  const char * ptr = serialised.data();
51  const char * end = ptr + serialised.size();
52  unserialise(&ptr, end);
53  if (ptr != end)
54  throw SerialisationError(
55  "Junk found at end of serialised LatLongCoord");
56 }
57 
58 void
59 LatLongCoord::unserialise(const char ** ptr, const char * end)
60 {
61  size_t len = end - *ptr;
62  if (len < 2) {
63  latitude = 0;
64  longitude = 0;
65  return;
66  }
67  GeoEncode::decode(*ptr, end - *ptr, latitude, longitude);
68  if (len < 6) {
69  *ptr = end;
70  } else {
71  *ptr += 6;
72  }
73 }
74 
75 string
77 {
78  string result;
80  return result;
81 }
82 
83 string
85 {
86  string res("Xapian::LatLongCoord(");
87  res += str(latitude);
88  res += ", ";
89  res += str(longitude);
90  res += ")";
91  return res;
92 }
93 
94 void
95 LatLongCoords::unserialise(string_view serialised)
96 {
97  const char * ptr = serialised.data();
98  const char * end_ptr = ptr + serialised.size();
99  coords.clear();
100  while (ptr != end_ptr) {
101  coords.emplace_back();
102  coords.back().unserialise(&ptr, end_ptr);
103  }
104 }
105 
106 string
108 {
109  string result;
110  for (auto&& coord : coords) {
111  GeoEncode::encode(coord.latitude, coord.longitude, result);
112  }
113  return result;
114 }
115 
116 string
118 {
119  string res("Xapian::LatLongCoords(");
120  for (auto coord = coords.begin(); coord != coords.end(); ++coord) {
121  if (coord != coords.begin()) {
122  res += ", ";
123  }
124  res += "(";
125  res += str(coord->latitude);
126  res += ", ";
127  res += str(coord->longitude);
128  res += ")";
129  }
130  res += ")";
131  return res;
132 }
InvalidArgumentError indicates an invalid parameter value was passed to the API.
Definition: error.h:229
std::string serialise() const
Return a serialised form of the coordinate list.
void unserialise(std::string_view serialised)
Unserialise a string and set this object to the coordinates in it.
Definition: latlongcoord.cc:95
std::string get_description() const
Return a string describing this object.
std::vector< LatLongCoord > coords
The coordinates.
Definition: geospatial.h:233
Indicates an error in the std::string serialisation of an object.
Definition: error.h:917
Hierarchy of classes which Xapian can throw as exceptions.
Encodings for geospatial coordinates.
Geospatial search support routines.
bool encode(double lat, double lon, std::string &result)
Encode a coordinate and append it to a string.
Definition: geoencode.cc:73
void decode(const char *value, size_t len, double &lat_ref, double &lon_ref)
Decode a coordinate from a buffer.
Definition: geoencode.cc:135
string str(int value)
Convert int to std::string.
Definition: str.cc:91
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:82
Convert types to std::string.
std::string get_description() const
Return a string describing this object.
Definition: latlongcoord.cc:84
LatLongCoord() noexcept
Construct an uninitialised coordinate.
Definition: geospatial.h:102
void unserialise(std::string_view serialised)
Unserialise a string and set this object to its coordinate.
Definition: latlongcoord.cc:48
double latitude
A latitude, as decimal degrees.
Definition: geospatial.h:88
double longitude
A longitude, as decimal degrees.
Definition: geospatial.h:98
std::string serialise() const
Return a serialised representation of the coordinate.
Definition: latlongcoord.cc:76