Files
MapLibreParticleLab/platform/darwin/src/MGLPolygon.mm
Jason Wray c067865e7b [ios, macos] Update minimum deployment versions to iOS 9, macOS 10.11 (#11776)
* [ios] Remove iOS 8 conditionals

* [ios, macos] Bump pod deployment targets

* [ios, macos] Add changelog entries

* [ios, macos] Update docs

* Remove unnecessary selector-based version checks

* Update SQLite note

* [ios, macos] Replace lightweight generics macros with direct use
   Lightweight generics for Foundation collections are available everywhere with the bump to iOS 9 and macOS 10.11.

* Update deprecated macOS method
2018-04-27 12:25:49 -04:00

236 lines
7.3 KiB
Plaintext

#import "MGLPolygon_Private.h"
#import "MGLMultiPoint_Private.h"
#import "MGLGeometry_Private.h"
#import "MGLFeature.h"
#import <mbgl/util/geojson.hpp>
#import <mapbox/polylabel.hpp>
@implementation MGLPolygon
@dynamic overlayBounds;
+ (instancetype)polygonWithCoordinates:(const CLLocationCoordinate2D *)coords count:(NSUInteger)count {
return [self polygonWithCoordinates:coords count:count interiorPolygons:nil];
}
+ (instancetype)polygonWithCoordinates:(const CLLocationCoordinate2D *)coords count:(NSUInteger)count interiorPolygons:(NSArray<MGLPolygon *> *)interiorPolygons {
return [[self alloc] initWithCoordinates:coords count:count interiorPolygons:interiorPolygons];
}
- (instancetype)initWithCoordinates:(const CLLocationCoordinate2D *)coords count:(NSUInteger)count interiorPolygons:(NSArray<MGLPolygon *> *)interiorPolygons {
if (self = [super initWithCoordinates:coords count:count]) {
if (interiorPolygons.count) {
_interiorPolygons = interiorPolygons;
}
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)decoder {
self = [super initWithCoder:decoder];
if (self) {
_interiorPolygons = [decoder decodeObjectOfClass:[NSArray class] forKey:@"interiorPolygons"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
[coder encodeObject:self.interiorPolygons forKey:@"interiorPolygons"];
}
- (BOOL)isEqual:(id)other {
if (self == other) return YES;
if (![other isKindOfClass:[MGLPolygon class]]) return NO;
MGLPolygon *otherPolygon = (MGLPolygon *)other;
return ([super isEqual:otherPolygon] &&
[[self geoJSONDictionary] isEqualToDictionary:[otherPolygon geoJSONDictionary]]);
}
- (NSUInteger)hash {
return [super hash] + [[self geoJSONDictionary] hash];
}
- (CLLocationCoordinate2D)coordinate {
// pole of inaccessibility
auto poi = mapbox::polylabel([self polygon]);
return MGLLocationCoordinate2DFromPoint(poi);
}
- (mbgl::LinearRing<double>)ring {
NSUInteger count = self.pointCount;
CLLocationCoordinate2D *coordinates = self.coordinates;
mbgl::LinearRing<double> result;
result.reserve(self.pointCount);
for (NSUInteger i = 0; i < count; i++) {
result.push_back(mbgl::Point<double>(coordinates[i].longitude, coordinates[i].latitude));
}
return result;
}
- (mbgl::Polygon<double>)polygon {
mbgl::Polygon<double> geometry;
geometry.push_back(self.ring);
for (MGLPolygon *polygon in self.interiorPolygons) {
geometry.push_back(polygon.ring);
}
return geometry;
}
- (mbgl::Geometry<double>)geometryObject {
return [self polygon];
}
- (mbgl::Annotation)annotationObjectWithDelegate:(id <MGLMultiPointDelegate>)delegate {
mbgl::FillAnnotation annotation { [self polygon] };
annotation.opacity = { static_cast<float>([delegate alphaForShapeAnnotation:self]) };
annotation.outlineColor = { [delegate strokeColorForShapeAnnotation:self] };
annotation.color = { [delegate fillColorForPolygonAnnotation:self] };
return annotation;
}
- (NSDictionary *)geoJSONDictionary {
return @{@"type": @"Polygon",
@"coordinates": self.mgl_coordinates};
}
- (NSArray<id> *)mgl_coordinates {
NSMutableArray *coordinates = [NSMutableArray array];
NSMutableArray *exteriorRing = [NSMutableArray array];
for (NSUInteger index = 0; index < self.pointCount; index++) {
CLLocationCoordinate2D coordinate = self.coordinates[index];
[exteriorRing addObject:@[@(coordinate.longitude), @(coordinate.latitude)]];
}
[coordinates addObject:exteriorRing];
for (MGLPolygon *interiorPolygon in self.interiorPolygons) {
NSMutableArray *interiorRing = [NSMutableArray array];
for (int index = 0; index < interiorPolygon.pointCount; index++) {
CLLocationCoordinate2D coordinate = interiorPolygon.coordinates[index];
[interiorRing addObject:@[@(coordinate.longitude), @(coordinate.latitude)]];
}
[coordinates addObject:interiorRing];
}
return [coordinates copy];
}
@end
@interface MGLMultiPolygon ()
@property (nonatomic, copy, readwrite) NSArray<MGLPolygon *> *polygons;
@end
@implementation MGLMultiPolygon {
MGLCoordinateBounds _overlayBounds;
}
@synthesize overlayBounds = _overlayBounds;
+ (instancetype)multiPolygonWithPolygons:(NSArray<MGLPolygon *> *)polygons {
return [[self alloc] initWithPolygons:polygons];
}
- (instancetype)initWithPolygons:(NSArray<MGLPolygon *> *)polygons {
if (self = [super init]) {
_polygons = polygons;
mbgl::LatLngBounds bounds = mbgl::LatLngBounds::empty();
for (MGLPolygon *polygon in _polygons) {
bounds.extend(MGLLatLngBoundsFromCoordinateBounds(polygon.overlayBounds));
}
_overlayBounds = MGLCoordinateBoundsFromLatLngBounds(bounds);
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
_polygons = [decoder decodeObjectOfClass:[NSArray class] forKey:@"polygons"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
[coder encodeObject:_polygons forKey:@"polygons"];
}
- (BOOL)isEqual:(id)other {
if (self == other) return YES;
if (![other isKindOfClass:[MGLMultiPolygon class]]) return NO;
MGLMultiPolygon *otherMultiPolygon = other;
return [super isEqual:other]
&& [self.polygons isEqualToArray:otherMultiPolygon.polygons];
}
- (NSUInteger)hash {
NSUInteger hash = [super hash];
for (MGLPolygon *polygon in self.polygons) {
hash += [polygon hash];
}
return hash;
}
- (CLLocationCoordinate2D)coordinate {
MGLPolygon *firstPolygon = self.polygons.firstObject;
return firstPolygon.coordinate;
}
- (BOOL)intersectsOverlayBounds:(MGLCoordinateBounds)overlayBounds {
return MGLCoordinateBoundsIntersectsCoordinateBounds(_overlayBounds, overlayBounds);
}
- (mbgl::MultiPolygon<double>)multiPolygon {
mbgl::MultiPolygon<double> multiPolygon;
multiPolygon.reserve(self.polygons.count);
for (MGLPolygon *polygon in self.polygons) {
mbgl::Polygon<double> geometry;
geometry.push_back(polygon.ring);
for (MGLPolygon *interiorPolygon in polygon.interiorPolygons) {
geometry.push_back(interiorPolygon.ring);
}
multiPolygon.push_back(geometry);
}
return multiPolygon;
}
- (mbgl::Geometry<double>)geometryObject {
return [self multiPolygon];
}
- (NSDictionary *)geoJSONDictionary {
NSMutableArray *coordinates = [[NSMutableArray alloc] initWithCapacity:self.polygons.count];
for (MGLPolygonFeature *feature in self.polygons) {
[coordinates addObject: feature.mgl_coordinates];
}
return @{@"type": @"MultiPolygon",
@"coordinates": coordinates};
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p; title = %@, subtitle: = %@, count = %lu; bounds = %@>",
NSStringFromClass([self class]), (void *)self,
self.title ? [NSString stringWithFormat:@"\"%@\"", self.title] : self.title,
self.subtitle ? [NSString stringWithFormat:@"\"%@\"", self.subtitle] : self.subtitle,
(unsigned long)self.polygons.count,
MGLStringFromCoordinateBounds(self.overlayBounds)];
}
@end