xapian-core  1.4.25
latlongcoord.cc
Go to the documentation of this file.
1 
4 /* Copyright 2008 Lemur Consulting Ltd
5  * Copyright 2011 Richard Boulton
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20  * USA
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 
33 using namespace Xapian;
34 using namespace std;
35 
36 LatLongCoord::LatLongCoord(double latitude_, double longitude_)
37  : latitude(latitude_),
38  longitude(longitude_)
39 {
40  if (latitude < -90.0 || latitude > 90.0)
41  throw InvalidArgumentError("Latitude out-of-range");
42  longitude = fmod(longitude_, 360);
43  if (longitude < 0) longitude += 360;
44 }
45 
46 void
47 LatLongCoord::unserialise(const string & serialised)
48 {
49  const char * ptr = serialised.data();
50  const char * end = ptr + serialised.size();
51  unserialise(&ptr, end);
52  if (ptr != end)
53  throw SerialisationError(
54  "Junk found at end of serialised LatLongCoord");
55 }
56 
57 void
58 LatLongCoord::unserialise(const char ** ptr, const char * end)
59 {
60  size_t len = end - *ptr;
61  if (len < 2) {
62  latitude = 0;
63  longitude = 0;
64  return;
65  }
66  GeoEncode::decode(*ptr, end - *ptr, latitude, longitude);
67  if (len < 6) {
68  *ptr = end;
69  } else {
70  *ptr += 6;
71  }
72 }
73 
74 string
76 {
77  string result;
79  return result;
80 }
81 
82 string
84 {
85  string res("Xapian::LatLongCoord(");
86  res += str(latitude);
87  res += ", ";
88  res += str(longitude);
89  res += ")";
90  return res;
91 }
92 
93 void
94 LatLongCoords::unserialise(const string & serialised)
95 {
96  const char * ptr = serialised.data();
97  const char * end_ptr = ptr + serialised.size();
98  coords.clear();
99  while (ptr != end_ptr) {
100  coords.emplace_back();
101  coords.back().unserialise(&ptr, end_ptr);
102  }
103 }
104 
105 string
107 {
108  string result;
109  vector<LatLongCoord>::const_iterator coord;
110  for (coord = coords.begin(); coord != coords.end(); ++coord)
111  {
112  GeoEncode::encode(coord->latitude, coord->longitude, result);
113  }
114  return result;
115 }
116 
117 string
119 {
120  string res("Xapian::LatLongCoords(");
121  vector<LatLongCoord>::const_iterator coord;
122  for (coord = coords.begin(); coord != coords.end(); ++coord) {
123  if (coord != coords.begin()) {
124  res += ", ";
125  }
126  res += "(";
127  res += str(coord->latitude);
128  res += ", ";
129  res += str(coord->longitude);
130  res += ")";
131  }
132  res += ")";
133  return res;
134 }
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:80
std::string get_description() const
Return a string describing this object.
STL namespace.
void decode(const char *value, size_t len, double &lat_ref, double &lon_ref)
Decode a coordinate from a buffer.
Definition: geoencode.cc:135
Convert types to std::string.
std::string serialise() const
Return a serialised representation of the coordinate.
Definition: latlongcoord.cc:75
std::string serialise() const
Return a serialised form of the coordinate list.
bool encode(double lat, double lon, std::string &result)
Encode a coordinate and append it to a string.
Definition: geoencode.cc:73
Hierarchy of classes which Xapian can throw as exceptions.
InvalidArgumentError indicates an invalid parameter value was passed to the API.
Definition: error.h:241
Geospatial search support routines.
LatLongCoord()
Construct an uninitialised coordinate.
Definition: geospatial.h:102
Indicates an error in the std::string serialisation of an object.
Definition: error.h:929
void unserialise(const std::string &serialised)
Unserialise a string and set this object to its coordinate.
Definition: latlongcoord.cc:47
double latitude
A latitude, as decimal degrees.
Definition: geospatial.h:88
string str(int value)
Convert int to std::string.
Definition: str.cc:90
Encodings for geospatial coordinates.
std::string get_description() const
Return a string describing this object.
Definition: latlongcoord.cc:83
void unserialise(const std::string &serialised)
Unserialise a string and set this object to the coordinates in it.
Definition: latlongcoord.cc:94
double longitude
A longitude, as decimal degrees.
Definition: geospatial.h:98