KPublicTransport

locationutil.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "locationutil_p.h"
8 
9 #include <KPublicTransport/Location>
10 #include <KPublicTransport/LocationRequest>
11 
12 #include <QCryptographicHash>
13 
14 #include <cmath>
15 
16 using namespace KPublicTransport;
17 
18 bool LocationUtil::sortLessThan(const LocationRequest &request, const Location &lhs, const Location &rhs)
19 {
20  if (request.hasCoordinate()) {
21  return Location::distance(request.latitude(), request.longitude(), lhs.latitude(), lhs.longitude())
22  < Location::distance(request.latitude(), request.longitude(), rhs.latitude(), rhs.longitude());
23  }
24  // for name based search, sort by Levenshtein distance or similar metric
25  // TODO so far this only sorts for matching or not matching
26  const auto lhsSame = Location::isSameName(request.name(), lhs.name());
27  const auto rhsSame = Location::isSameName(request.name(), rhs.name());
28 
29  if (lhsSame == rhsSame) {
30  return lhs.name().compare(rhs.name(), Qt::CaseInsensitive) < 0;
31  }
32 
33  return lhsSame && !rhsSame;
34 }
35 
36 static QString normalizeString(const QString &str)
37 {
38  QString n;
39  n.reserve(str.size());
40  for (const auto c : str) {
41  if (c.isLetter() || c.isDigit()) {
42  n.push_back(c.toCaseFolded());
43  }
44  }
45  return n;
46 }
47 
48 QString LocationUtil::cacheKey(const Location &loc)
49 {
50  if (loc.hasCoordinate()) {
51  return QString::number((int)(loc.latitude() * 1000000)) + QLatin1Char('x') + QString::number((int)(loc.longitude() * 1000000));
52  }
53 
55  hash.addData(normalizeString(loc.name()).toUtf8());
56  hash.addData(normalizeString(loc.streetAddress()).toUtf8());
57  hash.addData(normalizeString(loc.postalCode()).toUtf8());
58  hash.addData(normalizeString(loc.locality()).toUtf8());
59  hash.addData(normalizeString(loc.region()).toUtf8());
60  hash.addData(normalizeString(loc.country()).toUtf8());
61 
62  return QString::fromUtf8(hash.result().toHex());
63 }
Query operations and data types for accessing realtime public transport information from online servi...
Definition: attribution.cpp:16
QString locality
Locality/city of the location, if known.
Definition: location.h:61
int size() const const
static int distance(float lat1, float lon1, float lat2, float lon2)
Compute the distance between two geo coordinates, in meters.
Definition: location.cpp:424
float latitude
Latitude of the location, in degree, NaN if unknown.
Definition: location.h:52
QString postalCode
Postal code of the location, if known.
Definition: location.h:59
QString region
Region (as in ISO 3166-2) of the location, if known.
Definition: location.h:63
QString number(int n, int base)
QString fromUtf8(const char *str, int size)
static bool isSameName(const QString &lhs, const QString &rhs)
Checks if two location names refer to the same location.
Definition: location.cpp:321
float longitude
Longitude of the location, in degree, NaN if unknown.
Definition: location.h:54
CaseInsensitive
QString streetAddress
Street address of the location, if known.
Definition: location.h:57
QString country
Country of the location as ISO 3166-1 alpha 2 code, if known.
Definition: location.h:65
void push_back(QChar ch)
QString name
Human-readable name of the location.
Definition: location.h:50
bool hasCoordinate() const
Returns true if a valid geo coordinate has been set.
void reserve(int size)
Describes a location search.
int compare(const QString &other, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Oct 15 2021 23:07:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.