Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions platform/darwin/src/MGLGeoJSONSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ extern NSString * const MGLGeoJSONToleranceOption;
is set to `nil`. This property is unavailable until the receiver is passed into
`-[MGLStyle addSource]`.
*/
@property (nonatomic, readonly, nullable) NS_ARRAY_OF(id <MGLFeature>) *features;
@property (nonatomic, nullable) NS_ARRAY_OF(id <MGLFeature>) *features;

/**
A GeoJSON representation of the contents of the source.
Expand All @@ -123,15 +123,15 @@ extern NSString * const MGLGeoJSONToleranceOption;
This property is unavailable until the receiver is passed
into `-[MGLStyle addSource]`.
*/
@property (nonatomic, readonly, nullable, copy) NSData *geoJSONData;
@property (nonatomic, nullable, copy) NSData *geoJSONData;

/**
The URL to the GeoJSON document that specifies the contents of the source.

If the receiver was initialized using `-initWithIdentifier:geoJSONData:options`, this
property is set to `nil`.
*/
@property (nonatomic, readonly, nullable) NSURL *URL;
@property (nonatomic, nullable) NSURL *URL;


@end
Expand Down
105 changes: 84 additions & 21 deletions platform/darwin/src/MGLGeoJSONSource.mm
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#import "MGLGeoJSONSource.h"
#import "MGLGeoJSONSource_Private.h"

#import "MGLMapView_Private.h"
#import "MGLSource_Private.h"
#import "MGLFeature_Private.h"

#import "NSURL+MGLAdditions.h"

#include <mbgl/style/sources/geojson_source.hpp>


NSString * const MGLGeoJSONClusterOption = @"MGLGeoJSONCluster";
NSString * const MGLGeoJSONClusterRadiusOption = @"MGLGeoJSONClusterRadius";
NSString * const MGLGeoJSONClusterMaximumZoomLevelOption = @"MGLGeoJSONClusterMaximumZoomLevel";
Expand All @@ -17,17 +19,22 @@
@interface MGLGeoJSONSource ()

@property (nonatomic, readwrite) NSDictionary *options;
@property (nonatomic) mbgl::style::GeoJSONSource *rawSource;

@end

@implementation MGLGeoJSONSource
{
std::unique_ptr<mbgl::style::GeoJSONSource> _pendingSource;
}

- (instancetype)initWithIdentifier:(NSString *)identifier geoJSONData:(NSData *)data options:(NS_DICTIONARY_OF(NSString *, id) *)options
{
if (self = [super initWithIdentifier:identifier])
{
_geoJSONData = data;
_options = options;
[self commonInit];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-[MGLSource initWithIdentifier:] should implement a private -commonInit stub so that -[MGLSource initWithIdentifier:] should call it and consolidate all these disparate calls. Each MGLSource subclass can still override the method to do things specific to the particular kind of mbgl::style::Source it’s working with.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be a nice refactor. However, commonInit relies on having properties like URL and geoJSONData and features set before it runs. If MGLSource's initWithIdentifier: calls commonInit before the subclass init can set the requisite properties, then unexpected results will occur. I can imagine some ways to work around this but I think the disparate calls may actually be the best way to go, for now.

}
return self;
}
Expand All @@ -38,6 +45,7 @@ - (instancetype)initWithIdentifier:(NSString *)identifier URL:(NSURL *)url optio
{
_URL = url;
_options = options;
[self commonInit];
}
return self;
}
Expand All @@ -46,11 +54,45 @@ - (instancetype)initWithIdentifier:(NSString *)identifier features:(NSArray<id<M
if (self = [super initWithIdentifier:identifier]) {
_features = features;
_options = options;
[self commonInit];
}

return self;
}

- (void)addToMapView:(MGLMapView *)mapView
{
mapView.mbglMap->addSource(std::move(_pendingSource));
}

