File indexing completed on 2024-07-21 09:27:10

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2015 Dennis Nienhüser <nienhueser@kde.org>
0004 //
0005 
0006 #include <OsmNode.h>
0007 
0008 #include "osm/OsmObjectManager.h"
0009 #include <GeoDataStyle.h>
0010 #include <GeoDataIconStyle.h>
0011 #include <GeoDataDocument.h>
0012 #include <MarbleDirs.h>
0013 #include <StyleBuilder.h>
0014 
0015 #include <QXmlStreamAttributes>
0016 
0017 namespace Marble {
0018 
0019 void OsmNode::parseCoordinates(const QXmlStreamAttributes &attributes)
0020 {
0021     qreal const lon = attributes.value(QLatin1String("lon")).toDouble();
0022     qreal const lat = attributes.value(QLatin1String("lat")).toDouble();
0023     setCoordinates(GeoDataCoordinates(lon, lat, 0, GeoDataCoordinates::Degree));
0024 }
0025 
0026 void OsmNode::setCoordinates(const GeoDataCoordinates &coordinates)
0027 {
0028     m_coordinates = coordinates;
0029 }
0030 
0031 GeoDataPlacemark *OsmNode::create() const
0032 {
0033     GeoDataPlacemark::GeoDataVisualCategory const category = StyleBuilder::determineVisualCategory(m_osmData);
0034 
0035     if (category == GeoDataPlacemark::None && m_osmData.isEmpty()) {
0036         return nullptr;
0037     }
0038 
0039     GeoDataPlacemark* placemark = new GeoDataPlacemark;
0040     placemark->setOsmData(m_osmData);
0041     auto coordinates = m_coordinates;
0042     coordinates.setAltitude(m_osmData.tagValue("ele").toDouble());
0043     placemark->setCoordinate(coordinates);
0044 
0045     QHash<QString, QString>::const_iterator tagIter;
0046     if ((category == GeoDataPlacemark::TransportCarShare || category == GeoDataPlacemark::MoneyAtm)
0047             && (tagIter = m_osmData.findTag(QStringLiteral("operator"))) != m_osmData.tagsEnd()) {
0048         placemark->setName(tagIter.value());
0049     } else {
0050         placemark->setName(m_osmData.tagValue(QStringLiteral("name")));
0051     }
0052     if (category == GeoDataPlacemark::AerialwayStation && coordinates.altitude() != 0.0) {
0053         if (placemark->name().isEmpty()) {
0054             placemark->setName(QStringLiteral("%1 m").arg(coordinates.altitude()));
0055         } else {
0056             placemark->setName(QStringLiteral("%1 (%2 m)").arg(placemark->name()).arg(coordinates.altitude()));
0057         }
0058     }
0059     if (placemark->name().isEmpty()) {
0060         placemark->setName(m_osmData.tagValue(QStringLiteral("ref")));
0061     }
0062     placemark->setVisualCategory(category);
0063     placemark->setZoomLevel(StyleBuilder::minimumZoomLevel(category));
0064     placemark->setPopularity(StyleBuilder::popularity(placemark));
0065 
0066     if (category >= GeoDataPlacemark::PlaceCity && category <= GeoDataPlacemark::PlaceVillageNationalCapital) {
0067         int const population = m_osmData.tagValue(QStringLiteral("population")).toInt();
0068         placemark->setPopulation(qMax(0, population));
0069         if (population > 0) {
0070             placemark->setZoomLevel(populationIndex(population));
0071             placemark->setPopularity(population);
0072         }
0073     }
0074 
0075     if (m_osmData.containsTagKey(QLatin1String("marbleZoomLevel"))) {
0076         int const zoomLevel = m_osmData.tagValue(QLatin1String("marbleZoomLevel")).toInt();
0077         placemark->setZoomLevel(zoomLevel);
0078     }
0079 
0080     OsmObjectManager::registerId(m_osmData.id());
0081     return placemark;
0082 }
0083 
0084 int OsmNode::populationIndex(qint64 population) const
0085 {
0086     int popidx = 3;
0087 
0088     if ( population < 2500 )        popidx=10;
0089     else if ( population < 5000)    popidx=9;
0090     else if ( population < 25000)   popidx=8;
0091     else if ( population < 75000)   popidx=7;
0092     else if ( population < 250000)  popidx=6;
0093     else if ( population < 750000)  popidx=5;
0094     else if ( population < 2500000) popidx=4;
0095 
0096     return popidx;
0097 }
0098 
0099 const GeoDataCoordinates &OsmNode::coordinates() const
0100 {
0101     return m_coordinates;
0102 }
0103 
0104 OsmPlacemarkData &OsmNode::osmData()
0105 {
0106     return m_osmData;
0107 }
0108 
0109 const OsmPlacemarkData &OsmNode::osmData() const
0110 {
0111     return m_osmData;
0112 }
0113 
0114 }