From 50d2c01d7d0bad1aa1dee1a028e06f49f33c9b4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguye=CC=82=CC=83n?= Date: Wed, 5 Oct 2016 00:34:01 -0700 Subject: [PATCH] [ios, macos] Revamped MGLStyleLayer, MGLSource inheritance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removed the MGLStyleLayer protocol, because almost none of its members was actually implemented in every class that adopted the protocol. Removed the unused mapView backpointer property with no replacement. Renamed MGLBaseStyleLayer to MGLStyleLayer. Created the intermediate abstract classes MGLForegroundStyleLayer and MGLVectorStyleLayer to cover subsets of style layer classes with like functionality. Moved each MGLBaseStyleLayer initializer and the corresponding properties down to an abstract subclass such that the initializer makes sense for all concrete subclasses. Moved more initializers and the predicate property up to MGLVectorStyleLayer to eliminate duplication among the concrete subclasses. Marked these initializers as designated initializers. Removed “source” or “layer” before identifier wherever the type of identifier is apparent. Removed extra MGLGeoJSONSource initializer variants in favor of nullable parameters. Added copious documentation comments for source and style layer classes, including several previously undocumented methods and properties. In particular, some preconditions and postconditions have been documented. Added pragma marks to break up the jazzy documentation pages into sections. Reformatted exceptions for consistency. --- .../darwin/scripts/generate-style-code.js | 12 +- platform/darwin/src/MGLBackgroundStyleLayer.h | 7 +- .../darwin/src/MGLBackgroundStyleLayer.mm | 23 +-- platform/darwin/src/MGLBaseStyleLayer.h | 44 +++++- platform/darwin/src/MGLBaseStyleLayer.mm | 37 +---- platform/darwin/src/MGLCircleStyleLayer.h | 14 +- platform/darwin/src/MGLCircleStyleLayer.mm | 30 ++-- platform/darwin/src/MGLFillStyleLayer.h | 14 +- platform/darwin/src/MGLFillStyleLayer.mm | 30 ++-- platform/darwin/src/MGLGeoJSONSource.h | 90 +++++------- platform/darwin/src/MGLGeoJSONSource.mm | 28 +--- platform/darwin/src/MGLLineStyleLayer.h | 14 +- platform/darwin/src/MGLLineStyleLayer.mm | 30 ++-- platform/darwin/src/MGLRasterSource.h | 72 ++++++---- platform/darwin/src/MGLRasterSource.mm | 12 +- platform/darwin/src/MGLRasterStyleLayer.h | 5 +- platform/darwin/src/MGLRasterStyleLayer.mm | 15 +- .../darwin/src/MGLRuntimeStylingTests.m.ejs | 11 +- platform/darwin/src/MGLSource.h | 25 ++-- platform/darwin/src/MGLSource.mm | 17 ++- platform/darwin/src/MGLStyle.h | 23 +-- platform/darwin/src/MGLStyle.mm | 136 ++++++++++-------- platform/darwin/src/MGLStyleLayer.h | 74 +++++++++- platform/darwin/src/MGLStyleLayer.h.ejs | 26 +--- platform/darwin/src/MGLStyleLayer.mm | 30 +++- platform/darwin/src/MGLStyleLayer.mm.ejs | 46 +++--- platform/darwin/src/MGLStyleLayer_Private.h | 7 +- platform/darwin/src/MGLSymbolStyleLayer.h | 14 +- platform/darwin/src/MGLSymbolStyleLayer.mm | 30 ++-- platform/darwin/src/MGLVectorSource.h | 67 ++++++--- platform/darwin/src/MGLVectorSource.mm | 12 +- .../test/MGLBackgroundStyleLayerTests.m | 9 +- .../darwin/test/MGLCircleStyleLayerTests.m | 7 +- platform/darwin/test/MGLFillStyleLayerTests.m | 7 +- platform/darwin/test/MGLFilterTests.mm | 4 +- platform/darwin/test/MGLLineStyleLayerTests.m | 7 +- .../darwin/test/MGLRasterStyleLayerTests.m | 7 +- .../darwin/test/MGLSymbolStyleLayerTests.m | 7 +- platform/ios/app/MBXViewController.m | 8 +- platform/ios/jazzy.yml | 5 +- platform/ios/test/MGLGeoJSONSourceTests.mm | 4 +- platform/macos/app/MapDocument.m | 4 +- platform/macos/jazzy.yml | 5 +- 43 files changed, 542 insertions(+), 527 deletions(-) diff --git a/platform/darwin/scripts/generate-style-code.js b/platform/darwin/scripts/generate-style-code.js index a801145de..728a2902e 100644 --- a/platform/darwin/scripts/generate-style-code.js +++ b/platform/darwin/scripts/generate-style-code.js @@ -211,22 +211,14 @@ global.propertyType = function (property, _private) { return _private ? `id ` : `id `; }; -global.initLayerIdentifierOnly = function (layerType) { - return `_layer = new mbgl::style::${camelize(layerType)}Layer(layerIdentifier.UTF8String);` -} - global.initLayer = function (layerType) { if (layerType == "background") { - return `_layer = new mbgl::style::${camelize(layerType)}Layer(layerIdentifier.UTF8String);` + return `_layer = new mbgl::style::${camelize(layerType)}Layer(identifier.UTF8String);` } else { - return `_layer = new mbgl::style::${camelize(layerType)}Layer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String);` + return `_layer = new mbgl::style::${camelize(layerType)}Layer(identifier.UTF8String, source.identifier.UTF8String);` } } -global.initLayerWithSourceLayer = function(layerType) { - return `_layer = new mbgl::style::${camelize(layerType)}Layer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String);` -} - global.setSourceLayer = function() { return `_layer->setSourceLayer(sourceLayer.UTF8String);` } diff --git a/platform/darwin/src/MGLBackgroundStyleLayer.h b/platform/darwin/src/MGLBackgroundStyleLayer.h index 386b68fed..d464eb49f 100644 --- a/platform/darwin/src/MGLBackgroundStyleLayer.h +++ b/platform/darwin/src/MGLBackgroundStyleLayer.h @@ -12,12 +12,9 @@ NS_ASSUME_NONNULL_BEGIN `style` and obtain the background layer using the `-[MGLStyle layerWithIdentifier:]` method and passing `background` for the identifier. */ -@interface MGLBackgroundStyleLayer : MGLBaseStyleLayer - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier; - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source; +@interface MGLBackgroundStyleLayer : MGLStyleLayer +- (instancetype)initWithIdentifier:(NSString *)identifier NS_DESIGNATED_INITIALIZER; #pragma mark - Accessing the Paint Attributes diff --git a/platform/darwin/src/MGLBackgroundStyleLayer.mm b/platform/darwin/src/MGLBackgroundStyleLayer.mm index f15bc4cee..032aa196e 100644 --- a/platform/darwin/src/MGLBackgroundStyleLayer.mm +++ b/platform/darwin/src/MGLBackgroundStyleLayer.mm @@ -12,36 +12,19 @@ @interface MGLBackgroundStyleLayer () @property (nonatomic) mbgl::style::BackgroundLayer *layer; -@property (nonatomic, readwrite) NSString *layerIdentifier; -@property (nonatomic, readwrite) NSString *sourceIdentifier; -@property (nonatomic, readwrite) NSString *sourceLayerIdentifier; @end @implementation MGLBackgroundStyleLayer -@synthesize mapView; - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier +- (instancetype)initWithIdentifier:(NSString *)identifier { - if (self = [super init]) { - _layerIdentifier = layerIdentifier; - _layer = new mbgl::style::BackgroundLayer(layerIdentifier.UTF8String); + if (self = [super initWithIdentifier:identifier]) { + _layer = new mbgl::style::BackgroundLayer(identifier.UTF8String); } return self; } -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source -{ - if (self = [super init]) { - _layerIdentifier = layerIdentifier; - _sourceIdentifier = source.sourceIdentifier; - _layer = new mbgl::style::BackgroundLayer(layerIdentifier.UTF8String); - } - return self; -} - - #pragma mark - Accessing the Paint Attributes - (void)setBackgroundColor:(id )backgroundColor { diff --git a/platform/darwin/src/MGLBaseStyleLayer.h b/platform/darwin/src/MGLBaseStyleLayer.h index fee7707c6..cfdc03962 100644 --- a/platform/darwin/src/MGLBaseStyleLayer.h +++ b/platform/darwin/src/MGLBaseStyleLayer.h @@ -5,15 +5,45 @@ NS_ASSUME_NONNULL_BEGIN /** - The base style layer class from which all other style layer classes - inherit. Style layers allow runtime customization of all map styling - properties. + `MGLStyleLayer` is an abstract base class for style layers. A style layer + manages the layout and appearance of content at a specific z-index in a style. + An `MGLStyle` object consists of one or more `MGLStyleLayer` objects. - You should use the concrete subclasses of `MGLBaseStyleLayer` (which - conform to `MGLStyleLayer`) to style fill, line, symbol, and other layer - types. + Each style layer defined by the style JSON file is represented at runtime by an + `MGLStyleLayer` object, which you can use to refine the map’s appearance. You + can also add and remove style layers dynamically. + + Do not create instances of this class directly, and do not create your own + subclasses of this class. Instead, create instances of + `MGLBackgroundStyleLayer` and the concrete subclasses of + `MGLForegroundStyleLayer`. */ -@interface MGLBaseStyleLayer : NSObject +@interface MGLStyleLayer : NSObject + +#pragma mark Initializing a Style Layer + +/** + Returns a style layer object initialized with the given identifier. + + After initializing and configuring the style layer, add it to a map view’s + style using the `-[MGLStyle addLayer:]` or + `-[MGLStyle insertLayer:belowLayer:]` method. + + @param identifier A string that uniquely identifies the layer in the style to + which it is added. + @return An initialized style layer. + */ +- (instancetype)initWithIdentifier:(NSString *)identifier; + +#pragma mark Identifying a Style Layer + +/** + A string that uniquely identifies the style layer in the style to which it is + added. + */ +@property (nonatomic, copy, readonly) NSString *identifier; + +#pragma mark Configuring a Style Layer’s Visibility /** Whether this layer is displayed. A value of `NO` hides the layer. diff --git a/platform/darwin/src/MGLBaseStyleLayer.mm b/platform/darwin/src/MGLBaseStyleLayer.mm index 8aaabcc8a..8a599e67c 100644 --- a/platform/darwin/src/MGLBaseStyleLayer.mm +++ b/platform/darwin/src/MGLBaseStyleLayer.mm @@ -5,39 +5,14 @@ #include -@interface MGLBaseStyleLayer() -@end +@implementation MGLStyleLayer -@implementation MGLBaseStyleLayer - -@synthesize layerIdentifier; -@synthesize mapView; -@synthesize layer; -@synthesize sourceIdentifier; -@synthesize sourceLayerIdentifier; - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier +- (instancetype)initWithIdentifier:(NSString *)identifier { - [[NSException exceptionWithName:@"MGLAbstractClassException" - reason:@"MGLBaseStyleLayer is an abstract class" - userInfo:nil] raise]; - return nil; -} - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier sourceIdentifier:(NSString *)sourceIdentifier -{ - [[NSException exceptionWithName:@"MGLAbstractClassException" - reason:@"MGLBaseStyleLayer is an abstract class" - userInfo:nil] raise]; - return nil; -} - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier sourceIdentifier:(NSString *)sourceIdentifier sourceLayer:(NSString *)sourceLayer -{ - [[NSException exceptionWithName:@"MGLAbstractClassException" - reason:@"MGLBaseStyleLayer is an abstract class" - userInfo:nil] raise]; - return nil; + if (self = [super init]) { + _identifier = identifier; + } + return self; } - (void)setVisible:(BOOL)visible diff --git a/platform/darwin/src/MGLCircleStyleLayer.h b/platform/darwin/src/MGLCircleStyleLayer.h index 9e620eeee..bddc6bf52 100644 --- a/platform/darwin/src/MGLCircleStyleLayer.h +++ b/platform/darwin/src/MGLCircleStyleLayer.h @@ -40,19 +40,7 @@ typedef NS_ENUM(NSUInteger, MGLCircleStyleLayerCirclePitchScale) { `MGLMapView` for its `style` and obtain existing layers using the `-[MGLStyle layerWithIdentifier:]` method. */ -@interface MGLCircleStyleLayer : MGLBaseStyleLayer - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source; - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer; - -/** - A predicate that corresponds to the layer's filter. - - The predicate's left expression must be a string that identifies a feature - property, or one of the special keys. - */ -@property (nonatomic, nullable) NSPredicate *predicate; +@interface MGLCircleStyleLayer : MGLVectorStyleLayer #pragma mark - Accessing the Paint Attributes diff --git a/platform/darwin/src/MGLCircleStyleLayer.mm b/platform/darwin/src/MGLCircleStyleLayer.mm index 1db7ef875..6e805a994 100644 --- a/platform/darwin/src/MGLCircleStyleLayer.mm +++ b/platform/darwin/src/MGLCircleStyleLayer.mm @@ -12,36 +12,28 @@ @interface MGLCircleStyleLayer () @property (nonatomic) mbgl::style::CircleLayer *layer; -@property (nonatomic, readwrite) NSString *layerIdentifier; -@property (nonatomic, readwrite) NSString *sourceIdentifier; -@property (nonatomic, readwrite) NSString *sourceLayerIdentifier; @end @implementation MGLCircleStyleLayer -@synthesize mapView; - - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source +- (instancetype)initWithIdentifier:(NSString *)identifier source:(MGLSource *)source { - if (self = [super init]) { - _layerIdentifier = layerIdentifier; - _sourceIdentifier = source.sourceIdentifier; - _layer = new mbgl::style::CircleLayer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String); + if (self = [super initWithIdentifier:identifier source:source]) { + _layer = new mbgl::style::CircleLayer(identifier.UTF8String, source.identifier.UTF8String); } return self; } -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer +- (NSString *)sourceLayerIdentifier { - if (self = [super init]) { - _layerIdentifier = layerIdentifier; - _sourceIdentifier = source.sourceIdentifier; - _layer = new mbgl::style::CircleLayer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String); - _layer->setSourceLayer(sourceLayer.UTF8String); - } - return self; + auto layerID = self.layer->getSourceLayer(); + return layerID.empty() ? nil : @(layerID.c_str()); +} + +- (void)setSourceLayerIdentifier:(NSString *)sourceLayerIdentifier +{ + self.layer->setSourceLayer(sourceLayerIdentifier.UTF8String ?: ""); } - (void)setPredicate:(NSPredicate *)predicate diff --git a/platform/darwin/src/MGLFillStyleLayer.h b/platform/darwin/src/MGLFillStyleLayer.h index 9adfd7c43..ff1edddd5 100644 --- a/platform/darwin/src/MGLFillStyleLayer.h +++ b/platform/darwin/src/MGLFillStyleLayer.h @@ -26,19 +26,7 @@ typedef NS_ENUM(NSUInteger, MGLFillStyleLayerFillTranslateAnchor) { `MGLMapView` for its `style` and obtain existing layers using the `-[MGLStyle layerWithIdentifier:]` method. */ -@interface MGLFillStyleLayer : MGLBaseStyleLayer - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source; - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer; - -/** - A predicate that corresponds to the layer's filter. - - The predicate's left expression must be a string that identifies a feature - property, or one of the special keys. - */ -@property (nonatomic, nullable) NSPredicate *predicate; +@interface MGLFillStyleLayer : MGLVectorStyleLayer #pragma mark - Accessing the Paint Attributes diff --git a/platform/darwin/src/MGLFillStyleLayer.mm b/platform/darwin/src/MGLFillStyleLayer.mm index d36375b12..4810f4fef 100644 --- a/platform/darwin/src/MGLFillStyleLayer.mm +++ b/platform/darwin/src/MGLFillStyleLayer.mm @@ -12,36 +12,28 @@ @interface MGLFillStyleLayer () @property (nonatomic) mbgl::style::FillLayer *layer; -@property (nonatomic, readwrite) NSString *layerIdentifier; -@property (nonatomic, readwrite) NSString *sourceIdentifier; -@property (nonatomic, readwrite) NSString *sourceLayerIdentifier; @end @implementation MGLFillStyleLayer -@synthesize mapView; - - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source +- (instancetype)initWithIdentifier:(NSString *)identifier source:(MGLSource *)source { - if (self = [super init]) { - _layerIdentifier = layerIdentifier; - _sourceIdentifier = source.sourceIdentifier; - _layer = new mbgl::style::FillLayer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String); + if (self = [super initWithIdentifier:identifier source:source]) { + _layer = new mbgl::style::FillLayer(identifier.UTF8String, source.identifier.UTF8String); } return self; } -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer +- (NSString *)sourceLayerIdentifier { - if (self = [super init]) { - _layerIdentifier = layerIdentifier; - _sourceIdentifier = source.sourceIdentifier; - _layer = new mbgl::style::FillLayer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String); - _layer->setSourceLayer(sourceLayer.UTF8String); - } - return self; + auto layerID = self.layer->getSourceLayer(); + return layerID.empty() ? nil : @(layerID.c_str()); +} + +- (void)setSourceLayerIdentifier:(NSString *)sourceLayerIdentifier +{ + self.layer->setSourceLayer(sourceLayerIdentifier.UTF8String ?: ""); } - (void)setPredicate:(NSPredicate *)predicate diff --git a/platform/darwin/src/MGLGeoJSONSource.h b/platform/darwin/src/MGLGeoJSONSource.h index da772108f..0b25f1a9d 100644 --- a/platform/darwin/src/MGLGeoJSONSource.h +++ b/platform/darwin/src/MGLGeoJSONSource.h @@ -50,7 +50,6 @@ extern NSString * const MGLGeoJSONBufferOption; */ extern NSString * const MGLGeoJSONToleranceOption; - /** A GeoJSON source. @@ -59,12 +58,43 @@ extern NSString * const MGLGeoJSONToleranceOption; */ @interface MGLGeoJSONSource : MGLSource +#pragma mark Initializing a Source + +/** + Returns a GeoJSON source initialized with an identifier, GeoJSON data, and a + dictionary of options for the source according to the + style + specification. + + @param identifier A string that uniquely identifies the source. + @param geoJSONData An `NSData` object representing GeoJSON source code. + @param options An `NSDictionary` of options for this source. + @return An initialized GeoJSON source. + */ +- (instancetype)initWithIdentifier:(NSString *)identifier geoJSONData:(NSData *)data options:(nullable NS_DICTIONARY_OF(NSString *, id) *)options NS_DESIGNATED_INITIALIZER; + +/** + Returns a GeoJSON source with an identifier, URL, and dictionary of options for + the source according to the + style + specification. + + @param identifier A string that uniquely identifies the source. + @param URL An HTTP(S) URL, absolute file URL, or local file URL relative to the + current application’s resource bundle. + @param options An `NSDictionary` of options for this source. + @return An initialized GeoJSON source. + */ +- (instancetype)initWithIdentifier:(NSString *)identifier URL:(NSURL *)url options:(nullable NS_DICTIONARY_OF(NSString *, id) *)options NS_DESIGNATED_INITIALIZER; + +#pragma mark Accessing a Source’s Content + /** The contents of the source. - If the receiver was initialized using `-initWithSourceIdentifier:URL:`, this - property is set to `nil`. This property is unavailable until the receiver is - passed into `-[MGLStyle addSource]`. + If the receiver was initialized using `-initWithIdentifier:URL:`, this property + is set to `nil`. This property is unavailable until the receiver is passed into + `-[MGLStyle addSource]`. */ @property (nonatomic, readonly, nullable) NS_ARRAY_OF(id ) *features; @@ -75,62 +105,20 @@ extern NSString * const MGLGeoJSONToleranceOption; contents. Alternatively, use NSJSONSerialization with the value of this property to transform it into Foundation types. - If the receiver was initialized using `-initWithSourceIdentifier:URL:`, this - property is set to `nil`. This property is unavailable until the receiver is - passed into `-[MGLStyle addSource]`. + If the receiver was initialized using `-initWithIdentifier:URL:`, this property + is set to `nil`. This property is unavailable until the receiver is passed into + `-[MGLStyle addSource]`. */ @property (nonatomic, readonly, nullable, copy) NSData *geoJSONData; /** The URL to the GeoJSON document that specifies the contents of the source. - If the receiver was initialized using `-initWithSourceIdentifier:geoJSONData:`, - this property is set to `nil`. + If the receiver was initialized using `-initWithIdentifier:geoJSONData:`, this + property is set to `nil`. */ @property (nonatomic, readonly, nullable) NSURL *URL; -/** - Initializes a source with the given identifier and GeoJSON data. - - @param sourceIdentifier A string that uniquely identifies the source. - @param geoJSONData An NSData object representing GeoJSON source code. - */ -- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier geoJSONData:(NSData *)data NS_DESIGNATED_INITIALIZER; - -/** - Initializes a source with the given identifier, GeoJSON data, and a dictionary - of options for the source as specified by the - the - style specification. - - @param sourceIdentifier A string that uniquely identifies the source. - @param geoJSONData An NSData object representing GeoJSON source code. - @param options An NSDictionary of attributes for this source. - */ -- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier geoJSONData:(NSData *)data options:(NS_DICTIONARY_OF(NSString *, id) *)options NS_DESIGNATED_INITIALIZER; - -/** - Initializes a source with the given identifier and URL. - - @param sourceIdentifier A string that uniquely identifies the source. - @param URL An HTTP(S) URL, absolute file URL, or local file URL relative to the - current application’s resource bundle. - */ -- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier URL:(NSURL *)url NS_DESIGNATED_INITIALIZER; - -/** - Initializes a source with the given identifier, a URL, and a dictionary of - options for the source as specified by the - the - style specification. - - @param sourceIdentifier A string that uniquely identifies the source. - @param URL An HTTP(S) URL, absolute file URL, or local file URL relative to the - current application’s resource bundle. - @param options An NSDictionary of attributes for this source. - */ -- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier URL:(NSURL *)url options:(NS_DICTIONARY_OF(NSString *, id) *)options NS_DESIGNATED_INITIALIZER; - @end NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLGeoJSONSource.mm b/platform/darwin/src/MGLGeoJSONSource.mm index 652644be4..458f062ad 100644 --- a/platform/darwin/src/MGLGeoJSONSource.mm +++ b/platform/darwin/src/MGLGeoJSONSource.mm @@ -22,18 +22,9 @@ NSString * const MGLGeoJSONToleranceOption = @"MGLGeoJSONOptionsClusterTolerance @implementation MGLGeoJSONSource -- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier geoJSONData:(NSData *)data +- (instancetype)initWithIdentifier:(NSString *)identifier geoJSONData:(NSData *)data options:(NS_DICTIONARY_OF(NSString *, id) *)options { - 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]) + if (self = [super initWithIdentifier:identifier]) { _geoJSONData = data; _options = options; @@ -41,18 +32,9 @@ NSString * const MGLGeoJSONToleranceOption = @"MGLGeoJSONOptionsClusterTolerance return self; } -- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier URL:(NSURL *)url +- (instancetype)initWithIdentifier:(NSString *)identifier URL:(NSURL *)url options:(NS_DICTIONARY_OF(NSString *, id) *)options { - 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]) + if (self = [super initWithIdentifier:identifier]) { _URL = url; _options = options; @@ -113,7 +95,7 @@ NSString * const MGLGeoJSONToleranceOption = @"MGLGeoJSONOptionsClusterTolerance - (std::unique_ptr)mbglSource { - auto source = std::make_unique(self.sourceIdentifier.UTF8String, [self geoJSONOptions]); + auto source = std::make_unique(self.identifier.UTF8String, self.geoJSONOptions); if (self.URL) { NSURL *url = self.URL.mgl_URLByStandardizingScheme; diff --git a/platform/darwin/src/MGLLineStyleLayer.h b/platform/darwin/src/MGLLineStyleLayer.h index d0a341d02..f8a9970f3 100644 --- a/platform/darwin/src/MGLLineStyleLayer.h +++ b/platform/darwin/src/MGLLineStyleLayer.h @@ -62,19 +62,7 @@ typedef NS_ENUM(NSUInteger, MGLLineStyleLayerLineTranslateAnchor) { `MGLMapView` for its `style` and obtain existing layers using the `-[MGLStyle layerWithIdentifier:]` method. */ -@interface MGLLineStyleLayer : MGLBaseStyleLayer - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source; - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer; - -/** - A predicate that corresponds to the layer's filter. - - The predicate's left expression must be a string that identifies a feature - property, or one of the special keys. - */ -@property (nonatomic, nullable) NSPredicate *predicate; +@interface MGLLineStyleLayer : MGLVectorStyleLayer #pragma mark - Accessing the Layout Attributes diff --git a/platform/darwin/src/MGLLineStyleLayer.mm b/platform/darwin/src/MGLLineStyleLayer.mm index b35d3921f..6b58edcd6 100644 --- a/platform/darwin/src/MGLLineStyleLayer.mm +++ b/platform/darwin/src/MGLLineStyleLayer.mm @@ -12,36 +12,28 @@ @interface MGLLineStyleLayer () @property (nonatomic) mbgl::style::LineLayer *layer; -@property (nonatomic, readwrite) NSString *layerIdentifier; -@property (nonatomic, readwrite) NSString *sourceIdentifier; -@property (nonatomic, readwrite) NSString *sourceLayerIdentifier; @end @implementation MGLLineStyleLayer -@synthesize mapView; - - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source +- (instancetype)initWithIdentifier:(NSString *)identifier source:(MGLSource *)source { - if (self = [super init]) { - _layerIdentifier = layerIdentifier; - _sourceIdentifier = source.sourceIdentifier; - _layer = new mbgl::style::LineLayer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String); + if (self = [super initWithIdentifier:identifier source:source]) { + _layer = new mbgl::style::LineLayer(identifier.UTF8String, source.identifier.UTF8String); } return self; } -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer +- (NSString *)sourceLayerIdentifier { - if (self = [super init]) { - _layerIdentifier = layerIdentifier; - _sourceIdentifier = source.sourceIdentifier; - _layer = new mbgl::style::LineLayer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String); - _layer->setSourceLayer(sourceLayer.UTF8String); - } - return self; + auto layerID = self.layer->getSourceLayer(); + return layerID.empty() ? nil : @(layerID.c_str()); +} + +- (void)setSourceLayerIdentifier:(NSString *)sourceLayerIdentifier +{ + self.layer->setSourceLayer(sourceLayerIdentifier.UTF8String ?: ""); } - (void)setPredicate:(NSPredicate *)predicate diff --git a/platform/darwin/src/MGLRasterSource.h b/platform/darwin/src/MGLRasterSource.h index bf561df8d..78f5af8a6 100644 --- a/platform/darwin/src/MGLRasterSource.h +++ b/platform/darwin/src/MGLRasterSource.h @@ -15,9 +15,49 @@ NS_ASSUME_NONNULL_BEGIN */ @interface MGLRasterSource : MGLSource +#pragma mark Initializing a Source + /** - A URL to a TileJSON resource. Supported protocols are `http:`, `https:`, and - `mapbox://`. + Returns a raster source initialized with an identifier, TileJSON configuration + URL, and tile size. + + After initializing and configuring the source, add it to a map view’s style + using the `-[MGLStyle addSource:]` method. + + @param identifier A string that uniquely identifies the source in the style to + which it is added. + @param url A URL to a TileJSON configuration file describing the source’s + contents and other metadata. + @param tileSize The height and width (measured in points) at which to display + each tile in this source when the map’s zoom level is an integer. + @return An initialized raster source. + */ +- (instancetype)initWithIdentifier:(NSString *)identifier URL:(NSURL *)url tileSize:(CGFloat)tileSize NS_DESIGNATED_INITIALIZER; + +/** + Returns a raster source initialized with the given identifier, tile size, and + tile set. + + After initializing and configuring the source, add it to a map view’s style + using the `-[MGLStyle addSource:]` method. + + @param identifier A string that uniquely identifies the source in the style to + which it is added. + @param tileSet A tile set describing the source’s contents and other metadata. + @param tileSize The height and width (measured in points) at which to display + each tile in this source when the map’s zoom level is an integer. + @return An initialized raster source. + */ +- (instancetype)initWithIdentifier:(NSString *)identifier tileSet:(MGLTileSet *)tileSet tileSize:(CGFloat)tileSize NS_DESIGNATED_INITIALIZER; + +#pragma mark Accessing a Source’s Content + +/** + A URL to a TileJSON configuration file describing the source’s contents and + other metadata. + + The URL may be a full HTTP or HTTPS URL or a Mapbox URL indicating the tile + set’s map ID (`mapbox://`). @see The TileJSON specification. @@ -25,8 +65,12 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, readonly, copy) NSURL *URL; /** - The minimum visual size to display tiles for this source. Units in pixels. - Defaults to `512` on each tile side. + The height and width (measured in points) at which to display each tile in this + source when the map’s zoom level is an integer. + + A tile may be scaled up or down when the zoom level is between two integers. + + The default value of this property is 512 points. */ @property (nonatomic, readonly, assign) NSUInteger tileSize; @@ -40,26 +84,6 @@ NS_ASSUME_NONNULL_BEGIN */ @property (nonatomic, readonly, nullable) MGLTileSet *tileSet; -/** - Initializes a source with the given identifier, TileJSON configuration - URL, and tile size. - - @param sourceIdentifier A string that uniquely identifies the source. - @param url A URL to a TileJSON resource. - @param tileSize The minimum visual size to display tiles for the source. - */ -- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier URL:(NSURL *)url tileSize:(CGFloat)tileSize; - -/** - Initializes a source with the given identifier, tile size, and tile - URL template set. - - @param sourceIdentifier A string that uniquely identifies the source. - @param tileSet A tile set describing where to download tiles. - @param tileSize The minimum visual size to display tiles for the source. - */ -- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier tileSet:(MGLTileSet *)tileSet tileSize:(CGFloat)tileSize; - @end NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLRasterSource.mm b/platform/darwin/src/MGLRasterSource.mm index 3d8a11101..41b9a5b04 100644 --- a/platform/darwin/src/MGLRasterSource.mm +++ b/platform/darwin/src/MGLRasterSource.mm @@ -8,18 +8,18 @@ @implementation MGLRasterSource -- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier URL:(NSURL *)url tileSize:(CGFloat)tileSize +- (instancetype)initWithIdentifier:(NSString *)identifier URL:(NSURL *)url tileSize:(CGFloat)tileSize { - if (self = [super initWithSourceIdentifier:sourceIdentifier]) { + if (self = [super initWithIdentifier:identifier]) { _URL = url; _tileSize = tileSize; } return self; } -- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier tileSet:(MGLTileSet *)tileSet tileSize:(CGFloat)tileSize; +- (instancetype)initWithIdentifier:(NSString *)identifier tileSet:(MGLTileSet *)tileSet tileSize:(CGFloat)tileSize; { - if (self = [super initWithSourceIdentifier:sourceIdentifier]) + if (self = [super initWithIdentifier:identifier]) { _tileSet = tileSet; _tileSize = tileSize; @@ -33,13 +33,13 @@ if (self.URL) { - source = std::make_unique(self.sourceIdentifier.UTF8String, + source = std::make_unique(self.identifier.UTF8String, self.URL.mgl_URLByStandardizingScheme.absoluteString.UTF8String, uint16_t(self.tileSize)); } else { - source = std::make_unique(self.sourceIdentifier.UTF8String, + source = std::make_unique(self.identifier.UTF8String, self.tileSet.mbglTileset, uint16_t(self.tileSize)); diff --git a/platform/darwin/src/MGLRasterStyleLayer.h b/platform/darwin/src/MGLRasterStyleLayer.h index 7fdfb04c9..d0a08c822 100644 --- a/platform/darwin/src/MGLRasterStyleLayer.h +++ b/platform/darwin/src/MGLRasterStyleLayer.h @@ -12,10 +12,7 @@ NS_ASSUME_NONNULL_BEGIN `MGLMapView` for its `style` and obtain existing layers using the `-[MGLStyle layerWithIdentifier:]` method. */ -@interface MGLRasterStyleLayer : MGLBaseStyleLayer - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source; - +@interface MGLRasterStyleLayer : MGLForegroundStyleLayer #pragma mark - Accessing the Paint Attributes diff --git a/platform/darwin/src/MGLRasterStyleLayer.mm b/platform/darwin/src/MGLRasterStyleLayer.mm index 74cffc2ed..01d3bf2bc 100644 --- a/platform/darwin/src/MGLRasterStyleLayer.mm +++ b/platform/darwin/src/MGLRasterStyleLayer.mm @@ -12,28 +12,19 @@ @interface MGLRasterStyleLayer () @property (nonatomic) mbgl::style::RasterLayer *layer; -@property (nonatomic, readwrite) NSString *layerIdentifier; -@property (nonatomic, readwrite) NSString *sourceIdentifier; -@property (nonatomic, readwrite) NSString *sourceLayerIdentifier; @end @implementation MGLRasterStyleLayer -@synthesize mapView; - - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source +- (instancetype)initWithIdentifier:(NSString *)identifier source:(MGLSource *)source { - if (self = [super init]) { - _layerIdentifier = layerIdentifier; - _sourceIdentifier = source.sourceIdentifier; - _layer = new mbgl::style::RasterLayer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String); + if (self = [super initWithIdentifier:identifier source:source]) { + _layer = new mbgl::style::RasterLayer(identifier.UTF8String, source.identifier.UTF8String); } return self; } - #pragma mark - Accessing the Paint Attributes - (void)setRasterOpacity:(id )rasterOpacity { diff --git a/platform/darwin/src/MGLRuntimeStylingTests.m.ejs b/platform/darwin/src/MGLRuntimeStylingTests.m.ejs index 0d0317cb6..720ee4547 100644 --- a/platform/darwin/src/MGLRuntimeStylingTests.m.ejs +++ b/platform/darwin/src/MGLRuntimeStylingTests.m.ejs @@ -14,11 +14,15 @@ @implementation MGL<%- camelize(type) %>LayerTests - (void)test<%- camelize(type) %>Layer { +<% if (type === 'background') { -%> + MGL<%- camelize(type) %>StyleLayer *layer = [[MGL<%- camelize(type) %>StyleLayer alloc] initWithIdentifier:@"layerID"]; +<% } else { -%> NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"]; NSURL *url = [NSURL fileURLWithPath:filePath]; - MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"sourceID" URL:url]; -MGL<%- camelize(type) %>StyleLayer *layer = [[MGL<%- camelize(type) %>StyleLayer alloc] initWithLayerIdentifier:@"layerID" source:source]; + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"sourceID" URL:url options:nil]; [self.mapView.style addSource:source]; + MGL<%- camelize(type) %>StyleLayer *layer = [[MGL<%- camelize(type) %>StyleLayer alloc] initWithIdentifier:@"layerID" source:source]; +<% } -%> [self.mapView.style addLayer:layer]; <% for (const property of layoutProperties) { -%> @@ -28,7 +32,8 @@ MGL<%- camelize(type) %>StyleLayer *layer = [[MGL<%- camelize(type) %>StyleLayer <%- testImplementation(property, type) %> <% } -%> - MGL<%- camelize(type) %>StyleLayer *gLayer = [self.mapView.style layerWithIdentifier:@"layerID"]; + MGL<%- camelize(type) %>StyleLayer *gLayer = (MGL<%- camelize(type) %>StyleLayer *)[self.mapView.style layerWithIdentifier:@"layerID"]; + XCTAssertTrue([gLayer isKindOfClass:[MGL<%- camelize(type) %>StyleLayer class]]); <% for (const property of layoutProperties) { -%> <%- testGetterImplementation(property, type) %> <% } -%> diff --git a/platform/darwin/src/MGLSource.h b/platform/darwin/src/MGLSource.h index bb8f99082..79f6ce491 100644 --- a/platform/darwin/src/MGLSource.h +++ b/platform/darwin/src/MGLSource.h @@ -10,16 +10,25 @@ */ @interface MGLSource : NSObject -/** - A string that uniquely identifies the source. - */ -@property (nonatomic, copy) NSString *sourceIdentifier; +#pragma mark Initializing a Source /** - Initializes a source with the given identifier. - - @param sourceIdentifier A string that uniquely identifies the source. + Returns a source initialized with an identifier. + + After initializing and configuring the source, add it to a map view’s style + using the `-[MGLStyle addSource:]` method. + + @param identifier A string that uniquely identifies the source in the style to + which it is added. + @return An initialized source. */ -- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier; +- (instancetype)initWithIdentifier:(NSString *)identifier; + +#pragma mark Identifying a Source + +/** + A string that uniquely identifies the source in the style to which it is added. + */ +@property (nonatomic, copy) NSString *identifier; @end diff --git a/platform/darwin/src/MGLSource.mm b/platform/darwin/src/MGLSource.mm index cdf003cb0..85bbc0634 100644 --- a/platform/darwin/src/MGLSource.mm +++ b/platform/darwin/src/MGLSource.mm @@ -1,18 +1,27 @@ -#import "MGLSource.h" +#import "MGLSource_Private.h" #include +@interface MGLSource () + +@property (nonatomic) mbgl::style::Source *source; + +@end + @implementation MGLSource -- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier { +- (instancetype)initWithIdentifier:(NSString *)identifier { if (self = [super init]) { - _sourceIdentifier = sourceIdentifier; + _identifier = identifier; } return self; } - (std::unique_ptr)mbglSource { - [NSException raise:@"Subclasses must override this method" format:@""]; + [NSException raise:@"MGLAbstractClassException" format: + @"The source %@ cannot be added to the style. " + @"Make sure the source was created as a member of a concrete subclass of MGLSource.", + NSStringFromClass(self)]; return nil; } diff --git a/platform/darwin/src/MGLStyle.h b/platform/darwin/src/MGLStyle.h index d4972a3d3..d4868681e 100644 --- a/platform/darwin/src/MGLStyle.h +++ b/platform/darwin/src/MGLStyle.h @@ -178,42 +178,43 @@ static const NSInteger MGLStyleDefaultVersion = 9; Returns a layer that conforms to `MGLStyleLayer` if any layer with the given identifier was found. - @return layer A layer instance of the corresponding type. + @return An instance of a concrete subclass of `MGLStyleLayer` associated with + the given identifier. */ -- (nullable id )layerWithIdentifier:(NSString *)identifier; +- (nullable MGLStyleLayer *)layerWithIdentifier:(NSString *)identifier; /** Returns a source if any source with the given identifier was found. - @return source An instance of an `MGLSource` subclass. + @return An instance of a concrete subclass of `MGLSource` associated with the + given identifier. */ - (nullable MGLSource *)sourceWithIdentifier:(NSString *)identifier; /** Adds a new layer on top of existing layers. - @param styleLayer The layer object to add to the map view. This object - must conform to the `MGLStyleLayer` protocol. + @param layer The layer object to add to the map view. This object must be an + instance of a concrete subclass of `MGLStyleLayer`. */ -- (void)addLayer:(id )styleLayer; +- (void)addLayer:(MGLStyleLayer *)layer; /** Inserts a new layer below another layer. - @param styleLayer Layer to be inserted. + @param layer Layer to be inserted. @param belowLayer A layer that's already on the map view. */ -- (void)insertLayer:(id )styleLayer - belowLayer:(id )belowLayer; +- (void)insertLayer:(MGLStyleLayer *)layer belowLayer:(MGLStyleLayer *)otherLayer; /** Removes a layer from the map view. - @param styleLayer The layer object to remove from the map view. This object + @param layer The layer object to remove from the map view. This object must conform to the `MGLStyleLayer` protocol. */ -- (void)removeLayer:(id )styleLayer; +- (void)removeLayer:(MGLStyleLayer *)layer; /** Adds a new source to the map view. diff --git a/platform/darwin/src/MGLStyle.mm b/platform/darwin/src/MGLStyle.mm index 79d7d7e0f..cd9c4465b 100644 --- a/platform/darwin/src/MGLStyle.mm +++ b/platform/darwin/src/MGLStyle.mm @@ -92,95 +92,113 @@ static NSURL *MGLStyleURL_emerald; return @(self.mapView.mbglMap->getStyleName().c_str()); } -- (id )layerWithIdentifier:(NSString *)identifier +- (MGLStyleLayer *)layerWithIdentifier:(NSString *)identifier { - auto layer = self.mapView.mbglMap->getLayer(identifier.UTF8String); - - if (!layer) return nil; + auto mbglLayer = self.mapView.mbglMap->getLayer(identifier.UTF8String); + if (!mbglLayer) { + return nil; + } - Class clazz = [self classFromLayer:layer]; + MGLStyleLayer *styleLayer; + if (auto fillLayer = mbglLayer->as()) { + MGLSource *source = [self sourceWithIdentifier:@(fillLayer->getSourceID().c_str())]; + styleLayer = [[MGLFillStyleLayer alloc] initWithIdentifier:identifier source:source]; + } else if (auto lineLayer = mbglLayer->as()) { + MGLSource *source = [self sourceWithIdentifier:@(lineLayer->getSourceID().c_str())]; + styleLayer = [[MGLLineStyleLayer alloc] initWithIdentifier:identifier source:source]; + } else if (auto symbolLayer = mbglLayer->as()) { + MGLSource *source = [self sourceWithIdentifier:@(symbolLayer->getSourceID().c_str())]; + styleLayer = [[MGLSymbolStyleLayer alloc] initWithIdentifier:identifier source:source]; + } else if (auto rasterLayer = mbglLayer->as()) { + MGLSource *source = [self sourceWithIdentifier:@(rasterLayer->getSourceID().c_str())]; + styleLayer = [[MGLRasterStyleLayer alloc] initWithIdentifier:identifier source:source]; + } else if (auto circleLayer = mbglLayer->as()) { + MGLSource *source = [self sourceWithIdentifier:@(circleLayer->getSourceID().c_str())]; + styleLayer = [[MGLCircleStyleLayer alloc] initWithIdentifier:identifier source:source]; + } else if (mbglLayer->as()) { + styleLayer = [[MGLBackgroundStyleLayer alloc] initWithIdentifier:identifier]; + } else { + NSAssert(NO, @"Unrecognized layer type"); + return nil; + } - id styleLayer = [[clazz alloc] init]; - styleLayer.layerIdentifier = identifier; - styleLayer.layer = layer; - styleLayer.mapView = self.mapView; + styleLayer.layer = mbglLayer; return styleLayer; } - (MGLSource *)sourceWithIdentifier:(NSString *)identifier { - auto s = self.mapView.mbglMap->getSource(identifier.UTF8String); - - if (!s) return nil; + auto mbglSource = self.mapView.mbglMap->getSource(identifier.UTF8String); + if (!mbglSource) { + return nil; + } - Class clazz = [self classFromSource:s]; + // TODO: Fill in options specific to the respective source classes + // https://github.com/mapbox/mapbox-gl-native/issues/6584 + MGLSource *source; + if (mbglSource->is()) { + source = [[MGLVectorSource alloc] initWithIdentifier:identifier]; + } else if (mbglSource->is()) { + source = [[MGLGeoJSONSource alloc] initWithIdentifier:identifier]; + } else if (mbglSource->is()) { + source = [[MGLRasterSource alloc] initWithIdentifier:identifier]; + } else { + NSAssert(NO, @"Unrecognized source type"); + return nil; + } - MGLSource *source = [[clazz alloc] init]; - source.sourceIdentifier = identifier; - source.source = s; + source.source = mbglSource; return source; } -- (Class)classFromSource:(mbgl::style::Source *)source +- (void)removeLayer:(MGLStyleLayer *)layer { - if (source->is()) { - return MGLVectorSource.class; - } else if (source->is()) { - return MGLGeoJSONSource.class; - } else if (source->is()) { - return MGLRasterSource.class; + self.mapView.mbglMap->removeLayer(layer.identifier.UTF8String); +} + +- (void)addLayer:(MGLStyleLayer *)layer +{ + if (!layer.layer) { + [NSException raise:NSInvalidArgumentException format: + @"The style layer %@ cannot be added to the style. " + @"Make sure the style layer was created as a member of a concrete subclass of MGLStyleLayer.", + NSStringFromClass(self)]; } - [NSException raise:@"Source type not handled" format:@""]; - return Nil; + self.mapView.mbglMap->addLayer(std::unique_ptr(layer.layer)); } -- (Class)classFromLayer:(mbgl::style::Layer *)layer +- (void)insertLayer:(MGLStyleLayer *)layer belowLayer:(MGLStyleLayer *)otherLayer { - if (layer->is()) { - return MGLFillStyleLayer.class; - } else if (layer->is()) { - return MGLLineStyleLayer.class; - } else if (layer->is()) { - return MGLSymbolStyleLayer.class; - } else if (layer->is()) { - return MGLRasterStyleLayer.class; - } else if (layer->is()) { - return MGLCircleStyleLayer.class; - } else if (layer->is()) { - return MGLBackgroundStyleLayer.class; + if (!layer.layer) { + [NSException raise:NSInvalidArgumentException + format: + @"The style layer %@ cannot be added to the style. " + @"Make sure the style layer was created as a member of a concrete subclass of MGLStyleLayer.", + NSStringFromClass(layer)]; } - [NSException raise:@"Layer type not handled" format:@""]; - return Nil; -} - -- (void)removeLayer:(id )styleLayer -{ - self.mapView.mbglMap->removeLayer(styleLayer.layer->getID()); -} - -- (void)addLayer:(id )styleLayer -{ - self.mapView.mbglMap->addLayer(std::unique_ptr(styleLayer.layer)); -} - -- (void)insertLayer:(id )styleLayer - belowLayer:(id )belowLayer -{ - const mbgl::optional belowLayerId{[belowLayer layerIdentifier].UTF8String}; - self.mapView.mbglMap->addLayer(std::unique_ptr(styleLayer.layer), belowLayerId); + if (!otherLayer.layer) { + [NSException raise:NSInvalidArgumentException + format: + @"A style layer cannot be placed before %@ in the style. " + @"Make sure the style layer was created as a member of a concrete subclass of MGLStyleLayer.", + NSStringFromClass(otherLayer)]; + } + + const mbgl::optional belowLayerId{otherLayer.identifier.UTF8String}; + self.mapView.mbglMap->addLayer(std::unique_ptr(layer.layer), belowLayerId); } - (void)addSource:(MGLSource *)source { - self.mapView.mbglMap->addSource([source mbglSource]); + self.mapView.mbglMap->addSource(source.mbglSource); } - (void)removeSource:(MGLSource *)source { - self.mapView.mbglMap->removeSource(source.sourceIdentifier.UTF8String); + self.mapView.mbglMap->removeSource(source.identifier.UTF8String); } - (NS_ARRAY_OF(NSString *) *)styleClasses diff --git a/platform/darwin/src/MGLStyleLayer.h b/platform/darwin/src/MGLStyleLayer.h index d4ed525a3..763854f44 100644 --- a/platform/darwin/src/MGLStyleLayer.h +++ b/platform/darwin/src/MGLStyleLayer.h @@ -1,12 +1,74 @@ #import -@class MGLMapView; +#import "MGLBaseStyleLayer.h" -@protocol MGLStyleLayer +NS_ASSUME_NONNULL_BEGIN -@property (nonatomic, weak) MGLMapView *mapView; -@property (nonatomic, copy, readonly) NSString *layerIdentifier; -@property (nonatomic, readonly) NSString *sourceIdentifier; -@property (nonatomic, readonly) NSString *sourceLayerIdentifier; +@class MGLSource; + +/** + `MGLForegroundStyleLayer` is an abstract superclass for style layers whose + content is defined by an `MGLSource` object. + + Do not create instances of this class directly, and do not create your own + subclasses of this class. Instead, create instances of `MGLRasterStyleLayer` + and the concrete subclasses of `MGLVectorStyleLayer`. + */ +@interface MGLForegroundStyleLayer : MGLStyleLayer + +#pragma mark Initializing a Style Layer + +/** + Returns a foreground style layer initialized with an identifier and source. + + After initializing and configuring the style layer, add it to a map view’s + style using the `-[MGLStyle addLayer:]` or + `-[MGLStyle insertLayer:belowLayer:]` method. + + @param identifier A string that uniquely identifies the source in the style to + which it is added. + @param source The source from which to obtain the data to style. If the source + has not yet been added to the current style, the behavior is undefined. + @return An initialized foreground style layer. + */ +- (instancetype)initWithIdentifier:(NSString *)identifier source:(MGLSource *)source NS_DESIGNATED_INITIALIZER; + +#pragma mark Specifying a Style Layer’s Content + +/** + Identifier of the source from which the receiver obtains the data to style. + */ +@property (nonatomic, readonly, nullable) NSString *sourceIdentifier; @end + +/** + `MGLVectorStyleLayer` is an abstract superclass for style layers whose content + is defined by an `MGLGeoJSONSource` or `MGLVectorSource` object. + + Do not create instances of this class directly, and do not create your own + subclasses of this class. Instead, create instances of the following concrete + subclasses: `MGLCircleStyleLayer`, `MGLFillStyleLayer`, `MGLLineStyleLayer`, + and `MGLSymbolStyleLayer`. + */ +@interface MGLVectorStyleLayer : MGLForegroundStyleLayer + +#pragma mark Refining a Style Layer’s Content + +/** + Identifier of the layer within the source identified by the `sourceIdentifier` + property from which the receiver obtains the data to style. + */ +@property (nonatomic, nullable) NSString *sourceLayerIdentifier; + +/** + A predicate that corresponds to the layer's filter. + + The predicate's left expression must be a string that identifies a feature + property, or one of the special keys. + */ +@property (nonatomic, nullable) NSPredicate *predicate; + +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLStyleLayer.h.ejs b/platform/darwin/src/MGLStyleLayer.h.ejs index 40b750e73..e27808bfc 100644 --- a/platform/darwin/src/MGLStyleLayer.h.ejs +++ b/platform/darwin/src/MGLStyleLayer.h.ejs @@ -60,28 +60,16 @@ typedef NS_ENUM(NSUInteger, MGL<%- camelize(type) %>StyleLayer<%- camelize(prope `-[MGLStyle layerWithIdentifier:]` method. */ <% } -%> -@interface MGL<%- camelize(type) %>StyleLayer : MGLBaseStyleLayer -<% if (type == 'background') { -%> +@interface MGL<%- camelize(type) %>StyleLayer : <%- +(type === 'background' ? 'MGLStyleLayer' : + (type === 'raster' ? 'MGLForegroundStyleLayer' : + 'MGLVectorStyleLayer')) +%> +<% if (type === 'background') { -%> -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier; +- (instancetype)initWithIdentifier:(NSString *)identifier NS_DESIGNATED_INITIALIZER; <% } -%> -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source; - -<% if (type !== 'background' && type !== 'raster') { -%> -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer; -<% } -%> - -<% if (type !== 'background' && type !== 'raster') { -%> -/** - A predicate that corresponds to the layer's filter. - - The predicate's left expression must be a string that identifies a feature - property, or one of the special keys. - */ -@property (nonatomic, nullable) NSPredicate *predicate; - -<% } -%> <% if (layoutProperties.length) { -%> #pragma mark - Accessing the Layout Attributes diff --git a/platform/darwin/src/MGLStyleLayer.mm b/platform/darwin/src/MGLStyleLayer.mm index 43d98d62d..b0fbb38dc 100644 --- a/platform/darwin/src/MGLStyleLayer.mm +++ b/platform/darwin/src/MGLStyleLayer.mm @@ -1,4 +1,28 @@ -#import "MGLStyleLayer.h" - #import "MGLStyleLayer_Private.h" -#import "MGLMapView_Private.h" +#import "MGLSource.h" + +@implementation MGLForegroundStyleLayer + +- (instancetype)initWithIdentifier:(NSString *)identifier source:(MGLSource *)source { + if (self = [super initWithIdentifier:identifier]) { + _sourceIdentifier = source.identifier; + } + return self; +} + +@end + +@implementation MGLVectorStyleLayer + +- (void)setPredicate:(NSPredicate *)predicate { + [NSException raise:@"MGLAbstractClassException" + format:@"MGLVectorLayer is an abstract class"]; +} + +- (NSPredicate *)predicate { + [NSException raise:@"MGLAbstractClassException" + format:@"MGLVectorLayer is an abstract class"]; + return nil; +} + +@end diff --git a/platform/darwin/src/MGLStyleLayer.mm.ejs b/platform/darwin/src/MGLStyleLayer.mm.ejs index 233117fa9..8d3bba995 100644 --- a/platform/darwin/src/MGLStyleLayer.mm.ejs +++ b/platform/darwin/src/MGLStyleLayer.mm.ejs @@ -17,51 +17,41 @@ @interface MGL<%- camelize(type) %>StyleLayer () @property (nonatomic) mbgl::style::<%- camelize(type) %>Layer *layer; -@property (nonatomic, readwrite) NSString *layerIdentifier; -@property (nonatomic, readwrite) NSString *sourceIdentifier; -@property (nonatomic, readwrite) NSString *sourceLayerIdentifier; @end @implementation MGL<%- camelize(type) %>StyleLayer -@synthesize mapView; - <% if (type == 'background') { -%> -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier +- (instancetype)initWithIdentifier:(NSString *)identifier { - if (self = [super init]) { - _layerIdentifier = layerIdentifier; - <%- initLayerIdentifierOnly(type) %> + if (self = [super initWithIdentifier:identifier]) { + _layer = new mbgl::style::<%- camelize(type) %>Layer(identifier.UTF8String); } return self; } -<% } -%> - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source +<% } else { -%> +- (instancetype)initWithIdentifier:(NSString *)identifier source:(MGLSource *)source { - if (self = [super init]) { - _layerIdentifier = layerIdentifier; - _sourceIdentifier = source.sourceIdentifier; - <%- initLayer(type) %> - } - return self; -} - -<% if (type !== 'background' && type !== 'raster') { -%> -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer -{ - if (self = [super init]) { - _layerIdentifier = layerIdentifier; - _sourceIdentifier = source.sourceIdentifier; - <%- initLayerWithSourceLayer(type) %> - <%- setSourceLayer() %> + if (self = [super initWithIdentifier:identifier source:source]) { + _layer = new mbgl::style::<%- camelize(type) %>Layer(identifier.UTF8String, source.identifier.UTF8String); } return self; } <% } -%> <% if (type !== 'background' && type !== 'raster') { -%> +- (NSString *)sourceLayerIdentifier +{ + auto layerID = self.layer->getSourceLayer(); + return layerID.empty() ? nil : @(layerID.c_str()); +} + +- (void)setSourceLayerIdentifier:(NSString *)sourceLayerIdentifier +{ + self.layer->setSourceLayer(sourceLayerIdentifier.UTF8String ?: ""); +} + - (void)setPredicate:(NSPredicate *)predicate { self.layer->setFilter(predicate.mgl_filter); diff --git a/platform/darwin/src/MGLStyleLayer_Private.h b/platform/darwin/src/MGLStyleLayer_Private.h index bc3d87bcb..a499775a5 100644 --- a/platform/darwin/src/MGLStyleLayer_Private.h +++ b/platform/darwin/src/MGLStyleLayer_Private.h @@ -19,12 +19,9 @@ #import "NSColor+MGLStyleAttributeAdditions_Private.h" #endif -@class MGLMapView; +@interface MGLStyleLayer (Private) -@protocol MGLStyleLayer_Private - -@property (nonatomic, weak) MGLMapView *mapView; -@property (nonatomic, readwrite, copy) NSString *layerIdentifier; +@property (nonatomic, readwrite, copy) NSString *identifier; @property (nonatomic) mbgl::style::Layer *layer; @end diff --git a/platform/darwin/src/MGLSymbolStyleLayer.h b/platform/darwin/src/MGLSymbolStyleLayer.h index b805ee18d..45387bdf8 100644 --- a/platform/darwin/src/MGLSymbolStyleLayer.h +++ b/platform/darwin/src/MGLSymbolStyleLayer.h @@ -208,19 +208,7 @@ typedef NS_ENUM(NSUInteger, MGLSymbolStyleLayerTextTranslateAnchor) { `MGLMapView` for its `style` and obtain existing layers using the `-[MGLStyle layerWithIdentifier:]` method. */ -@interface MGLSymbolStyleLayer : MGLBaseStyleLayer - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source; - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer; - -/** - A predicate that corresponds to the layer's filter. - - The predicate's left expression must be a string that identifies a feature - property, or one of the special keys. - */ -@property (nonatomic, nullable) NSPredicate *predicate; +@interface MGLSymbolStyleLayer : MGLVectorStyleLayer #pragma mark - Accessing the Layout Attributes diff --git a/platform/darwin/src/MGLSymbolStyleLayer.mm b/platform/darwin/src/MGLSymbolStyleLayer.mm index 4d9b20fbc..48673acf8 100644 --- a/platform/darwin/src/MGLSymbolStyleLayer.mm +++ b/platform/darwin/src/MGLSymbolStyleLayer.mm @@ -12,36 +12,28 @@ @interface MGLSymbolStyleLayer () @property (nonatomic) mbgl::style::SymbolLayer *layer; -@property (nonatomic, readwrite) NSString *layerIdentifier; -@property (nonatomic, readwrite) NSString *sourceIdentifier; -@property (nonatomic, readwrite) NSString *sourceLayerIdentifier; @end @implementation MGLSymbolStyleLayer -@synthesize mapView; - - -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source +- (instancetype)initWithIdentifier:(NSString *)identifier source:(MGLSource *)source { - if (self = [super init]) { - _layerIdentifier = layerIdentifier; - _sourceIdentifier = source.sourceIdentifier; - _layer = new mbgl::style::SymbolLayer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String); + if (self = [super initWithIdentifier:identifier source:source]) { + _layer = new mbgl::style::SymbolLayer(identifier.UTF8String, source.identifier.UTF8String); } return self; } -- (instancetype)initWithLayerIdentifier:(NSString *)layerIdentifier source:(MGLSource *)source sourceLayer:(NSString *)sourceLayer +- (NSString *)sourceLayerIdentifier { - if (self = [super init]) { - _layerIdentifier = layerIdentifier; - _sourceIdentifier = source.sourceIdentifier; - _layer = new mbgl::style::SymbolLayer(layerIdentifier.UTF8String, source.sourceIdentifier.UTF8String); - _layer->setSourceLayer(sourceLayer.UTF8String); - } - return self; + auto layerID = self.layer->getSourceLayer(); + return layerID.empty() ? nil : @(layerID.c_str()); +} + +- (void)setSourceLayerIdentifier:(NSString *)sourceLayerIdentifier +{ + self.layer->setSourceLayer(sourceLayerIdentifier.UTF8String ?: ""); } - (void)setPredicate:(NSPredicate *)predicate diff --git a/platform/darwin/src/MGLVectorSource.h b/platform/darwin/src/MGLVectorSource.h index 2106a5cf4..6cfab0948 100644 --- a/platform/darwin/src/MGLVectorSource.h +++ b/platform/darwin/src/MGLVectorSource.h @@ -13,28 +13,57 @@ NS_ASSUME_NONNULL_BEGIN */ @interface MGLVectorSource : MGLSource +#pragma mark Initializing a Source + +/** + Returns a vector source initialized with an identifier and TileJSON URL. + + After initializing and configuring the source, add it to a map view’s style + using the `-[MGLStyle addSource:]` method. + + @param identifier A string that uniquely identifies the source in the style to + which it is added. + @param url A URL to a TileJSON configuration file describing the source’s + contents and other metadata. + @return An initialized vector source. + */ +- (instancetype)initWithIdentifier:(NSString *)identifier URL:(NSURL *)url NS_DESIGNATED_INITIALIZER; + +/** + Returns a vector source initialized an identifier and tile set. + + After initializing and configuring the source, add it to a map view’s style + using the `-[MGLStyle addSource:]` method. + + @param identifier A string that uniquely identifies the source in the style to + which it is added. + @param tileSet A tile set describing the source’s contents and other metadata. + @return An initialized vector source. + */ +- (instancetype)initWithIdentifier:(NSString *)identifier tileSet:(MGLTileSet *)tileSet NS_DESIGNATED_INITIALIZER; + +#pragma mark Accessing a Source’s Content + +/** + A URL to a TileJSON configuration file describing the source’s contents and + other metadata. + + The URL may be a full HTTP or HTTPS URL or a Mapbox URL indicating the tile + set’s map ID (`mapbox://`). + + If the receiver was initialized using `-initWithIdentifier:tileSet:`, this + property is set to `nil`. + */ @property (nonatomic, readonly, copy) NSURL *URL; + +/** + A tile set describing the source’s contents and other metadata. + + If the receiver was initialized using `-initWithIdentifier:URL:`, this property + is set to `nil`. + */ @property (nonatomic, readonly, nullable) MGLTileSet *tileSet; -/** - Initializes and returns a vector source from a remote URL. - - @param sourceIdentifier The source identifier. - @param URL A remote URL holding the vector source data. - - @return An `MGLVectorSource`. - */ -- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier URL:(NSURL *)url; - -/** - Initializes a source with the given identifier, tile size, and tile - URL template set. - - @param sourceIdentifier A string that uniquely identifies the source. - @param tileSet A tile set describing where to download tiles. - */ -- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier tileSet:(MGLTileSet *)tileSet; - @end NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLVectorSource.mm b/platform/darwin/src/MGLVectorSource.mm index 2ab9627ef..359781235 100644 --- a/platform/darwin/src/MGLVectorSource.mm +++ b/platform/darwin/src/MGLVectorSource.mm @@ -10,18 +10,18 @@ static NSString *MGLVectorSourceType = @"vector"; -- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier URL:(NSURL *)url +- (instancetype)initWithIdentifier:(NSString *)identifier URL:(NSURL *)url { - if (self = [super initWithSourceIdentifier:sourceIdentifier]) + if (self = [super initWithIdentifier:identifier]) { _URL = url; } return self; } -- (instancetype)initWithSourceIdentifier:(NSString *)sourceIdentifier tileSet:(MGLTileSet *)tileSet +- (instancetype)initWithIdentifier:(NSString *)identifier tileSet:(MGLTileSet *)tileSet { - if (self = [super initWithSourceIdentifier:sourceIdentifier]) + if (self = [super initWithIdentifier:identifier]) { _tileSet = tileSet; } @@ -34,12 +34,12 @@ static NSString *MGLVectorSourceType = @"vector"; if (self.URL) { - source = std::make_unique(self.sourceIdentifier.UTF8String, + source = std::make_unique(self.identifier.UTF8String, self.URL.mgl_URLByStandardizingScheme.absoluteString.UTF8String); } else { - source = std::make_unique(self.sourceIdentifier.UTF8String, + source = std::make_unique(self.identifier.UTF8String, self.tileSet.mbglTileset); } diff --git a/platform/darwin/test/MGLBackgroundStyleLayerTests.m b/platform/darwin/test/MGLBackgroundStyleLayerTests.m index f4fdae61a..2681b773a 100644 --- a/platform/darwin/test/MGLBackgroundStyleLayerTests.m +++ b/platform/darwin/test/MGLBackgroundStyleLayerTests.m @@ -9,18 +9,15 @@ @implementation MGLBackgroundLayerTests - (void)testBackgroundLayer { - NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"]; - NSURL *url = [NSURL fileURLWithPath:filePath]; - MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"sourceID" URL:url]; -MGLBackgroundStyleLayer *layer = [[MGLBackgroundStyleLayer alloc] initWithLayerIdentifier:@"layerID" source:source]; - [self.mapView.style addSource:source]; + MGLBackgroundStyleLayer *layer = [[MGLBackgroundStyleLayer alloc] initWithIdentifier:@"layerID"]; [self.mapView.style addLayer:layer]; layer.backgroundColor = [MGLRuntimeStylingHelper testColor]; layer.backgroundPattern = [MGLRuntimeStylingHelper testString]; layer.backgroundOpacity = [MGLRuntimeStylingHelper testNumber]; - MGLBackgroundStyleLayer *gLayer = [self.mapView.style layerWithIdentifier:@"layerID"]; + MGLBackgroundStyleLayer *gLayer = (MGLBackgroundStyleLayer *)[self.mapView.style layerWithIdentifier:@"layerID"]; + XCTAssertTrue([gLayer isKindOfClass:[MGLBackgroundStyleLayer class]]); XCTAssertEqualObjects(gLayer.backgroundColor, [MGLRuntimeStylingHelper testColor]); XCTAssertEqualObjects(gLayer.backgroundPattern, [MGLRuntimeStylingHelper testString]); XCTAssertEqualObjects(gLayer.backgroundOpacity, [MGLRuntimeStylingHelper testNumber]); diff --git a/platform/darwin/test/MGLCircleStyleLayerTests.m b/platform/darwin/test/MGLCircleStyleLayerTests.m index 0e70c3f83..7b26f21fc 100644 --- a/platform/darwin/test/MGLCircleStyleLayerTests.m +++ b/platform/darwin/test/MGLCircleStyleLayerTests.m @@ -11,9 +11,9 @@ - (void)testCircleLayer { NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"]; NSURL *url = [NSURL fileURLWithPath:filePath]; - MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"sourceID" URL:url]; -MGLCircleStyleLayer *layer = [[MGLCircleStyleLayer alloc] initWithLayerIdentifier:@"layerID" source:source]; + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"sourceID" URL:url options:nil]; [self.mapView.style addSource:source]; + MGLCircleStyleLayer *layer = [[MGLCircleStyleLayer alloc] initWithIdentifier:@"layerID" source:source]; [self.mapView.style addLayer:layer]; layer.circleRadius = [MGLRuntimeStylingHelper testNumber]; @@ -24,7 +24,8 @@ MGLCircleStyleLayer *layer = [[MGLCircleStyleLayer alloc] initWithLayerIdentifie layer.circleTranslateAnchor = [MGLRuntimeStylingHelper testEnum:MGLCircleStyleLayerCircleTranslateAnchorViewport type:@encode(MGLCircleStyleLayerCircleTranslateAnchor)]; layer.circlePitchScale = [MGLRuntimeStylingHelper testEnum:MGLCircleStyleLayerCirclePitchScaleViewport type:@encode(MGLCircleStyleLayerCirclePitchScale)]; - MGLCircleStyleLayer *gLayer = [self.mapView.style layerWithIdentifier:@"layerID"]; + MGLCircleStyleLayer *gLayer = (MGLCircleStyleLayer *)[self.mapView.style layerWithIdentifier:@"layerID"]; + XCTAssertTrue([gLayer isKindOfClass:[MGLCircleStyleLayer class]]); XCTAssertEqualObjects(gLayer.circleRadius, [MGLRuntimeStylingHelper testNumber]); XCTAssertEqualObjects(gLayer.circleColor, [MGLRuntimeStylingHelper testColor]); XCTAssertEqualObjects(gLayer.circleBlur, [MGLRuntimeStylingHelper testNumber]); diff --git a/platform/darwin/test/MGLFillStyleLayerTests.m b/platform/darwin/test/MGLFillStyleLayerTests.m index 7994d7651..779851a27 100644 --- a/platform/darwin/test/MGLFillStyleLayerTests.m +++ b/platform/darwin/test/MGLFillStyleLayerTests.m @@ -11,9 +11,9 @@ - (void)testFillLayer { NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"]; NSURL *url = [NSURL fileURLWithPath:filePath]; - MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"sourceID" URL:url]; -MGLFillStyleLayer *layer = [[MGLFillStyleLayer alloc] initWithLayerIdentifier:@"layerID" source:source]; + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"sourceID" URL:url options:nil]; [self.mapView.style addSource:source]; + MGLFillStyleLayer *layer = [[MGLFillStyleLayer alloc] initWithIdentifier:@"layerID" source:source]; [self.mapView.style addLayer:layer]; layer.fillAntialias = [MGLRuntimeStylingHelper testBool]; @@ -24,7 +24,8 @@ MGLFillStyleLayer *layer = [[MGLFillStyleLayer alloc] initWithLayerIdentifier:@" layer.fillTranslateAnchor = [MGLRuntimeStylingHelper testEnum:MGLFillStyleLayerFillTranslateAnchorViewport type:@encode(MGLFillStyleLayerFillTranslateAnchor)]; layer.fillPattern = [MGLRuntimeStylingHelper testString]; - MGLFillStyleLayer *gLayer = [self.mapView.style layerWithIdentifier:@"layerID"]; + MGLFillStyleLayer *gLayer = (MGLFillStyleLayer *)[self.mapView.style layerWithIdentifier:@"layerID"]; + XCTAssertTrue([gLayer isKindOfClass:[MGLFillStyleLayer class]]); XCTAssertEqualObjects(gLayer.fillAntialias, [MGLRuntimeStylingHelper testBool]); XCTAssertEqualObjects(gLayer.fillOpacity, [MGLRuntimeStylingHelper testNumber]); XCTAssertEqualObjects(gLayer.fillColor, [MGLRuntimeStylingHelper testColor]); diff --git a/platform/darwin/test/MGLFilterTests.mm b/platform/darwin/test/MGLFilterTests.mm index d73313e27..2bb188575 100644 --- a/platform/darwin/test/MGLFilterTests.mm +++ b/platform/darwin/test/MGLFilterTests.mm @@ -18,9 +18,9 @@ NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"]; NSURL *url = [NSURL fileURLWithPath:filePath]; NSData *geoJSONData = [NSData dataWithContentsOfURL:url]; - source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"test-source" geoJSONData:geoJSONData]; + source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"test-source" geoJSONData:geoJSONData options:nil]; [self.mapView.style addSource:source]; - layer = [[MGLLineStyleLayer alloc] initWithLayerIdentifier:@"test-layer" source:source]; + layer = [[MGLLineStyleLayer alloc] initWithIdentifier:@"test-layer" source:source]; } - (void)tearDown diff --git a/platform/darwin/test/MGLLineStyleLayerTests.m b/platform/darwin/test/MGLLineStyleLayerTests.m index ff607f2e2..165c29900 100644 --- a/platform/darwin/test/MGLLineStyleLayerTests.m +++ b/platform/darwin/test/MGLLineStyleLayerTests.m @@ -11,9 +11,9 @@ - (void)testLineLayer { NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"]; NSURL *url = [NSURL fileURLWithPath:filePath]; - MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"sourceID" URL:url]; -MGLLineStyleLayer *layer = [[MGLLineStyleLayer alloc] initWithLayerIdentifier:@"layerID" source:source]; + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"sourceID" URL:url options:nil]; [self.mapView.style addSource:source]; + MGLLineStyleLayer *layer = [[MGLLineStyleLayer alloc] initWithIdentifier:@"layerID" source:source]; [self.mapView.style addLayer:layer]; layer.lineCap = [MGLRuntimeStylingHelper testEnum:MGLLineStyleLayerLineCapSquare type:@encode(MGLLineStyleLayerLineCap)]; @@ -31,7 +31,8 @@ MGLLineStyleLayer *layer = [[MGLLineStyleLayer alloc] initWithLayerIdentifier:@" layer.lineDasharray = [MGLRuntimeStylingHelper testDashArray]; layer.linePattern = [MGLRuntimeStylingHelper testString]; - MGLLineStyleLayer *gLayer = [self.mapView.style layerWithIdentifier:@"layerID"]; + MGLLineStyleLayer *gLayer = (MGLLineStyleLayer *)[self.mapView.style layerWithIdentifier:@"layerID"]; + XCTAssertTrue([gLayer isKindOfClass:[MGLLineStyleLayer class]]); XCTAssert([(NSValue *)gLayer.lineCap isEqualToValue:[MGLRuntimeStylingHelper testEnum:MGLLineStyleLayerLineCapSquare type:@encode(MGLLineStyleLayerLineCap)]], @"%@ is not equal to %@", gLayer.lineCap, [MGLRuntimeStylingHelper testEnum:MGLLineStyleLayerLineCapSquare type:@encode(MGLLineStyleLayerLineCap)]); XCTAssert([(NSValue *)gLayer.lineJoin isEqualToValue:[MGLRuntimeStylingHelper testEnum:MGLLineStyleLayerLineJoinMiter type:@encode(MGLLineStyleLayerLineJoin)]], @"%@ is not equal to %@", gLayer.lineJoin, [MGLRuntimeStylingHelper testEnum:MGLLineStyleLayerLineJoinMiter type:@encode(MGLLineStyleLayerLineJoin)]); XCTAssertEqualObjects(gLayer.lineMiterLimit, [MGLRuntimeStylingHelper testNumber]); diff --git a/platform/darwin/test/MGLRasterStyleLayerTests.m b/platform/darwin/test/MGLRasterStyleLayerTests.m index 7158c9aef..f68f6d328 100644 --- a/platform/darwin/test/MGLRasterStyleLayerTests.m +++ b/platform/darwin/test/MGLRasterStyleLayerTests.m @@ -11,9 +11,9 @@ - (void)testRasterLayer { NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"]; NSURL *url = [NSURL fileURLWithPath:filePath]; - MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"sourceID" URL:url]; -MGLRasterStyleLayer *layer = [[MGLRasterStyleLayer alloc] initWithLayerIdentifier:@"layerID" source:source]; + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"sourceID" URL:url options:nil]; [self.mapView.style addSource:source]; + MGLRasterStyleLayer *layer = [[MGLRasterStyleLayer alloc] initWithIdentifier:@"layerID" source:source]; [self.mapView.style addLayer:layer]; layer.rasterOpacity = [MGLRuntimeStylingHelper testNumber]; @@ -24,7 +24,8 @@ MGLRasterStyleLayer *layer = [[MGLRasterStyleLayer alloc] initWithLayerIdentifie layer.rasterContrast = [MGLRuntimeStylingHelper testNumber]; layer.rasterFadeDuration = [MGLRuntimeStylingHelper testNumber]; - MGLRasterStyleLayer *gLayer = [self.mapView.style layerWithIdentifier:@"layerID"]; + MGLRasterStyleLayer *gLayer = (MGLRasterStyleLayer *)[self.mapView.style layerWithIdentifier:@"layerID"]; + XCTAssertTrue([gLayer isKindOfClass:[MGLRasterStyleLayer class]]); XCTAssertEqualObjects(gLayer.rasterOpacity, [MGLRuntimeStylingHelper testNumber]); XCTAssertEqualObjects(gLayer.rasterHueRotate, [MGLRuntimeStylingHelper testNumber]); XCTAssertEqualObjects(gLayer.rasterBrightnessMin, [MGLRuntimeStylingHelper testNumber]); diff --git a/platform/darwin/test/MGLSymbolStyleLayerTests.m b/platform/darwin/test/MGLSymbolStyleLayerTests.m index 096a49b57..798cfca71 100644 --- a/platform/darwin/test/MGLSymbolStyleLayerTests.m +++ b/platform/darwin/test/MGLSymbolStyleLayerTests.m @@ -11,9 +11,9 @@ - (void)testSymbolLayer { NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"]; NSURL *url = [NSURL fileURLWithPath:filePath]; - MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"sourceID" URL:url]; -MGLSymbolStyleLayer *layer = [[MGLSymbolStyleLayer alloc] initWithLayerIdentifier:@"layerID" source:source]; + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"sourceID" URL:url options:nil]; [self.mapView.style addSource:source]; + MGLSymbolStyleLayer *layer = [[MGLSymbolStyleLayer alloc] initWithIdentifier:@"layerID" source:source]; [self.mapView.style addLayer:layer]; layer.symbolPlacement = [MGLRuntimeStylingHelper testEnum:MGLSymbolStyleLayerSymbolPlacementLine type:@encode(MGLSymbolStyleLayerSymbolPlacement)]; @@ -65,7 +65,8 @@ MGLSymbolStyleLayer *layer = [[MGLSymbolStyleLayer alloc] initWithLayerIdentifie layer.textTranslate = [MGLRuntimeStylingHelper testOffset]; layer.textTranslateAnchor = [MGLRuntimeStylingHelper testEnum:MGLSymbolStyleLayerTextTranslateAnchorViewport type:@encode(MGLSymbolStyleLayerTextTranslateAnchor)]; - MGLSymbolStyleLayer *gLayer = [self.mapView.style layerWithIdentifier:@"layerID"]; + MGLSymbolStyleLayer *gLayer = (MGLSymbolStyleLayer *)[self.mapView.style layerWithIdentifier:@"layerID"]; + XCTAssertTrue([gLayer isKindOfClass:[MGLSymbolStyleLayer class]]); XCTAssert([(NSValue *)gLayer.symbolPlacement isEqualToValue:[MGLRuntimeStylingHelper testEnum:MGLSymbolStyleLayerSymbolPlacementLine type:@encode(MGLSymbolStyleLayerSymbolPlacement)]], @"%@ is not equal to %@", gLayer.symbolPlacement, [MGLRuntimeStylingHelper testEnum:MGLSymbolStyleLayerSymbolPlacementLine type:@encode(MGLSymbolStyleLayerSymbolPlacement)]); XCTAssertEqualObjects(gLayer.symbolSpacing, [MGLRuntimeStylingHelper testNumber]); XCTAssertEqualObjects(gLayer.symbolAvoidEdges, [MGLRuntimeStylingHelper testBool]); diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m index feebb59a2..5a3c56af6 100644 --- a/platform/ios/app/MBXViewController.m +++ b/platform/ios/app/MBXViewController.m @@ -677,10 +677,10 @@ typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) { - (void)styleRasterLayer { NSURL *rasterURL = [NSURL URLWithString:@"mapbox://mapbox.satellite"]; - MGLRasterSource *rasterSource = [[MGLRasterSource alloc] initWithSourceIdentifier:@"my-raster-source" URL:rasterURL tileSize:512]; + MGLRasterSource *rasterSource = [[MGLRasterSource alloc] initWithIdentifier:@"my-raster-source" URL:rasterURL tileSize:512]; [self.mapView.style addSource:rasterSource]; - MGLRasterStyleLayer *rasterLayer = [[MGLRasterStyleLayer alloc] initWithLayerIdentifier:@"my-raster-layer" source:rasterSource]; + MGLRasterStyleLayer *rasterLayer = [[MGLRasterStyleLayer alloc] initWithIdentifier:@"my-raster-layer" source:rasterSource]; MGLStyleAttributeFunction *opacityFunction = [[MGLStyleAttributeFunction alloc] init]; opacityFunction.stops = @{@20.0f: @1.0f, @5.0f: @0.0f}; @@ -692,10 +692,10 @@ typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) { { NSString *filePath = [[NSBundle bundleForClass:self.class] pathForResource:@"amsterdam" ofType:@"geojson"]; NSURL *geoJSONURL = [NSURL fileURLWithPath:filePath]; - MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"ams" URL:geoJSONURL]; + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"ams" URL:geoJSONURL options:nil]; [self.mapView.style addSource:source]; - MGLFillStyleLayer *fillLayer = [[MGLFillStyleLayer alloc] initWithLayerIdentifier:@"test" source:source]; + MGLFillStyleLayer *fillLayer = [[MGLFillStyleLayer alloc] initWithIdentifier:@"test" source:source]; fillLayer.fillColor = [UIColor purpleColor]; [self.mapView.style addLayer:fillLayer]; } diff --git a/platform/ios/jazzy.yml b/platform/ios/jazzy.yml index b2b3d3878..c41dddc89 100644 --- a/platform/ios/jazzy.yml +++ b/platform/ios/jazzy.yml @@ -53,14 +53,15 @@ custom_categories: - MGLShapeCollectionFeature - name: Style Layers children: - - MGLStyleLayer - - MGLBaseStyleLayer - MGLBackgroundStyleLayer - MGLCircleStyleLayer - MGLFillStyleLayer + - MGLForegroundStyleLayer - MGLLineStyleLayer - MGLRasterStyleLayer + - MGLStyleLayer - MGLSymbolStyleLayer + - MGLVectorStyleLayer - name: Data Sources children: - MGLSource diff --git a/platform/ios/test/MGLGeoJSONSourceTests.mm b/platform/ios/test/MGLGeoJSONSourceTests.mm index 76353dc76..eed211221 100644 --- a/platform/ios/test/MGLGeoJSONSourceTests.mm +++ b/platform/ios/test/MGLGeoJSONSourceTests.mm @@ -20,7 +20,7 @@ MGLGeoJSONMaximumZoomLevelOption: @99, MGLGeoJSONBufferOption: @1976, MGLGeoJSONToleranceOption: @0.42}; - MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"source-id" URL:url options:options]; + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"source-id" URL:url options:options]; auto mbglOptions = [source geoJSONOptions]; XCTAssertTrue(mbglOptions.cluster); @@ -32,7 +32,7 @@ // when the supplied option cluster value is not of the correct type options = @{MGLGeoJSONClusterOption: @"number 1"}; - source = [[MGLGeoJSONSource alloc] initWithSourceIdentifier:@"source-id" URL:url options:options]; + source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"source-id" URL:url options:options]; XCTAssertThrows([source geoJSONOptions]); } diff --git a/platform/macos/app/MapDocument.m b/platform/macos/app/MapDocument.m index 42398ff0a..9c3abc1a1 100644 --- a/platform/macos/app/MapDocument.m +++ b/platform/macos/app/MapDocument.m @@ -504,10 +504,10 @@ NS_ARRAY_OF(id ) *MBXFlattenedShapes(NS_ARRAY_OF(id