[core] Merge GeoJSONTileSource into GeoJSONTileData

This commit is contained in:
John Firebaugh
2016-06-06 17:34:52 -07:00
committed by Konstantin Käfer
parent d62baffb1b
commit 91ddcbce20
4 changed files with 70 additions and 143 deletions

View File

@@ -1,21 +1,86 @@
#include <mbgl/tile/geojson_tile_data.hpp>
#include <mbgl/tile/geojson_tile_source.hpp>
#include <mbgl/tile/geojson_tile.hpp>
#include <mbgl/style/update_parameters.hpp>
#include <mapbox/geojsonvt.hpp>
namespace mbgl {
// Converts the geojsonvt::Tile to a a GeoJSONTile. They have a differing internal structure.
std::unique_ptr<GeoJSONTile> convertTile(const mapbox::geojsonvt::Tile& tile) {
std::shared_ptr<GeoJSONTileLayer> layer;
if (tile) {
std::vector<std::shared_ptr<const GeoJSONTileFeature>> 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<mapbox::geojsonvt::TilePoints>()) {
line.clear();
for (auto& point : feature.tileGeometry.get<mapbox::geojsonvt::TilePoints>()) {
line.emplace_back(point.x, point.y);
}
geometry.emplace_back(std::move(line));
} else if (feature.tileGeometry.is<mapbox::geojsonvt::TileRings>()) {
for (auto& ring : feature.tileGeometry.get<mapbox::geojsonvt::TileRings>()) {
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<GeoJSONTileFeature>(
featureType, std::move(geometry), std::move(properties)));
}
layer = std::make_unique<GeoJSONTileLayer>(std::move(features));
}
return std::make_unique<GeoJSONTile>(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<GeoJSONTileSource>(*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<TileSource::Necessity>(necessity));
}
void GeoJSONTileData::setNecessity(Necessity) {}
} // namespace mbgl

View File

@@ -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> tileSource;
};
} // namespace mbgl

View File

@@ -1,99 +0,0 @@
#include <mbgl/tile/geojson_tile_source.hpp>
#include <mbgl/tile/geojson_tile.hpp>
#include <mbgl/tile/geometry_tile_data.hpp>
#include <mbgl/util/async_request.hpp>
#include <mapbox/geojsonvt.hpp>
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<GeoJSONTile> convertTile(const mapbox::geojsonvt::Tile& tile) {
std::shared_ptr<GeoJSONTileLayer> layer;
if (tile) {
std::vector<std::shared_ptr<const GeoJSONTileFeature>> 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<mapbox::geojsonvt::TilePoints>()) {
line.clear();
for (auto& point : feature.tileGeometry.get<mapbox::geojsonvt::TilePoints>()) {
line.emplace_back(point.x, point.y);
}
geometry.emplace_back(std::move(line));
} else if (feature.tileGeometry.is<mapbox::geojsonvt::TileRings>()) {
for (auto& ring : feature.tileGeometry.get<mapbox::geojsonvt::TileRings>()) {
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<GeoJSONTileFeature>(
featureType, std::move(geometry), std::move(properties)));
}
layer = std::make_unique<GeoJSONTileLayer>(std::move(features));
}
return std::make_unique<GeoJSONTile>(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

View File

@@ -1,34 +0,0 @@
#pragma once
#include <mbgl/tile/tile_source.hpp>
#include <mbgl/tile/geometry_tile.hpp>
#include <mbgl/tile/tile_id.hpp>
#include <unordered_map>
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