diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/renderer/render_source.cpp | 3 | ||||
-rw-r--r-- | src/mbgl/renderer/sources/render_custom_vector_source.cpp | 118 | ||||
-rw-r--r-- | src/mbgl/renderer/sources/render_custom_vector_source.hpp | 51 | ||||
-rw-r--r-- | src/mbgl/style/sources/custom_vector_source.cpp | 18 | ||||
-rw-r--r-- | src/mbgl/style/sources/custom_vector_source_impl.cpp | 32 | ||||
-rw-r--r-- | src/mbgl/style/sources/custom_vector_source_impl.hpp | 27 | ||||
-rw-r--r-- | src/mbgl/style/types.cpp | 1 |
7 files changed, 250 insertions, 0 deletions
diff --git a/src/mbgl/renderer/render_source.cpp b/src/mbgl/renderer/render_source.cpp index 7723a1c7ca..8c5abfb32d 100644 --- a/src/mbgl/renderer/render_source.cpp +++ b/src/mbgl/renderer/render_source.cpp @@ -6,6 +6,7 @@ #include <mbgl/renderer/tile_parameters.hpp> #include <mbgl/annotation/render_annotation_source.hpp> #include <mbgl/renderer/sources/render_image_source.hpp> +#include <mbgl/renderer/sources/render_custom_vector_source.hpp> #include <mbgl/tile/tile.hpp> namespace mbgl { @@ -27,6 +28,8 @@ std::unique_ptr<RenderSource> RenderSource::create(Immutable<Source::Impl> impl) return std::make_unique<RenderAnnotationSource>(staticImmutableCast<AnnotationSource::Impl>(impl)); case SourceType::Image: return std::make_unique<RenderImageSource>(staticImmutableCast<ImageSource::Impl>(impl)); + case SourceType::CustomVector: + return std::make_unique<RenderCustomVectorSource>(staticImmutableCast<CustomVectorSource::Impl>(impl)); } // Not reachable, but placate GCC. diff --git a/src/mbgl/renderer/sources/render_custom_vector_source.cpp b/src/mbgl/renderer/sources/render_custom_vector_source.cpp new file mode 100644 index 0000000000..b6c27bb00c --- /dev/null +++ b/src/mbgl/renderer/sources/render_custom_vector_source.cpp @@ -0,0 +1,118 @@ +#include <mbgl/renderer/sources/render_custom_vector_source.hpp> +#include <mbgl/renderer/render_tile.hpp> +#include <mbgl/renderer/paint_parameters.hpp> +#include <mbgl/tile/geojson_tile.hpp> +#include <mbgl/util/logging.hpp> + +#include <mbgl/algorithm/generate_clip_ids.hpp> +#include <mbgl/algorithm/generate_clip_ids_impl.hpp> + +#include <mapbox/geojsonvt.hpp> + +namespace mbgl { + +using namespace style; + +RenderCustomVectorSource::RenderCustomVectorSource(Immutable<style::CustomVectorSource::Impl> impl_) + : RenderSource(impl_) { + tilePyramid.setObserver(this); +} + +const style::CustomVectorSource::Impl& RenderCustomVectorSource::impl() const { + return static_cast<const style::CustomVectorSource::Impl&>(*baseImpl); +} + +bool RenderCustomVectorSource::isLoaded() const { + return tilePyramid.isLoaded(); +} + +void RenderCustomVectorSource::setTileData(const CanonicalTileID& tileID, + const style::FetchTileResult& result) { + if (result.is<style::Error>()) { + Log::Error(Event::Render, "FetchTile (%d, %d, %d) error: %s", tileID.z, tileID.x, tileID.y, result.get<style::Error>().message.c_str()); + return; + } + + auto geoJSON = result.get<mapbox::geojson::geojson>(); + auto data = mapbox::geometry::feature_collection<int16_t>(); + if (geoJSON.is<FeatureCollection>() && !geoJSON.get<FeatureCollection>().empty()) { + const GeoJSONOptions options = impl().getOptions(); + + const double scale = util::EXTENT / options.tileSize; + + mapbox::geojsonvt::Options vtOptions; + vtOptions.maxZoom = options.maxzoom; + vtOptions.extent = util::EXTENT; + vtOptions.buffer = std::round(scale * options.buffer); + vtOptions.tolerance = scale * options.tolerance; + auto geojsonVt = std::make_unique<mapbox::geojsonvt::GeoJSONVT>(geoJSON, vtOptions); + data = geojsonVt->getTile(tileID.z, tileID.x, tileID.y).features; + } + for (auto const& item : tilePyramid.tiles) { + if (item.first.canonical == tileID) { + static_cast<GeoJSONTile*>(item.second.get())->updateData(data); + } + } +} + +void RenderCustomVectorSource::update(Immutable<style::Source::Impl> baseImpl_, + const std::vector<Immutable<Layer::Impl>>& layers, + const bool needsRendering, + const bool needsRelayout, + const TileParameters& parameters) { + std::swap(baseImpl, baseImpl_); + + enabled = needsRendering; + + auto fetchTile = impl().getFetchTileFunction(); + + FetchTileCallback fetchTileCallback = std::bind(&RenderCustomVectorSource::setTileData, this, std::placeholders::_1, std::placeholders::_2); + + const GeoJSONOptions options = impl().getOptions(); + tilePyramid.update(layers, + needsRendering, + needsRelayout, + parameters, + SourceType::CustomVector, + util::tileSize, + { options.minzoom, options.maxzoom }, + [&] (const OverscaledTileID& tileID) { + fetchTile(tileID.canonical, fetchTileCallback); + return std::make_unique<GeoJSONTile>(tileID, impl().id, parameters, mapbox::geometry::feature_collection<int16_t>()); + }); +} + +void RenderCustomVectorSource::startRender(PaintParameters& parameters) { + parameters.clipIDGenerator.update(tilePyramid.getRenderTiles()); + tilePyramid.startRender(parameters); +} + +void RenderCustomVectorSource::finishRender(PaintParameters& parameters) { + tilePyramid.finishRender(parameters); +} + +std::vector<std::reference_wrapper<RenderTile>> RenderCustomVectorSource::getRenderTiles() { + return tilePyramid.getRenderTiles(); +} + +std::unordered_map<std::string, std::vector<Feature>> +RenderCustomVectorSource::queryRenderedFeatures(const ScreenLineString& geometry, + const TransformState& transformState, + const std::vector<const RenderLayer*>& layers, + const RenderedQueryOptions& options) const { + return tilePyramid.queryRenderedFeatures(geometry, transformState, layers, options); +} + +std::vector<Feature> RenderCustomVectorSource::querySourceFeatures(const SourceQueryOptions& options) const { + return tilePyramid.querySourceFeatures(options); +} + +void RenderCustomVectorSource::onLowMemory() { + tilePyramid.onLowMemory(); +} + +void RenderCustomVectorSource::dumpDebugLogs() const { + tilePyramid.dumpDebugLogs(); +} + +} // namespace mbgl diff --git a/src/mbgl/renderer/sources/render_custom_vector_source.hpp b/src/mbgl/renderer/sources/render_custom_vector_source.hpp new file mode 100644 index 0000000000..43e5094cc1 --- /dev/null +++ b/src/mbgl/renderer/sources/render_custom_vector_source.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include <mbgl/renderer/render_source.hpp> +#include <mbgl/renderer/tile_pyramid.hpp> +#include <mbgl/style/sources/custom_vector_source_impl.hpp> + +namespace mbgl { + +class RenderCustomVectorSource : public RenderSource { +public: + RenderCustomVectorSource(Immutable<style::CustomVectorSource::Impl>); + + bool isLoaded() const final; + + void update(Immutable<style::Source::Impl>, + const std::vector<Immutable<style::Layer::Impl>>&, + bool needsRendering, + bool needsRelayout, + const TileParameters&) final; + + void startRender(PaintParameters&) final; + void finishRender(PaintParameters&) final; + + std::vector<std::reference_wrapper<RenderTile>> getRenderTiles() final; + + std::unordered_map<std::string, std::vector<Feature>> + queryRenderedFeatures(const ScreenLineString& geometry, + const TransformState& transformState, + const std::vector<const RenderLayer*>& layers, + const RenderedQueryOptions& options) const final; + + std::vector<Feature> + querySourceFeatures(const SourceQueryOptions&) const final; + + void onLowMemory() final; + void dumpDebugLogs() const final; + + void setTileData(const CanonicalTileID& tileID, const style::FetchTileResult& result); + +private: + const style::CustomVectorSource::Impl& impl() const; + + TilePyramid tilePyramid; +}; + +template <> +inline bool RenderSource::is<RenderCustomVectorSource>() const { + return baseImpl->type == SourceType::CustomVector; +} + +} // namespace mbgl diff --git a/src/mbgl/style/sources/custom_vector_source.cpp b/src/mbgl/style/sources/custom_vector_source.cpp new file mode 100644 index 0000000000..e0bbd85af7 --- /dev/null +++ b/src/mbgl/style/sources/custom_vector_source.cpp @@ -0,0 +1,18 @@ +#include <mbgl/style/sources/custom_vector_source.hpp> +#include <mbgl/style/sources/custom_vector_source_impl.hpp> + +namespace mbgl { +namespace style { + +CustomVectorSource::CustomVectorSource(std::string id, + const GeoJSONOptions options, + FetchTileFunction fetchTileFn) + : Source(makeMutable<CustomVectorSource::Impl>(std::move(id), options, fetchTileFn)) { +} + +void CustomVectorSource::loadDescription(FileSource&) { + loaded = true; +} + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/sources/custom_vector_source_impl.cpp b/src/mbgl/style/sources/custom_vector_source_impl.cpp new file mode 100644 index 0000000000..1b5ee4e5d1 --- /dev/null +++ b/src/mbgl/style/sources/custom_vector_source_impl.cpp @@ -0,0 +1,32 @@ +#include <mbgl/style/sources/custom_vector_source_impl.hpp> + +#include <mbgl/style/source_observer.hpp> +#include <mbgl/tile/geojson_tile.hpp> +#include <mbgl/tile/vector_tile.hpp> +#include <mbgl/util/tile_cover.hpp> + +namespace mbgl { +namespace style { + +CustomVectorSource::Impl::Impl(std::string id_, + const GeoJSONOptions options_, + FetchTileFunction fetchTileFn_) + : Source::Impl(SourceType::CustomVector, std::move(id_)), + options(options_), + fetchTileFn(fetchTileFn_) { +} + +optional<std::string> CustomVectorSource::Impl::getAttribution() const { + return {}; +} + +GeoJSONOptions CustomVectorSource::Impl::getOptions() const { + return options; +} + +FetchTileFunction CustomVectorSource::Impl::getFetchTileFunction() const { + return fetchTileFn; +} + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/sources/custom_vector_source_impl.hpp b/src/mbgl/style/sources/custom_vector_source_impl.hpp new file mode 100644 index 0000000000..aa07bc800d --- /dev/null +++ b/src/mbgl/style/sources/custom_vector_source_impl.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include <mbgl/style/source_impl.hpp> +#include <mbgl/style/sources/custom_vector_source.hpp> +#include <mbgl/tile/tile_id.hpp> + +namespace mbgl { +namespace style { + +class CustomVectorSource::Impl : public Source::Impl { +public: + Impl(std::string id, + GeoJSONOptions options, + FetchTileFunction fetchTileFn); + + optional<std::string> getAttribution() const final; + + GeoJSONOptions getOptions() const; + FetchTileFunction getFetchTileFunction() const; + +private: + GeoJSONOptions options; + FetchTileFunction fetchTileFn; +}; + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/types.cpp b/src/mbgl/style/types.cpp index 0a1781e01b..cd5e597fc0 100644 --- a/src/mbgl/style/types.cpp +++ b/src/mbgl/style/types.cpp @@ -12,6 +12,7 @@ MBGL_DEFINE_ENUM(SourceType, { { SourceType::Video, "video" }, { SourceType::Annotations, "annotations" }, { SourceType::Image, "image" }, + { SourceType::CustomVector, "customvector" } }); MBGL_DEFINE_ENUM(VisibilityType, { |