xapian-core  1.4.26
geospatial.h
Go to the documentation of this file.
1 
4 /* Copyright 2008,2009 Lemur Consulting Ltd
5  * Copyright 2010,2011 Richard Boulton
6  * Copyright 2012,2013,2014,2015,2016 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, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21  * USA
22  */
23 
24 #ifndef XAPIAN_INCLUDED_GEOSPATIAL_H
25 #define XAPIAN_INCLUDED_GEOSPATIAL_H
26 
27 #if !defined XAPIAN_IN_XAPIAN_H && !defined XAPIAN_LIB_BUILD
28 # error Never use <xapian/geospatial.h> directly; include <xapian.h> instead.
29 #endif
30 
31 #include <iterator>
32 #include <vector>
33 #include <string>
34 
35 #include <xapian/attributes.h>
36 #include <xapian/derefwrapper.h>
37 #include <xapian/keymaker.h>
38 #include <xapian/postingsource.h>
39 #include <xapian/queryparser.h> // For sortable_serialise
40 #include <xapian/visibility.h>
41 
42 namespace Xapian {
43 
44 class Registry;
45 
46 double
47 XAPIAN_NOTHROW(miles_to_metres(double miles)) XAPIAN_CONST_FUNCTION;
48 
53 inline double
55 {
56  return 1609.344 * miles;
57 }
58 
59 double
60 XAPIAN_NOTHROW(metres_to_miles(double metres)) XAPIAN_CONST_FUNCTION;
61 
66 inline double
68 {
69  return metres * (1.0 / 1609.344);
70 }
71 
88  double latitude;
89 
98  double longitude;
99 
102  XAPIAN_NOTHROW(LatLongCoord()) {}
103 
116  LatLongCoord(double latitude_, double longitude_);
117 
126  void unserialise(const std::string & serialised);
127 
139  void unserialise(const char ** ptr, const char * end);
140 
143  std::string serialise() const;
144 
150  bool XAPIAN_NOTHROW(operator<(const LatLongCoord & other) const)
151  {
152  if (latitude < other.latitude) return true;
153  if (latitude > other.latitude) return false;
154  return (longitude < other.longitude);
155  }
156 
158  std::string get_description() const;
159 };
160 
167  friend class LatLongCoords;
168 
170  std::vector<LatLongCoord>::const_iterator iter;
171 
173  LatLongCoordsIterator(std::vector<LatLongCoord>::const_iterator iter_)
174  : iter(iter_) {}
175 
176  public:
179 
181  const LatLongCoord & operator*() const {
182  return *iter;
183  }
184 
187  ++iter;
188  return *this;
189  }
190 
193  const LatLongCoord & tmp = **this;
194  ++iter;
195  return DerefWrapper_<LatLongCoord>(tmp);
196  }
197 
199  bool operator==(const LatLongCoordsIterator &other) const
200  {
201  return iter == other.iter;
202  }
203 
214  // @{
216  typedef std::input_iterator_tag iterator_category;
220  typedef size_t difference_type;
225  // @}
226 };
227 
234  std::vector<LatLongCoord> coords;
235 
236  public:
239  return LatLongCoordsIterator(coords.begin());
240  }
241 
244  return LatLongCoordsIterator(coords.end());
245  }
246 
248  size_t size() const
249  {
250  return coords.size();
251  }
252 
254  bool empty() const
255  {
256  return coords.empty();
257  }
258 
260  void append(const LatLongCoord & coord)
261  {
262  coords.push_back(coord);
263  }
264 
266  LatLongCoords() : coords() {}
267 
269  LatLongCoords(const LatLongCoord & coord) : coords()
270  {
271  coords.push_back(coord);
272  }
273 
282  void unserialise(const std::string & serialised);
283 
286  std::string serialise() const;
287 
289  std::string get_description() const;
290 };
291 
293 inline bool
295 {
296  return !(a == b);
297 }
298 
304  public:
306  virtual ~LatLongMetric();
307 
310  virtual double pointwise_distance(const LatLongCoord & a,
311  const LatLongCoord & b) const = 0;
312 
323  double operator()(const LatLongCoords & a, const LatLongCoords & b) const;
324 
337  double operator()(const LatLongCoords & a, const std::string & b) const
338  {
339  return (*this)(a, b.data(), b.size());
340  }
341 
357  double operator()(const LatLongCoords & a,
358  const char * b_ptr, size_t b_len) const;
359 
361  virtual LatLongMetric * clone() const = 0;
362 
373  virtual std::string name() const = 0;
374 
380  virtual std::string serialise() const = 0;
381 
386  virtual LatLongMetric * unserialise(const std::string & serialised) const = 0;
387 };
388 
402  double radius;
403 
404  public:
411 
419  explicit GreatCircleMetric(double radius_);
420 
423  double pointwise_distance(const LatLongCoord & a,
424  const LatLongCoord &b) const;
425 
426  LatLongMetric * clone() const;
427  std::string name() const;
428  std::string serialise() const;
429  LatLongMetric * unserialise(const std::string & serialised) const;
430 };
431 
455 {
457  double dist;
458 
461 
464 
466  double max_range;
467 
469  double k1;
470 
472  double k2;
473 
475  void calc_distance();
476 
479  const LatLongCoords & centre_,
480  const LatLongMetric * metric_,
481  double max_range_,
482  double k1_,
483  double k2_);
484 
485  public:
497  const LatLongCoords & centre_,
498  const LatLongMetric & metric_,
499  double max_range_ = 0.0,
500  double k1_ = 1000.0,
501  double k2_ = 1.0);
502 
515  const LatLongCoords & centre_,
516  double max_range_ = 0.0,
517  double k1_ = 1000.0,
518  double k2_ = 1.0);
520 
521  void next(double min_wt);
522  void skip_to(Xapian::docid min_docid, double min_wt);
523  bool check(Xapian::docid min_docid, double min_wt);
524 
525  double get_weight() const;
526  LatLongDistancePostingSource * clone() const;
527  std::string name() const;
528  std::string serialise() const;
530  unserialise_with_registry(const std::string &serialised,
531  const Registry & registry) const;
532  void init(const Database & db_);
533 
534  std::string get_description() const;
535 };
536 
552 
555 
558 
561 
563  std::string defkey;
564 
565  public:
575  const LatLongCoords & centre_,
576  const LatLongMetric & metric_,
577  double defdistance)
578  : slot(slot_),
579  centre(centre_),
580  metric(metric_.clone()),
581  defkey(sortable_serialise(defdistance))
582  {}
583 
595  const LatLongCoords & centre_,
596  const LatLongMetric & metric_)
597  : slot(slot_),
598  centre(centre_),
599  metric(metric_.clone()),
600  defkey(9, '\xff')
601  {}
602 
615  const LatLongCoords & centre_)
616  : slot(slot_),
617  centre(centre_),
618  metric(new Xapian::GreatCircleMetric()),
619  defkey(9, '\xff')
620  {}
621 
630  const LatLongCoord & centre_,
631  const LatLongMetric & metric_,
632  double defdistance)
633  : slot(slot_),
634  centre(),
635  metric(metric_.clone()),
636  defkey(sortable_serialise(defdistance))
637  {
638  centre.append(centre_);
639  }
640 
651  const LatLongCoord & centre_,
652  const LatLongMetric & metric_)
653  : slot(slot_),
654  centre(),
655  metric(metric_.clone()),
656  defkey(9, '\xff')
657  {
658  centre.append(centre_);
659  }
660 
672  const LatLongCoord & centre_)
673  : slot(slot_),
674  centre(),
675  metric(new Xapian::GreatCircleMetric()),
676  defkey(9, '\xff')
677  {
678  centre.append(centre_);
679  }
680 
682 
683  std::string operator()(const Xapian::Document & doc) const;
684 };
685 
686 }
687 
688 #endif /* XAPIAN_INCLUDED_GEOSPATIAL_H */
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:80
double radius
The radius of the sphere in metres.
Definition: geospatial.h:402
This class is used to access a database, or a group of databases.
Definition: database.h:68
bool empty() const
Return true if and only if there are no coordinates in the container.
Definition: geospatial.h:254
LatLongCoords centre
Centre, to compute distance from.
Definition: geospatial.h:460
LatLongCoords(const LatLongCoord &coord)
Construct a container holding one coordinate.
Definition: geospatial.h:269
LatLongDistanceKeyMaker(Xapian::valueno slot_, const LatLongCoords &centre_, const LatLongMetric &metric_)
Construct a LatLongDistanceKeyMaker.
Definition: geospatial.h:594
Compiler attribute macros.
std::vector< LatLongCoord > coords
The coordinates.
Definition: geospatial.h:234
External sources of posting information.
bool operator!=(const ESetIterator &a, const ESetIterator &b)
Inequality test for ESetIterator objects.
Definition: eset.h:287
double dist
Current distance from centre.
Definition: geospatial.h:457
std::string sortable_serialise(double value)
Convert a floating point number to a string, preserving sort order.
Definition: queryparser.h:1382
DerefWrapper_< LatLongCoord > operator++(int)
Advance the iterator to the next position (postfix version).
Definition: geospatial.h:192
Xapian::valueno slot
The value slot to read.
Definition: geospatial.h:554
double miles_to_metres(double miles)
Convert from miles to metres.
Definition: geospatial.h:54
#define XAPIAN_VISIBILITY_DEFAULT
Definition: visibility.h:28
std::vector< LatLongCoord >::const_iterator iter
The current position of the iterator.
Definition: geospatial.h:170
Build key strings for MSet ordering or collapsing.
LatLongCoords centre
The centre point (or points) for distance calculation.
Definition: geospatial.h:557
Posting source which returns a weight based on geospatial distance.
Definition: geospatial.h:454
LatLongDistanceKeyMaker(Xapian::valueno slot_, const LatLongCoord &centre_)
Construct a LatLongDistanceKeyMaker.
Definition: geospatial.h:671
LatLongCoordsIterator(std::vector< LatLongCoord >::const_iterator iter_)
Constructor used by LatLongCoords.
Definition: geospatial.h:173
double max_range
Maximum range to allow. If set to 0, there is no maximum range.
Definition: geospatial.h:466
LatLongDistanceKeyMaker(Xapian::valueno slot_, const LatLongCoord &centre_, const LatLongMetric &metric_)
Construct a LatLongDistanceKeyMaker.
Definition: geospatial.h:650
LatLongCoordsIterator begin() const
Get a begin iterator for the coordinates.
Definition: geospatial.h:238
A latitude-longitude coordinate.
Definition: geospatial.h:81
#define XAPIAN_CONST_FUNCTION
A function which does not examine any values except its arguments and has no effects except its retur...
Definition: attributes.h:62
LatLongCoord()
Construct an uninitialised coordinate.
Definition: geospatial.h:102
double metres_to_miles(double metres)
Convert from metres to miles.
Definition: geospatial.h:67
LatLongCoords()
Construct an empty container.
Definition: geospatial.h:266
std::string defkey
The default key to return, for documents with no value stored.
Definition: geospatial.h:563
Define XAPIAN_VISIBILITY_* macros.
Registry for user subclasses.
Definition: registry.h:47
std::input_iterator_tag iterator_category
Definition: geospatial.h:216
double latitude
A latitude, as decimal degrees.
Definition: geospatial.h:88
LatLongDistanceKeyMaker(Xapian::valueno slot_, const LatLongCoords &centre_)
Construct a LatLongDistanceKeyMaker.
Definition: geospatial.h:614
Base class for calculating distances between two lat/long coordinates.
Definition: geospatial.h:303
double operator()(const LatLongCoords &a, const std::string &b) const
Return the distance between two coordinate lists, in metres.
Definition: geospatial.h:337
const LatLongMetric * metric
Metric to compute the distance with.
Definition: geospatial.h:463
double k1
Constant used in weighting function.
Definition: geospatial.h:469
A sequence of latitude-longitude coordinates.
Definition: geospatial.h:232
A posting source which generates weights from a value slot.
KeyMaker subclass which sorts by distance from a latitude/longitude.
Definition: geospatial.h:551
void append(const LatLongCoord &coord)
Append a coordinate to the end of the sequence.
Definition: geospatial.h:260
char name[9]
Definition: dbcheck.cc:55
const LatLongMetric * metric
The metric to use when calculating distances.
Definition: geospatial.h:560
An iterator across the values in a LatLongCoords object.
Definition: geospatial.h:165
LatLongDistanceKeyMaker(Xapian::valueno slot_, const LatLongCoords &centre_, const LatLongMetric &metric_, double defdistance)
Construct a LatLongDistanceKeyMaker.
Definition: geospatial.h:574
unsigned valueno
The number for a value slot in a document.
Definition: types.h:108
double k2
Constant used in weighting function.
Definition: geospatial.h:472
LatLongCoordsIterator()
Default constructor. Produces an uninitialised iterator.
Definition: geospatial.h:178
LatLongCoordsIterator end() const
Get an end iterator for the coordinates.
Definition: geospatial.h:243
Class for wrapping type returned by an input_iterator.
LatLongDistanceKeyMaker(Xapian::valueno slot_, const LatLongCoord &centre_, const LatLongMetric &metric_, double defdistance)
Construct a LatLongDistanceKeyMaker.
Definition: geospatial.h:629
const LatLongCoord & operator*() const
Get the LatLongCoord for the current position.
Definition: geospatial.h:181
Calculate the great-circle distance between two coordinates on a sphere.
Definition: geospatial.h:399
LatLongCoordsIterator & operator++()
Advance the iterator to the next position.
Definition: geospatial.h:186
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:52
bool operator==(const LatLongCoordsIterator &other) const
Equality test for LatLongCoordsIterator objects.
Definition: geospatial.h:199
#define XAPIAN_NOEXCEPT
Definition: attributes.h:39
A handle representing a document in a Xapian database.
Definition: document.h:61
Virtual base class for key making functors.
Definition: keymaker.h:41
size_t size() const
Get the number of coordinates in the container.
Definition: geospatial.h:248
double longitude
A longitude, as decimal degrees.
Definition: geospatial.h:98
parsing a user query string to build a Xapian::Query object