File indexing completed on 2024-07-21 12:04:58

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2016 Akshat Tandon <akshat.tandon@research.iiit.ac.in>
0004 //
0005 
0006 #include "TagsFilter.h"
0007 
0008 #include <QStringList>
0009 
0010 #include "GeoDataObject.h"
0011 #include "GeoDataDocument.h"
0012 #include "GeoDataPlacemark.h"
0013 
0014 namespace Marble {
0015 
0016 TagsFilter::TagsFilter(GeoDataDocument *document, const Tags &tagsList, FilterFlag filterFlag)
0017     : m_accepted(new GeoDataDocument)
0018 {
0019     for (GeoDataFeature *feature: document->featureList()) {
0020         if (const auto placemark = geodata_cast<GeoDataPlacemark>(feature)) {
0021             bool acceptPlacemark = false;
0022             auto const & osmData = placemark->osmData();
0023 
0024             if (filterFlag == FilterRailwayService &&
0025                     osmData.containsTagKey(QStringLiteral("railway")) &&
0026                     osmData.containsTagKey(QStringLiteral("service"))) {
0027                 acceptPlacemark = false;
0028             } else {
0029                 for (auto const &tag: tagsList) {
0030                     bool contains;
0031                     if (tag.second == QLatin1String("*")) {
0032                         contains = osmData.containsTagKey(tag.first);
0033                     } else {
0034                         contains = osmData.containsTag(tag.first, tag.second);
0035                     }
0036                     if (contains) {
0037                         acceptPlacemark = true;
0038                         break;
0039                     }
0040                 }
0041             }
0042 
0043             if (acceptPlacemark) {
0044                 m_accepted->append(placemark->clone());
0045             } else {
0046                 m_rejectedObjects.append(placemark->clone());
0047             }
0048         }
0049         else {
0050             m_accepted->append(feature->clone());
0051         }
0052     }
0053 }
0054 
0055 TagsFilter::~TagsFilter()
0056 {
0057     delete m_accepted;
0058     qDeleteAll(m_rejectedObjects);
0059 }
0060 
0061 QVector<GeoDataFeature*>::const_iterator TagsFilter::rejectedObjectsBegin() const
0062 {
0063     return m_rejectedObjects.begin();
0064 }
0065 
0066 QVector<GeoDataFeature*>::const_iterator TagsFilter::rejectedObjectsEnd() const
0067 {
0068     return m_rejectedObjects.end();
0069 }
0070 
0071 GeoDataDocument *TagsFilter::accepted()
0072 {
0073     return m_accepted;
0074 }
0075 
0076 void TagsFilter::removeAnnotationTags(GeoDataDocument *document)
0077 {
0078     for (auto placemark: document->placemarkList()) {
0079         auto & osmData = placemark->osmData();
0080         removeAnnotationTags(osmData);
0081         for (auto & reference: osmData.nodeReferences()) {
0082             removeAnnotationTags(reference);
0083         }
0084         for (auto & reference: osmData.memberReferences()) {
0085             removeAnnotationTags(reference);
0086         }
0087     }
0088 }
0089 
0090 void TagsFilter::removeAnnotationTags(OsmPlacemarkData &osmData)
0091 {
0092     osmData.removeTag(QStringLiteral("comment"));
0093     osmData.removeTag(QStringLiteral("note"));
0094     osmData.removeTag(QStringLiteral("note:de"));
0095     osmData.removeTag(QStringLiteral("fixme"));
0096     osmData.removeTag(QStringLiteral("todo"));
0097     osmData.removeTag(QStringLiteral("source"));
0098     osmData.removeTag(QStringLiteral("source:geometry"));
0099     osmData.removeTag(QStringLiteral("source:name"));
0100     osmData.removeTag(QStringLiteral("source:addr"));
0101     osmData.removeTag(QStringLiteral("source:ref"));
0102     osmData.removeTag(QStringLiteral("source_ref"));
0103 }
0104 
0105 }