Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit e73b3d2b authored by Volker Krause's avatar Volker Krause

Determine airport country also if we have multiple IATA code candidates

As long as they are all in the same country, that is good enough. The
same will also work for timezones.

This is a common scenario with multiple airports in the same city for
example, especially when one is much more relevant than the others and
therefore often resulting in use of simplified/incomplete names.
parent f48519d3
......@@ -27,14 +27,18 @@
#include <KItinerary/Organization>
#include <QDateTime>
#include <QDebug>
#include <QTimeZone>
using namespace KItinerary;
Flight FlightPostProcessor::processFlight(Flight flight) const
Flight FlightPostProcessor::processFlight(Flight flight)
{
flight.setDepartureAirport(processAirport(flight.departureAirport()));
flight.setArrivalAirport(processAirport(flight.arrivalAirport()));
lookupAirportCodes(flight.departureAirport(), m_departureCodes);
lookupAirportCodes(flight.arrivalAirport(), m_arrivalCodes);
flight.setDepartureAirport(processAirport(flight.departureAirport(), m_departureCodes));
flight.setArrivalAirport(processAirport(flight.arrivalAirport(), m_arrivalCodes));
flight.setAirline(processAirline(flight.airline()));
flight.setBoardingTime(processFlightTime(flight.boardingTime(), flight, flight.departureAirport()));
flight.setDepartureTime(processFlightTime(flight.departureTime(), flight, flight.departureAirport()));
......@@ -43,24 +47,20 @@ Flight FlightPostProcessor::processFlight(Flight flight) const
return flight;
}
Airport FlightPostProcessor::processAirport(Airport airport) const
Airport FlightPostProcessor::processAirport(Airport airport, const std::vector<KnowledgeDb::IataCode> &codes) const
{
// clean up name
airport.setName(airport.name().simplified());
// complete missing IATA codes
auto iataCode = airport.iataCode();
if (iataCode.isEmpty()) {
iataCode = KnowledgeDb::iataCodeFromName(airport.name()).toString();
if (!iataCode.isEmpty()) {
airport.setIataCode(iataCode);
}
if (airport.iataCode().isEmpty() && codes.size() == 1) {
airport.setIataCode(codes[0].toString());
}
// complete missing geo coordinates
auto geo = airport.geo();
if (!geo.isValid()) {
const auto coord = KnowledgeDb::coordinateForAirport(KnowledgeDb::IataCode{iataCode});
if (!geo.isValid() && codes.size() == 1) {
const auto coord = KnowledgeDb::coordinateForAirport(codes[0]);
if (coord.isValid()) {
geo.setLatitude(coord.latitude);
geo.setLongitude(coord.longitude);
......@@ -68,11 +68,11 @@ Airport FlightPostProcessor::processAirport(Airport airport) const
}
}
// add country
// add country, if all candidates are from the same country
auto addr = airport.address();
if (addr.addressCountry().isEmpty()) {
const auto isoCode = KnowledgeDb::countryForAirport(KnowledgeDb::IataCode{iataCode});
if (isoCode.isValid()) {
if (addr.addressCountry().isEmpty() && codes.size() >= 1) {
const auto isoCode = KnowledgeDb::countryForAirport(codes[0]);
if (isoCode.isValid() && std::all_of(codes.begin(), codes.end(), [isoCode](const auto iataCode) { return KnowledgeDb::countryForAirport(iataCode) == isoCode; })) {
addr.setAddressCountry(isoCode.toString());
airport.setAddress(addr);
}
......@@ -120,3 +120,19 @@ QDateTime FlightPostProcessor::processFlightTime(QDateTime dt, const Flight &fli
return dt;
}
void FlightPostProcessor::lookupAirportCodes(const Airport &airport, std::vector<KnowledgeDb::IataCode>& codes) const
{
if (!airport.iataCode().isEmpty()) {
codes.push_back(KnowledgeDb::IataCode(airport.iataCode()));
return;
}
// TODO if we don't need this elsewhere, maybe merge those two methods and do this logic internally more efficently?
const auto code = KnowledgeDb::iataCodeFromName(airport.name());
if (code.isValid()) {
codes.push_back(code);
} else {
codes = KnowledgeDb::iataCodesFromName(airport.name());
}
}
......@@ -20,6 +20,10 @@
#ifndef KITINERARY_FLIGHTPOSTPROCESSOR_P_H
#define KITINERARY_FLIGHTPOSTPROCESSOR_P_H
#include "knowledgedb/iatacode.h"
#include <vector>
class QDateTime;
namespace KItinerary {
......@@ -32,12 +36,17 @@ class Flight;
class FlightPostProcessor
{
public:
Flight processFlight(Flight flight) const;
Flight processFlight(Flight flight);
private:
Airport processAirport(Airport airport) const;
Airport processAirport(Airport airport, const std::vector<KnowledgeDb::IataCode> &codes) const;
Airline processAirline(Airline airline) const;
QDateTime processFlightTime(QDateTime dt, const Flight &flight, const Airport &airport) const;
void lookupAirportCodes(const Airport &airport, std::vector<KnowledgeDb::IataCode> &codes) const;
std::vector<KnowledgeDb::IataCode> m_departureCodes;
std::vector<KnowledgeDb::IataCode> m_arrivalCodes;
};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment