summaryrefslogtreecommitdiff
path: root/include/mbgl/style/conversion
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2016-10-28 16:39:50 -0700
committerJohn Firebaugh <john.firebaugh@gmail.com>2017-02-02 09:44:42 -0800
commit141e995806576364d185626176c1b993fc519291 (patch)
treeecdc41fc7699f2a1a9e9456157348451ebe99597 /include/mbgl/style/conversion
parent6a6bddb4537004cc1bfc506e76772de74d33f3f7 (diff)
downloadqtlocation-mapboxgl-141e995806576364d185626176c1b993fc519291.tar.gz
[core] Add support for data-driven styling
Diffstat (limited to 'include/mbgl/style/conversion')
-rw-r--r--include/mbgl/style/conversion/data_driven_property_value.hpp46
-rw-r--r--include/mbgl/style/conversion/function.hpp338
-rw-r--r--include/mbgl/style/conversion/property_setter.hpp7
-rw-r--r--include/mbgl/style/conversion/property_value.hpp2
4 files changed, 355 insertions, 38 deletions
diff --git a/include/mbgl/style/conversion/data_driven_property_value.hpp b/include/mbgl/style/conversion/data_driven_property_value.hpp
new file mode 100644
index 0000000000..83f44fdb27
--- /dev/null
+++ b/include/mbgl/style/conversion/data_driven_property_value.hpp
@@ -0,0 +1,46 @@
+#pragma once
+
+#include <mbgl/style/data_driven_property_value.hpp>
+#include <mbgl/style/conversion.hpp>
+#include <mbgl/style/conversion/constant.hpp>
+#include <mbgl/style/conversion/function.hpp>
+
+namespace mbgl {
+namespace style {
+namespace conversion {
+
+template <class T>
+struct Converter<DataDrivenPropertyValue<T>> {
+ template <class V>
+ Result<DataDrivenPropertyValue<T>> operator()(const V& value) const {
+ if (isUndefined(value)) {
+ return {};
+ } else if (!isObject(value)) {
+ Result<T> constant = convert<T>(value);
+ if (!constant) {
+ return constant.error();
+ }
+ return DataDrivenPropertyValue<T>(*constant);
+ } else if (!objectMember(value, "property")) {
+ Result<CameraFunction<T>> function = convert<CameraFunction<T>>(value);
+ if (!function) {
+ return function.error();
+ }
+ return DataDrivenPropertyValue<T>(*function);
+ } else {
+ Result<CompositeFunction<T>> composite = convert<CompositeFunction<T>>(value);
+ if (composite) {
+ return DataDrivenPropertyValue<T>(*composite);
+ }
+ Result<SourceFunction<T>> source = convert<SourceFunction<T>>(value);
+ if (!source) {
+ return source.error();
+ }
+ return DataDrivenPropertyValue<T>(*source);
+ }
+ }
+};
+
+} // namespace conversion
+} // namespace style
+} // namespace mbgl
diff --git a/include/mbgl/style/conversion/function.hpp b/include/mbgl/style/conversion/function.hpp
index 6a0b67618f..90b4b95063 100644
--- a/include/mbgl/style/conversion/function.hpp
+++ b/include/mbgl/style/conversion/function.hpp
@@ -1,70 +1,340 @@
#pragma once
-#include <mbgl/style/function.hpp>
+#include <mbgl/style/function/camera_function.hpp>
+#include <mbgl/style/function/source_function.hpp>
+#include <mbgl/style/function/composite_function.hpp>
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/constant.hpp>
+#include <mbgl/util/ignore.hpp>
namespace mbgl {
namespace style {
namespace conversion {
+template <class D, class R, class V>
+Result<std::map<D, R>> convertStops(const V& value) {
+ auto stopsValue = objectMember(value, "stops");
+ if (!stopsValue) {
+ return Error { "function value must specify stops" };
+ }
+
+ if (!isArray(*stopsValue)) {
+ return Error { "function stops must be an array" };
+ }
+
+ if (arrayLength(*stopsValue) == 0) {
+ return Error { "function must have at least one stop" };
+ }
+
+ std::map<D, R> stops;
+ for (std::size_t i = 0; i < arrayLength(*stopsValue); ++i) {
+ const auto& stopValue = arrayMember(*stopsValue, i);
+
+ if (!isArray(stopValue)) {
+ return Error { "function stop must be an array" };
+ }
+
+ if (arrayLength(stopValue) != 2) {
+ return Error { "function stop must have two elements" };
+ }
+
+ Result<D> d = convert<D>(arrayMember(stopValue, 0));
+ if (!d) {
+ return d.error();
+ }
+
+ Result<R> r = convert<R>(arrayMember(stopValue, 1));
+ if (!r) {
+ return r.error();
+ }
+
+ stops.emplace(*d, *r);
+ }
+
+ return stops;
+}
+
template <class T>
-struct Converter<Function<T>> {
+struct Converter<ExponentialStops<T>> {
+ static constexpr const char * type = "exponential";
+
template <class V>
- Result<Function<T>> operator()(const V& value) const {
+ Result<ExponentialStops<T>> operator()(const V& value) const {
+ auto stops = convertStops<float, T>(value);
+ if (!stops) {
+ return stops.error();
+ }
+
+ auto baseValue = objectMember(value, "base");
+ if (!baseValue) {
+ return ExponentialStops<T>(*stops);
+ }
+
+ optional<float> base = toNumber(*baseValue);
+ if (!base) {
+ return Error { "function base must be a number"};
+ }
+
+ return ExponentialStops<T>(*stops, *base);
+ }
+};
+
+template <class T>
+struct Converter<IntervalStops<T>> {
+ static constexpr const char * type = "interval";
+
+ template <class V>
+ Result<IntervalStops<T>> operator()(const V& value) const {
+ auto stops = convertStops<float, T>(value);
+ if (!stops) {
+ return stops.error();
+ }
+ return IntervalStops<T>(*stops);
+ }
+};
+
+template <>
+struct Converter<CategoricalValue> {
+ template <class V>
+ Result<CategoricalValue> operator()(const V& value) const {
+ auto b = toBool(value);
+ if (b) {
+ return *b;
+ }
+
+ auto n = toNumber(value);
+ if (n) {
+ return int64_t(*n);
+ }
+
+ auto s = toString(value);
+ if (s) {
+ return *s;
+ }
+
+ return Error { "stop domain value must be a number, string, or boolean" };
+ }
+};
+
+template <class T>
+struct Converter<CategoricalStops<T>> {
+ static constexpr const char * type = "categorical";
+
+ template <class V>
+ Result<CategoricalStops<T>> operator()(const V& value) const {
+ auto stops = convertStops<CategoricalValue, T>(value);
+ if (!stops) {
+ return stops.error();
+ }
+ return CategoricalStops<T>(
+ std::map<CategoricalValue, T>((*stops).begin(), (*stops).end()));
+ }
+};
+
+template <class T>
+struct Converter<IdentityStops<T>> {
+ static constexpr const char * type = "identity";
+
+ template <class V>
+ Result<IdentityStops<T>> operator()(const V&) const {
+ return IdentityStops<T>();
+ }
+};
+
+template <class, class>
+struct StopsConverter;
+
+template <class T, class... Ts>
+struct StopsConverter<T, variant<Ts...>> {
+public:
+ template <class V>
+ Result<variant<Ts...>> operator()(const V& value) const {
+ std::string type = util::Interpolatable<T> ? "exponential" : "interval";
+
+ auto typeValue = objectMember(value, "type");
+ if (typeValue && toString(*typeValue)) {
+ type = *toString(*typeValue);
+ }
+
+ optional<Result<variant<Ts...>>> result;
+
+ // Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47226
+ auto tryConvert = [&] (auto* tp) {
+ using Stops = std::decay_t<decltype(*tp)>;
+ if (type == Converter<Stops>::type) {
+ auto stops = convert<Stops>(value);
+ result = stops
+ ? Result<variant<Ts...>>(*stops)
+ : Result<variant<Ts...>>(stops.error());
+ }
+ };
+
+ util::ignore({
+ (tryConvert((Ts*)nullptr), 0)...
+ });
+
+ if (!result) {
+ return Error { "unsupported function type" };
+ }
+
+ return *result;
+ }
+};
+
+template <class T>
+struct Converter<CameraFunction<T>> {
+ template <class V>
+ Result<CameraFunction<T>> operator()(const V& value) const {
+ if (!isObject(value)) {
+ return Error { "function must be an object" };
+ }
+
+ auto stops = StopsConverter<T, typename CameraFunction<T>::Stops>()(value);
+ if (!stops) {
+ return stops.error();
+ }
+
+ return CameraFunction<T>(*stops);
+ }
+};
+
+template <class T>
+struct Converter<SourceFunction<T>> {
+ template <class V>
+ Result<SourceFunction<T>> operator()(const V& value) const {
+ if (!isObject(value)) {
+ return Error { "function must be an object" };
+ }
+
+ auto propertyValue = objectMember(value, "property");
+ if (!propertyValue) {
+ return Error { "function must specify property" };
+ }
+
+ auto propertyString = toString(*propertyValue);
+ if (!propertyString) {
+ return Error { "function property must be a string" };
+ }
+
+ auto stops = StopsConverter<T, typename SourceFunction<T>::Stops>()(value);
+ if (!stops) {
+ return stops.error();
+ }
+
+ return SourceFunction<T>(*propertyString, *stops);
+ }
+};
+
+template <class S>
+struct CompositeValue : std::pair<float, S> {
+ using std::pair<float, S>::pair;
+};
+
+template <class S>
+struct Converter<CompositeValue<S>> {
+ template <class V>
+ Result<CompositeValue<S>> operator()(const V& value) const {
+ if (!isObject(value)) {
+ return Error { "stop must be an object" };
+ }
+
+ auto zoomValue = objectMember(value, "zoom");
+ if (!zoomValue) {
+ return Error { "stop must specify zoom" };
+ }
+
+ auto propertyValue = objectMember(value, "value");
+ if (!propertyValue) {
+ return Error { "stop must specify value" };
+ }
+
+ Result<float> z = convert<float>(*zoomValue);
+ if (!z) {
+ return z.error();
+ }
+
+ Result<S> s = convert<S>(*propertyValue);
+ if (!s) {
+ return s.error();
+ }
+
+ return CompositeValue<S> { *z, *s };
+ }
+};
+
+template <class T>
+struct Converter<CompositeFunction<T>> {
+ template <class V>
+ Result<CompositeFunction<T>> operator()(const V& value) const {
if (!isObject(value)) {
return Error { "function must be an object" };
}
- auto stopsValue = objectMember(value, "stops");
- if (!stopsValue) {
- return Error { "function value must specify stops" };
+ auto propertyValue = objectMember(value, "property");
+ if (!propertyValue) {
+ return Error { "function must specify property" };
}
- if (!isArray(*stopsValue)) {
- return Error { "function stops must be an array" };
+ auto propertyString = toString(*propertyValue);
+ if (!propertyString) {
+ return Error { "function property must be a string" };
}
- if (arrayLength(*stopsValue) == 0) {
- return Error { "function must have at least one stop" };
+ std::string type = "exponential";
+ auto typeValue = objectMember(value, "type");
+ if (typeValue && toString(*typeValue)) {
+ type = *toString(*typeValue);
}
- std::vector<std::pair<float, T>> stops;
- for (std::size_t i = 0; i < arrayLength(*stopsValue); ++i) {
- const auto& stopValue = arrayMember(*stopsValue, i);
+ if (type == "exponential") {
+ auto stops = convertStops<CompositeValue<float>, T>(value);
+ if (!stops) {
+ return stops.error();
+ }
- if (!isArray(stopValue)) {
- return Error { "function stop must be an array" };
+ auto base = 1.0f;
+ auto baseValue = objectMember(value, "base");
+ if (baseValue && toNumber(*baseValue)) {
+ base = *toNumber(*baseValue);
}
- if (arrayLength(stopValue) != 2) {
- return Error { "function stop must have two elements" };
+ std::map<float, ExponentialStops<T>> convertedStops;
+ for (const auto& stop : *stops) {
+ auto& inner = convertedStops[stop.first.first];
+ inner.base = base;
+ inner.stops.emplace(stop.first.second, stop.second);
}
- optional<float> z = toNumber(arrayMember(stopValue, 0));
- if (!z) {
- return Error { "function stop zoom level must be a number" };
+ return CompositeFunction<T>(*propertyString, convertedStops);
+ } else if (type == "interval") {
+ auto stops = convertStops<CompositeValue<float>, T>(value);
+ if (!stops) {
+ return stops.error();
}
- Result<T> v = convert<T>(arrayMember(stopValue, 1));
- if (!v) {
- return v.error();
+ std::map<float, IntervalStops<T>> convertedStops;
+ for (const auto& stop : *stops) {
+ auto& inner = convertedStops[stop.first.first];
+ inner.stops.emplace(stop.first.second, stop.second);
}
- stops.emplace_back(*z, *v);
- }
+ return CompositeFunction<T>(*propertyString, convertedStops);
+ } else if (type == "categorical") {
+ auto stops = convertStops<CompositeValue<CategoricalValue>, T>(value);
+ if (!stops) {
+ return stops.error();
+ }
- auto baseValue = objectMember(value, "base");
- if (!baseValue) {
- return Function<T>(stops, 1.0f);
- }
+ std::map<float, CategoricalStops<T>> convertedStops;
+ for (const auto& stop : *stops) {
+ auto& inner = convertedStops[stop.first.first];
+ inner.stops.emplace(stop.first.second, stop.second);
+ }
- optional<float> base = toNumber(*baseValue);
- if (!base) {
- return Error { "function base must be a number"};
+ return CompositeFunction<T>(*propertyString, convertedStops);
+ } else {
+ return Error { "unsupported function type" };
}
-
- return Function<T>(stops, *base);
}
};
diff --git a/include/mbgl/style/conversion/property_setter.hpp b/include/mbgl/style/conversion/property_setter.hpp
index 1a601c7c1b..52fb160fde 100644
--- a/include/mbgl/style/conversion/property_setter.hpp
+++ b/include/mbgl/style/conversion/property_setter.hpp
@@ -4,6 +4,7 @@
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/constant.hpp>
#include <mbgl/style/conversion/property_value.hpp>
+#include <mbgl/style/conversion/data_driven_property_value.hpp>
#include <functional>
#include <string>
@@ -18,15 +19,15 @@ using LayoutPropertySetter = std::function<optional<Error> (Layer&, const V&)>;
template <class V>
using PaintPropertySetter = std::function<optional<Error> (Layer&, const V&, const optional<std::string>&)>;
-template <class V, class L, class T, class...Args>
-auto makePropertySetter(void (L::*setter)(PropertyValue<T>, const Args&...args)) {
+template <class V, class L, class PropertyValue, class...Args>
+auto makePropertySetter(void (L::*setter)(PropertyValue, const Args&...args)) {
return [setter] (Layer& layer, const V& value, const Args&...args) -> optional<Error> {
L* typedLayer = layer.as<L>();
if (!typedLayer) {
return Error { "layer doesn't support this property" };
}
- Result<PropertyValue<T>> typedValue = convert<PropertyValue<T>>(value);
+ Result<PropertyValue> typedValue = convert<PropertyValue>(value);
if (!typedValue) {
return typedValue.error();
}
diff --git a/include/mbgl/style/conversion/property_value.hpp b/include/mbgl/style/conversion/property_value.hpp
index de95b56155..d5e2a5c3c8 100644
--- a/include/mbgl/style/conversion/property_value.hpp
+++ b/include/mbgl/style/conversion/property_value.hpp
@@ -16,7 +16,7 @@ struct Converter<PropertyValue<T>> {
if (isUndefined(value)) {
return {};
} else if (isObject(value)) {
- Result<Function<T>> function = convert<Function<T>>(value);
+ Result<CameraFunction<T>> function = convert<CameraFunction<T>>(value);
if (!function) {
return function.error();
}