-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Allow initializing MGLGeoJSONSource from a shape #7145
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the documentation for this property. |
||
|
||
/** | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 @@ - (instancetype)initWithIdentifier:(NSString *)identifier URL:(NSURL *)url optio | |
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 @@ - (void)commonInit | |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't assign features here if we're gonna support plain geometries or a single feature. Would it make sense to rename features to geometries and write a GeoJSON evaluator which overloads featureCollection, feature and geometry? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a |
||
_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 @@ - (void)setGeoJSONData:(NSData *)geoJSONData | |
} | ||
|
||
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 @@ - (void)setURL:(NSURL *)URL | |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does it mean to create a source from no shapes at all? It does work in the current release branch, using an empty array of features, but we should make sure it works when shape is nil.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nil shape works but I can't think of any case where you'd want to do that.