KPublicTransport

locationutil.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
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
16using namespace KPublicTransport;
17
18bool 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
36static 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
48QString 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}
Describes a location search.
bool hasCoordinate() const
Returns true if a valid geo coordinate has been set.
static bool isSameName(const QString &lhs, const QString &rhs)
Checks if two location names refer to the same location.
Definition location.cpp:322
QString country
Country of the location as ISO 3166-1 alpha 2 code, if known.
Definition location.h:65
static float distance(float lat1, float lon1, float lat2, float lon2)
Compute the distance between two geo coordinates, in meters.
Definition location.cpp:425
float latitude
Latitude of the location, in degree, NaN if unknown.
Definition location.h:52
QString region
Region (as in ISO 3166-2) of the location, if known.
Definition location.h:63
QString streetAddress
Street address of the location, if known.
Definition location.h:57
QString locality
Locality/city of the location, if known.
Definition location.h:61
QString name
Human-readable name of the location.
Definition location.h:50
QString postalCode
Postal code of the location, if known.
Definition location.h:59
float longitude
Longitude of the location, in degree, NaN if unknown.
Definition location.h:54
Query operations and data types for accessing realtime public transport information from online servi...
int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs)
QString fromUtf8(QByteArrayView str)
QString number(double n, char format, int precision)
void push_back(QChar ch)
void reserve(qsizetype size)
qsizetype size() const const
QByteArray toUtf8() const const
CaseInsensitive
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:06 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.