KPublicTransport

coveragearea.cpp
1 /*
2  SPDX-FileCopyrightText: 2021 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "coveragearea.h"
8 #include "datatypes_p.h"
9 #include "json_p.h"
10 #include "location.h"
11 #include "geo/geojson_p.h"
12 
13 #include <QJsonObject>
14 #include <QPolygonF>
15 #include <QStringList>
16 
17 using namespace KPublicTransport;
18 
19 namespace KPublicTransport {
20 class CoverageAreaPrivate : public QSharedData {
21 public:
22  CoverageArea::Type type = CoverageArea::Any;
23  QStringList regions;
24  QStringList uicCompanyCodes;
25  std::vector<QPolygonF> areas;
26  QRectF boundingBox;
27 };
28 }
29 
30 KPUBLICTRANSPORT_MAKE_GADGET(CoverageArea)
31 KPUBLICTRANSPORT_MAKE_PROPERTY(CoverageArea, CoverageArea::Type, type, setType)
32 KPUBLICTRANSPORT_MAKE_PROPERTY(CoverageArea, QStringList, regions, setRegions)
33 KPUBLICTRANSPORT_MAKE_PROPERTY(CoverageArea, QStringList, uicCompanyCodes, setUicCompanyCodes)
34 
35 bool CoverageArea::isEmpty() const
36 {
37  return d->regions.isEmpty() && d->areas.empty();
38 }
39 
41 {
42  if (d->regions.size() == 1 && d->regions.at(0) == QLatin1String("UN")) {
43  return true;
44  }
45 
46  return d->boundingBox.topLeft() == QPointF(-180.0, -90.0) && d->boundingBox.bottomRight() == QPointF(180.0, 90.0);
47 }
48 
49 static QStringView countryCode(QStringView isoCode)
50 {
51  return isoCode.size() < 2 ? QStringView() : isoCode.left(2);
52 }
53 
54 bool CoverageArea::coversLocation(const Location &loc) const
55 {
56  if (loc.hasCoordinate() && !d->areas.empty()) {
57  if (d->boundingBox.contains({loc.longitude(), loc.latitude()})) {
58  return std::any_of(d->areas.begin(), d->areas.end(), [&loc](const auto &area) {
59  return area.containsPoint({loc.longitude(), loc.latitude()}, Qt::WindingFill);
60  });
61  }
62  return false;
63  }
64 
65  // TODO we could do a more precise check for ISO 3166-2 subdivision codes when available
66 
67  if (loc.country().size() == 2 && !d->regions.empty()) {
68  if (d->regions.size() == 1 && d->regions.at(0) == QLatin1String("UN")) { // global coverage
69  return true;
70  }
71  return std::binary_search(d->regions.begin(), d->regions.end(), loc.country(), [](const auto &lhs, const auto &rhs) {
72  return countryCode(lhs) < countryCode(rhs);
73  });
74  }
75 
76  // if we have nothing to check against, assume the backend can help (otherwise none would trigger)
77  return true;
78 }
79 
81 {
82  CoverageArea ca;
83  ca.setRegions(Json::toStringList(obj.value(QLatin1String("region"))));
84  ca.setUicCompanyCodes(Json::toStringList(obj.value(QLatin1String("uicCompanyCodes"))));
85  std::sort(ca.d->regions.begin(), ca.d->regions.end());
86  ca.d->areas = GeoJson::readOuterPolygons(obj.value(QLatin1String("area")).toObject());
87  for (const auto &area : ca.d->areas) {
88  ca.d->boundingBox |= area.boundingRect();
89  }
90  return ca;
91 }
92 
93 #include "moc_coveragearea.cpp"
Describes the area a specific KPublicTransport::Backend can provide information for.
Definition: coveragearea.h:22
Query operations and data types for accessing realtime public transport information from online servi...
Definition: attribution.cpp:16
qsizetype size() const const
WindingFill
bool isGlobal() const
Returns true if this area covers the entire world.
int size() const const
float latitude
Latitude of the location, in degree, NaN if unknown.
Definition: location.h:52
static CoverageArea fromJson(const QJsonObject &obj)
Read a single coverage area information from a JSON object in Transport API Repository format...
bool coversLocation(const Location &loc) const
Checks whether loc is covered by this area.
float longitude
Longitude of the location, in degree, NaN if unknown.
Definition: location.h:54
QJsonObject toObject() const const
QString country
Country of the location as ISO 3166-1 alpha 2 code, if known.
Definition: location.h:65
QList::iterator end()
bool isEmpty() const const
QStringList regions
ISO 3166-1/2 codes of covered regions.
Definition: coveragearea.h:37
Type
Coverage quality as defined by the Transport API Repository format.
Definition: coveragearea.h:27
QJsonValue value(const QString &key) const const
QStringView left(qsizetype length) const const
QList::iterator begin()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 23 2021 23:05:21 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.