xapian-core  2.0.0
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,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 #ifndef XAPIAN_INCLUDED_GEOSPATIAL_H
24 #define XAPIAN_INCLUDED_GEOSPATIAL_H
25 
26 #if !defined XAPIAN_IN_XAPIAN_H && !defined XAPIAN_LIB_BUILD
27 # error Never use <xapian/geospatial.h> directly; include <xapian.h> instead.
28 #endif
29 
30 #include <iterator>
31 #include <vector>
32 #include <string>
33 #include <string_view>
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 miles_to_metres(double miles) noexcept XAPIAN_CONST_FUNCTION;
48 
53 inline double
54 miles_to_metres(double miles) noexcept
55 {
56  return 1609.344 * miles;
57 }
58 
59 double
60 metres_to_miles(double metres) noexcept XAPIAN_CONST_FUNCTION;
61 
66 inline double
67 metres_to_miles(double metres) noexcept
68 {
69  return metres * (1.0 / 1609.344);
70 }
71 
88  double latitude;
89 
98  double longitude;
99 
102  LatLongCoord() noexcept {}
103 
116  LatLongCoord(double latitude_, double longitude_);
117 
126  void unserialise(std::string_view serialised);
127 
139  void unserialise(const char ** ptr, const char * end);
140 
143  std::string serialise() const;
144 
150  bool operator<(const LatLongCoord& other) const noexcept {
151  if (latitude < other.latitude) return true;
152  if (latitude > other.latitude) return false;
153  return (longitude < other.longitude);
154  }
155 
157  std::string get_description() const;
158 };
159 
166  friend class LatLongCoords;
167 
169  std::vector<LatLongCoord>::const_iterator iter;
170 
172  LatLongCoordsIterator(std::vector<LatLongCoord>::const_iterator iter_)
173  : iter(iter_) {}
174 
175  public:
178 
180  const LatLongCoord & operator*() const {
181  return *iter;
182  }
183 
186  ++iter;
187  return *this;
188  }
189 
192  const LatLongCoord & tmp = **this;
193  ++iter;
194  return DerefWrapper_<LatLongCoord>(tmp);
195  }
196 
198  bool operator==(const LatLongCoordsIterator &other) const
199  {
200  return iter == other.iter;
201  }
202 
213  // @{
215  typedef std::input_iterator_tag iterator_category;
219  typedef size_t difference_type;
224  // @}
225 };
226 
233  std::vector<LatLongCoord> coords;
234 
235  public:
238  return LatLongCoordsIterator(coords.begin());
239  }
240 
243  return LatLongCoordsIterator(coords.end());
244  }
245 
247  size_t size() const
248  {
249  return coords.size();
250  }
251 
253  bool empty() const
254  {
255  return coords.empty();
256  }
257 
259  void append(const LatLongCoord & coord)
260  {
261  coords.push_back(coord);
262  }
263 
265  LatLongCoords() : coords() {}
266 
268  LatLongCoords(const LatLongCoord & coord) : coords()
269  {
270  coords.push_back(coord);
271  }
272 
281  void unserialise(std::string_view serialised);
282 
285  std::string serialise() const;
286 
288  std::string get_description() const;
289 };
290 
292 inline bool
294 {
295  return !(a == b);
296 }
297 
303  public:
305  virtual ~LatLongMetric();
306 
309  virtual double pointwise_distance(const LatLongCoord & a,
310  const LatLongCoord & b) const = 0;
311 
322  double operator()(const LatLongCoords & a, const LatLongCoords & b) const;
323 
336  double operator()(const LatLongCoords& a, std::string_view b) const
337  {
338  return (*this)(a, b.data(), b.size());
339  }
340 
356  double operator()(const LatLongCoords & a,
357  const char * b_ptr, size_t b_len) const;
358 
360  virtual LatLongMetric * clone() const = 0;
361 
372  virtual std::string name() const = 0;
373 
379  virtual std::string serialise() const = 0;
380 
385  virtual LatLongMetric * unserialise(const std::string & serialised) const = 0;
386 };
387 
401  double radius;
402 
403  public:
410 
418  explicit GreatCircleMetric(double radius_);
419 
422  double pointwise_distance(const LatLongCoord & a,
423  const LatLongCoord &b) const;
424 
425  LatLongMetric * clone() const;
426  std::string name() const;
427  std::string serialise() const;
428  LatLongMetric * unserialise(const std::string & serialised) const;
429 };
430 
454 {
456  double dist;
457 
460 
463 
465  double max_range;
466 
468  double k1;
469 
471  double k2;
472 
474  void calc_distance();
475 
478  const LatLongCoords & centre_,
479  const LatLongMetric * metric_,
480  double max_range_,
481  double k1_,
482  double k2_);
483 
484  public:
496  const LatLongCoords & centre_,
497  const LatLongMetric & metric_,
498  double max_range_ = 0.0,
499  double k1_ = 1000.0,
500  double k2_ = 1.0);
501 
514  const LatLongCoords & centre_,
515  double max_range_ = 0.0,
516  double k1_ = 1000.0,
517  double k2_ = 1.0);
519 
520  void next(double min_wt);
521  void skip_to(Xapian::docid min_docid, double min_wt);
522  bool check(Xapian::docid min_docid, double min_wt);
523 
524  double get_weight() const;
525  LatLongDistancePostingSource * clone() const;
526  std::string name() const;
527  std::string serialise() const;
529  unserialise_with_registry(const std::string &serialised,
530  const Registry & registry) const;
531  void reset(const Database& db_, Xapian::doccount shard_index);
532 
533  std::string get_description() const;
534 };
535 
551 
554 
557 
560 
562  std::string defkey;
563 
564  public:
574  const LatLongCoords & centre_,
575  const LatLongMetric & metric_,
576  double defdistance)
577  : slot(slot_),
578  centre(centre_),
579  metric(metric_.clone()),
580  defkey(sortable_serialise(defdistance))
581  {}
582 
594  const LatLongCoords & centre_,
595  const LatLongMetric & metric_)
596  : slot(slot_),
597  centre(centre_),
598  metric(metric_.clone()),
599  defkey(9, '\xff')
600  {}
601 
614  const LatLongCoords & centre_)
615  : slot(slot_),
616  centre(centre_),
617  metric(new Xapian::GreatCircleMetric()),
618  defkey(9, '\xff')
619  {}
620 
629  const LatLongCoord & centre_,
630  const LatLongMetric & metric_,
631  double defdistance)
632  : slot(slot_),
633  centre(),
634  metric(metric_.clone()),
635  defkey(sortable_serialise(defdistance))
636  {
637  centre.append(centre_);
638  }
639 
650  const LatLongCoord & centre_,
651  const LatLongMetric & metric_)
652  : slot(slot_),
653  centre(),
654  metric(metric_.clone()),
655  defkey(9, '\xff')
656  {
657  centre.append(centre_);
658  }
659 
671  const LatLongCoord & centre_)
672  : slot(slot_),
673  centre(),
674  metric(new Xapian::GreatCircleMetric()),
675  defkey(9, '\xff')
676  {
677  centre.append(centre_);
678  }
679 
681 
682  std::string operator()(const Xapian::Document & doc) const;
683 };
684 
685 }
686 
687 #endif /* XAPIAN_INCLUDED_GEOSPATIAL_H */
Compiler attribute macros.
#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:54
char name[9]
Definition: dbcheck.cc:57
An indexed database of documents.
Definition: database.h:75
Class representing a document.
Definition: document.h:64
Calculate the great-circle distance between two coordinates on a sphere.
Definition: geospatial.h:398
double radius
The radius of the sphere in metres.
Definition: geospatial.h:401
Virtual base class for key making functors.
Definition: keymaker.h:44
An iterator across the values in a LatLongCoords object.
Definition: geospatial.h:164
bool operator==(const LatLongCoordsIterator &other) const
Equality test for LatLongCoordsIterator objects.
Definition: geospatial.h:198
const LatLongCoord & operator*() const
Get the LatLongCoord for the current position.
Definition: geospatial.h:180
std::vector< LatLongCoord >::const_iterator iter
The current position of the iterator.
Definition: geospatial.h:169
std::input_iterator_tag iterator_category
Definition: geospatial.h:215
DerefWrapper_< LatLongCoord > operator++(int)
Advance the iterator to the next position (postfix version).
Definition: geospatial.h:191
LatLongCoordsIterator()
Default constructor. Produces an uninitialised iterator.
Definition: geospatial.h:177
LatLongCoordsIterator & operator++()
Advance the iterator to the next position.
Definition: geospatial.h:185
LatLongCoordsIterator(std::vector< LatLongCoord >::const_iterator iter_)
Constructor used by LatLongCoords.
Definition: geospatial.h:172
A sequence of latitude-longitude coordinates.
Definition: geospatial.h:231
LatLongCoords(const LatLongCoord &coord)
Construct a container holding one coordinate.
Definition: geospatial.h:268
LatLongCoords()
Construct an empty container.
Definition: geospatial.h:265
size_t size() const
Get the number of coordinates in the container.
Definition: geospatial.h:247
LatLongCoordsIterator begin() const
Get a begin iterator for the coordinates.
Definition: geospatial.h:237
void append(const LatLongCoord &coord)
Append a coordinate to the end of the sequence.
Definition: geospatial.h:259
LatLongCoordsIterator end() const
Get an end iterator for the coordinates.
Definition: geospatial.h:242
bool empty() const
Return true if and only if there are no coordinates in the container.
Definition: geospatial.h:253
std::vector< LatLongCoord > coords
The coordinates.
Definition: geospatial.h:233
KeyMaker subclass which sorts by distance from a latitude/longitude.
Definition: geospatial.h:550
LatLongDistanceKeyMaker(Xapian::valueno slot_, const LatLongCoords &centre_, const LatLongMetric &metric_, double defdistance)
Construct a LatLongDistanceKeyMaker.
Definition: geospatial.h:573
LatLongCoords centre
The centre point (or points) for distance calculation.
Definition: geospatial.h:556
std::string defkey
The default key to return, for documents with no value stored.
Definition: geospatial.h:562
LatLongDistanceKeyMaker(Xapian::valueno slot_, const LatLongCoords &centre_)
Construct a LatLongDistanceKeyMaker.
Definition: geospatial.h:613
Xapian::valueno slot
The value slot to read.
Definition: geospatial.h:553
LatLongDistanceKeyMaker(Xapian::valueno slot_, const LatLongCoord &centre_, const LatLongMetric &metric_, double defdistance)
Construct a LatLongDistanceKeyMaker.
Definition: geospatial.h:628
LatLongDistanceKeyMaker(Xapian::valueno slot_, const LatLongCoords &centre_, const LatLongMetric &metric_)
Construct a LatLongDistanceKeyMaker.
Definition: geospatial.h:593
LatLongDistanceKeyMaker(Xapian::valueno slot_, const LatLongCoord &centre_)
Construct a LatLongDistanceKeyMaker.
Definition: geospatial.h:670
const LatLongMetric * metric
The metric to use when calculating distances.
Definition: geospatial.h:559
LatLongDistanceKeyMaker(Xapian::valueno slot_, const LatLongCoord &centre_, const LatLongMetric &metric_)
Construct a LatLongDistanceKeyMaker.
Definition: geospatial.h:649
Posting source which returns a weight based on geospatial distance.
Definition: geospatial.h:454
double k1
Constant used in weighting function.
Definition: geospatial.h:468
double k2
Constant used in weighting function.
Definition: geospatial.h:471
double dist
Current distance from centre.
Definition: geospatial.h:456
double max_range
Maximum range to allow. If set to 0, there is no maximum range.
Definition: geospatial.h:465
LatLongCoords centre
Centre, to compute distance from.
Definition: geospatial.h:459
const LatLongMetric * metric
Metric to compute the distance with.
Definition: geospatial.h:462
Base class for calculating distances between two lat/long coordinates.
Definition: geospatial.h:302
double operator()(const LatLongCoords &a, std::string_view b) const
Return the distance between two coordinate lists, in metres.
Definition: geospatial.h:336
virtual double pointwise_distance(const LatLongCoord &a, const LatLongCoord &b) const =0
Return the distance between two coordinates, in metres.
virtual LatLongMetric * unserialise(const std::string &serialised) const =0
Create object given string serialisation returned by serialise().
virtual std::string serialise() const =0
Serialise object parameters into a string.
virtual LatLongMetric * clone() const =0
Clone the metric.
virtual std::string name() const =0
Return the full name of the metric.
Registry for user subclasses.
Definition: registry.h:47
A posting source which generates weights from a value slot.
Class for wrapping type returned by an input_iterator.
Build key strings for MSet ordering or collapsing.
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:82
std::string sortable_serialise(double value)
Convert a floating point number to a string, preserving sort order.
Definition: queryparser.h:1229
unsigned valueno
The number for a value slot in a document.
Definition: types.h:90
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:37
bool operator!=(const ESetIterator &a, const ESetIterator &b) noexcept
Inequality test for ESetIterator objects.
Definition: eset.h:278
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:51
double metres_to_miles(double metres) noexcept
Convert from metres to miles.
Definition: geospatial.h:67
double miles_to_metres(double miles) noexcept
Convert from miles to metres.
Definition: geospatial.h:54
External sources of posting information.
parsing a user query string to build a Xapian::Query object
A latitude-longitude coordinate.
Definition: geospatial.h:81
bool operator<(const LatLongCoord &other) const noexcept
Compare with another LatLongCoord.
Definition: geospatial.h:150
LatLongCoord() noexcept
Construct an uninitialised coordinate.
Definition: geospatial.h:102
double latitude
A latitude, as decimal degrees.
Definition: geospatial.h:88
double longitude
A longitude, as decimal degrees.
Definition: geospatial.h:98
Define XAPIAN_VISIBILITY_* macros.
#define XAPIAN_VISIBILITY_DEFAULT
Definition: visibility.h:28