summaryrefslogtreecommitdiff
path: root/platform/darwin/src/MGLGeoJSONSource.mm
blob: 46bea779a33ca2875f3ef03e9a41ad509224f094 (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
#import "MGLGeoJSONSource.h"

#import "MGLSource_Private.h"
#import "MGLFeature_Private.h"

#import "NSURL+MGLAdditions.h"

#include <mbgl/style/sources/geojson_source.hpp>

@implementation MGLGeoJSONSource

- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier geoJSONData:(NSData *)data {
    if (self = [super initWithSourceIdentifier:sourceIdentifier]) {
        _geoJSONData = data;
    }
    return self;
}

- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier geoJSONData:(NSData *)data options:(MGLGeoJSONOptions *)options
{
    if (self = [super initWithSourceIdentifier:sourceIdentifier]) {
        _geoJSONData = data;
        _geoJSONOptions = options;
    }
    return self;
}

- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier URL:(NSURL *)url {
    if (self = [super initWithSourceIdentifier:sourceIdentifier]) {
        _URL = url;
    }
    return self;
}

- (mbgl::style::GeoJSONOptions)mbgl_geoJSONOptions
{
    auto options = mbgl::style::GeoJSONOptions();
    options.maxzoom = self.geoJSONOptions.maximumZoom;
    options.buffer = self.geoJSONOptions.buffer;
    options.tolerance = self.geoJSONOptions.tolerance;
    options.cluster = self.geoJSONOptions.cluster;
    options.clusterRadius = self.geoJSONOptions.clusterRadius;
    options.clusterMaxZoom = self.geoJSONOptions.clusterMaximumZoom;
    return options;
}

- (std::unique_ptr<mbgl::style::Source>)mbgl_source
{
    auto source = self.geoJSONOptions
    ? std::make_unique<mbgl::style::GeoJSONSource>(self.sourceIdentifier.UTF8String, [self mbgl_geoJSONOptions])
    : std::make_unique<mbgl::style::GeoJSONSource>(self.sourceIdentifier.UTF8String);
    
    if (self.URL) {
        NSURL *url = self.URL.mgl_URLByStandardizingScheme;
        source->setURL(url.absoluteString.UTF8String);
    } else {
        NSString *string = [[NSString alloc] initWithData:self.geoJSONData encoding:NSUTF8StringEncoding];
        const auto geojson = mapbox::geojson::parse(string.UTF8String).get<mapbox::geojson::feature_collection>();
        source->setGeoJSON(geojson);
        _features = MGLFeaturesFromMBGLFeatures(geojson);
    }
    
    return std::move(source);
}

@end