summaryrefslogtreecommitdiff
path: root/src/mbgl/style/sources/geojson_source.cpp
blob: b626855d57927ede8515b9ebb9cb1c2f0527ac4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <mbgl/style/sources/geojson_source.hpp>
#include <mbgl/style/sources/geojson_source_impl.hpp>
#include <mbgl/style/source_observer.hpp>
#include <mbgl/style/conversion/json.hpp>
#include <mbgl/style/conversion/geojson.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/util/logging.hpp>

namespace mbgl {
namespace style {

GeoJSONSource::GeoJSONSource(const std::string& id, const GeoJSONOptions& options)
    : Source(makeMutable<Impl>(std::move(id), options)),
     stateChanges(makeMutable<std::vector<FeatureStateChange>>()) {
}

GeoJSONSource::~GeoJSONSource() = default;

const GeoJSONSource::Impl& GeoJSONSource::impl() const {
    return static_cast<const Impl&>(*baseImpl);
}

void GeoJSONSource::setURL(const std::string& url_) {
    url = std::move(url_);

    // Signal that the source description needs a reload
    if (loaded || req) {
        loaded = false;
        req.reset();
        observer->onSourceDescriptionChanged(*this);
    }
}

void GeoJSONSource::setGeoJSON(const mapbox::geojson::geojson& geoJSON) {
    req.reset();
    baseImpl = makeMutable<Impl>(impl(), geoJSON);
    observer->onSourceChanged(*this);
}

void GeoJSONSource::setFeatureState(const FeatureIdentifier& featureId, const std::string& key, const mbgl::Value& value) {
    if ( featureId.valid() && !key.empty()) {
        stateChanges->emplace_back(FeatureStateChange::ChangeType::Insert, featureId, key, value);
//        observer->onSourceChanged(*this);
    }
}

Immutable<std::vector<FeatureStateChange>> GeoJSONSource::collectFeatureStates() {
    Immutable<std::vector<FeatureStateChange>> immutable(std::move(stateChanges));
    stateChanges = makeMutable<std::vector<FeatureStateChange>>();
    printf("!)!)! Collecting feature state!@$!@$\n");
    return immutable;
}

optional<std::string> GeoJSONSource::getURL() const {
    return url;
}

void GeoJSONSource::loadDescription(FileSource& fileSource) {
    if (!url) {
        loaded = true;
        return;
    }

    if (req) {
        return;
    }

    req = fileSource.request(Resource::source(*url), [this](Response res) {
        if (res.error) {
            observer->onSourceError(
                *this, std::make_exception_ptr(std::runtime_error(res.error->message)));
        } else if (res.notModified) {
            return;
        } else if (res.noContent) {
            observer->onSourceError(
                *this, std::make_exception_ptr(std::runtime_error("unexpectedly empty GeoJSON")));
        } else {
            conversion::Error error;
            optional<GeoJSON> geoJSON = conversion::convertJSON<GeoJSON>(*res.data, error);
            if (!geoJSON) {
                Log::Error(Event::ParseStyle, "Failed to parse GeoJSON data: %s",
                           error.message.c_str());
                // Create an empty GeoJSON VT object to make sure we're not infinitely waiting for
                // tiles to load.
                baseImpl = makeMutable<Impl>(impl(), GeoJSON{ FeatureCollection{} });
            } else {
                baseImpl = makeMutable<Impl>(impl(), *geoJSON);
            }

            loaded = true;
            observer->onSourceLoaded(*this);
        }
    });
}

} // namespace style
} // namespace mbgl