diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/style/conversion/geojson_options.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/style/expression/dsl.cpp | 19 | ||||
-rw-r--r-- | src/mbgl/style/property_expression.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/style/sources/geojson_source_impl.cpp | 60 | ||||
-rw-r--r-- | src/mbgl/style/sources/geojson_source_impl.hpp | 2 |
5 files changed, 43 insertions, 42 deletions
diff --git a/src/mbgl/style/conversion/geojson_options.cpp b/src/mbgl/style/conversion/geojson_options.cpp index 11bd7cc507..0a654e406e 100644 --- a/src/mbgl/style/conversion/geojson_options.cpp +++ b/src/mbgl/style/conversion/geojson_options.cpp @@ -88,7 +88,7 @@ optional<GeoJSONOptions> Converter<GeoJSONOptions>::operator()(const Convertible } } - return { options }; + return { std::move(options) }; } } // namespace conversion diff --git a/src/mbgl/style/expression/dsl.cpp b/src/mbgl/style/expression/dsl.cpp index 70442de968..87494344fe 100644 --- a/src/mbgl/style/expression/dsl.cpp +++ b/src/mbgl/style/expression/dsl.cpp @@ -10,6 +10,10 @@ #include <mbgl/style/expression/compound_expression.hpp> #include <mbgl/style/expression/format_expression.hpp> +#include <mapbox/geojsonvt.hpp> +#include <mbgl/style/conversion/json.hpp> +#include <rapidjson/document.h> + namespace mbgl { namespace style { namespace expression { @@ -21,6 +25,21 @@ std::unique_ptr<Expression> compound(const char* op, std::vector<std::unique_ptr assert(result); return std::move(*result); } + +std::unique_ptr<Expression> createExpression(const char* expr) { + using JSValue = rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator>; + rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> document; + document.Parse<0>(expr); + assert(!document.HasParseError()); + + // optional<expression::TypeAnnotationOption> typeAnnotationOption; + const JSValue* expression = &document; + expression::ParsingContext ctx; + expression::ParseResult parsed = + ctx.parseExpression(mbgl::style::conversion::Convertible(expression)); + assert(parsed); + return std::move(*parsed); +} std::unique_ptr<Expression> error(std::string message) { return std::make_unique<Error>(std::move(message)); diff --git a/src/mbgl/style/property_expression.cpp b/src/mbgl/style/property_expression.cpp index 9ebecc4b40..e438b4ee60 100644 --- a/src/mbgl/style/property_expression.cpp +++ b/src/mbgl/style/property_expression.cpp @@ -9,7 +9,7 @@ PropertyExpressionBase::PropertyExpressionBase(std::unique_ptr<expression::Expre isZoomConstant_ = expression::isZoomConstant(*expression); isFeatureConstant_ = expression::isFeatureConstant(*expression); } - + bool PropertyExpressionBase::isZoomConstant() const noexcept { return isZoomConstant_; } diff --git a/src/mbgl/style/sources/geojson_source_impl.cpp b/src/mbgl/style/sources/geojson_source_impl.cpp index c11728c085..fcef35730b 100644 --- a/src/mbgl/style/sources/geojson_source_impl.cpp +++ b/src/mbgl/style/sources/geojson_source_impl.cpp @@ -1,4 +1,3 @@ -#include <mbgl/style/property_expression.hpp> #include <mbgl/style/sources/geojson_source_impl.hpp> #include <mbgl/tile/tile_id.hpp> #include <mbgl/util/constants.hpp> @@ -14,21 +13,6 @@ namespace mbgl { namespace style { -template <class T> -PropertyExpression<T> createPropertyExpression(const char* expr) { - using JSValue = rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator>; - rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> document; - document.Parse<0>(expr); - assert(!document.HasParseError()); - - // optional<expression::TypeAnnotationOption> typeAnnotationOption; - const JSValue* expression = &document; - expression::ParsingContext ctx; - expression::ParseResult parsed = - ctx.parseExpression(mbgl::style::conversion::Convertible(expression)); - return PropertyExpression<T>(std::move(*parsed)); -} - class GeoJSONVTData : public GeoJSONData { public: GeoJSONVTData(const GeoJSON& geoJSON, const mapbox::geojsonvt::Options& options) @@ -85,12 +69,26 @@ private: mapbox::supercluster::Supercluster impl; }; +template <class T> +T EvaluateFeature(optional<T> accumulated, + mapbox::feature::feature<T>& f, + const std::shared_ptr<expression::Expression> expression, + T finalDefaultValue = T()) { + const expression::EvaluationResult result = expression->evaluate(accumulated, f); + if (result) { + const optional<T> typed = expression::fromExpressionValue<T>(*result); + return typed ? *typed : finalDefaultValue; + } + return finalDefaultValue; +} + GeoJSONSource::Impl::Impl(std::string id_, GeoJSONOptions options_) - : Source::Impl(SourceType::GeoJSON, std::move(id_)), options(std::move(options_)) { + : Source::Impl(SourceType::GeoJSON, std::move(id_)) { + options = std::move(options_); } -GeoJSONSource::Impl::Impl(const Impl& other, const GeoJSON& geoJSON) - : Source::Impl(other), options(other.options) { +GeoJSONSource::Impl::Impl(const Impl& other, const GeoJSON& geoJSON) : Source::Impl(other) { + options = std::move(other.options); constexpr double scale = util::EXTENT / util::tileSize; if (options.cluster && geoJSON.is<mapbox::feature::feature_collection<double>>() && !geoJSON.get<mapbox::feature::feature_collection<double>>().empty()) { @@ -98,28 +96,14 @@ GeoJSONSource::Impl::Impl(const Impl& other, const GeoJSON& geoJSON) clusterOptions.maxZoom = options.clusterMaxZoom; clusterOptions.extent = util::EXTENT; clusterOptions.radius = ::round(scale * options.clusterRadius); - std::unordered_map<std::string, PropertyExpression<double>> mapExpressions; - std::unordered_map<std::string, PropertyExpression<double>> reduceExpressions; - for (const auto& p : options.clusterProperties) { - mapExpressions.emplace(p.first, - createPropertyExpression<double>(p.second.second.c_str())); - std::stringstream ss; - // [operator, ['accumulated'], ['get', key]] - ss << std::string(R"([")") << p.second.first - << std::string(R"(", ["accumulated"], ["get", ")") << p.first - << std::string(R"("]])"); - reduceExpressions.emplace(p.first, createPropertyExpression<double>(ss.str().c_str())); - } + clusterOptions.map = [&](const mapbox::feature::property_map& properties) -> mapbox::feature::property_map { mapbox::feature::property_map ret{}; for (const auto& p : options.clusterProperties) { auto feature = mapbox::feature::feature<double>(); feature.properties = properties; - auto iter = mapExpressions.find(p.first); - if (iter != mapExpressions.end()) { - ret[p.first] = iter->second.evaluate(nullopt, feature); - } + ret[p.first] = EvaluateFeature<double>(nullopt, feature, p.second.first); } return ret; }; @@ -129,10 +113,8 @@ GeoJSONSource::Impl::Impl(const Impl& other, const GeoJSON& geoJSON) auto feature = mapbox::feature::feature<double>(); feature.properties = toFill; optional<double> accumulated(toReturn[p.first].get<double>()); - auto iter = reduceExpressions.find(p.first); - if (iter != reduceExpressions.end()) { - toReturn[p.first] = iter->second.evaluate(accumulated, feature); - } + toReturn[p.first] = EvaluateFeature<double>(accumulated, feature, p.second.second); + ; } }; data = std::make_shared<SuperclusterData>( diff --git a/src/mbgl/style/sources/geojson_source_impl.hpp b/src/mbgl/style/sources/geojson_source_impl.hpp index b88ab35ee0..8d40b58100 100644 --- a/src/mbgl/style/sources/geojson_source_impl.hpp +++ b/src/mbgl/style/sources/geojson_source_impl.hpp @@ -29,7 +29,7 @@ public: Impl(std::string id, GeoJSONOptions); Impl(const GeoJSONSource::Impl&, const GeoJSON&); ~Impl() final; - + Range<uint8_t> getZoomRange() const; std::weak_ptr<GeoJSONData> getData() const; |