Files
MapLibreParticleLab/platform/darwin/src/MLNPointCollection.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

134 lines
4.3 KiB
Plaintext

#import "MLNGeometry_Private.h"
#import "MLNLoggingConfiguration_Private.h"
#import "MLNPointCollection_Private.h"
#import "NSArray+MLNAdditions.h"
#import <mln/util/geojson.hpp>
#import <mln/util/geometry.hpp>
NS_ASSUME_NONNULL_BEGIN
@implementation MLNPointCollection {
std::optional<mln::LatLngBounds> _bounds;
std::vector<CLLocationCoordinate2D> _coordinates;
}
+ (BOOL)supportsSecureCoding {
return YES;
}
+ (instancetype)pointCollectionWithCoordinates:(const CLLocationCoordinate2D *)coords
count:(NSUInteger)count {
return [[self alloc] initWithCoordinates:coords count:count];
}
- (instancetype)initWithCoordinates:(const CLLocationCoordinate2D *)coords count:(NSUInteger)count {
MLNLogDebug(@"Initializing with %lu coordinates.", (unsigned long)count);
self = [super init];
if (self) {
_coordinates = {coords, coords + count};
}
return self;
}
- (nullable instancetype)initWithCoder:(NSCoder *)decoder {
MLNLogInfo(@"Initializing with coder.O");
if (self = [super initWithCoder:decoder]) {
NSSet<Class> *coordinatesClasses =
[NSSet setWithArray:@[ [NSDictionary class], [NSArray class] ]];
NSArray *coordinates = [decoder decodeObjectOfClasses:coordinatesClasses forKey:@"coordinates"];
_coordinates = [coordinates mgl_coordinates];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
[coder encodeObject:[NSArray mgl_coordinatesFromCoordinates:_coordinates] forKey:@"coordinates"];
}
- (BOOL)isEqual:(id)other {
if (self == other) return YES;
if (![other isKindOfClass:[MLNPointCollection class]]) return NO;
MLNPointCollection *otherCollection = (MLNPointCollection *)other;
return ([super isEqual:other] &&
((![self geoJSONDictionary] && ![otherCollection geoJSONDictionary]) ||
[[self geoJSONDictionary] isEqualToDictionary:[otherCollection geoJSONDictionary]]));
}
- (MLNCoordinateBounds)overlayBounds {
if (!_bounds) {
mln::LatLngBounds bounds = mln::LatLngBounds::empty();
for (auto coordinate : _coordinates) {
if (!MLNLocationCoordinate2DIsValid(coordinate)) {
bounds = mln::LatLngBounds::empty();
break;
}
bounds.extend(MLNLatLngFromLocationCoordinate2D(coordinate));
}
_bounds = bounds;
}
return MLNCoordinateBoundsFromLatLngBounds(*_bounds);
}
- (NSUInteger)pointCount {
return _coordinates.size();
}
- (CLLocationCoordinate2D *)coordinates {
return _coordinates.data();
}
- (CLLocationCoordinate2D)coordinate {
MLNAssert([self pointCount] > 0, @"A multipoint must have coordinates");
return _coordinates.at(0);
}
- (void)getCoordinates:(CLLocationCoordinate2D *)coords range:(NSRange)range {
if (range.location + range.length > [self pointCount]) {
[NSException
raise:NSRangeException
format:@"Invalid coordinate range %@ extends beyond current coordinate count of %ld",
NSStringFromRange(range), (unsigned long)[self pointCount]];
}
std::copy(_coordinates.begin() + range.location, _coordinates.begin() + NSMaxRange(range),
coords);
}
- (BOOL)intersectsOverlayBounds:(MLNCoordinateBounds)overlayBounds {
return MLNCoordinateBoundsIntersectsCoordinateBounds(self.overlayBounds, overlayBounds);
}
- (mln::Geometry<double>)geometryObject {
mln::MultiPoint<double> multiPoint;
multiPoint.reserve(self.pointCount);
for (NSUInteger i = 0; i < self.pointCount; i++) {
multiPoint.push_back(
mln::Point<double>(self.coordinates[i].longitude, self.coordinates[i].latitude));
}
return multiPoint;
}
- (NSDictionary *)geoJSONDictionary {
NSMutableArray *coordinates = [[NSMutableArray alloc] initWithCapacity:self.pointCount];
for (NSUInteger index = 0; index < self.pointCount; index++) {
CLLocationCoordinate2D coordinate = self.coordinates[index];
[coordinates addObject:@[ @(coordinate.longitude), @(coordinate.latitude) ]];
}
return @{@"type" : @"MultiPoint", @"coordinates" : coordinates};
}
- (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p; count = %lu; bounds = %@>",
NSStringFromClass([self class]), (void *)self,
(unsigned long)[self pointCount],
MLNStringFromCoordinateBounds(self.overlayBounds)];
}
@end
NS_ASSUME_NONNULL_END