summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com>2020-05-15 13:37:31 +0300
committerThiago Marcos P. Santos <thiago@mapbox.com>2020-05-26 20:35:05 +0300
commitcc89ece719fcb93133d3a6f33aac9c8a12a9f4a5 (patch)
treef2c338db6d8150f16ab1656f7106598ccaf56d25
parent0489a8d88abca0966b18b028ec0b5bc33180885d (diff)
downloadqtlocation-mapboxgl-cc89ece719fcb93133d3a6f33aac9c8a12a9f4a5.tar.gz
[core] Get default property API for GeoJSON and Raster DEM sources
-rw-r--r--include/mbgl/style/conversion/geojson_options.hpp5
-rw-r--r--include/mbgl/style/sources/geojson_source.hpp2
-rw-r--r--include/mbgl/style/sources/raster_dem_source.hpp2
-rw-r--r--src/mbgl/style/conversion/geojson_options.cpp14
-rw-r--r--src/mbgl/style/sources/geojson_source.cpp14
-rw-r--r--src/mbgl/style/sources/raster_dem_source.cpp34
-rw-r--r--test/style/conversion/source.test.cpp31
7 files changed, 91 insertions, 11 deletions
diff --git a/include/mbgl/style/conversion/geojson_options.hpp b/include/mbgl/style/conversion/geojson_options.hpp
index 89511848dd..f7aee124c9 100644
--- a/include/mbgl/style/conversion/geojson_options.hpp
+++ b/include/mbgl/style/conversion/geojson_options.hpp
@@ -13,6 +13,11 @@ struct Converter<GeoJSONOptions> {
optional<GeoJSONOptions> operator()(const Convertible& value, Error& error) const;
};
+template <>
+struct ValueFactory<GeoJSONOptions> {
+ static Value make(const GeoJSONOptions&);
+};
+
} // namespace conversion
} // namespace style
} // namespace mbgl
diff --git a/include/mbgl/style/sources/geojson_source.hpp b/include/mbgl/style/sources/geojson_source.hpp
index 122b5cfbb9..4366dcf858 100644
--- a/include/mbgl/style/sources/geojson_source.hpp
+++ b/include/mbgl/style/sources/geojson_source.hpp
@@ -84,6 +84,8 @@ public:
protected:
optional<conversion::Error> setPropertyInternal(const std::string& name,
const conversion::Convertible& value) override;
+ Value getPropertyInternal(const std::string&) const override;
+ Value getPropertyDefaultValueInternal(const std::string&) const override;
Mutable<Source::Impl> createMutable() const noexcept final;
private:
diff --git a/include/mbgl/style/sources/raster_dem_source.hpp b/include/mbgl/style/sources/raster_dem_source.hpp
index 14eed64545..dc068886e5 100644
--- a/include/mbgl/style/sources/raster_dem_source.hpp
+++ b/include/mbgl/style/sources/raster_dem_source.hpp
@@ -21,6 +21,8 @@ public:
Value serialize() const override;
protected:
+ Value getPropertyInternal(const std::string&) const override;
+ Value getPropertyDefaultValueInternal(const std::string&) const override;
Mutable<Source::Impl> createMutable() const noexcept final;
Mutable<Source::Impl> createMutable(Tileset tileset) const noexcept final;
};
diff --git a/src/mbgl/style/conversion/geojson_options.cpp b/src/mbgl/style/conversion/geojson_options.cpp
index 08553e34bb..5245cee1fa 100644
--- a/src/mbgl/style/conversion/geojson_options.cpp
+++ b/src/mbgl/style/conversion/geojson_options.cpp
@@ -154,6 +154,20 @@ optional<GeoJSONOptions> Converter<GeoJSONOptions>::operator()(const Convertible
return { std::move(options) };
}
+Value ValueFactory<GeoJSONOptions>::make(const GeoJSONOptions& options) {
+ mapbox::base::ValueObject result;
+ result["minzoom"] = options.minzoom;
+ result["maxzoom"] = options.maxzoom;
+ result["tileSize"] = options.tileSize;
+ result["buffer"] = options.buffer;
+ result["tolerance"] = options.tolerance;
+ result["lineMetrics"] = options.lineMetrics;
+ result["cluster"] = options.cluster;
+ result["clusterRadius"] = options.clusterRadius;
+ result["clusterMaxZoom"] = options.clusterMaxZoom;
+ return result;
+};
+
} // namespace conversion
} // namespace style
} // namespace mbgl
diff --git a/src/mbgl/style/sources/geojson_source.cpp b/src/mbgl/style/sources/geojson_source.cpp
index f73661d820..2ac80517af 100644
--- a/src/mbgl/style/sources/geojson_source.cpp
+++ b/src/mbgl/style/sources/geojson_source.cpp
@@ -1,7 +1,9 @@
#include <mbgl/storage/file_source.hpp>
#include <mbgl/style/conversion/constant.hpp>
#include <mbgl/style/conversion/geojson.hpp>
+#include <mbgl/style/conversion/geojson_options.hpp>
#include <mbgl/style/conversion/json.hpp>
+#include <mbgl/style/conversion_impl.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/source_observer.hpp>
#include <mbgl/style/sources/geojson_source.hpp>
@@ -154,6 +156,18 @@ optional<conversion::Error> GeoJSONSource::setPropertyInternal(const std::string
return error;
}
+Value GeoJSONSource::getPropertyInternal(const std::string& name) const {
+ using namespace conversion;
+ if (name == "options") return makeValue(getOptions());
+ return Value();
+}
+
+Value GeoJSONSource::getPropertyDefaultValueInternal(const std::string& name) const {
+ using namespace conversion;
+ if (name == "options") return makeValue(*GeoJSONOptions::defaultOptions());
+ return Value();
+}
+
Value GeoJSONSource::serialize() const {
auto value = Source::serialize();
assert(value.getObject());
diff --git a/src/mbgl/style/sources/raster_dem_source.cpp b/src/mbgl/style/sources/raster_dem_source.cpp
index 2535772f85..95dc7d1edd 100644
--- a/src/mbgl/style/sources/raster_dem_source.cpp
+++ b/src/mbgl/style/sources/raster_dem_source.cpp
@@ -5,9 +5,15 @@
#include <mbgl/style/sources/raster_dem_source.hpp>
#include <mbgl/style/sources/raster_dem_source_impl.hpp>
#include <mbgl/tile/tile.hpp>
+#include <mbgl/util/enum.hpp>
#include <utility>
namespace mbgl {
+
+using TilesetDEMEncoding = Tileset::DEMEncoding;
+MBGL_DEFINE_ENUM(TilesetDEMEncoding,
+ {{TilesetDEMEncoding::Mapbox, "mapbox"}, {TilesetDEMEncoding::Terrarium, "terrarium"}});
+
namespace style {
RasterDEMSource::RasterDEMSource(std::string id, variant<std::string, Tileset> urlOrTileset_, uint16_t tileSize)
@@ -17,20 +23,10 @@ bool RasterDEMSource::supportsLayerType(const mbgl::style::LayerTypeInfo* info)
return mbgl::underlying_type(TileKind::RasterDEM) == mbgl::underlying_type(info->tileKind);
}
-static std::string tilesetDEMEncodingToString(mbgl::Tileset::DEMEncoding encoding) {
- switch (encoding) {
- case mbgl::Tileset::DEMEncoding::Mapbox:
- return "mapbox";
- case mbgl::Tileset::DEMEncoding::Terrarium:
- return "terrarium";
- }
- return "terrarium";
-}
-
Value RasterDEMSource::serialize() const {
auto value = RasterSource::serialize();
if (auto* tileset = getTileset()) {
- value.getObject()->insert({"encoding", tilesetDEMEncodingToString(tileset->encoding)});
+ value.getObject()->insert({"encoding", conversion::makeValue(tileset->encoding)});
}
return value;
}
@@ -39,6 +35,22 @@ const RasterDEMSource::Impl& RasterDEMSource::impl() const {
return static_cast<const Impl&>(*baseImpl);
}
+Value RasterDEMSource::getPropertyInternal(const std::string& name) const {
+ if (name == "encoding") {
+ if (auto* tileset = getTileset()) {
+ return conversion::makeValue(tileset->encoding);
+ }
+ }
+ return NullValue();
+}
+
+Value RasterDEMSource::getPropertyDefaultValueInternal(const std::string& name) const {
+ if (name == "encoding") {
+ return conversion::makeValue(TilesetDEMEncoding::Mapbox);
+ }
+ return NullValue();
+}
+
Mutable<Source::Impl> RasterDEMSource::createMutable() const noexcept {
return staticMutableCast<Source::Impl>(makeMutable<Impl>(impl()));
}
diff --git a/test/style/conversion/source.test.cpp b/test/style/conversion/source.test.cpp
index f969384746..88b8aa0b12 100644
--- a/test/style/conversion/source.test.cpp
+++ b/test/style/conversion/source.test.cpp
@@ -12,6 +12,7 @@ namespace {
std::unique_ptr<Source> parseSource(const std::string& src, const std::string& sourceName) {
Error error;
auto source = convertJSON<std::unique_ptr<mbgl::style::Source>>(src, error, sourceName);
+ EXPECT_NE(nullopt, source) << sourceName << " source creation: " << error.message;
if (source) return std::move(*source);
return nullptr;
}
@@ -110,4 +111,34 @@ TEST(StyleConversion, GetSourceGenericPropertyDefaultValues) {
checkGetPropertyDefaultValue(source, "minzoom", 0u);
checkGetPropertyDefaultValue(source, "maxzoom", 22u);
checkGetPropertyDefaultValue(source, "scheme", "xyz");
+
+ source = parseSource(R"JSON({
+ "type": "geojson",
+ "data": "http://127.0.0.1:3000/geojson.json"
+ })JSON",
+ "geojson_source");
+
+ ASSERT_NE(nullptr, source);
+ mapbox::base::ValueObject expected;
+ expected["minzoom"] = 0u;
+ expected["maxzoom"] = 18u;
+ expected["tileSize"] = 512u;
+ expected["buffer"] = 128u;
+ expected["tolerance"] = 0.375;
+ expected["lineMetrics"] = false;
+ expected["cluster"] = false;
+ expected["clusterRadius"] = 50u;
+ expected["clusterMaxZoom"] = 17u;
+ checkGetPropertyDefaultValue(source, "options", expected);
+
+ source = parseSource(R"JSON({
+ "type": "raster-dem",
+ "encoding": "terrarium",
+ "tiles": [
+ "local://tiles/{z}-{x}-{y}.terrain.png"
+ ]
+ })JSON",
+ "rasterdem_source");
+ ASSERT_NE(nullptr, source);
+ checkGetPropertyDefaultValue(source, "encoding", "mapbox");
}