diff options
author | Jesse Bounds <jesse@rebounds.net> | 2016-12-12 10:38:49 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-12 10:38:49 -0800 |
commit | e38b4b590df2f74b47e25df9c44f4152ef1c582c (patch) | |
tree | a1c1579773b8cb70b0a6743c1bcdfb906e8662a3 /platform/darwin | |
parent | 3fed3c10edb2cd015bd733a22c56ace7af900ef8 (diff) | |
download | qtlocation-mapboxgl-e38b4b590df2f74b47e25df9c44f4152ef1c582c.tar.gz |
[ios, macos] Load features into shape source if possible (#7339)
This checks the kind of MGLShape passed into the source and, if it
is a feature, it mbgl feature objects to pass to core. This keeps the
feature id and attributes data intact. If the shape is a
`MGLShapeCollectionFeature` it creates an `mbgl::FeatureCollection`
object (also to keep feature properties). If the shape is not any
sort of feature, it passes along just the geometry.
This also uses the MGLFeatureFromMBGLFeature converter for the case where
GeoJSON data passed in to a source contains a single feature. The
converter has logic to keep the id and attributes properties intact.
Before, these properties were lost because only geometry conversion
was done.
Finally, logic to handle (and associated tests) of nested shape
collection features is removed since that is not supported by
the GeoJSON spec or the core implementation.
[ios] Add test of drawing plain shape to iosapp
Diffstat (limited to 'platform/darwin')
-rw-r--r-- | platform/darwin/src/MGLFeature.mm | 13 | ||||
-rw-r--r-- | platform/darwin/src/MGLShapeCollectionFeature_Private.h | 13 | ||||
-rw-r--r-- | platform/darwin/src/MGLShapeSource.mm | 12 | ||||
-rw-r--r-- | platform/darwin/test/MGLShapeSourceTests.mm | 21 |
4 files changed, 48 insertions, 11 deletions
diff --git a/platform/darwin/src/MGLFeature.mm b/platform/darwin/src/MGLFeature.mm index c45b0bbda1..ba2ea6e605 100644 --- a/platform/darwin/src/MGLFeature.mm +++ b/platform/darwin/src/MGLFeature.mm @@ -1,4 +1,5 @@ #import "MGLFeature_Private.h" +#import "MGLShapeCollectionFeature_Private.h" #import "MGLPointAnnotation.h" #import "MGLPolyline.h" @@ -177,6 +178,15 @@ return mbgl::Feature{geometry}; } +- (mbgl::FeatureCollection)mbglFeatureCollection { + mbgl::FeatureCollection featureCollection; + featureCollection.reserve(self.shapes.count); + for (id <MGLFeaturePrivate> feature in self.shapes) { + featureCollection.push_back([feature mbglFeature]); + } + return featureCollection; +} + @end /** @@ -277,8 +287,7 @@ public: } MGLShape <MGLFeaturePrivate> * operator()(const mbgl::Feature &feature) const { - GeometryEvaluator<T> evaluator; - MGLShape <MGLFeaturePrivate> *shape = mapbox::geometry::geometry<T>::visit(feature.geometry, evaluator); + MGLShape <MGLFeaturePrivate> *shape = (MGLShape <MGLFeaturePrivate> *)MGLFeatureFromMBGLFeature(feature); return shape; } diff --git a/platform/darwin/src/MGLShapeCollectionFeature_Private.h b/platform/darwin/src/MGLShapeCollectionFeature_Private.h new file mode 100644 index 0000000000..4f07a93b35 --- /dev/null +++ b/platform/darwin/src/MGLShapeCollectionFeature_Private.h @@ -0,0 +1,13 @@ +#import "MGLFeature.h" + +#import <mbgl/util/feature.hpp> + +NS_ASSUME_NONNULL_BEGIN + +@interface MGLShapeCollectionFeature () + +- (mbgl::FeatureCollection)mbglFeatureCollection; + +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLShapeSource.mm b/platform/darwin/src/MGLShapeSource.mm index 434e6fcb1b..987306a228 100644 --- a/platform/darwin/src/MGLShapeSource.mm +++ b/platform/darwin/src/MGLShapeSource.mm @@ -3,6 +3,7 @@ #import "MGLMapView_Private.h" #import "MGLSource_Private.h" #import "MGLFeature_Private.h" +#import "MGLShapeCollectionFeature_Private.h" #import "MGLShape_Private.h" #import "NSURL+MGLAdditions.h" @@ -99,8 +100,15 @@ const MGLShapeSourceOption MGLShapeSourceOptionSimplificationTolerance = @"MGLSh source->setGeoJSON(geojson); _shape = MGLShapeFromGeoJSON(geojson); } else { - const auto geojson = mbgl::GeoJSON{self.shape.geometryObject}; - source->setGeoJSON(geojson); + if ([self.shape isKindOfClass:[MGLShapeCollectionFeature class]]) { + MGLShapeCollectionFeature *feature = (MGLShapeCollectionFeature *)self.shape; + source->setGeoJSON(mbgl::GeoJSON{[feature mbglFeatureCollection]}); + } else if ([self.shape conformsToProtocol:@protocol(MGLFeature)]) { + id<MGLFeaturePrivate> feature = (id<MGLFeaturePrivate>)self.shape; + source->setGeoJSON(mbgl::GeoJSON{[feature mbglFeature]}); + } else { + source->setGeoJSON(mbgl::GeoJSON{self.shape.geometryObject}); + } } _pendingSource = std::move(source); diff --git a/platform/darwin/test/MGLShapeSourceTests.mm b/platform/darwin/test/MGLShapeSourceTests.mm index c4273e6e3f..df90ea74d7 100644 --- a/platform/darwin/test/MGLShapeSourceTests.mm +++ b/platform/darwin/test/MGLShapeSourceTests.mm @@ -53,7 +53,7 @@ XCTAssertTrue([collection.shapes.firstObject isMemberOfClass:[MGLPolylineFeature class]]); } -- (void)testMGLShapeSourceWithSingleFeature { +- (void)testMGLShapeSourceWithSingleGeometry { MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"geojson" geoJSONData:[@"{\"type\": \"Point\", \"coordinates\": [0, 0]}" dataUsingEncoding:NSUTF8StringEncoding] options:nil]; @@ -61,6 +61,17 @@ XCTAssert([source.shape isKindOfClass:[MGLPointFeature class]]); } +- (void)testMGLGeoJSONSourceWithSingleFeature { + NSString *geoJSON = @"{\"type\": \"Feature\", \"properties\": {\"color\": \"green\"}, \"geometry\": { \"type\": \"Point\", \"coordinates\": [ -114.06847000122069, 51.050459433092655 ] }}"; + MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"geojson" + geoJSONData:[geoJSON dataUsingEncoding:NSUTF8StringEncoding] + options:nil]; + XCTAssertNotNil(source.shape); + XCTAssert([source.shape isKindOfClass:[MGLPointFeature class]]); + MGLPointFeature *feature = (MGLPointFeature *)source.shape; + XCTAssert([feature.attributes.allKeys containsObject:@"color"]); +} + - (void)testMGLShapeSourceWithPolylineFeatures { CLLocationCoordinate2D coordinates[] = { CLLocationCoordinate2DMake(0, 0), CLLocationCoordinate2DMake(10, 10)}; MGLPolylineFeature *polylineFeature = [MGLPolylineFeature polylineWithCoordinates:coordinates count:2]; @@ -240,15 +251,11 @@ MGLShapeCollectionFeature *shapeCollectionFeature = [MGLShapeCollectionFeature shapeCollectionWithShapes:@[polygonFeature, polylineFeature, multiPolygonFeature, multiPolylineFeature, pointCollectionFeature, pointFeature]]; - MGLShapeCollectionFeature *shapeCollectionFeature_1 = [MGLShapeCollectionFeature shapeCollectionWithShapes:@[polygonFeature, polylineFeature, multiPolygonFeature, multiPolylineFeature, pointCollectionFeature, pointFeature, shapeCollectionFeature]]; - - - MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"source-id" shape:shapeCollectionFeature_1 options:nil]; + MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"source-id" shape:shapeCollectionFeature options:nil]; MGLShapeCollectionFeature *shape = (MGLShapeCollectionFeature *)source.shape; - XCTAssertNotNil(shape); - XCTAssert(shape.shapes.count == 7, @"Shape collection should contain 7 shapes"); + XCTAssert(shape.shapes.count == 6, @"Shape collection should contain 6 shapes"); } @end |