|
| 1 | +#import "MGLComputedShapeSource.h" |
| 2 | + |
| 3 | +#import "MGLMapView_Private.h" |
| 4 | +#import "MGLSource_Private.h" |
| 5 | +#import "MGLShape_Private.h" |
| 6 | +#import "MGLAbstractShapeSource_Private.h" |
| 7 | +#import "MGLGeometry_Private.h" |
| 8 | + |
| 9 | +#include <mbgl/map/map.hpp> |
| 10 | +#include <mbgl/style/sources/custom_vector_source.hpp> |
| 11 | +#include <mbgl/tile/tile_id.hpp> |
| 12 | +#include <mbgl/util/geo.hpp> |
| 13 | +#include <mbgl/util/geojson.hpp> |
| 14 | + |
| 15 | +@interface MGLComputedShapeSource () { |
| 16 | + std::unique_ptr<mbgl::style::CustomVectorSource> _pendingSource; |
| 17 | +} |
| 18 | + |
| 19 | +@property (nonatomic, readwrite) NSDictionary *options; |
| 20 | +@property (nonnull) mbgl::style::CustomVectorSource *rawSource; |
| 21 | +@property (nonatomic, assign) BOOL dataSourceImplementsFeaturesForTile; |
| 22 | +@property (nonatomic, assign) BOOL dataSourceImplementsFeaturesForBounds; |
| 23 | + |
| 24 | +@end |
| 25 | + |
| 26 | +@interface MGLComputedShapeSourceFetchOperation : NSOperation |
| 27 | + |
| 28 | +@property (nonatomic, readonly) uint8_t z; |
| 29 | +@property (nonatomic, readonly) uint32_t x; |
| 30 | +@property (nonatomic, readonly) uint32_t y; |
| 31 | +@property (nonatomic, assign) BOOL dataSourceImplementsFeaturesForTile; |
| 32 | +@property (nonatomic, assign) BOOL dataSourceImplementsFeaturesForBounds; |
| 33 | +@property (nonatomic, weak, nullable) id<MGLComputedShapeSourceDataSource> dataSource; |
| 34 | +@property (nonatomic, nullable) mbgl::style::CustomVectorSource *rawSource; |
| 35 | + |
| 36 | +- (instancetype)initForSource:(MGLComputedShapeSource*)source tile:(const mbgl::CanonicalTileID&)tileId; |
| 37 | + |
| 38 | +@end |
| 39 | + |
| 40 | +@implementation MGLComputedShapeSourceFetchOperation |
| 41 | + |
| 42 | +- (instancetype)initForSource:(MGLComputedShapeSource*)source tile:(const mbgl::CanonicalTileID&)tileID { |
| 43 | + self = [super init]; |
| 44 | + _z = tileID.z; |
| 45 | + _x = tileID.x; |
| 46 | + _y = tileID.y; |
| 47 | + _dataSourceImplementsFeaturesForTile = source.dataSourceImplementsFeaturesForTile; |
| 48 | + _dataSourceImplementsFeaturesForBounds = source.dataSourceImplementsFeaturesForBounds; |
| 49 | + _dataSource = source.dataSource; |
| 50 | + mbgl::style::CustomVectorSource *rawSource = (mbgl::style::CustomVectorSource *)source.rawSource; |
| 51 | + _rawSource = rawSource; |
| 52 | + return self; |
| 53 | +} |
| 54 | + |
| 55 | +- (void)main { |
| 56 | + if ([self isCancelled]) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + NSArray<MGLShape <MGLFeature> *> *data; |
| 61 | + if(!self.dataSource) { |
| 62 | + data = nil; |
| 63 | + } else if(self.dataSourceImplementsFeaturesForTile) { |
| 64 | + data = [self.dataSource featuresInTileAtX:self.x |
| 65 | + y:self.y |
| 66 | + zoomLevel:self.z]; |
| 67 | + } else { |
| 68 | + mbgl::CanonicalTileID tileID = mbgl::CanonicalTileID(self.z, self.x, self.y); |
| 69 | + mbgl::LatLngBounds tileBounds = mbgl::LatLngBounds(tileID); |
| 70 | + data = [self.dataSource featuresInCoordinateBounds:MGLCoordinateBoundsFromLatLngBounds(tileBounds) |
| 71 | + zoomLevel:self.z]; |
| 72 | + } |
| 73 | + |
| 74 | + if(![self isCancelled]) { |
| 75 | + mbgl::FeatureCollection featureCollection; |
| 76 | + featureCollection.reserve(data.count); |
| 77 | + for (MGLShape <MGLFeature> * feature in data) { |
| 78 | + mbgl::Feature geoJsonObject = [feature geoJSONObject].get<mbgl::Feature>(); |
| 79 | + featureCollection.push_back(geoJsonObject); |
| 80 | + } |
| 81 | + const auto geojson = mbgl::GeoJSON{featureCollection}; |
| 82 | + dispatch_sync(dispatch_get_main_queue(), ^{ |
| 83 | + if(![self isCancelled] && self.rawSource) { |
| 84 | + self.rawSource->setTileData(mbgl::CanonicalTileID(self.z, self.x, self.y), geojson); |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +- (void)cancel { |
| 91 | + [super cancel]; |
| 92 | + self.rawSource = NULL; |
| 93 | +} |
| 94 | + |
| 95 | +@end |
| 96 | + |
| 97 | +@implementation MGLComputedShapeSource |
| 98 | + |
| 99 | +- (instancetype)initWithIdentifier:(NSString *)identifier options:(NS_DICTIONARY_OF(MGLShapeSourceOption, id) *)options { |
| 100 | + if (self = [super initWithIdentifier:identifier]) { |
| 101 | + _requestQueue = [[NSOperationQueue alloc] init]; |
| 102 | + self.requestQueue.name = [NSString stringWithFormat:@"mgl.MGLComputedShapeSource.%@", identifier]; |
| 103 | + auto geoJSONOptions = MGLGeoJSONOptionsFromDictionary(options); |
| 104 | + auto source = std::make_unique<mbgl::style::CustomVectorSource> |
| 105 | + (self.identifier.UTF8String, geoJSONOptions, |
| 106 | + ^void(const mbgl::CanonicalTileID& tileID) |
| 107 | + { |
| 108 | + NSOperation *operation = [[MGLComputedShapeSourceFetchOperation alloc] initForSource:self tile:tileID]; |
| 109 | + [self.requestQueue addOperation:operation]; |
| 110 | + }); |
| 111 | + |
| 112 | + _pendingSource = std::move(source); |
| 113 | + self.rawSource = _pendingSource.get(); |
| 114 | + } |
| 115 | + return self; |
| 116 | +} |
| 117 | + |
| 118 | +- (void)dealloc { |
| 119 | + [self.requestQueue cancelAllOperations]; |
| 120 | +} |
| 121 | + |
| 122 | +- (void)setDataSource:(id<MGLComputedShapeSourceDataSource>)dataSource { |
| 123 | + [self.requestQueue cancelAllOperations]; |
| 124 | + // Check which method the datasource implements, to avoid having to check for each tile |
| 125 | + self.dataSourceImplementsFeaturesForTile = [dataSource respondsToSelector:@selector(featuresInTileAtX:y:zoomLevel:)]; |
| 126 | + self.dataSourceImplementsFeaturesForBounds = [dataSource respondsToSelector:@selector(featuresInCoordinateBounds:zoomLevel:)]; |
| 127 | + |
| 128 | + if(!self.dataSourceImplementsFeaturesForBounds && !self.dataSourceImplementsFeaturesForTile) { |
| 129 | + [NSException raise:@"Invalid Datasource" format:@"Datasource does not implement any MGLComputedShapeSourceDataSource methods"]; |
| 130 | + } else if(self.dataSourceImplementsFeaturesForBounds && self.dataSourceImplementsFeaturesForTile) { |
| 131 | + [NSException raise:@"Invalid Datasource" format:@"Datasource implements multiple MGLComputedShapeSourceDataSource methods"]; |
| 132 | + } |
| 133 | + |
| 134 | + _dataSource = dataSource; |
| 135 | +} |
| 136 | + |
| 137 | +- (void)addToMapView:(MGLMapView *)mapView { |
| 138 | + if (_pendingSource == nullptr) { |
| 139 | + [NSException raise:@"MGLRedundantSourceException" |
| 140 | + format:@"This instance %@ was already added to %@. Adding the same source instance " \ |
| 141 | + "to the style more than once is invalid.", self, mapView.style]; |
| 142 | + } |
| 143 | + |
| 144 | + mapView.mbglMap->addSource(std::move(_pendingSource)); |
| 145 | +} |
| 146 | + |
| 147 | +- (void)removeFromMapView:(MGLMapView *)mapView { |
| 148 | + [self.requestQueue cancelAllOperations]; |
| 149 | + if (self.rawSource != mapView.mbglMap->getSource(self.identifier.UTF8String)) { |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + auto removedSource = mapView.mbglMap->removeSource(self.identifier.UTF8String); |
| 154 | + |
| 155 | + mbgl::style::CustomVectorSource *source = dynamic_cast<mbgl::style::CustomVectorSource *>(removedSource.get()); |
| 156 | + if (!source) { |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + removedSource.release(); |
| 161 | + |
| 162 | + _pendingSource = std::unique_ptr<mbgl::style::CustomVectorSource>(source); |
| 163 | + self.rawSource = _pendingSource.get(); |
| 164 | +} |
| 165 | + |
| 166 | +- (void)reloadTileInCoordinateBounds:(MGLCoordinateBounds)bounds zoomLevel:(NSUInteger)zoomLevel { |
| 167 | + self.rawSource->reloadRegion(MGLLatLngBoundsFromCoordinateBounds(bounds), (uint8_t)zoomLevel); |
| 168 | +} |
| 169 | + |
| 170 | +- (void)setNeedsUpdateAtZoomLevel:(NSUInteger)z x:(NSUInteger)x y:(NSUInteger)y { |
| 171 | + mbgl::CanonicalTileID tileID = mbgl::CanonicalTileID((uint8_t)z, (uint32_t)x, (uint32_t)y); |
| 172 | + self.rawSource->reloadTile(tileID); |
| 173 | +} |
| 174 | + |
| 175 | +- (void)reloadData { |
| 176 | + [self.requestQueue cancelAllOperations]; |
| 177 | + self.rawSource->reload(); |
| 178 | +} |
| 179 | + |
| 180 | +@end |
0 commit comments