1 #include <mbgl/tile/geometry_tile_data.hpp>
2 
3 namespace mbgl {
4 
5 // Implements a simple in-memory Tile type that holds GeoJSON values. A GeoJSON tile can only have
6 // one layer, and it is always returned regardless of which layer is requested.
7 
8 class GeoJSONTileFeature : public GeometryTileFeature {
9 public:
10     const mapbox::geometry::feature<int16_t>& feature;
11 
GeoJSONTileFeature(const mapbox::geometry::feature<int16_t> & feature_)12     GeoJSONTileFeature(const mapbox::geometry::feature<int16_t>& feature_)
13         : feature(feature_) {
14     }
15 
getType() const16     FeatureType getType() const override  {
17         return apply_visitor(ToFeatureType(), feature.geometry);
18     }
19 
getProperties() const20     PropertyMap getProperties() const override {
21         return feature.properties;
22     }
23 
getID() const24     optional<FeatureIdentifier> getID() const override {
25         return feature.id;
26     }
27 
getGeometries() const28     GeometryCollection getGeometries() const override {
29         GeometryCollection geometry = apply_visitor(ToGeometryCollection(), feature.geometry);
30 
31         // https://github.com/mapbox/geojson-vt-cpp/issues/44
32         if (getType() == FeatureType::Polygon) {
33             geometry = fixupPolygons(geometry);
34         }
35 
36         return geometry;
37     }
38 
getValue(const std::string & key) const39     optional<Value> getValue(const std::string& key) const override {
40         auto it = feature.properties.find(key);
41         if (it != feature.properties.end()) {
42             return optional<Value>(it->second);
43         }
44         return optional<Value>();
45     }
46 };
47 
48 class GeoJSONTileLayer : public GeometryTileLayer {
49 public:
GeoJSONTileLayer(std::shared_ptr<const mapbox::geometry::feature_collection<int16_t>> features_)50     GeoJSONTileLayer(std::shared_ptr<const mapbox::geometry::feature_collection<int16_t>> features_)
51         : features(std::move(features_)) {
52     }
53 
featureCount() const54     std::size_t featureCount() const override {
55         return features->size();
56     }
57 
getFeature(std::size_t i) const58     std::unique_ptr<GeometryTileFeature> getFeature(std::size_t i) const override {
59         return std::make_unique<GeoJSONTileFeature>((*features)[i]);
60     }
61 
getName() const62     std::string getName() const override {
63         return "";
64     }
65 
66 private:
67     std::shared_ptr<const mapbox::geometry::feature_collection<int16_t>> features;
68 };
69 
70 class GeoJSONTileData : public GeometryTileData {
71 public:
GeoJSONTileData(mapbox::geometry::feature_collection<int16_t> features_)72     GeoJSONTileData(mapbox::geometry::feature_collection<int16_t> features_)
73         : features(std::make_shared<mapbox::geometry::feature_collection<int16_t>>(
74               std::move(features_))) {
75     }
76 
GeoJSONTileData(std::shared_ptr<const mapbox::geometry::feature_collection<int16_t>> features_)77     GeoJSONTileData(std::shared_ptr<const mapbox::geometry::feature_collection<int16_t>> features_)
78         : features(std::move(features_)) {
79     }
80 
clone() const81     std::unique_ptr<GeometryTileData> clone() const override {
82         return std::make_unique<GeoJSONTileData>(features);
83     }
84 
getLayer(const std::string &) const85     std::unique_ptr<GeometryTileLayer> getLayer(const std::string&) const override {
86         return std::make_unique<GeoJSONTileLayer>(features);
87     }
88 
89 
90 private:
91     std::shared_ptr<const mapbox::geometry::feature_collection<int16_t>> features;
92 };
93 
94 } // namespace mbgl
95