[core] Clean up and simplify geojson_tile.cpp
This commit is contained in:
@@ -12,11 +12,29 @@ namespace mbgl {
|
||||
|
||||
class GeoJSONTileFeature : public GeometryTileFeature {
|
||||
public:
|
||||
GeoJSONTileFeature(FeatureType, GeometryCollection&&, PropertyMap&&);
|
||||
FeatureType getType() const override;
|
||||
optional<Value> getValue(const std::string&) const override;
|
||||
PropertyMap getProperties() const override { return properties; }
|
||||
GeometryCollection getGeometries() const override;
|
||||
GeoJSONTileFeature(FeatureType type_, GeometryCollection&& geometries_, PropertyMap&& properties_)
|
||||
: type(type_), geometries(std::move(geometries_)), properties(std::move(properties_)) {
|
||||
}
|
||||
|
||||
FeatureType getType() const override {
|
||||
return type;
|
||||
}
|
||||
|
||||
PropertyMap getProperties() const override {
|
||||
return properties;
|
||||
}
|
||||
|
||||
GeometryCollection getGeometries() const override {
|
||||
return geometries;
|
||||
}
|
||||
|
||||
optional<Value> getValue(const std::string& key) const override {
|
||||
auto it = properties.find(key);
|
||||
if (it != properties.end()) {
|
||||
return optional<Value>(it->second);
|
||||
}
|
||||
return optional<Value>();
|
||||
}
|
||||
|
||||
private:
|
||||
const FeatureType type;
|
||||
@@ -26,60 +44,54 @@ private:
|
||||
|
||||
class GeoJSONTileLayer : public GeometryTileLayer {
|
||||
public:
|
||||
using Features = std::vector<std::shared_ptr<const GeoJSONTileFeature>>;
|
||||
std::vector<std::shared_ptr<const GeoJSONTileFeature>> features;
|
||||
|
||||
GeoJSONTileLayer(Features&&);
|
||||
std::size_t featureCount() const override;
|
||||
util::ptr<const GeometryTileFeature> getFeature(std::size_t) const override;
|
||||
std::string getName() const override { return ""; };
|
||||
std::string getName() const override {
|
||||
return "";
|
||||
}
|
||||
|
||||
private:
|
||||
const Features features;
|
||||
std::size_t featureCount() const override {
|
||||
return features.size();
|
||||
}
|
||||
|
||||
util::ptr<const GeometryTileFeature> getFeature(std::size_t i) const override {
|
||||
return features[i];
|
||||
}
|
||||
};
|
||||
|
||||
class GeoJSONTileData : public GeometryTileData {
|
||||
public:
|
||||
GeoJSONTileData(std::shared_ptr<GeoJSONTileLayer>);
|
||||
util::ptr<const GeometryTileLayer> getLayer(const std::string&) const override;
|
||||
std::shared_ptr<GeoJSONTileLayer> layer = std::make_shared<GeoJSONTileLayer>();
|
||||
|
||||
private:
|
||||
const std::shared_ptr<GeoJSONTileLayer> layer;
|
||||
util::ptr<const GeometryTileLayer> getLayer(const std::string&) const override {
|
||||
// We're ignoring the layer name because GeoJSON tiles only have one layer.
|
||||
return layer;
|
||||
}
|
||||
};
|
||||
|
||||
// Converts the geojsonvt::Tile to a a GeoJSONTile. They have a differing internal structure.
|
||||
std::unique_ptr<GeoJSONTileData> convertTile(const mapbox::geometry::feature_collection<int16_t>& features) {
|
||||
std::shared_ptr<GeoJSONTileLayer> layer;
|
||||
auto result = std::make_unique<GeoJSONTileData>();
|
||||
|
||||
if (!features.empty()) {
|
||||
std::vector<std::shared_ptr<const GeoJSONTileFeature>> convertedFeatures;
|
||||
for (auto& feature : features) {
|
||||
const FeatureType featureType = apply_visitor(ToFeatureType(), feature.geometry);
|
||||
|
||||
ToFeatureType toFeatureType;
|
||||
ToGeometryCollection toGeometryCollection;
|
||||
|
||||
for (auto& feature : features) {
|
||||
const FeatureType featureType = apply_visitor(toFeatureType, feature.geometry);
|
||||
|
||||
if (featureType == FeatureType::Unknown) {
|
||||
continue;
|
||||
}
|
||||
|
||||
GeometryCollection geometry = apply_visitor(toGeometryCollection, feature.geometry);
|
||||
|
||||
// https://github.com/mapbox/geojson-vt-cpp/issues/44
|
||||
if (featureType == FeatureType::Polygon) {
|
||||
geometry = fixupPolygons(geometry);
|
||||
}
|
||||
|
||||
PropertyMap properties = feature.properties;
|
||||
|
||||
convertedFeatures.emplace_back(std::make_shared<GeoJSONTileFeature>(
|
||||
featureType, std::move(geometry), std::move(properties)));
|
||||
if (featureType == FeatureType::Unknown) {
|
||||
continue;
|
||||
}
|
||||
|
||||
layer = std::make_unique<GeoJSONTileLayer>(std::move(convertedFeatures));
|
||||
GeometryCollection geometry = apply_visitor(ToGeometryCollection(), feature.geometry);
|
||||
|
||||
// https://github.com/mapbox/geojson-vt-cpp/issues/44
|
||||
if (featureType == FeatureType::Polygon) {
|
||||
geometry = fixupPolygons(geometry);
|
||||
}
|
||||
|
||||
result->layer->features.emplace_back(std::make_shared<GeoJSONTileFeature>(
|
||||
featureType, std::move(geometry), PropertyMap(feature.properties)));
|
||||
}
|
||||
|
||||
return std::make_unique<GeoJSONTileData>(layer);
|
||||
return result;
|
||||
}
|
||||
|
||||
GeoJSONTile::GeoJSONTile(const OverscaledTileID& overscaledTileID,
|
||||
@@ -100,45 +112,4 @@ GeoJSONTile::GeoJSONTile(const OverscaledTileID& overscaledTileID,
|
||||
|
||||
void GeoJSONTile::setNecessity(Necessity) {}
|
||||
|
||||
GeoJSONTileFeature::GeoJSONTileFeature(FeatureType type_,
|
||||
GeometryCollection&& geometries_,
|
||||
PropertyMap&& properties_)
|
||||
: type(type_), geometries(std::move(geometries_)), properties(std::move(properties_)) {
|
||||
}
|
||||
|
||||
FeatureType GeoJSONTileFeature::getType() const {
|
||||
return type;
|
||||
}
|
||||
|
||||
optional<Value> GeoJSONTileFeature::getValue(const std::string& key) const {
|
||||
auto it = properties.find(key);
|
||||
if (it != properties.end()) {
|
||||
return optional<Value>(it->second);
|
||||
}
|
||||
return optional<Value>();
|
||||
}
|
||||
|
||||
GeometryCollection GeoJSONTileFeature::getGeometries() const {
|
||||
return geometries;
|
||||
}
|
||||
|
||||
GeoJSONTileLayer::GeoJSONTileLayer(Features&& features_) : features(std::move(features_)) {
|
||||
}
|
||||
|
||||
std::size_t GeoJSONTileLayer::featureCount() const {
|
||||
return features.size();
|
||||
}
|
||||
|
||||
util::ptr<const GeometryTileFeature> GeoJSONTileLayer::getFeature(std::size_t i) const {
|
||||
return features[i];
|
||||
}
|
||||
|
||||
GeoJSONTileData::GeoJSONTileData(std::shared_ptr<GeoJSONTileLayer> layer_) : layer(std::move(layer_)) {
|
||||
}
|
||||
|
||||
util::ptr<const GeometryTileLayer> GeoJSONTileData::getLayer(const std::string&) const {
|
||||
// We're ignoring the layer name because GeoJSON tiles only have one layer.
|
||||
return layer;
|
||||
}
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
Reference in New Issue
Block a user