Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Add queryFeatureExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
alexshalamov committed Nov 15, 2018
1 parent 11ac364 commit 847e1f7
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/mbgl/renderer/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ class Renderer {
AnnotationIDs queryShapeAnnotations(const ScreenBox& box) const;
AnnotationIDs getAnnotationIDs(const std::vector<Feature>&) const;

// Feature extension query
using FeatureExtensionValue = mapbox::util::variant<NullValue, Value, std::vector<Feature>>;
FeatureExtensionValue queryFeatureExtensions(const std::string& sourceID,
const Feature& feature,
const std::string& extension,
const std::string& extensionField,
const optional<std::map<std::string, Value>>& args = {}) const;

// Debug
void dumpDebugLogs();

Expand Down
10 changes: 10 additions & 0 deletions src/mbgl/renderer/render_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ class RenderSource : protected TileObserver {

void setObserver(RenderSourceObserver*);

class FeatureExtension {
public:
virtual ~FeatureExtension() = default;
virtual mapbox::util::variant<NullValue, Value, std::vector<Feature>>
query(const Feature&,
const std::string& extensionField,
const optional<std::map<std::string, Value>>& args) const = 0;
};
virtual const FeatureExtension* getFeatureExtension() const { return nullptr; };

Immutable<style::Source::Impl> baseImpl;

protected:
Expand Down
8 changes: 8 additions & 0 deletions src/mbgl/renderer/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ std::vector<Feature> Renderer::querySourceFeatures(const std::string& sourceID,
return impl->querySourceFeatures(sourceID, options);
}

Renderer::FeatureExtensionValue Renderer::queryFeatureExtensions(const std::string& sourceID,
const Feature& feature,
const std::string& extension,
const std::string& extensionField,
const optional<std::map<std::string, Value>>& args) const {
return impl->queryFeatureExtensions(sourceID, feature, extension, extensionField, args);
}

void Renderer::dumpDebugLogs() {
impl->dumDebugLogs();
}
Expand Down
13 changes: 13 additions & 0 deletions src/mbgl/renderer/renderer_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,19 @@ std::vector<Feature> Renderer::Impl::querySourceFeatures(const std::string& sour
return source->querySourceFeatures(options);
}

// TODO: const std::string& extension is unused
Renderer::FeatureExtensionValue
Renderer::Impl::queryFeatureExtensions(const std::string& sourceID,
const Feature& feature,
const std::string&,
const std::string& extensionField,
const optional<std::map<std::string, Value>>& args) const {
if (const auto* extension = getRenderSource(sourceID)->getFeatureExtension()) {
return extension->query(feature, extensionField, args);
}
return {};
}

void Renderer::Impl::reduceMemoryUse() {
assert(BackendScope::exists());
for (const auto& entry : renderSources) {
Expand Down
6 changes: 6 additions & 0 deletions src/mbgl/renderer/renderer_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class Renderer::Impl : public GlyphManagerObserver,
std::vector<Feature> querySourceFeatures(const std::string& sourceID, const SourceQueryOptions&) const;
std::vector<Feature> queryShapeAnnotations(const ScreenLineString&) const;

FeatureExtensionValue queryFeatureExtensions(const std::string& sourceID,
const Feature& feature,
const std::string& extension,
const std::string& extensionField,
const optional<std::map<std::string, Value>>& args) const;

void reduceMemoryUse();
void dumDebugLogs();

Expand Down
85 changes: 85 additions & 0 deletions src/mbgl/renderer/sources/render_geojson_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,88 @@ namespace mbgl {

using namespace style;

namespace {
template<typename T, typename C>
optional<T> getProperty(const C& cont, const typename C::key_type& name) {
const auto it = cont.find(name);
if (it == cont.end() || !(it->second.template is<T>())) {
return nullopt;
}
return it->second.template get<T>();
}
} // namespace

class RenderGeoJSONSource::RenderGeoJSONSourceFeatureExtension : public RenderSource::FeatureExtension {
public:
using FeatureExtensionValue = mapbox::util::variant<NullValue, Value, std::vector<Feature>>;
using FeatureExtensionGetterFn = FeatureExtensionValue (*)(std::shared_ptr<style::GeoJSONData>, uint64_t, const optional<std::map<std::string, Value>>&);
RenderGeoJSONSourceFeatureExtension() {
featureGetters["children"] = [](auto clusterData,
auto clusterID,
const auto&) -> FeatureExtensionValue {
return clusterData->getChildren(clusterID);
};

featureGetters["leaves"] = [](auto clusterData,
auto clusterID,
const auto& args) -> FeatureExtensionValue {
if (args) {
const auto limit = getProperty<uint64_t>(*args, "limit");
const auto offset = getProperty<uint64_t>(*args, "offset");
// Offset cannot be set without limit.
if (limit) {
if (offset) {
return clusterData->getLeaves(clusterID, *limit, *offset);
}
return clusterData->getLeaves(clusterID, *limit);
}
}

return clusterData->getLeaves(clusterID);
};

featureGetters["expansion_zoom"] = [](auto clusterData,
auto clusterID,
const auto&) -> FeatureExtensionValue {
return static_cast<uint64_t>(clusterData->getClusterExpansionZoom(clusterID));
};
}

FeatureExtensionValue query(const Feature& feature,
const std::string& extensionField,
const optional<std::map<std::string, Value>>& args) const final {
const auto extensionIt = featureGetters.find(extensionField);
if (extensionIt == featureGetters.end()) {
return {};
}

const auto clusterID = getProperty<uint64_t>(feature.properties, "cluster_id");
if (!clusterID) {
return {};
}

auto jsonData = data.lock();
if (!jsonData) {
return {};
}

return extensionIt->second(std::move(jsonData), *clusterID, args);
}

void setData(std::weak_ptr<style::GeoJSONData> data_) { data = data_; }

private:
std::weak_ptr<style::GeoJSONData> data;
std::map<std::string, FeatureExtensionGetterFn> featureGetters;
};

RenderGeoJSONSource::RenderGeoJSONSource(Immutable<style::GeoJSONSource::Impl> impl_)
: RenderSource(impl_) {
tilePyramid.setObserver(this);
}

RenderGeoJSONSource::~RenderGeoJSONSource() = default;

const style::GeoJSONSource::Impl& RenderGeoJSONSource::impl() const {
return static_cast<const style::GeoJSONSource::Impl&>(*baseImpl);
}
Expand Down Expand Up @@ -94,6 +171,14 @@ std::vector<Feature> RenderGeoJSONSource::querySourceFeatures(const SourceQueryO
return tilePyramid.querySourceFeatures(options);
}

const RenderSource::FeatureExtension* RenderGeoJSONSource::getFeatureExtension() const {
if (!extension) {
extension = std::make_unique<RenderGeoJSONSourceFeatureExtension>();
}
extension->setData(data);
return extension.get();
}

void RenderGeoJSONSource::reduceMemoryUse() {
tilePyramid.reduceMemoryUse();
}
Expand Down
4 changes: 4 additions & 0 deletions src/mbgl/renderer/sources/render_geojson_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class GeoJSONData;
class RenderGeoJSONSource : public RenderSource {
public:
RenderGeoJSONSource(Immutable<style::GeoJSONSource::Impl>);
~RenderGeoJSONSource() final;

bool isLoaded() const final;

Expand All @@ -36,6 +37,7 @@ class RenderGeoJSONSource : public RenderSource {

std::vector<Feature>
querySourceFeatures(const SourceQueryOptions&) const final;
const FeatureExtension* getFeatureExtension() const final;

void reduceMemoryUse() final;
void dumpDebugLogs() const final;
Expand All @@ -45,6 +47,8 @@ class RenderGeoJSONSource : public RenderSource {

TilePyramid tilePyramid;
std::weak_ptr<style::GeoJSONData> data;
class RenderGeoJSONSourceFeatureExtension;
mutable std::unique_ptr<RenderGeoJSONSourceFeatureExtension> extension;
};

template <>
Expand Down

0 comments on commit 847e1f7

Please sign in to comment.