summaryrefslogtreecommitdiff
path: root/src/mbgl/sourcemanager/geojson_source_factory.cpp
blob: 3152fe04aee9502463478b319ce21fb62dbdfaa3 (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
#include <mbgl/renderer/sources/render_geojson_source.hpp>
#include <mbgl/sourcemanager/geojson_source_factory.hpp>
#include <mbgl/style/conversion/geojson.hpp>
#include <mbgl/style/conversion/geojson_options.hpp>
#include <mbgl/style/conversion_impl.hpp>
#include <mbgl/style/sources/geojson_source.hpp>
#include <mbgl/style/sources/geojson_source_impl.hpp>

namespace mbgl {

const style::SourceTypeInfo* GeoJSONSourceFactory::getTypeInfo() const noexcept {
    return style::GeoJSONSource::Impl::staticTypeInfo();
}

std::unique_ptr<style::Source> GeoJSONSourceFactory::createSource(const std::string& id,
                                                                  const style::conversion::Convertible& value,
                                                                  style::conversion::Error& error) noexcept {
    auto dataValue = objectMember(value, "data");
    if (!dataValue) {
        error.message = "GeoJSON source must have a data value";
        return nullptr;
    }

    Immutable<style::GeoJSONOptions> options = style::GeoJSONOptions::defaultOptions();
    if (optional<style::GeoJSONOptions> converted = style::conversion::convert<style::GeoJSONOptions>(value, error)) {
        options = makeMutable<style::GeoJSONOptions>(std::move(*converted));
    }

    auto result = std::make_unique<style::GeoJSONSource>(id, std::move(options));

    if (isObject(*dataValue)) {
        optional<GeoJSON> geoJSON = style::conversion::convert<GeoJSON>(*dataValue, error);
        if (!geoJSON) {
            return nullptr;
        }
        result->setGeoJSON(*geoJSON);
    } else if (toString(*dataValue)) {
        result->setURL(*toString(*dataValue));
    } else {
        error.message = "GeoJSON data must be a URL or an object";
        return nullptr;
    }

    return {std::move(result)};
}

std::unique_ptr<RenderSource> GeoJSONSourceFactory::createRenderSource(Immutable<style::Source::Impl> impl) noexcept {
    assert(impl->getTypeInfo() == getTypeInfo());
    return std::make_unique<RenderGeoJSONSource>(staticImmutableCast<style::GeoJSONSource::Impl>(impl));
}

} // namespace mbgl