diff options
author | ituaijagbone <i.ijagbone@gmail.com> | 2016-09-29 16:44:10 -0700 |
---|---|---|
committer | Jesse Bounds <jesse@rebounds.net> | 2016-10-14 14:59:28 -0700 |
commit | f558f6bc4a12fc621589c0b0f50d42c18e00453c (patch) | |
tree | c43e9b4722d72b8220e936719cbe14a8f9fa5958 /platform/darwin/src/MGLFeature.mm | |
parent | 242c7f8fe62eb863cf300855aee2f04022311288 (diff) | |
download | qtlocation-mapboxgl-f558f6bc4a12fc621589c0b0f50d42c18e00453c.tar.gz |
[ios, macos] Add feature initializer
This adds a features initializer to MGLGeoJSONSource. The initializer
takes shapes and converts them to JSON representation before passing
to core. This also adds methods to the MGLShape concrete subclasses
so that they can represent themselves in NSDictionary form suitable
for transforming to JSON (GeoJSON).
Diffstat (limited to 'platform/darwin/src/MGLFeature.mm')
-rw-r--r-- | platform/darwin/src/MGLFeature.mm | 115 |
1 files changed, 114 insertions, 1 deletions
diff --git a/platform/darwin/src/MGLFeature.mm b/platform/darwin/src/MGLFeature.mm index 6d4f76a71d..142a5e7ae6 100644 --- a/platform/darwin/src/MGLFeature.mm +++ b/platform/darwin/src/MGLFeature.mm @@ -6,7 +6,8 @@ #import "MGLValueEvaluator.h" #import "MGLMultiPoint_Private.h" - +#import "MGLPolyline+MGLAdditions.h" +#import "MGLPolygon+MGLAdditions.h" #import <mbgl/util/geometry.hpp> @protocol MGLFeaturePrivate <MGLFeature> @@ -28,6 +29,17 @@ return self.attributes[key]; } +- (NS_DICTIONARY_OF(NSString *, id) *)featureDictionary { + + return @{@"type":@"Feature", + @"properties":(self.attributes) ? self.attributes : @{}, + @"geometry":@{ + @"type":@"Point", + @"coordinates":@[@(self.coordinate.longitude), @(self.coordinate.latitude)] + } + }; +} + @end @interface MGLPolylineFeature () <MGLFeaturePrivate> @@ -37,11 +49,22 @@ @synthesize identifier; @synthesize attributes; +// - (id)attributeForKey:(NSString *)key { return self.attributes[key]; } +- (NS_DICTIONARY_OF(NSString *, id) *)featureDictionary { + return @{@"type":@"Feature", + @"properties":(self.attributes) ? self.attributes : @{}, + @"geometry":@{ + @"type":@"LineString", + @"coordinates":self.mgl_coordinates + } + };; +} + @end @interface MGLPolygonFeature () <MGLFeaturePrivate> @@ -56,6 +79,16 @@ return self.attributes[key]; } +- (NS_DICTIONARY_OF(NSString *, id) *)featureDictionary { + return @{@"type":@"Feature", + @"properties":(self.attributes) ? self.attributes : @{}, + @"geometry":@{ + @"type":@"Polygon", + @"coordinates":self.mgl_coordinates + } + }; +} + @end @interface MGLMultiPointFeature () <MGLFeaturePrivate> @@ -70,6 +103,23 @@ return self.attributes[key]; } +- (NS_DICTIONARY_OF(NSString *, id) *)featureDictionary { + NSMutableArray *coordinates = [NSMutableArray array]; + + for (NSUInteger index = 0; index < self.pointCount; index++) { + CLLocationCoordinate2D coordinate = self.coordinates[index]; + [coordinates addObject:@[@(coordinate.longitude), @(coordinate.latitude)]]; + } + + return @{@"type":@"Feature", + @"properties":(self.attributes) ? self.attributes : @{}, + @"geometry":@{ + @"type":@"Multipoint", + @"coordinates":coordinates + } + }; +} + @end @interface MGLMultiPolylineFeature () <MGLFeaturePrivate> @@ -84,6 +134,21 @@ return self.attributes[key]; } +- (NS_DICTIONARY_OF(NSString *, id) *)featureDictionary { + NSMutableArray *coordinates = [NSMutableArray array]; + for (MGLPolylineFeature *feature in self.polylines) { + [coordinates addObject:feature.mgl_coordinates]; + } + + return @{@"type":@"Feature", + @"properties":(self.attributes) ? self.attributes : @{}, + @"geometry":@{ + @"type":@"MultiLineString", + @"coordinates":coordinates + } + }; +} + @end @interface MGLMultiPolygonFeature () <MGLFeaturePrivate> @@ -98,6 +163,21 @@ return self.attributes[key]; } +- (NS_DICTIONARY_OF(NSString *, id) *)featureDictionary { + NSMutableArray *coordinates = [NSMutableArray array]; + for (MGLPolygonFeature *feature in self.polygons) { + [coordinates addObject:feature.mgl_coordinates]; + } + + return @{@"type":@"Feature", + @"properties":(self.attributes) ? self.attributes : @{}, + @"geometry":@{ + @"type":@"MultiPolygon", + @"coordinates":coordinates + } + }; +} + @end @interface MGLShapeCollectionFeature () <MGLFeaturePrivate> @@ -108,10 +188,43 @@ @synthesize identifier; @synthesize attributes; +@dynamic shapes; + ++ (instancetype)shapeCollectionWithShapes:(NS_ARRAY_OF(MGLShape<MGLFeature> *) *)shapes { + return [super shapeCollectionWithShapes:shapes]; +} + - (id)attributeForKey:(NSString *)key { return self.attributes[key]; } +- (NS_DICTIONARY_OF(NSString *, id) *)featureDictionary { + + return @{@"type":@"Feature", + @"properties":(self.attributes) ? self.attributes : @{}, + @"geometry":@{ + @"type":@"GeometryCollection", + @"geometries":[self geometryCollection:self.shapes] + } + }; +} + +- (NS_MUTABLE_ARRAY_OF(NS_DICTIONARY_OF(NSString *, id) *) *)geometryCollection:(NS_ARRAY_OF(MGLShape<MGLFeature> *) *)shapes { + NSMutableArray *geometries = [NSMutableArray array]; + + for (MGLShape<MGLFeature> *shape in shapes) { + if ([shape isKindOfClass:[MGLShapeCollectionFeature class]]) { + [geometries addObject:@{@"type":@"GeometryCollection", + @"geometries":[self geometryCollection:((MGLShapeCollectionFeature *)shape).shapes]}]; + } else { + NSDictionary *geometry = shape.featureDictionary[@"geometry"]; + [geometries addObject:@{@"type":geometry[@"type"], + @"coordinates":geometry[@"coordinates"] }]; + } + } + + return geometries; +} @end |