diff options
Diffstat (limited to 'src/mbgl/annotation/shape_annotation_impl.cpp')
-rw-r--r-- | src/mbgl/annotation/shape_annotation_impl.cpp | 109 |
1 files changed, 12 insertions, 97 deletions
diff --git a/src/mbgl/annotation/shape_annotation_impl.cpp b/src/mbgl/annotation/shape_annotation_impl.cpp index f22debdd81..620b1acc76 100644 --- a/src/mbgl/annotation/shape_annotation_impl.cpp +++ b/src/mbgl/annotation/shape_annotation_impl.cpp @@ -1,5 +1,3 @@ -#include <mapbox/geojsonvt/convert.hpp> - #include <mbgl/annotation/shape_annotation_impl.hpp> #include <mbgl/annotation/annotation_tile.hpp> #include <mbgl/tile/tile_id.hpp> @@ -7,6 +5,7 @@ #include <mbgl/math/clamp.hpp> #include <mbgl/util/string.hpp> #include <mbgl/util/constants.hpp> +#include <mbgl/util/geometry.hpp> namespace mbgl { @@ -19,86 +18,16 @@ ShapeAnnotationImpl::ShapeAnnotationImpl(const AnnotationID id_, const uint8_t m layerID("com.mapbox.annotations.shape." + util::toString(id)) { } -struct ToGeoJSONVT { - const double tolerance; - - ToGeoJSONVT(const double tolerance_) - : tolerance(tolerance_) { - } - - geojsonvt::ProjectedFeature operator()(const LineString<double>& line) const { - geojsonvt::ProjectedRings converted; - converted.push_back(convertPoints(geojsonvt::ProjectedFeatureType::LineString, line)); - return convertFeature(geojsonvt::ProjectedFeatureType::LineString, converted); - } - - geojsonvt::ProjectedFeature operator()(const Polygon<double>& polygon) const { - geojsonvt::ProjectedRings converted; - for (const auto& ring : polygon) { - converted.push_back(convertPoints(geojsonvt::ProjectedFeatureType::Polygon, ring)); - } - return convertFeature(geojsonvt::ProjectedFeatureType::Polygon, converted); - } - - geojsonvt::ProjectedFeature operator()(const MultiLineString<double>& lines) const { - geojsonvt::ProjectedRings converted; - for (const auto& line : lines) { - converted.push_back(convertPoints(geojsonvt::ProjectedFeatureType::LineString, line)); - } - return convertFeature(geojsonvt::ProjectedFeatureType::LineString, converted); - } - - geojsonvt::ProjectedFeature operator()(const MultiPolygon<double>& polygons) const { - geojsonvt::ProjectedRings converted; - for (const auto& polygon : polygons) { - for (const auto& ring : polygon) { - converted.push_back(convertPoints(geojsonvt::ProjectedFeatureType::Polygon, ring)); - } - } - return convertFeature(geojsonvt::ProjectedFeatureType::Polygon, converted); - } - -private: - geojsonvt::LonLat convertPoint(const Point<double>& p) const { - return { - util::wrap(p.x, -util::LONGITUDE_MAX, util::LONGITUDE_MAX), - util::clamp(p.y, -util::LATITUDE_MAX, util::LATITUDE_MAX) - }; - } - - geojsonvt::ProjectedRing convertPoints(geojsonvt::ProjectedFeatureType type, const std::vector<Point<double>>& points) const { - std::vector<geojsonvt::LonLat> converted; - - for (const auto& p : points) { - converted.push_back(convertPoint(p)); - } - - assert(points.size() > 0); - if (type == geojsonvt::ProjectedFeatureType::Polygon && points.size() > 0 && points.front() != points.back()) { - converted.push_back(converted.front()); - } - - return geojsonvt::Convert::projectRing(converted, tolerance); - } - - geojsonvt::ProjectedFeature convertFeature(geojsonvt::ProjectedFeatureType type, const geojsonvt::ProjectedRings& rings) const { - return geojsonvt::Convert::create(geojsonvt::Tags(), type, rings); - } -}; - void ShapeAnnotationImpl::updateTileData(const CanonicalTileID& tileID, AnnotationTileData& data) { static const double baseTolerance = 4; if (!shapeTiler) { - const uint64_t maxAmountOfTileFeatures = (1ull << maxZoom) * util::EXTENT; - const double tolerance = baseTolerance / maxAmountOfTileFeatures; - - std::vector<geojsonvt::ProjectedFeature> features = { - ShapeAnnotationGeometry::visit(geometry(), ToGeoJSONVT(tolerance)) - }; - + mapbox::geometry::feature_collection<double> features; + features.emplace_back(ShapeAnnotationGeometry::visit(geometry(), [] (auto&& geom) { + return Feature(std::move(geom)); + })); mapbox::geojsonvt::Options options; - options.maxZoom = maxZoom; + options.maxZoom = util::clamp<uint8_t>(maxZoom, 0, 18); options.buffer = 255u; options.extent = util::EXTENT; options.tolerance = baseTolerance; @@ -106,34 +35,20 @@ void ShapeAnnotationImpl::updateTileData(const CanonicalTileID& tileID, Annotati } const auto& shapeTile = shapeTiler->getTile(tileID.z, tileID.x, tileID.y); - if (!shapeTile) + if (shapeTile.features.empty()) return; AnnotationTileLayer& layer = *data.layers.emplace(layerID, std::make_unique<AnnotationTileLayer>(layerID)).first->second; - for (auto& shapeFeature : shapeTile.features) { - FeatureType featureType = FeatureType::Unknown; - - if (shapeFeature.type == geojsonvt::TileFeatureType::LineString) { - featureType = FeatureType::LineString; - } else if (shapeFeature.type == geojsonvt::TileFeatureType::Polygon) { - featureType = FeatureType::Polygon; - } + ToGeometryCollection toGeometryCollection; + ToFeatureType toFeatureType; + for (const auto& shapeFeature : shapeTile.features) { + FeatureType featureType = apply_visitor(toFeatureType, shapeFeature.geometry); + GeometryCollection renderGeometry = apply_visitor(toGeometryCollection, shapeFeature.geometry); assert(featureType != FeatureType::Unknown); - GeometryCollection renderGeometry; - for (auto& shapeRing : shapeFeature.tileGeometry.get<geojsonvt::TileRings>()) { - GeometryCoordinates renderLine; - - for (auto& shapePoint : shapeRing) { - renderLine.emplace_back(shapePoint.x, shapePoint.y); - } - - renderGeometry.push_back(renderLine); - } - // https://github.com/mapbox/geojson-vt-cpp/issues/44 if (featureType == FeatureType::Polygon) { renderGeometry = fixupPolygons(renderGeometry); |