diff --git a/src/mbgl/tile/geojson_tile_data.cpp b/src/mbgl/tile/geojson_tile_data.cpp index fb0cd13f3..fb68a158f 100644 --- a/src/mbgl/tile/geojson_tile_data.cpp +++ b/src/mbgl/tile/geojson_tile_data.cpp @@ -1,21 +1,86 @@ #include -#include +#include #include +#include + namespace mbgl { +// Converts the geojsonvt::Tile to a a GeoJSONTile. They have a differing internal structure. +std::unique_ptr convertTile(const mapbox::geojsonvt::Tile& tile) { + std::shared_ptr layer; + + if (tile) { + std::vector> features; + GeometryCoordinates line; + + for (auto& feature : tile.features) { + const FeatureType featureType = + (feature.type == mapbox::geojsonvt::TileFeatureType::Point + ? FeatureType::Point + : (feature.type == mapbox::geojsonvt::TileFeatureType::LineString + ? FeatureType::LineString + : (feature.type == mapbox::geojsonvt::TileFeatureType::Polygon + ? FeatureType::Polygon + : FeatureType::Unknown))); + if (featureType == FeatureType::Unknown) { + continue; + } + + GeometryCollection geometry; + + // Flatten the geometry; GeoJSONVT distinguishes between a Points array and Rings array + // (Points = GeoJSON types Point, MultiPoint, LineString) + // (Rings = GeoJSON types MultiLineString, Polygon, MultiPolygon) + // However, in Mapbox GL, we use one structure for both types, and just have one outer + // element for Points. + if (feature.tileGeometry.is()) { + line.clear(); + for (auto& point : feature.tileGeometry.get()) { + line.emplace_back(point.x, point.y); + } + geometry.emplace_back(std::move(line)); + } else if (feature.tileGeometry.is()) { + for (auto& ring : feature.tileGeometry.get()) { + line.clear(); + for (auto& point : ring) { + line.emplace_back(point.x, point.y); + } + geometry.emplace_back(std::move(line)); + } + } + + // https://github.com/mapbox/geojson-vt-cpp/issues/44 + if (featureType == FeatureType::Polygon) { + geometry = fixupPolygons(geometry); + } + + Feature::property_map properties{ feature.tags.begin(), feature.tags.end() }; + + features.emplace_back(std::make_shared( + featureType, std::move(geometry), std::move(properties))); + } + + layer = std::make_unique(std::move(features)); + } + + return std::make_unique(layer); +} + GeoJSONTileData::GeoJSONTileData(const OverscaledTileID& overscaledTileID, std::string sourceID, const style::UpdateParameters& parameters, mapbox::geojsonvt::GeoJSONVT* geojsonvt) : GeometryTileData(overscaledTileID, sourceID, parameters.style, parameters.mode) { - tileSource = std::make_unique(*this, geojsonvt, overscaledTileID); + if (geojsonvt) { + auto tile = convertTile( + geojsonvt->getTile(id.canonical.z, id.canonical.x, id.canonical.y)); + setData(std::move(tile), {}, {}); + } } GeoJSONTileData::~GeoJSONTileData() = default; -void GeoJSONTileData::setNecessity(Necessity necessity) { - tileSource->setNecessity(static_cast(necessity)); -} +void GeoJSONTileData::setNecessity(Necessity) {} } // namespace mbgl diff --git a/src/mbgl/tile/geojson_tile_data.hpp b/src/mbgl/tile/geojson_tile_data.hpp index 2110b4b9c..613ef2d96 100644 --- a/src/mbgl/tile/geojson_tile_data.hpp +++ b/src/mbgl/tile/geojson_tile_data.hpp @@ -10,8 +10,6 @@ class GeoJSONVT; namespace mbgl { -class TileSource; - namespace style { class UpdateParameters; } @@ -25,9 +23,6 @@ public: ~GeoJSONTileData(); void setNecessity(Necessity) final; - -private: - std::unique_ptr tileSource; }; } // namespace mbgl diff --git a/src/mbgl/tile/geojson_tile_source.cpp b/src/mbgl/tile/geojson_tile_source.cpp deleted file mode 100644 index fb526fd9f..000000000 --- a/src/mbgl/tile/geojson_tile_source.cpp +++ /dev/null @@ -1,99 +0,0 @@ -#include -#include -#include - -#include - -#include - -namespace mbgl { - -GeoJSONTileSource::GeoJSONTileSource(GeometryTileData& tileData_, - mapbox::geojsonvt::GeoJSONVT* geojsonvt_, - const OverscaledTileID& id) - : GeometryTileSource(tileData_), tileID(id), geojsonvt(geojsonvt_) { - update(); -} - -GeoJSONTileSource::~GeoJSONTileSource() = default; - -// A monitor can have its GeoJSONVT object swapped out (e.g. when loading a new GeoJSON file). -// In that case, we're sending new notifications to all observers. -void GeoJSONTileSource::setGeoJSONVT(mapbox::geojsonvt::GeoJSONVT* vt) { - // Don't duplicate notifications in case of nil changes. - if (geojsonvt != vt) { - geojsonvt = vt; - update(); - } -} - -// Converts the geojsonvt::Tile to a a GeoJSONTile. They have a differing internal structure. -std::unique_ptr convertTile(const mapbox::geojsonvt::Tile& tile) { - std::shared_ptr layer; - - if (tile) { - std::vector> features; - GeometryCoordinates line; - - for (auto& feature : tile.features) { - const FeatureType featureType = - (feature.type == mapbox::geojsonvt::TileFeatureType::Point - ? FeatureType::Point - : (feature.type == mapbox::geojsonvt::TileFeatureType::LineString - ? FeatureType::LineString - : (feature.type == mapbox::geojsonvt::TileFeatureType::Polygon - ? FeatureType::Polygon - : FeatureType::Unknown))); - if (featureType == FeatureType::Unknown) { - continue; - } - - GeometryCollection geometry; - - // Flatten the geometry; GeoJSONVT distinguishes between a Points array and Rings array - // (Points = GeoJSON types Point, MultiPoint, LineString) - // (Rings = GeoJSON types MultiLineString, Polygon, MultiPolygon) - // However, in Mapbox GL, we use one structure for both types, and just have one outer - // element for Points. - if (feature.tileGeometry.is()) { - line.clear(); - for (auto& point : feature.tileGeometry.get()) { - line.emplace_back(point.x, point.y); - } - geometry.emplace_back(std::move(line)); - } else if (feature.tileGeometry.is()) { - for (auto& ring : feature.tileGeometry.get()) { - line.clear(); - for (auto& point : ring) { - line.emplace_back(point.x, point.y); - } - geometry.emplace_back(std::move(line)); - } - } - - // https://github.com/mapbox/geojson-vt-cpp/issues/44 - if (featureType == FeatureType::Polygon) { - geometry = fixupPolygons(geometry); - } - - Feature::property_map properties{ feature.tags.begin(), feature.tags.end() }; - - features.emplace_back(std::make_shared( - featureType, std::move(geometry), std::move(properties))); - } - - layer = std::make_unique(std::move(features)); - } - - return std::make_unique(layer); -} - -void GeoJSONTileSource::update() { - if (geojsonvt) { - auto tile = convertTile( - geojsonvt->getTile(tileID.canonical.z, tileID.canonical.x, tileID.canonical.y)); - tileData.setData(std::move(tile), {}, {}); - } -} - -} // namespace mbgl diff --git a/src/mbgl/tile/geojson_tile_source.hpp b/src/mbgl/tile/geojson_tile_source.hpp deleted file mode 100644 index faae757eb..000000000 --- a/src/mbgl/tile/geojson_tile_source.hpp +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include -#include -#include - -#include - -namespace mapbox { -namespace geojsonvt { -class GeoJSONVT; -} // namespace geojsonvt -} // namespace mapbox - -namespace mbgl { - -class GeoJSONTileSource final : public GeometryTileSource { -public: - GeoJSONTileSource(GeometryTileData&, mapbox::geojsonvt::GeoJSONVT*, const OverscaledTileID&); - virtual ~GeoJSONTileSource(); - - void setGeoJSONVT(mapbox::geojsonvt::GeoJSONVT*); - -private: - void update(); - -public: - const OverscaledTileID tileID; - -private: - mapbox::geojsonvt::GeoJSONVT* geojsonvt = nullptr; -}; - -} // namespace mbgl