- (void)commonInit
{
auto source = std::make_unique<mbgl::style::GeoJSONSource>(self.identifier.UTF8String, self.geoJSONOptions);

if (self.URL) {
NSURL *url = self.URL.mgl_URLByStandardizingScheme;
source->setURL(url.absoluteString.UTF8String);
_features = 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>();
source->setGeoJSON(geojson);
_features = MGLFeaturesFromMBGLFeatures(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};
source->setGeoJSON(geojson);
_features = MGLFeaturesFromMBGLFeatures(featureCollection);
}

_pendingSource = std::move(source);
self.rawSource = _pendingSource.get();
}

- (mbgl::style::GeoJSONOptions)geoJSONOptions
{
auto mbglOptions = mbgl::style::GeoJSONOptions();
Expand Down Expand Up @@ -102,30 +144,51 @@ - (void)validateValue:(id)value
}
}

- (std::unique_ptr<mbgl::style::Source>)mbglSource
- (void)setGeoJSONData:(NSData *)geoJSONData
{
auto source = std::make_unique<mbgl::style::GeoJSONSource>(self.identifier.UTF8String, self.geoJSONOptions);
_geoJSONData = geoJSONData;

if (self.URL) {
NSURL *url = self.URL.mgl_URLByStandardizingScheme;
source->setURL(url.absoluteString.UTF8String);
} 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>();
source->setGeoJSON(geojson);
_features = MGLFeaturesFromMBGLFeatures(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};
source->setGeoJSON(geojson);
_features = MGLFeaturesFromMBGLFeatures(featureCollection);
if (self.rawSource == NULL)
{
[self commonInit];
}

NSString *string = [[NSString alloc] initWithData:_geoJSONData encoding:NSUTF8StringEncoding];
const auto geojson = mapbox::geojson::parse(string.UTF8String).get<mapbox::geojson::feature_collection>();
self.rawSource->setGeoJSON(geojson);

_features = MGLFeaturesFromMBGLFeatures(geojson);
}

- (void)setURL:(NSURL *)URL
{
_URL = URL;

if (self.rawSource == NULL)
{
[self commonInit];
}

NSURL *url = self.URL.mgl_URLByStandardizingScheme;
self.rawSource->setURL(url.absoluteString.UTF8String);
}

- (void)setFeatures:(NSArray *)features
{
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};
self.rawSource->setGeoJSON(geojson);

return std::move(source);
_features = MGLFeaturesFromMBGLFeatures(featureCollection);
}

@end
1 change: 1 addition & 0 deletions platform/darwin/src/MGLGeoJSONSource_Private.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#import "MGLGeoJSONSource.h"
#import "MGLGeoJSONSource_Private.h"

#include <mbgl/style/sources/geojson_source.hpp>
Expand Down
23 changes: 20 additions & 3 deletions platform/darwin/src/MGLRasterSource.mm
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
#import "MGLRasterSource.h"

#import "MGLMapView_Private.h"
#import "MGLSource_Private.h"
#import "MGLTileSet_Private.h"
#import "NSURL+MGLAdditions.h"

#include <mbgl/style/sources/raster_source.hpp>

@interface MGLRasterSource ()

@property (nonatomic) mbgl::style::RasterSource *rawSource;

@end

@implementation MGLRasterSource
{
std::unique_ptr<mbgl::style::RasterSource> _pendingSource;
}

- (instancetype)initWithIdentifier:(NSString *)identifier URL:(NSURL *)url tileSize:(CGFloat)tileSize
{
if (self = [super initWithIdentifier:identifier]) {
_URL = url;
_tileSize = tileSize;
[self commonInit];
}
return self;
}
Expand All @@ -23,11 +34,12 @@ - (instancetype)initWithIdentifier:(NSString *)identifier tileSet:(MGLTileSet *)
{
_tileSet = tileSet;
_tileSize = tileSize;
[self commonInit];
}
return self;
}

