2 * @brief Latitude and longitude representations.
4 /* Copyright 2008 Lemur Consulting Ltd
5 * Copyright 2011 Richard Boulton
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.
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.
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
25 #include "xapian/geospatial.h"
26 #include "xapian/error.h"
28 #include "geoencode.h"
33 using namespace Xapian
;
36 LatLongCoord::LatLongCoord(double latitude_
, double longitude_
)
37 : latitude(latitude_
),
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;
47 LatLongCoord::unserialise(const string
& serialised
)
49 const char * ptr
= serialised
.data();
50 const char * end
= ptr
+ serialised
.size();
51 unserialise(&ptr
, end
);
53 throw SerialisationError(
54 "Junk found at end of serialised LatLongCoord");
58 LatLongCoord::unserialise(const char ** ptr
, const char * end
)
60 size_t len
= end
- *ptr
;
66 GeoEncode::decode(*ptr
, end
- *ptr
, latitude
, longitude
);
75 LatLongCoord::serialise() const
78 GeoEncode::encode(latitude
, longitude
, result
);
83 LatLongCoord::get_description() const
85 string
res("Xapian::LatLongCoord(");
88 res
+= str(longitude
);
94 LatLongCoords::unserialise(const string
& serialised
)
96 const char * ptr
= serialised
.data();
97 const char * end_ptr
= ptr
+ serialised
.size();
99 while (ptr
!= end_ptr
) {
100 coords
.push_back(LatLongCoord());
101 coords
.back().unserialise(&ptr
, end_ptr
);
106 LatLongCoords::serialise() const
109 vector
<LatLongCoord
>::const_iterator coord
;
110 for (coord
= coords
.begin(); coord
!= coords
.end(); ++coord
)
112 GeoEncode::encode(coord
->latitude
, coord
->longitude
, result
);
118 LatLongCoords::get_description() const
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()) {
127 res
+= str(coord
->latitude
);
129 res
+= str(coord
->longitude
);