summaryrefslogtreecommitdiff
path: root/platform/darwin/src/MGLGeoJSONSource.mm
blob: 652644be47293fa2b227929485e2c766c10104c6 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#import "MGLGeoJSONSource.h"

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

#import "NSURL+MGLAdditions.h"

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

NSString * const MGLGeoJSONClusterOption = @"MGLGeoJSONCluster";
NSString * const MGLGeoJSONClusterRadiusOption = @"MGLGeoJSONClusterRadius";
NSString * const MGLGeoJSONClusterMaximumZoomLevelOption = @"MGLGeoJSONClusterMaximumZoomLevel";
NSString * const MGLGeoJSONMaximumZoomLevelOption = @"MGLGeoJSONMaximumZoomLevel";
NSString * const MGLGeoJSONBufferOption = @"MGLGeoJSONBuffer";
NSString * const MGLGeoJSONToleranceOption = @"MGLGeoJSONOptionsClusterTolerance";

@interface MGLGeoJSONSource ()

@property (nonatomic, readwrite) NSDictionary *options;

@end

@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:(NS_DICTIONARY_OF(NSString *, id) *)options
{
    if (self = [super initWithSourceIdentifier:sourceIdentifier])
    {
        _geoJSONData = data;
        _options = options;
    }
    return self;
}

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

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

- (mbgl::style::GeoJSONOptions)geoJSONOptions
{
    auto mbglOptions = mbgl::style::GeoJSONOptions();
    
    if (self.options[MGLGeoJSONMaximumZoomLevelOption]) {
        id value = self.options[MGLGeoJSONMaximumZoomLevelOption];
        [self validateValue:value];
        mbglOptions.maxzoom = [value integerValue];
    }
    
    if (self.options[MGLGeoJSONBufferOption]) {
        id value = self.options[MGLGeoJSONBufferOption];
        [self validateValue:value];
        mbglOptions.buffer = [value integerValue];
    }
    
    if (self.options[MGLGeoJSONToleranceOption]) {
        id value = self.options[MGLGeoJSONToleranceOption];
        [self validateValue:value];
        mbglOptions.tolerance = [value doubleValue];
    }
    
    if (self.options[MGLGeoJSONClusterRadiusOption]) {
        id value = self.options[MGLGeoJSONClusterRadiusOption];
        [self validateValue:value];
        mbglOptions.clusterRadius = [value integerValue];
    }
    
    if (self.options[MGLGeoJSONClusterMaximumZoomLevelOption]) {
        id value = self.options[MGLGeoJSONClusterMaximumZoomLevelOption];
        [self validateValue:value];
        mbglOptions.clusterMaxZoom = [value integerValue];
    }
    
    if (self.options[MGLGeoJSONClusterOption]) {
        id value = self.options[MGLGeoJSONClusterOption];
        [self validateValue:value];
        mbglOptions.cluster = [value boolValue];
    }
    
    return mbglOptions;
}

- (void)validateValue:(id)value
{
    if (! [value isKindOfClass:[NSNumber class]])
    {
        [NSException raise:@"Value not handled" format:@"%@ is not an NSNumber", value];
    }
}

- (std::unique_ptr<mbgl::style::Source>)mbglSource
{
    auto source = std::make_unique<mbgl::style::GeoJSONSource>(self.sourceIdentifier.UTF8String, [self geoJSONOptions]);
    
    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