- (std::unique_ptr<mbgl::style::Source>)mbglSource
- (void)commonInit
{
std::unique_ptr<mbgl::style::RasterSource> source;

Expand All @@ -42,10 +54,15 @@ - (instancetype)initWithIdentifier:(NSString *)identifier tileSet:(MGLTileSet *)
source = std::make_unique<mbgl::style::RasterSource>(self.identifier.UTF8String,
self.tileSet.mbglTileset,
uint16_t(self.tileSize));

}

return std::move(source);
_pendingSource = std::move(source);
self.rawSource = _pendingSource.get();
}

- (void)addToMapView:(MGLMapView *)mapView
{
mapView.mbglMap->addSource(std::move(_pendingSource));
}

@end
17 changes: 2 additions & 15 deletions platform/darwin/src/MGLSource.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,14 @@

#include <mbgl/style/source.hpp>

@interface MGLSource ()

@property (nonatomic) mbgl::style::Source *source;

@end

@implementation MGLSource

- (instancetype)initWithIdentifier:(NSString *)identifier {
- (instancetype)initWithIdentifier:(NSString *)identifier
{
if (self = [super init]) {
_identifier = identifier;
}
return self;
}

- (std::unique_ptr<mbgl::style::Source>)mbglSource {
[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;
}

@end
23 changes: 21 additions & 2 deletions platform/darwin/src/MGLSource_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,29 @@
#include <mbgl/mbgl.hpp>
#include <mbgl/style/source.hpp>

@class MGLMapView;

@interface MGLSource (Private)

- (std::unique_ptr<mbgl::style::Source>)mbglSource;
/**
A raw pointer to the mbgl object, which is always initialized, either to the
value returned by `mbgl::Map getSource`, or for independently created objects,
to the pointer value held in `pendingSource`. In the latter case, this raw
pointer value stays even after ownership of the object is transferred via
`mbgl::Map addSource`.
*/
@property (nonatomic) mbgl::style::Source *rawSource;

@property (nonatomic) mbgl::style::Source *source;
/**
Adds the mbgl source that this object represents to the mbgl map.

Once a mbgl source is added, ownership of the object is transferred to the
`mbgl::Map` and this object no longer has an active unique_ptr reference to the
`mbgl::Source`. If this object's mbgl source is in that state, the mbgl source
can still be changed but the changes will not be visible until the `MGLSource`
is added back to the map via `-[MGLStyle addSource:]` and styled with a
`MGLLayer`.
*/
- (void)addToMapView:(MGLMapView *)mapView;

@end
12 changes: 9 additions & 3 deletions platform/darwin/src/MGLStyle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ - (MGLStyleLayer *)layerWithIdentifier:(NSString *)identifier
- (MGLSource *)sourceWithIdentifier:(NSString *)identifier
{
auto mbglSource = self.mapView.mbglMap->getSource(identifier.UTF8String);

if (!mbglSource) {
return nil;
}
Expand All @@ -159,8 +160,8 @@ - (MGLSource *)sourceWithIdentifier:(NSString *)identifier
NSAssert(NO, @"Unrecognized source type");
return nil;
}

source.source = mbglSource;
source.rawSource = mbglSource;

return source;
}
Expand Down Expand Up @@ -205,12 +206,17 @@ - (void)insertLayer:(MGLStyleLayer *)layer belowLayer:(MGLStyleLayer *)otherLaye

- (void)addSource:(MGLSource *)source
{
self.mapView.mbglMap->addSource(source.mbglSource);
[source addToMapView:self.mapView];
}

- (void)removeSource:(MGLSource *)source
{
self.mapView.mbglMap->removeSource(source.identifier.UTF8String);

// Once a mbgl source is removed from the map, ownership does not return
// to the MGL source. Therefore, the rawSource pointer is set to NULL
// so that the implementation of MGL source can avoid using it again.
source.rawSource = NULL;
}

- (NS_ARRAY_OF(NSString *) *)styleClasses
Expand Down
Loading