Skip to content

Commit

Permalink
Include magnetic field strength as property in CCMagneticDeclination …
Browse files Browse the repository at this point in the history
…class (probably should name that class now that we're including more than just the declination...)
  • Loading branch information
stephent committed Aug 31, 2015
1 parent d965df8 commit bcb91f0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
3 changes: 2 additions & 1 deletion ObjectiveWMM/CCMagneticDeclination.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

@interface CCMagneticDeclination : NSObject <NSCopying, NSCoding>

- (id) initWithCoordinate:(CLLocationCoordinate2D)coordinate elevation:(CLLocationDistance)elevation date:(NSDate *)date magneticDeclination:(CLLocationDirection)magneticDeclination;
- (id) initWithCoordinate:(CLLocationCoordinate2D)coordinate elevation:(CLLocationDistance)elevation date:(NSDate *)date magneticDeclination:(CLLocationDirection)magneticDeclination fieldStrength:(double)fieldStrength;

@property (readonly, nonatomic) CLLocationCoordinate2D coordinate;
@property (readonly, nonatomic) CLLocationDistance elevation;
@property (readonly, nonatomic, strong) NSDate *date;
@property (readonly, nonatomic) CLLocationDirection magneticDeclination;
@property (readonly, nonatomic) double fieldStrength; // in μT

- (CLLocationDirection) trueHeadingFromMagneticHeading:(CLLocationDirection)magneticHeading;
- (CLLocationDirection) magneticHeadingFromTrueHeading:(CLLocationDirection)trueHeading;
Expand Down
25 changes: 18 additions & 7 deletions ObjectiveWMM/CCMagneticDeclination.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,38 @@ @interface CCMagneticDeclination()
@property (readwrite, nonatomic) CLLocationDistance elevation;
@property (readwrite, nonatomic, strong) NSDate *date;
@property (readwrite, nonatomic) CLLocationDirection magneticDeclination;
@property (readwrite, nonatomic) double fieldStrength;

@end

@implementation CCMagneticDeclination

- (id) initWithCoordinate:(CLLocationCoordinate2D)coordinate elevation:(CLLocationDistance)elevation date:(NSDate *)date magneticDeclination:(CLLocationDirection)magneticDeclination {
- (id) initWithCoordinate:(CLLocationCoordinate2D)coordinate elevation:(CLLocationDistance)elevation date:(NSDate *)date magneticDeclination:(CLLocationDirection)magneticDeclination fieldStrength:(double)fieldStrength {

self = [super init];
if (self) {
self.coordinate = coordinate;
self.elevation = elevation;
self.date = date;
self.magneticDeclination = magneticDeclination;
_coordinate = coordinate;
_elevation = elevation;
_date = date;
_magneticDeclination = magneticDeclination;
_fieldStrength = fieldStrength;
}

return self;
}

- (NSString *) description {

NSString *desc = [NSString stringWithFormat:@"%1.3f°,%1.3f° / %1.2fm at %@: decl: %1.2f° F: %1.2fμT", self.coordinate.latitude, self.coordinate.longitude, self.elevation, self.date.description, self.magneticDeclination, self.fieldStrength];

return desc;
}

#pragma mark - NSCopying

- (id)copyWithZone:(NSZone *)zone {

CCMagneticDeclination *magneticDeclination = [[CCMagneticDeclination allocWithZone:zone] initWithCoordinate:self.coordinate elevation:self.elevation date:self.date magneticDeclination:self.magneticDeclination];
CCMagneticDeclination *magneticDeclination = [[CCMagneticDeclination allocWithZone:zone] initWithCoordinate:self.coordinate elevation:self.elevation date:self.date magneticDeclination:self.magneticDeclination fieldStrength:self.fieldStrength];

return magneticDeclination;
}
Expand All @@ -53,8 +62,9 @@ - (id)initWithCoder:(NSCoder *)aDecoder {
CLLocationDistance elevation = [aDecoder decodeDoubleForKey:@"elevation"];
NSDate *date = [aDecoder decodeObjectForKey:@"date"];
CLLocationDirection magneticDeclination = [aDecoder decodeDoubleForKey:@"magneticDeclination"];
double fieldStrength = [aDecoder decodeDoubleForKey:@"fieldStrength"];

self = [self initWithCoordinate:coord elevation:elevation date:date magneticDeclination:magneticDeclination];
self = [self initWithCoordinate:coord elevation:elevation date:date magneticDeclination:magneticDeclination fieldStrength:fieldStrength];
if (!self) {
return nil;
}
Expand All @@ -69,6 +79,7 @@ - (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeDouble:self.elevation forKey:@"elevation"];
[aCoder encodeObject:self.date forKey:@"date"];
[aCoder encodeDouble:self.magneticDeclination forKey:@"magneticDeclination"];
[aCoder encodeDouble:self.fieldStrength forKey:@"fieldStrength"];
}

#pragma mark - Public
Expand Down
2 changes: 1 addition & 1 deletion ObjectiveWMM/CCMagneticModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ - (CCMagneticDeclination *) declinationForCoordinate:(CLLocationCoordinate2D)coo

MAG_CalculateGridVariation(coordGeodetic, &geoMagneticElements);

CCMagneticDeclination *result = [[CCMagneticDeclination alloc] initWithCoordinate:coordinate elevation:elevation date:date magneticDeclination:geoMagneticElements.Decl];
CCMagneticDeclination *result = [[CCMagneticDeclination alloc] initWithCoordinate:coordinate elevation:elevation date:date magneticDeclination:geoMagneticElements.Decl fieldStrength:geoMagneticElements.F/1000.0];

return result;
}
Expand Down

0 comments on commit bcb91f0

Please sign in to comment.