Files
MapLibreParticleLab/platform/darwin/src/MLNShape.mm
Bart Louwers 2a8ebc4906 core: rename mbgl directories to mln (#4511)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-08-22 20:50:14 +02:00

112 lines
3.3 KiB
Plaintext

#import "MLNShape_Private.h"
#import "MLNFeature_Private.h"
#import "MLNTypes.h"
#import "NSString+MLNAdditions.h"
#import <mln/util/geo.hpp>
bool operator==(const CLLocationCoordinate2D lhs, const CLLocationCoordinate2D rhs) {
return lhs.latitude == rhs.latitude && lhs.longitude == rhs.longitude;
}
@implementation MLNShape
+ (nullable MLNShape *)shapeWithData:(NSData *)data
encoding:(NSStringEncoding)encoding
error:(NSError *_Nullable *)outError {
NSString *string = [[NSString alloc] initWithData:data encoding:encoding];
if (!string) {
if (outError) {
*outError = [NSError errorWithDomain:MLNErrorDomain code:MLNErrorCodeUnknown userInfo:nil];
}
return nil;
}
try {
const auto geojson = mapbox::geojson::parse(string.UTF8String);
return MLNShapeFromGeoJSON(geojson);
} catch (std::runtime_error &err) {
if (outError) {
*outError = [NSError errorWithDomain:MLNErrorDomain
code:MLNErrorCodeUnknown
userInfo:@{
NSLocalizedFailureReasonErrorKey : @(err.what()),
}];
}
return nil;
}
}
- (mln::GeoJSON)geoJSONObject {
return self.geometryObject;
}
- (mln::Geometry<double>)geometryObject {
[NSException raise:MLNAbstractClassException format:@"MLNShape is an abstract class"];
return mln::Point<double>();
}
- (NSData *)geoJSONDataUsingEncoding:(NSStringEncoding)encoding {
auto geometry = self.geoJSONObject;
NSString *string = @(mapbox::geojson::stringify(geometry).c_str());
return [string dataUsingEncoding:NSUTF8StringEncoding];
}
+ (BOOL)supportsSecureCoding {
return YES;
}
- (instancetype)initWithCoder:(NSCoder *)coder {
if (self = [super init]) {
_title = [coder decodeObjectOfClass:[NSString class] forKey:@"title"];
_subtitle = [coder decodeObjectOfClass:[NSString class] forKey:@"subtitle"];
#if !TARGET_OS_IPHONE
_toolTip = [coder decodeObjectOfClass:[NSString class] forKey:@"toolTip"];
#endif
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:_title forKey:@"title"];
[coder encodeObject:_subtitle forKey:@"subtitle"];
#if !TARGET_OS_IPHONE
[coder encodeObject:_toolTip forKey:@"toolTip"];
#endif
}
- (BOOL)isEqual:(id)other {
if (other == self) {
return YES;
}
id<MLNAnnotation> annotation = other;
#if TARGET_OS_IPHONE
return ((!_title && ![annotation title]) || [_title isEqualToString:[annotation title]]) &&
((!_subtitle && ![annotation subtitle]) ||
[_subtitle isEqualToString:[annotation subtitle]]);
#else
return ((!_title && ![annotation title]) || [_title isEqualToString:[annotation title]]) &&
((!_subtitle && ![annotation subtitle]) ||
[_subtitle isEqualToString:[annotation subtitle]]) &&
((!_toolTip && ![annotation toolTip]) || [_toolTip isEqualToString:[annotation toolTip]]);
#endif
}
- (NSUInteger)hash {
NSUInteger hash = _title.hash + _subtitle.hash;
#if !TARGET_OS_IPHONE
hash += _toolTip.hash;
#endif
return hash;
}
- (CLLocationCoordinate2D)coordinate {
[NSException raise:MLNAbstractClassException format:@"MLNShape is an abstract class"];
return kCLLocationCoordinate2DInvalid;
}
@end