File indexing completed on 2024-05-12 04:42:09

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "osmaddress.h"
0008 
0009 #include <KCountry>
0010 #include <KCountrySubdivision>
0011 
0012 using namespace KOSMIndoorMap;
0013 
0014 OSMAddress::OSMAddress() = default;
0015 OSMAddress::OSMAddress(OSM::Element elem)
0016     : m_element(elem)
0017 {
0018 }
0019 
0020 OSMAddress::~OSMAddress() = default;
0021 
0022 QString OSMAddress::street() const
0023 {
0024     return QString::fromUtf8(m_element.tagValue("addr:street", "contact:street", "addr:housename"));
0025 }
0026 
0027 QString OSMAddress::houseNumber() const
0028 {
0029     return QString::fromUtf8(m_element.tagValue("addr:housenumber", "contact:housenumber"));
0030 }
0031 
0032 QString OSMAddress::postalCode() const
0033 {
0034     return QString::fromUtf8(m_element.tagValue("addr:postcode", "contact:postcode"));
0035 }
0036 
0037 QString OSMAddress::city() const
0038 {
0039     return QString::fromUtf8(m_element.tagValue("addr:city", "contact:city"));
0040 }
0041 
0042 QString OSMAddress::state() const
0043 {
0044     const auto state = QString::fromUtf8(m_element.tagValue("addr:state"));
0045     if (!state.isEmpty()) {
0046         return state;
0047     }
0048 
0049     const auto s = KCountrySubdivision::fromLocation(m_element.center().latF(), m_element.center().lonF());
0050     return s.isValid() ? s.code().mid(3) : QString();
0051 }
0052 
0053 QString OSMAddress::country() const
0054 {
0055     const auto country = QString::fromUtf8(m_element.tagValue("addr:country", "contact:country"));
0056     if (!country.isEmpty()) {
0057         return country;
0058     }
0059 
0060     const auto c = KCountry::fromLocation(m_element.center().latF(), m_element.center().lonF());
0061     return c.alpha2();
0062 }
0063 
0064 #include "moc_osmaddress.cpp"