summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2016-08-15 16:16:45 +0200
committerKonstantin Käfer <mail@kkaefer.com>2016-08-19 17:34:10 +0200
commitcb33c861b4cfa82d7afcb5b9ec85d7797679a7d8 (patch)
tree8ad68018af87fd9e41e58a19f5d0286698f203e7
parentdfd40b27a54115cdf769abdda3ec4d3a1329758c (diff)
downloadqtlocation-mapboxgl-cb33c861b4cfa82d7afcb5b9ec85d7797679a7d8.tar.gz
[core] parse more Style JSON properties
We're now parsing "name", "center", "zoom", "bearing", and "pitch" from the stylesheet.
-rw-r--r--include/mbgl/map/map.hpp7
-rw-r--r--src/mbgl/map/map.cpp22
-rw-r--r--src/mbgl/style/parser.cpp37
-rw-r--r--src/mbgl/style/parser.hpp7
-rw-r--r--src/mbgl/style/style.cpp26
-rw-r--r--src/mbgl/style/style.hpp14
-rw-r--r--test/style/style.cpp34
7 files changed, 147 insertions, 0 deletions
diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp
index fac110e2a8..866d292b99 100644
--- a/include/mbgl/map/map.hpp
+++ b/include/mbgl/map/map.hpp
@@ -158,6 +158,13 @@ public:
void addLayer(std::unique_ptr<style::Layer>, const optional<std::string>& beforeLayerID = {});
void removeLayer(const std::string& layerID);
+ // Defaults
+ std::string getStyleName() const;
+ LatLng getDefaultLatLng() const;
+ double getDefaultZoom() const;
+ double getDefaultBearing() const;
+ double getDefaultPitch() const;
+
// Feature queries
std::vector<Feature> queryRenderedFeatures(const ScreenCoordinate&, const optional<std::vector<std::string>>& layerIDs = {});
std::vector<Feature> queryRenderedFeatures(const ScreenBox&, const optional<std::vector<std::string>>& layerIDs = {});
diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp
index 7570a8d303..15f7779f83 100644
--- a/src/mbgl/map/map.cpp
+++ b/src/mbgl/map/map.cpp
@@ -781,6 +781,28 @@ void Map::removeLayer(const std::string& id) {
impl->view.deactivate();
}
+#pragma mark - Defaults
+
+std::string Map::getStyleName() const {
+ return impl->style->getName();
+}
+
+LatLng Map::getDefaultLatLng() const {
+ return impl->style->getDefaultLatLng();
+}
+
+double Map::getDefaultZoom() const {
+ return impl->style->getDefaultZoom();
+}
+
+double Map::getDefaultBearing() const {
+ return impl->style->getDefaultBearing();
+}
+
+double Map::getDefaultPitch() const {
+ return impl->style->getDefaultPitch();
+}
+
#pragma mark - Toggles
void Map::setDebug(MapDebugOptions debugOptions) {
diff --git a/src/mbgl/style/parser.cpp b/src/mbgl/style/parser.cpp
index 2746b8af92..bc05b5484a 100644
--- a/src/mbgl/style/parser.cpp
+++ b/src/mbgl/style/parser.cpp
@@ -42,6 +42,43 @@ void Parser::parse(const std::string& json) {
}
}
+ if (document.HasMember("name")) {
+ const JSValue& value = document["name"];
+ if (value.IsString()) {
+ name = { value.GetString(), value.GetStringLength() };
+ }
+ }
+
+ if (document.HasMember("center")) {
+ const JSValue& value = document["center"];
+ if (value.IsArray() && value.Size() >= 2) {
+ // Style spec uses lon/lat order
+ latLng.longitude = value[0].IsNumber() ? value[0].GetDouble() : 0;
+ latLng.latitude = value[1].IsNumber() ? value[1].GetDouble() : 0;
+ }
+ }
+
+ if (document.HasMember("zoom")) {
+ const JSValue& value = document["zoom"];
+ if (value.IsNumber()) {
+ zoom = value.GetDouble();
+ }
+ }
+
+ if (document.HasMember("bearing")) {
+ const JSValue& value = document["bearing"];
+ if (value.IsNumber()) {
+ bearing = value.GetDouble();
+ }
+ }
+
+ if (document.HasMember("pitch")) {
+ const JSValue& value = document["pitch"];
+ if (value.IsNumber()) {
+ pitch = value.GetDouble();
+ }
+ }
+
if (document.HasMember("sources")) {
parseSources(document["sources"]);
}
diff --git a/src/mbgl/style/parser.hpp b/src/mbgl/style/parser.hpp
index faf80cee93..a6a71ed817 100644
--- a/src/mbgl/style/parser.hpp
+++ b/src/mbgl/style/parser.hpp
@@ -5,6 +5,7 @@
#include <mbgl/util/rapidjson.hpp>
#include <mbgl/util/font_stack.hpp>
+#include <mbgl/util/geo.hpp>
#include <vector>
#include <memory>
@@ -27,6 +28,12 @@ public:
std::vector<std::unique_ptr<Source>> sources;
std::vector<std::unique_ptr<Layer>> layers;
+ std::string name;
+ LatLng latLng;
+ double zoom = 0;
+ double bearing = 0;
+ double pitch = 0;
+
// Statically evaluate layer properties to determine what font stacks are used.
std::vector<FontStack> fontStacks() const;
diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp
index 9df734aeed..1cf64002fb 100644
--- a/src/mbgl/style/style.cpp
+++ b/src/mbgl/style/style.cpp
@@ -100,6 +100,12 @@ void Style::setJSON(const std::string& json) {
addLayer(std::move(layer));
}
+ name = parser.name;
+ defaultLatLng = parser.latLng;
+ defaultZoom = parser.zoom;
+ defaultBearing = parser.bearing;
+ defaultPitch = parser.pitch;
+
glyphStore->setURL(parser.glyphURL);
spriteStore->load(parser.spriteURL, fileSource);
@@ -163,6 +169,26 @@ void Style::removeLayer(const std::string& id) {
layers.erase(it);
}
+std::string Style::getName() const {
+ return name;
+}
+
+LatLng Style::getDefaultLatLng() const {
+ return defaultLatLng;
+}
+
+double Style::getDefaultZoom() const {
+ return defaultZoom;
+}
+
+double Style::getDefaultBearing() const {
+ return defaultBearing;
+}
+
+double Style::getDefaultPitch() const {
+ return defaultPitch;
+}
+
void Style::update(const UpdateParameters& parameters) {
bool allTilesUpdated = true;
diff --git a/src/mbgl/style/style.hpp b/src/mbgl/style/style.hpp
index 35369ffca3..149a0f0803 100644
--- a/src/mbgl/style/style.hpp
+++ b/src/mbgl/style/style.hpp
@@ -13,6 +13,7 @@
#include <mbgl/util/worker.hpp>
#include <mbgl/util/optional.hpp>
#include <mbgl/util/feature.hpp>
+#include <mbgl/util/geo.hpp>
#include <cstdint>
#include <string>
@@ -68,6 +69,12 @@ public:
optional<std::string> beforeLayerID = {});
void removeLayer(const std::string& layerID);
+ std::string getName() const;
+ LatLng getDefaultLatLng() const;
+ double getDefaultZoom() const;
+ double getDefaultBearing() const;
+ double getDefaultPitch() const;
+
bool addClass(const std::string&, const TransitionOptions& = {});
bool removeClass(const std::string&, const TransitionOptions& = {});
bool hasClass(const std::string&) const;
@@ -98,6 +105,13 @@ private:
std::vector<std::string> classes;
optional<TransitionOptions> transitionProperties;
+ // Defaults
+ std::string name;
+ LatLng defaultLatLng;
+ double defaultZoom;
+ double defaultBearing;
+ double defaultPitch;
+
std::vector<std::unique_ptr<Layer>>::const_iterator findLayer(const std::string& layerID) const;
// GlyphStoreObserver implementation.
diff --git a/test/style/style.cpp b/test/style/style.cpp
index 1681bac1c9..b1c7d50baf 100644
--- a/test/style/style.cpp
+++ b/test/style/style.cpp
@@ -61,3 +61,37 @@ TEST(Style, UnusedSourceActiveViaClassUpdate) {
EXPECT_TRUE(unusedSource);
EXPECT_FALSE(unusedSource->baseImpl->isLoaded());
}
+
+TEST(Style, Properties) {
+ util::RunLoop loop;
+
+ StubFileSource fileSource;
+ Style style { fileSource, 1.0 };
+
+ style.setJSON(R"STYLE({"name": "Test"})STYLE");
+ ASSERT_EQ("Test", style.getName());
+
+ style.setJSON(R"STYLE({"center": [10, 20]})STYLE");
+ ASSERT_EQ("", style.getName());
+ ASSERT_EQ((LatLng{20, 10}), style.getDefaultLatLng());
+
+ style.setJSON(R"STYLE({"bearing": 24})STYLE");
+ ASSERT_EQ("", style.getName());
+ ASSERT_EQ((LatLng{0, 0}), style.getDefaultLatLng());
+ ASSERT_EQ(24, style.getDefaultBearing());
+
+ style.setJSON(R"STYLE({"zoom": 13.3})STYLE");
+ ASSERT_EQ("", style.getName());
+ ASSERT_EQ(13.3, style.getDefaultZoom());
+
+ style.setJSON(R"STYLE({"pitch": 60})STYLE");
+ ASSERT_EQ("", style.getName());
+ ASSERT_EQ(60, style.getDefaultPitch());
+
+ style.setJSON(R"STYLE({"name": 23, "center": {}, "bearing": "north", "zoom": null, "pitch": "wide"})STYLE");
+ ASSERT_EQ("", style.getName());
+ ASSERT_EQ((LatLng{0, 0}), style.getDefaultLatLng());
+ ASSERT_EQ(0, style.getDefaultBearing());
+ ASSERT_EQ(0, style.getDefaultZoom());
+ ASSERT_EQ(0, style.getDefaultPitch());
+}