summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFriedrich W. H. Kossebau <kossebau@kde.org>2016-09-06 15:38:14 (GMT)
committerFriedrich W. H. Kossebau <kossebau@kde.org>2016-09-06 19:04:57 (GMT)
commit6a5e0d810d43dab354784f2dc877e8243a395bf0 (patch)
tree5c596a5956326d94eb4b5e3ec2e270b508f98ea5
parentc04efe5a74cc1d5e64562216d7d82adcec5e1add (diff)
Add and use OsmPlacemarkData::findTag(const QString& key)
Summary: Some code getting data from OsmPlacemarkData instances currently does a double-lookup with data.containsTagKey(x) & data.tagValue(x). As OsmPlacemarkData already exposes begin and end iterators for the tags, adding a findTag(const QString &key) to allow single-lookup of a tag on conditional access to its value seems matching. Resulting code trades readability (IMHO) for speed, but for a library this option seems to make sense. Reviewers: shentey, sanjibanb, nienhueser, #marble Reviewed By: nienhueser, #marble Differential Revision: https://phabricator.kde.org/D2684
-rw-r--r--src/lib/marble/StyleBuilder.cpp42
-rw-r--r--src/lib/marble/geodata/graphicsitem/GeoPolygonGraphicsItem.cpp37
-rw-r--r--src/lib/marble/osm/OsmPlacemarkData.cpp5
-rw-r--r--src/lib/marble/osm/OsmPlacemarkData.h6
-rw-r--r--src/plugins/runner/json/JsonParser.cpp6
-rw-r--r--src/plugins/runner/osm/OsmNode.cpp5
-rw-r--r--src/plugins/runner/osm/OsmRelation.cpp9
7 files changed, 71 insertions, 39 deletions
diff --git a/src/lib/marble/StyleBuilder.cpp b/src/lib/marble/StyleBuilder.cpp
index 4e5bea9..143b1ef 100644
--- a/src/lib/marble/StyleBuilder.cpp
+++ b/src/lib/marble/StyleBuilder.cpp
@@ -1253,8 +1253,9 @@ GeoDataStyle::ConstPtr StyleBuilder::createStyle(const StyleParameters &paramete
}
}
else if (visualCategory == GeoDataFeature::Bathymetry) {
- if (osmData.containsTagKey(QStringLiteral("ele"))) {
- QString elevation = osmData.tagValue(QStringLiteral("ele"));
+ auto tagIter = osmData.findTag(QStringLiteral("ele"));
+ if (tagIter != osmData.tagsEnd()) {
+ const QString& elevation = tagIter.value();
if (elevation == QLatin1String("4000")) {
polyStyle.setColor("#a5c9c9");
lineStyle.setColor("#a5c9c9");
@@ -1263,15 +1264,19 @@ GeoDataStyle::ConstPtr StyleBuilder::createStyle(const StyleParameters &paramete
}
}
else if (visualCategory == GeoDataFeature::AmenityGraveyard || visualCategory == GeoDataFeature::LanduseCemetery) {
- if (osmData.containsTag(QStringLiteral("religion"), QStringLiteral("jewish"))) {
- polyStyle.setTexturePath(MarbleDirs::path("bitmaps/osmcarto/patterns/grave_yard_jewish.png"));
- adjustStyle = true;
- } else if (osmData.containsTag(QStringLiteral("religion"), QStringLiteral("christian"))) {
- polyStyle.setTexturePath(MarbleDirs::path("bitmaps/osmcarto/patterns/grave_yard_christian.png"));
- adjustStyle = true;
- } else if (osmData.containsTag(QStringLiteral("religion"), QStringLiteral("INT-generic"))) {
- polyStyle.setTexturePath(MarbleDirs::path("bitmaps/osmcarto/patterns/grave_yard_generic.png"));
- adjustStyle = true;
+ auto tagIter = osmData.findTag(QStringLiteral("religion"));
+ if (tagIter != osmData.tagsEnd()) {
+ const QString& religion = tagIter.value();
+ if (religion == QLatin1String("jewish")) {
+ polyStyle.setTexturePath(MarbleDirs::path("bitmaps/osmcarto/patterns/grave_yard_jewish.png"));
+ adjustStyle = true;
+ } else if (religion == QLatin1String("christian")) {
+ polyStyle.setTexturePath(MarbleDirs::path("bitmaps/osmcarto/patterns/grave_yard_christian.png"));
+ adjustStyle = true;
+ } else if (religion == QLatin1String("INT-generic")) {
+ polyStyle.setTexturePath(MarbleDirs::path("bitmaps/osmcarto/patterns/grave_yard_generic.png"));
+ adjustStyle = true;
+ }
}
} else if (visualCategory == GeoDataFeature::HighwayPedestrian) {
polyStyle.setOutline(false);
@@ -1321,8 +1326,9 @@ GeoDataStyle::ConstPtr StyleBuilder::createStyle(const StyleParameters &paramete
lineStyle.setPhysicalWidth(0.0);
lineStyle.setWidth(4.0);
} else {
- if (osmData.containsTagKey(QStringLiteral("width"))) {
- QString const widthValue = osmData.tagValue(QStringLiteral("width")).remove(QStringLiteral(" meters")).remove(QStringLiteral(" m"));
+ auto tagIter = osmData.findTag(QStringLiteral("width"));
+ if (tagIter != osmData.tagsEnd()) {
+ QString const widthValue = QString(tagIter.value()).remove(QStringLiteral(" meters")).remove(QStringLiteral(" m"));
bool ok;
float const width = widthValue.toFloat(&ok);
lineStyle.setPhysicalWidth(ok ? qBound(0.1f, width, 200.0f) : 0.0f);
@@ -1390,8 +1396,9 @@ GeoDataStyle::ConstPtr StyleBuilder::createStyle(const StyleParameters &paramete
GeoDataLineStyle lineStyle = style->lineStyle();
bool adjustStyle = false;
if (visualCategory == GeoDataFeature::Bathymetry) {
- if (osmData.containsTagKey(QStringLiteral("ele"))) {
- QString elevation = osmData.tagValue(QStringLiteral("ele"));
+ auto tagIter = osmData.findTag(QStringLiteral("ele"));
+ if (tagIter != osmData.tagsEnd()) {
+ const QString& elevation = tagIter.value();
if (elevation == QLatin1String("4000")) {
polyStyle.setColor("#a5c9c9");
lineStyle.setColor("#a5c9c9");
@@ -1956,8 +1963,9 @@ GeoDataFeature::GeoDataVisualCategory StyleBuilder::determineVisualCategory(cons
return GeoDataFeature::None;
}
- if (osmData.containsTagKey(QStringLiteral("building")) &&
- buildingValues().contains(osmData.tagValue(QStringLiteral("building"))) ) {
+ auto tagIter = osmData.findTag(QStringLiteral("building"));
+ if (tagIter != osmData.tagsEnd() &&
+ buildingValues().contains(tagIter.value())) {
return GeoDataFeature::Building;
}
diff --git a/src/lib/marble/geodata/graphicsitem/GeoPolygonGraphicsItem.cpp b/src/lib/marble/geodata/graphicsitem/GeoPolygonGraphicsItem.cpp
index 0113ae1..0c1faff 100644
--- a/src/lib/marble/geodata/graphicsitem/GeoPolygonGraphicsItem.cpp
+++ b/src/lib/marble/geodata/graphicsitem/GeoPolygonGraphicsItem.cpp
@@ -98,9 +98,11 @@ int GeoPolygonGraphicsItem::extractBathymetryElevation(const GeoDataFeature *fea
if (feature->nodeType() == GeoDataTypes::GeoDataPlacemarkType) {
const GeoDataPlacemark *placemark = static_cast<const GeoDataPlacemark *>(feature);
+ const OsmPlacemarkData &osmData = placemark->osmData();
- if (placemark->osmData().containsTagKey(QStringLiteral("ele"))) {
- elevation = placemark->osmData().tagValue(QStringLiteral("ele")).toInt();
+ const auto tagIter = osmData.findTag(QStringLiteral("ele"));
+ if (tagIter != osmData.tagsEnd()) {
+ elevation = tagIter.value().toInt();
}
}
@@ -252,18 +254,19 @@ double GeoPolygonGraphicsItem::extractBuildingHeight(const GeoDataFeature *featu
if (feature->nodeType() == GeoDataTypes::GeoDataPlacemarkType) {
const GeoDataPlacemark *placemark = static_cast<const GeoDataPlacemark *>(feature);
-
- if (placemark->osmData().containsTagKey(QStringLiteral("height"))) {
+ const OsmPlacemarkData &osmData = placemark->osmData();
+ QHash<QString, QString>::const_iterator tagIter;
+ if ((tagIter = osmData.findTag(QStringLiteral("height"))) != osmData.tagsEnd()) {
/** @todo Also parse non-SI units, see https://wiki.openstreetmap.org/wiki/Key:height#Height_of_buildings */
- QString const heightValue = placemark->osmData().tagValue(QStringLiteral("height")).remove(QStringLiteral(" meters")).remove(QStringLiteral(" m"));
+ QString const heightValue = QString(tagIter.value()).remove(QStringLiteral(" meters")).remove(QStringLiteral(" m"));
bool extracted = false;
double extractedHeight = heightValue.toDouble(&extracted);
if (extracted) {
height = extractedHeight;
}
- } else if (placemark->osmData().containsTagKey(QStringLiteral("building:levels"))) {
- int const levels = placemark->osmData().tagValue(QStringLiteral("building:levels")).toInt();
- int const skipLevels = placemark->osmData().tagValue(QStringLiteral("building:min_level")).toInt();
+ } else if ((tagIter = osmData.findTag(QStringLiteral("building:levels"))) != osmData.tagsEnd()) {
+ int const levels = tagIter.value().toInt();
+ int const skipLevels = osmData.tagValue(QStringLiteral("building:min_level")).toInt();
/** @todo Is 35 as an upper bound for the number of levels sane? */
height = 3.0 * qBound(1, 1+levels-skipLevels, 35);
}
@@ -279,10 +282,15 @@ QString GeoPolygonGraphicsItem::extractBuildingLabel(const GeoDataFeature *featu
if (!placemark->name().isEmpty()) {
return placemark->name();
- } else if (placemark->osmData().containsTagKey(QStringLiteral("addr:housename"))) {
- return placemark->osmData().tagValue(QStringLiteral("addr:housename"));
- } else if (placemark->osmData().containsTagKey(QStringLiteral("addr:housenumber"))) {
- return placemark->osmData().tagValue(QStringLiteral("addr:housenumber"));
+ }
+ const OsmPlacemarkData &osmData = placemark->osmData();
+ auto tagIter = osmData.findTag(QStringLiteral("addr:housename"));
+ if (tagIter != osmData.tagsEnd()) {
+ return tagIter.value();
+ }
+ tagIter = osmData.findTag(QStringLiteral("addr:housenumber"));
+ if (tagIter != osmData.tagsEnd()) {
+ return tagIter.value();
}
}
@@ -298,10 +306,11 @@ QVector<GeoPolygonGraphicsItem::NamedEntry> GeoPolygonGraphicsItem::extractNamed
const auto end = placemark->osmData().nodeReferencesEnd();
for (auto iter = placemark->osmData().nodeReferencesBegin(); iter != end; ++iter) {
- if (iter.value().containsTagKey(QStringLiteral("addr:housenumber"))) {
+ const auto tagIter = iter.value().findTag(QStringLiteral("addr:housenumber"));
+ if (tagIter != iter.value().tagsEnd()) {
NamedEntry entry;
entry.point = iter.key();
- entry.label = iter.value().tagValue(QStringLiteral("addr:housenumber"));
+ entry.label = tagIter.value();
entries.push_back(entry);
}
}
diff --git a/src/lib/marble/osm/OsmPlacemarkData.cpp b/src/lib/marble/osm/OsmPlacemarkData.cpp
index 13f0204..452f73b 100644
--- a/src/lib/marble/osm/OsmPlacemarkData.cpp
+++ b/src/lib/marble/osm/OsmPlacemarkData.cpp
@@ -137,6 +137,11 @@ bool OsmPlacemarkData::containsTagKey( const QString &key ) const
return m_tags.contains( key );
}
+QHash<QString, QString>::const_iterator OsmPlacemarkData::findTag(const QString &key) const
+{
+ return m_tags.constFind(key);
+}
+
QHash< QString, QString >::const_iterator OsmPlacemarkData::tagsBegin() const
{
return m_tags.begin();
diff --git a/src/lib/marble/osm/OsmPlacemarkData.h b/src/lib/marble/osm/OsmPlacemarkData.h
index f1c04a5..7dca644 100644
--- a/src/lib/marble/osm/OsmPlacemarkData.h
+++ b/src/lib/marble/osm/OsmPlacemarkData.h
@@ -107,6 +107,12 @@ public:
bool containsTagKey( const QString& key ) const;
/**
+ * @brief tagValue returns a pointer to the tag that has @p key as key
+ * or the end iterator if there is no such tag
+ */
+ QHash<QString, QString>::const_iterator findTag(const QString &key) const;
+
+ /**
* @brief iterators for the tags hash.
*/
QHash< QString, QString >::const_iterator tagsBegin() const;
diff --git a/src/plugins/runner/json/JsonParser.cpp b/src/plugins/runner/json/JsonParser.cpp
index 21e4288..3b62471 100644
--- a/src/plugins/runner/json/JsonParser.cpp
+++ b/src/plugins/runner/json/JsonParser.cpp
@@ -259,9 +259,11 @@ bool JsonParser::read( QIODevice* device )
}
// If the property read, is the features name
- if (osmData.containsTagKey(QStringLiteral("name"))) {
+ const auto tagIter = osmData.findTag(QStringLiteral("name"));
+ if (tagIter != osmData.tagsEnd()) {
+ const QString& name = tagIter.value();
for (int pl = 0 ; pl < placemarkList.length(); ++pl) {
- placemarkList.at(pl)->setName(osmData.tagValue(QStringLiteral("name")));
+ placemarkList.at(pl)->setName(name);
}
}
diff --git a/src/plugins/runner/osm/OsmNode.cpp b/src/plugins/runner/osm/OsmNode.cpp
index db37dc1..5d6b8e3 100644
--- a/src/plugins/runner/osm/OsmNode.cpp
+++ b/src/plugins/runner/osm/OsmNode.cpp
@@ -45,9 +45,10 @@ void OsmNode::create(GeoDataDocument *document) const
placemark->setOsmData(m_osmData);
placemark->setCoordinate(m_coordinates);
+ QHash<QString, QString>::const_iterator tagIter;
if ((category == GeoDataFeature::TransportCarShare || category == GeoDataFeature::MoneyAtm)
- && m_osmData.containsTagKey(QStringLiteral("operator"))) {
- placemark->setName(m_osmData.tagValue(QStringLiteral("operator")));
+ && (tagIter = m_osmData.findTag(QStringLiteral("operator"))) != m_osmData.tagsEnd()) {
+ placemark->setName(tagIter.value());
} else {
placemark->setName(m_osmData.tagValue(QStringLiteral("name")));
}
diff --git a/src/plugins/runner/osm/OsmRelation.cpp b/src/plugins/runner/osm/OsmRelation.cpp
index 7fd63f6..1a816d8 100644
--- a/src/plugins/runner/osm/OsmRelation.cpp
+++ b/src/plugins/runner/osm/OsmRelation.cpp
@@ -126,10 +126,11 @@ void OsmRelation::create(GeoDataDocument *document, OsmWays &ways, const OsmNode
if (outerCategory == GeoDataFeature::Bathymetry) {
// In case of a bathymetry store elevation info since it is required during styling
// The ele=* tag is present in the outermost way
- const OsmPlacemarkData outerWayData = ways[*outerWays.begin()].osmData();
- if (outerWayData.containsTagKey(QStringLiteral("ele"))) {
- const QString value = outerWayData.tagValue(QStringLiteral("ele"));
- osmData.addTag(QStringLiteral("ele"), value);
+ const QString ele = QStringLiteral("ele");
+ const OsmPlacemarkData &outerWayData = ways[*outerWays.begin()].osmData();
+ auto tagIter = outerWayData.findTag(ele);
+ if (tagIter != outerWayData.tagsEnd()) {
+ osmData.addTag(ele, tagIter.value());
}
}