diff options
author | Fredrik Karlsson <bjorn.fredrik.karlsson@gmail.com> | 2016-11-22 15:54:38 +0100 |
---|---|---|
committer | Fredrik Karlsson <bjorn.fredrik.karlsson@gmail.com> | 2016-12-06 13:59:56 +0100 |
commit | dc2dc31c0bdd4f0b27412125032bfa694ade26b9 (patch) | |
tree | a573ec5e83342e1a4dcd9f335a8fbc2ee0db4cfd /platform/darwin | |
parent | 9456bec1721f5cd69e7813c68b829db4c7ff9660 (diff) | |
download | qtlocation-mapboxgl-dc2dc31c0bdd4f0b27412125032bfa694ade26b9.tar.gz |
[ios, macos] MGLGeoJSONSource can now be initialized with any shape
Diffstat (limited to 'platform/darwin')
-rw-r--r-- | platform/darwin/src/MGLFeature.mm | 75 | ||||
-rw-r--r-- | platform/darwin/src/MGLFeature_Private.h | 12 | ||||
-rw-r--r-- | platform/darwin/src/MGLGeoJSONSource.h | 13 | ||||
-rw-r--r-- | platform/darwin/src/MGLGeoJSONSource.mm | 43 | ||||
-rw-r--r-- | platform/darwin/src/MGLPointAnnotation.mm | 4 | ||||
-rw-r--r-- | platform/darwin/src/MGLPointCollection.mm | 4 | ||||
-rw-r--r-- | platform/darwin/src/MGLPolygon.mm | 21 | ||||
-rw-r--r-- | platform/darwin/src/MGLPolyline.mm | 8 | ||||
-rw-r--r-- | platform/darwin/src/MGLShapeCollection.mm (renamed from platform/darwin/src/MGLShapeCollection.m) | 13 | ||||
-rw-r--r-- | platform/darwin/src/MGLShape_Private.h | 6 | ||||
-rw-r--r-- | platform/darwin/test/MGLGeoJSONSourceTests.mm | 82 | ||||
-rw-r--r-- | platform/darwin/test/MGLStyleTests.mm | 4 | ||||
-rw-r--r-- | platform/darwin/test/MGLStyleValueTests.swift | 4 |
13 files changed, 176 insertions, 113 deletions
diff --git a/platform/darwin/src/MGLFeature.mm b/platform/darwin/src/MGLFeature.mm index 5483433710..c45b0bbda1 100644 --- a/platform/darwin/src/MGLFeature.mm +++ b/platform/darwin/src/MGLFeature.mm @@ -14,6 +14,7 @@ #import "NSExpression+MGLAdditions.h" #import <mbgl/util/geometry.hpp> +#import <mbgl/style/conversion/geojson.hpp> #import <mapbox/geometry/feature.hpp> @interface MGLPointFeature () <MGLFeaturePrivate> @@ -33,7 +34,7 @@ } - (mbgl::Feature)mbglFeature { - return mbglFeature([self featureObject], identifier, self.attributes); + return mbglFeature({[self geometryObject]}, identifier, self.attributes); } @end @@ -55,7 +56,7 @@ } - (mbgl::Feature)mbglFeature { - return mbglFeature([self featureObject], identifier, self.attributes); + return mbglFeature({[self geometryObject]}, identifier, self.attributes); } @end @@ -77,7 +78,7 @@ } - (mbgl::Feature)mbglFeature { - return mbglFeature([self featureObject], identifier, self.attributes); + return mbglFeature({[self geometryObject]}, identifier, self.attributes); } @end @@ -99,7 +100,7 @@ } - (mbgl::Feature)mbglFeature { - return mbglFeature([self featureObject], identifier, self.attributes); + return mbglFeature({[self geometryObject]}, identifier, self.attributes); } @end @@ -121,7 +122,7 @@ } - (mbgl::Feature)mbglFeature { - return mbglFeature([self featureObject], identifier, self.attributes); + return mbglFeature({[self geometryObject]}, identifier, self.attributes); } @end @@ -143,7 +144,7 @@ } - (mbgl::Feature)mbglFeature { - return mbglFeature([self featureObject], identifier, self.attributes); + return mbglFeature({[self geometryObject]}, identifier, self.attributes); } @end @@ -266,27 +267,61 @@ private: } }; +template <typename T> +class GeoJSONEvaluator { +public: + MGLShape <MGLFeaturePrivate> * operator()(const mbgl::Geometry<T> &geometry) const { + GeometryEvaluator<T> evaluator; + MGLShape <MGLFeaturePrivate> *shape = mapbox::geometry::geometry<T>::visit(geometry, evaluator); + return shape; + } + + MGLShape <MGLFeaturePrivate> * operator()(const mbgl::Feature &feature) const { + GeometryEvaluator<T> evaluator; + MGLShape <MGLFeaturePrivate> *shape = mapbox::geometry::geometry<T>::visit(feature.geometry, evaluator); + return shape; + } + + MGLShape <MGLFeaturePrivate> * operator()(const mbgl::FeatureCollection &collection) const { + NSMutableArray *shapes = [NSMutableArray arrayWithCapacity:collection.size()]; + for (const auto &feature : collection) { + [shapes addObject:MGLFeatureFromMBGLFeature(feature)]; + } + return [MGLShapeCollection<MGLFeaturePrivate> shapeCollectionWithShapes:shapes]; + } +}; + NS_ARRAY_OF(MGLShape <MGLFeature> *) *MGLFeaturesFromMBGLFeatures(const std::vector<mbgl::Feature> &features) { NSMutableArray *shapes = [NSMutableArray arrayWithCapacity:features.size()]; for (const auto &feature : features) { - NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:feature.properties.size()]; - for (auto &pair : feature.properties) { - auto &value = pair.second; - ValueEvaluator evaluator; - attributes[@(pair.first.c_str())] = mbgl::Value::visit(value, evaluator); - } - - GeometryEvaluator<double> evaluator; - MGLShape <MGLFeaturePrivate> *shape = mapbox::geometry::geometry<double>::visit(feature.geometry, evaluator); - if (feature.id) { - shape.identifier = mbgl::FeatureIdentifier::visit(*feature.id, ValueEvaluator()); - } - shape.attributes = attributes; - [shapes addObject:shape]; + [shapes addObject:MGLFeatureFromMBGLFeature(feature)]; } return shapes; } +id <MGLFeature> MGLFeatureFromMBGLFeature(const mbgl::Feature &feature) { + NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:feature.properties.size()]; + for (auto &pair : feature.properties) { + auto &value = pair.second; + ValueEvaluator evaluator; + attributes[@(pair.first.c_str())] = mbgl::Value::visit(value, evaluator); + } + GeometryEvaluator<double> evaluator; + MGLShape <MGLFeaturePrivate> *shape = mapbox::geometry::geometry<double>::visit(feature.geometry, evaluator); + if (feature.id) { + shape.identifier = mbgl::FeatureIdentifier::visit(*feature.id, ValueEvaluator()); + } + shape.attributes = attributes; + + return shape; +} + +MGLShape* MGLShapeFromGeoJSON(const mapbox::geojson::geojson &geojson) { + GeoJSONEvaluator<double> evaluator; + MGLShape *shape = mapbox::geojson::geojson::visit(geojson, evaluator); + return shape; +} + mbgl::Feature mbglFeature(mbgl::Feature feature, id identifier, NSDictionary *attributes) { if (identifier) { diff --git a/platform/darwin/src/MGLFeature_Private.h b/platform/darwin/src/MGLFeature_Private.h index e6858c7c11..a3c7432f4a 100644 --- a/platform/darwin/src/MGLFeature_Private.h +++ b/platform/darwin/src/MGLFeature_Private.h @@ -3,6 +3,7 @@ #import <mbgl/util/geo.hpp> #import <mbgl/util/feature.hpp> +#import <mbgl/style/conversion/geojson.hpp> NS_ASSUME_NONNULL_BEGIN @@ -13,6 +14,17 @@ NS_ASSUME_NONNULL_BEGIN NS_ARRAY_OF(MGLShape <MGLFeature> *) *MGLFeaturesFromMBGLFeatures(const std::vector<mbgl::Feature> &features); /** + Returns an `MGLFeature` object converted from the given mbgl::Feature + */ +id <MGLFeature> MGLFeatureFromMBGLFeature(const mbgl::Feature &feature); + +/** + Returns an `MGLShape` representing the given geojson. The shape can be + a feature, a collection of features, or a geometry. + */ +MGLShape* MGLShapeFromGeoJSON(const mapbox::geojson::geojson &geojson); + +/** Takes an `mbgl::Feature` object, an identifer, and attributes dictionary and returns the feature object with converted `mbgl::FeatureIdentifier` and `mbgl::PropertyMap` properties. diff --git a/platform/darwin/src/MGLGeoJSONSource.h b/platform/darwin/src/MGLGeoJSONSource.h index 30232c6211..2e86cf5b4f 100644 --- a/platform/darwin/src/MGLGeoJSONSource.h +++ b/platform/darwin/src/MGLGeoJSONSource.h @@ -1,6 +1,7 @@ #import "MGLSource.h" #import "MGLTypes.h" +#import "MGLShape.h" NS_ASSUME_NONNULL_BEGIN @@ -93,28 +94,29 @@ extern const MGLGeoJSONSourceOption MGLGeoJSONSourceOptionSimplificationToleranc - (instancetype)initWithIdentifier:(NSString *)identifier URL:(NSURL *)url options:(nullable NS_DICTIONARY_OF(MGLGeoJSONSourceOption, id) *)options NS_DESIGNATED_INITIALIZER; /** - Returns a GeoJSON source with an identifier, features dictionary, and dictionary + Returns a GeoJSON source with an identifier, features dictionary, and dictionary of options for the source according to the <a href="https://www.mapbox.com/mapbox-gl-style-spec/#sources-geojson">style specification</a>. @param identifier A string that uniquely identifies the source. - @param features An array of features that conform to the `MGLFeature` protocol. + @param shape A concrete subclass of `MGLShape` @param options An `NSDictionary` of options for this source. @return An initialized GeoJSON source. */ -- (instancetype)initWithIdentifier:(NSString *)identifier features:(NSArray<id<MGLFeature>> *)features options:(nullable NS_DICTIONARY_OF(MGLGeoJSONSourceOption, id) *)options NS_DESIGNATED_INITIALIZER; +- (instancetype)initWithIdentifier:(NSString *)identifier shape:(nullable MGLShape *)shape options:(nullable NS_DICTIONARY_OF(MGLGeoJSONSourceOption, id) *)options NS_DESIGNATED_INITIALIZER; #pragma mark Accessing a Source’s Content /** - The contents of the source. + The contents of the source. A shape can represent a GeoJSON geometry, a feature, + or a collection of features. If the receiver was initialized using `-initWithIdentifier:URL:options:`, this property is set to `nil`. This property is unavailable until the receiver is passed into `-[MGLStyle addSource]`. */ -@property (nonatomic, nullable) NS_ARRAY_OF(id <MGLFeature>) *features; +@property (nonatomic, nullable) MGLShape *shape; /** A GeoJSON representation of the contents of the source. @@ -138,7 +140,6 @@ extern const MGLGeoJSONSourceOption MGLGeoJSONSourceOptionSimplificationToleranc */ @property (nonatomic, nullable) NSURL *URL; - @end NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLGeoJSONSource.mm b/platform/darwin/src/MGLGeoJSONSource.mm index 8b37ba47cd..7fd89ddc74 100644 --- a/platform/darwin/src/MGLGeoJSONSource.mm +++ b/platform/darwin/src/MGLGeoJSONSource.mm @@ -3,6 +3,7 @@ #import "MGLMapView_Private.h" #import "MGLSource_Private.h" #import "MGLFeature_Private.h" +#import "MGLShape_Private.h" #import "NSURL+MGLAdditions.h" @@ -49,13 +50,13 @@ const MGLGeoJSONSourceOption MGLGeoJSONSourceOptionSimplificationTolerance = @"M return self; } -- (instancetype)initWithIdentifier:(NSString *)identifier features:(NSArray<id<MGLFeature>> *)features options:(NS_DICTIONARY_OF(NSString *,id) *)options { +- (instancetype)initWithIdentifier:(NSString *)identifier shape:(nullable MGLShape *)shape options:(NSDictionary<MGLGeoJSONSourceOption,id> *)options +{ if (self = [super initWithIdentifier:identifier]) { - _features = features; + _shape = shape; _options = options; [self commonInit]; } - return self; } @@ -85,21 +86,15 @@ const MGLGeoJSONSourceOption MGLGeoJSONSourceOptionSimplificationTolerance = @"M if (self.URL) { NSURL *url = self.URL.mgl_URLByStandardizingScheme; source->setURL(url.absoluteString.UTF8String); - _features = nil; + _shape = nil; } else if (self.geoJSONData) { NSString *string = [[NSString alloc] initWithData:self.geoJSONData encoding:NSUTF8StringEncoding]; - const auto geojson = mapbox::geojson::parse(string.UTF8String).get<mapbox::geojson::feature_collection>(); + const auto geojson = mapbox::geojson::parse(string.UTF8String); source->setGeoJSON(geojson); - _features = MGLFeaturesFromMBGLFeatures(geojson); + _shape = MGLShapeFromGeoJSON(geojson); } else { - mbgl::FeatureCollection featureCollection; - featureCollection.reserve(self.features.count); - for (id <MGLFeaturePrivate> feature in self.features) { - featureCollection.push_back([feature mbglFeature]); - } - const auto geojson = mbgl::GeoJSON{featureCollection}; + const auto geojson = mbgl::GeoJSON{self.shape.geometryObject}; source->setGeoJSON(geojson); - _features = MGLFeaturesFromMBGLFeatures(featureCollection); } _pendingSource = std::move(source); @@ -161,10 +156,10 @@ const MGLGeoJSONSourceOption MGLGeoJSONSourceOptionSimplificationTolerance = @"M } NSString *string = [[NSString alloc] initWithData:_geoJSONData encoding:NSUTF8StringEncoding]; - const auto geojson = mapbox::geojson::parse(string.UTF8String).get<mapbox::geojson::feature_collection>(); + const auto geojson = mapbox::geojson::parse(string.UTF8String); self.rawSource->setGeoJSON(geojson); - _features = MGLFeaturesFromMBGLFeatures(geojson); + _shape = MGLShapeFromGeoJSON(geojson); } - (void)setURL:(NSURL *)URL @@ -180,28 +175,24 @@ const MGLGeoJSONSourceOption MGLGeoJSONSourceOptionSimplificationTolerance = @"M self.rawSource->setURL(url.absoluteString.UTF8String); } -- (void)setFeatures:(NSArray *)features + +- (void)setShape:(MGLShape *)shape { if (self.rawSource == NULL) { [self commonInit]; } - - mbgl::FeatureCollection featureCollection; - featureCollection.reserve(features.count); - for (id <MGLFeaturePrivate> feature in features) { - featureCollection.push_back([feature mbglFeature]); - } - const auto geojson = mbgl::GeoJSON{featureCollection}; + + const auto geojson = mbgl::GeoJSON{shape.geometryObject}; self.rawSource->setGeoJSON(geojson); - _features = MGLFeaturesFromMBGLFeatures(featureCollection); + _shape = shape; } - (NSString *)description { - return [NSString stringWithFormat:@"<%@: %p; identifier = %@; URL = %@; geoJSONData = %@; features = %@>", - NSStringFromClass([self class]), (void *)self, self.identifier, self.URL, self.geoJSONData, self.features]; + return [NSString stringWithFormat:@"<%@: %p; identifier = %@; URL = %@; geoJSONData = %@; shape = %@>", + NSStringFromClass([self class]), (void *)self, self.identifier, self.URL, self.geoJSONData, self.shape]; } @end diff --git a/platform/darwin/src/MGLPointAnnotation.mm b/platform/darwin/src/MGLPointAnnotation.mm index ce8e4a2355..d2e87f07d1 100644 --- a/platform/darwin/src/MGLPointAnnotation.mm +++ b/platform/darwin/src/MGLPointAnnotation.mm @@ -24,10 +24,10 @@ @"coordinates": @[@(self.coordinate.longitude), @(self.coordinate.latitude)]}; } -- (mbgl::Feature)featureObject +- (mbgl::Geometry<double>)geometryObject { mbgl::Point<double> point = { self.coordinate.longitude, self.coordinate.latitude }; - return mbgl::Feature {point}; + return point; } @end diff --git a/platform/darwin/src/MGLPointCollection.mm b/platform/darwin/src/MGLPointCollection.mm index ab4a9c978e..f2bde38bc7 100644 --- a/platform/darwin/src/MGLPointCollection.mm +++ b/platform/darwin/src/MGLPointCollection.mm @@ -71,7 +71,7 @@ NS_ASSUME_NONNULL_BEGIN return MGLLatLngBoundsFromCoordinateBounds(_bounds).intersects(MGLLatLngBoundsFromCoordinateBounds(overlayBounds)); } -- (mbgl::Feature)featureObject +- (mbgl::Geometry<double>)geometryObject { mbgl::MultiPoint<double> multiPoint; multiPoint.reserve(self.pointCount); @@ -79,7 +79,7 @@ NS_ASSUME_NONNULL_BEGIN { multiPoint.push_back(mbgl::Point<double>(self.coordinates[i].longitude, self.coordinates[i].latitude)); } - return mbgl::Feature {multiPoint}; + return multiPoint; } - (NSDictionary *)geoJSONDictionary diff --git a/platform/darwin/src/MGLPolygon.mm b/platform/darwin/src/MGLPolygon.mm index 7562db6e61..f4d999b98b 100644 --- a/platform/darwin/src/MGLPolygon.mm +++ b/platform/darwin/src/MGLPolygon.mm @@ -38,23 +38,22 @@ return result; } -- (mbgl::Feature)featureObject { +- (mbgl::Polygon<double>)polygon { mbgl::Polygon<double> geometry; geometry.push_back(self.ring); for (MGLPolygon *polygon in self.interiorPolygons) { geometry.push_back(polygon.ring); } - return mbgl::Feature{geometry}; + return geometry; } -- (mbgl::Annotation)annotationObjectWithDelegate:(id <MGLMultiPointDelegate>)delegate { - mbgl::Polygon<double> geometry; - geometry.push_back(self.ring); - for (MGLPolygon *polygon in self.interiorPolygons) { - geometry.push_back(polygon.ring); - } +- (mbgl::Geometry<double>)geometryObject { + return [self polygon]; +} - mbgl::FillAnnotation annotation { geometry }; +- (mbgl::Annotation)annotationObjectWithDelegate:(id <MGLMultiPointDelegate>)delegate { + + mbgl::FillAnnotation annotation { [self polygon] }; annotation.opacity = { static_cast<float>([delegate alphaForShapeAnnotation:self]) }; annotation.outlineColor = { [delegate strokeColorForShapeAnnotation:self] }; annotation.color = { [delegate fillColorForPolygonAnnotation:self] }; @@ -103,7 +102,7 @@ return MGLLatLngBoundsFromCoordinateBounds(_overlayBounds).intersects(MGLLatLngBoundsFromCoordinateBounds(overlayBounds)); } -- (mbgl::Feature)featureObject { +- (mbgl::Geometry<double>)geometryObject { mbgl::MultiPolygon<double> multiPolygon; multiPolygon.reserve(self.polygons.count); for (MGLPolygon *polygon in self.polygons) { @@ -114,7 +113,7 @@ } multiPolygon.push_back(geometry); } - return mbgl::Feature {multiPolygon}; + return multiPolygon; } - (NSDictionary *)geoJSONDictionary { diff --git a/platform/darwin/src/MGLPolyline.mm b/platform/darwin/src/MGLPolyline.mm index 1801dfd44e..5b6346f46a 100644 --- a/platform/darwin/src/MGLPolyline.mm +++ b/platform/darwin/src/MGLPolyline.mm @@ -37,8 +37,8 @@ return annotation; } -- (mbgl::Feature)featureObject { - return mbgl::Feature {[self lineString]}; +- (mbgl::Geometry<double>)geometryObject { + return [self lineString]; } - (NSDictionary *)geoJSONDictionary { @@ -82,13 +82,13 @@ return MGLLatLngBoundsFromCoordinateBounds(_overlayBounds).intersects(MGLLatLngBoundsFromCoordinateBounds(overlayBounds)); } -- (mbgl::Feature)featureObject { +- (mbgl::Geometry<double>)geometryObject { mbgl::MultiLineString<double> multiLineString; multiLineString.reserve(self.polylines.count); for (MGLPolyline *polyline in self.polylines) { multiLineString.push_back([polyline lineString]); } - return mbgl::Feature {multiLineString}; + return multiLineString; } - (NSDictionary *)geoJSONDictionary { diff --git a/platform/darwin/src/MGLShapeCollection.m b/platform/darwin/src/MGLShapeCollection.mm index 0f011bfd20..e317a443fe 100644 --- a/platform/darwin/src/MGLShapeCollection.m +++ b/platform/darwin/src/MGLShapeCollection.mm @@ -1,5 +1,9 @@ #import "MGLShapeCollection.h" +#import "MGLShape_Private.h" + +#import <mbgl/style/conversion/geojson.hpp> + @implementation MGLShapeCollection + (instancetype)shapeCollectionWithShapes:(NS_ARRAY_OF(MGLShape *) *)shapes { @@ -32,4 +36,13 @@ return [geometries copy]; } +- (mbgl::Geometry<double>)geometryObject { + mapbox::geojson::geometry_collection collection; + collection.reserve(self.shapes.count); + for (MGLShape *shape in self.shapes) { + collection.push_back([shape geometryObject]); + } + return collection; +} + @end diff --git a/platform/darwin/src/MGLShape_Private.h b/platform/darwin/src/MGLShape_Private.h index a8ee12c207..2c7c4700a3 100644 --- a/platform/darwin/src/MGLShape_Private.h +++ b/platform/darwin/src/MGLShape_Private.h @@ -1,13 +1,13 @@ #import "MGLShape.h" -#import <mbgl/util/feature.hpp> +#import <mbgl/util/geometry.hpp> @interface MGLShape (Private) /** - Returns an `mbgl::Feature` representation of the `MGLShape`. + Returns an `mbgl::Geometry<double>` representation of the `MGLShape`. */ -- (mbgl::Feature)featureObject; +- (mbgl::Geometry<double>)geometryObject; /** Returns a dictionary with the GeoJSON geometry member object. diff --git a/platform/darwin/test/MGLGeoJSONSourceTests.mm b/platform/darwin/test/MGLGeoJSONSourceTests.mm index be8bb143ce..1b6da95a69 100644 --- a/platform/darwin/test/MGLGeoJSONSourceTests.mm +++ b/platform/darwin/test/MGLGeoJSONSourceTests.mm @@ -35,27 +35,40 @@ XCTAssertThrows([[MGLGeoJSONSource alloc] initWithIdentifier:@"source-id" URL:url options:options]); } -- (void)testMGLGeoJSONSourceWithData { +- (void)testNilShape { + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"id" shape:nil options:nil]; + XCTAssertNil(source.shape); +} + +- (void)testMGLGeoJSONSourceWithDataMultipleFeatures { NSString *geoJSON = @"{\"type\": \"FeatureCollection\",\"features\": [{\"type\": \"Feature\",\"properties\": {},\"geometry\": {\"type\": \"LineString\",\"coordinates\": [[-107.75390625,40.329795743702064],[-104.34814453125,37.64903402157866]]}}]}"; NSData *data = [geoJSON dataUsingEncoding:NSUTF8StringEncoding]; MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"source-id" geoJSONData:data options:nil]; - XCTAssertNotNil(source.features); - XCTAssertEqual(source.features.count, 1); - XCTAssertTrue([source.features.firstObject isMemberOfClass:[MGLPolylineFeature class]]); + MGLShapeCollection *collection = source.shape; + XCTAssertNotNil(collection); + XCTAssertEqual(collection.shapes.count, 1); + XCTAssertTrue([collection.shapes.firstObject isMemberOfClass:[MGLPolylineFeature class]]); +} + +- (void)testMGLGeoJSONSourceWithSingleFeature { + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"geojson" + geoJSONData:[@"{\"type\": \"Point\", \"coordinates\": [0, 0]}" dataUsingEncoding:NSUTF8StringEncoding] + options:nil]; + XCTAssertNotNil(source.shape); + XCTAssert([source.shape isKindOfClass:[MGLPointFeature class]]); } - (void)testMGLGeoJSONSourceWithPolylineFeatures { CLLocationCoordinate2D coordinates[] = { CLLocationCoordinate2DMake(0, 0), CLLocationCoordinate2DMake(10, 10)}; MGLPolylineFeature *polylineFeature = [MGLPolylineFeature polylineWithCoordinates:coordinates count:2]; - MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"source-id" features:@[polylineFeature] options:nil]; + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"source-id" shape:polylineFeature options:nil]; - XCTAssertNotNil(source.features); - XCTAssertEqual(source.features.count, 1); - XCTAssertTrue([source.features.firstObject isMemberOfClass:[MGLPolylineFeature class]]); + XCTAssertNotNil(source.shape); + XCTAssertTrue([source.shape isMemberOfClass:[MGLPolylineFeature class]]); } - (void)testMGLGeoJSONSourceWithPolygonFeatures { @@ -88,11 +101,11 @@ @"array-of-array-attribute": arrayOfArrays, @"array-of-dictionary-attribute": arrayOfDictionaries}; - MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"source-id" features:@[polygonFeature] options:nil]; + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"source-id" shape:polygonFeature options:nil]; - XCTAssertNotNil(source.features); - XCTAssertEqual(source.features.count, 1); - MGLPolygonFeature *expectedPolygonFeature = (MGLPolygonFeature *)source.features.firstObject; + XCTAssertNotNil(source.shape); + MGLPolygonFeature *expectedPolygonFeature = (MGLPolygonFeature *)source.shape; + XCTAssertEqualObjects(expectedPolygonFeature.identifier, polygonFeature.identifier); XCTAssertTrue([expectedPolygonFeature isMemberOfClass:[MGLPolygonFeature class]]); XCTAssertEqualObjects(expectedPolygonFeature.identifier, polygonFeature.identifier); XCTAssertEqualObjects(expectedPolygonFeature.attributes[@"name"], stringAttribute); @@ -123,12 +136,10 @@ MGLPolygonFeature *polygonFeature = [MGLPolygonFeature polygonWithCoordinates:coordinates count:5 interiorPolygons:@[polygon]]; - MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"source-id" features:@[polygonFeature] options:nil]; - + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"source-id" shape:polygonFeature options:nil]; - XCTAssertNotNil(source.features); - XCTAssertEqual(source.features.count, 1); - XCTAssertTrue([source.features.firstObject isMemberOfClass:[MGLPolygonFeature class]]); + XCTAssertNotNil(source.shape); + XCTAssertTrue([source.shape isMemberOfClass:[MGLPolygonFeature class]]); } - (void)testMGLGeoJSONSourceWithMultiPolylineFeatures { @@ -138,11 +149,10 @@ MGLPolylineFeature *secondPolylineFeature = [MGLPolylineFeature polylineWithCoordinates:secondCoordinates count:2]; MGLMultiPolylineFeature *multiPolylineFeature = [MGLMultiPolylineFeature multiPolylineWithPolylines:@[firstPolylineFeature, secondPolylineFeature]]; - MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"source-id" features:@[multiPolylineFeature] options:nil]; + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"source-id" shape:multiPolylineFeature options:nil]; - XCTAssertNotNil(source.features); - XCTAssertEqual(source.features.count, 1); - XCTAssertTrue([source.features.firstObject isMemberOfClass:[MGLMultiPolylineFeature class]]); + XCTAssertNotNil(source.shape); + XCTAssertTrue([source.shape isMemberOfClass:[MGLMultiPolylineFeature class]]); } - (void)testMGLGeoJSONSourceWithMultiPolygonFeatures { @@ -167,23 +177,20 @@ MGLMultiPolygonFeature *multiPolygonFeature = [MGLMultiPolygonFeature multiPolygonWithPolygons:@[firstPolygon, secondPolygon]]; - MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"source-id" features:@[multiPolygonFeature] options:nil]; + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"source-id" shape:multiPolygonFeature options:nil]; - XCTAssertNotNil(source.features); - XCTAssertEqual(source.features.count, 1); - XCTAssertTrue([source.features.firstObject isMemberOfClass:[MGLMultiPolygonFeature class]]); - + XCTAssertNotNil(source.shape); + XCTAssertTrue([source.shape isMemberOfClass:[MGLMultiPolygonFeature class]]); } - (void)testMGLGeoJSONSourceWithPointFeature { MGLPointFeature *pointFeature = [MGLPointFeature new]; pointFeature.coordinate = CLLocationCoordinate2DMake(100.2, 0.2); - MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"souce-id" features:@[pointFeature] options:nil]; + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"souce-id" shape:pointFeature options:nil]; - XCTAssertNotNil(source.features); - XCTAssertEqual(source.features.count, 1); - XCTAssertTrue([source.features.firstObject isMemberOfClass:[MGLPointFeature class]]); + XCTAssertNotNil(source.shape); + XCTAssertTrue([source.shape isMemberOfClass:[MGLPointFeature class]]); } - (void)testMGLGeoJSONSourceWithPointCollectionFeature { @@ -194,11 +201,10 @@ CLLocationCoordinate2DMake(100.0, 1.0), CLLocationCoordinate2DMake(100.0, 0.0)}; MGLPointCollectionFeature *pointCollectionFeature = [MGLPointCollectionFeature pointCollectionWithCoordinates:coordinates count:5]; - MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"souce-id" features:@[pointCollectionFeature] options:nil]; + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"souce-id" shape:pointCollectionFeature options:nil]; - XCTAssertNotNil(source.features); - XCTAssertEqual(source.features.count, 1); - XCTAssertTrue([source.features.firstObject isMemberOfClass:[MGLPointCollectionFeature class]]); + XCTAssertNotNil(source.shape); + XCTAssertTrue([source.shape isMemberOfClass:[MGLPointCollectionFeature class]]); } - (void)testMGLGeoJSONSourceWithShapeCollectionFeatures { @@ -236,7 +242,13 @@ MGLShapeCollectionFeature *shapeCollectionFeature_1 = [MGLShapeCollectionFeature shapeCollectionWithShapes:@[polygonFeature, polylineFeature, multiPolygonFeature, multiPolylineFeature, pointCollectionFeature, pointFeature, shapeCollectionFeature]]; - XCTAssertThrowsSpecificNamed([[MGLGeoJSONSource alloc] initWithIdentifier:@"source-id" features:@[shapeCollectionFeature_1] options:nil], NSException, @"Method unavailable"); + + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"source-id" shape:shapeCollectionFeature_1 options:nil]; + + MGLShapeCollectionFeature *shape = source.shape; + + XCTAssertNotNil(shape); + XCTAssert(shape.shapes.count == 7, @"Shape collection should contain 7 shapes"); } @end diff --git a/platform/darwin/test/MGLStyleTests.mm b/platform/darwin/test/MGLStyleTests.mm index 8f0d2502fb..bc1e6078f0 100644 --- a/platform/darwin/test/MGLStyleTests.mm +++ b/platform/darwin/test/MGLStyleTests.mm @@ -121,7 +121,7 @@ } - (void)testAddingSourcesTwice { - MGLGeoJSONSource *geoJSONSource = [[MGLGeoJSONSource alloc] initWithIdentifier:@"geoJSONSource" features:@[] options:nil]; + MGLGeoJSONSource *geoJSONSource = [[MGLGeoJSONSource alloc] initWithIdentifier:@"geoJSONSource" shape:nil options:nil]; [self.style addSource:geoJSONSource]; XCTAssertThrowsSpecificNamed([self.style addSource:geoJSONSource], NSException, @"MGLRedundantSourceException"); @@ -143,7 +143,7 @@ } - (void)testAddingLayersTwice { - MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"geoJSONSource" features:@[] options:nil]; + MGLGeoJSONSource *source = [[MGLGeoJSONSource alloc] initWithIdentifier:@"geoJSONSource" shape:nil options:nil]; MGLBackgroundStyleLayer *backgroundLayer = [[MGLBackgroundStyleLayer alloc] initWithIdentifier:@"backgroundLayer"]; [self.style addLayer:backgroundLayer]; diff --git a/platform/darwin/test/MGLStyleValueTests.swift b/platform/darwin/test/MGLStyleValueTests.swift index f7bf343852..aa9b003236 100644 --- a/platform/darwin/test/MGLStyleValueTests.swift +++ b/platform/darwin/test/MGLStyleValueTests.swift @@ -3,7 +3,7 @@ import Mapbox class MGLStyleValueTests: XCTestCase { func testConstantValues() { - let geoJSONSource = MGLGeoJSONSource(identifier: "test", features: [], options: nil) + let geoJSONSource = MGLGeoJSONSource(identifier: "test", shape: nil, options: nil) let symbolStyleLayer = MGLSymbolStyleLayer(identifier: "test", source: geoJSONSource) // Boolean @@ -20,7 +20,7 @@ class MGLStyleValueTests: XCTestCase { } func testFunctions() { - let geoJSONSource = MGLGeoJSONSource(identifier: "test", features: [], options: nil) + let geoJSONSource = MGLGeoJSONSource(identifier: "test", shape: nil, options: nil) let symbolStyleLayer = MGLSymbolStyleLayer(identifier: "test", source: geoJSONSource) // Boolean |