-
Notifications
You must be signed in to change notification settings - Fork 0
/
TLLocation.m
116 lines (95 loc) · 3.61 KB
/
TLLocation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// TLLocation.m
// Mercatalog
//
// Created by Nathan Vander Wilt on 7/1/08.
// Copyright 2008 Calf Trail Software, LLC. All rights reserved.
//
#import "TLLocation.h"
#include "TLRandom.h"
#include "TLProjectionInfo.h"
#include "TLCoordGeometry.h"
static CGSize TLProjectionInfoMetersPerDegree(TLCoordinate targetCoord);
static TLCoordinate TLCoordinatePerturb(TLCoordinate origCoord, TLCoordinateAccuracy accuracy);
@implementation TLLocation
#pragma mark Lifecycle
+ (void)initialize {
if (self != [TLLocation class]) return;
TLRandomInit();
}
- (id)initWithCoordinate:(TLCoordinate)coord
horizontalAccuracy:(TLCoordinateAccuracy)hAccuracy
altitude:(TLCoordinateAltitude)alt
verticalAccuracy:(TLCoordinateAccuracy)vAccuracy
{
self = [super init];
if (self) {
coordinate = coord;
horizontalAccuracy = hAccuracy;
altitude = alt;
verticalAccuracy = vAccuracy;
}
return self;
}
- (void)dealloc {
[super dealloc];
}
- (id)copyWithZone:(NSZone*)zone {
return [[TLLocation allocWithZone:zone] initWithCoordinate:[self coordinate]
horizontalAccuracy:[self horizontalAccuracy]
altitude:[self altitude]
verticalAccuracy:[self verticalAccuracy]];
}
#pragma mark Convenience creators
+ (id)locationWithCoordinate:(TLCoordinate)coord
horizontalAccuracy:(TLCoordinateAccuracy)hAccuracy
altitude:(TLCoordinateAltitude)alt
verticalAccuracy:(TLCoordinateAccuracy)vAccuracy
{
TLLocation* location = [[TLLocation alloc] initWithCoordinate:coord
horizontalAccuracy:hAccuracy
altitude:alt
verticalAccuracy:vAccuracy];
return [location autorelease];
}
+ (id)locationWithCoordinate:(TLCoordinate)coord
horizontalAccuracy:(TLCoordinateAccuracy)hAccuracy
{
TLLocation* location = [[TLLocation alloc] initWithCoordinate:coord
horizontalAccuracy:hAccuracy
altitude:TLCoordinateAltitudeUnknown
verticalAccuracy:TLCoordinateAccuracyUnknown];
return [location autorelease];
}
- (id)perturbedLocation {
TLLocation* location = [self copy];
location->coordinate = TLCoordinatePerturb([self coordinate],
[self horizontalAccuracy]);
return [location autorelease];
}
#pragma mark Accessors
@synthesize coordinate;
@synthesize horizontalAccuracy;
@synthesize altitude;
@synthesize verticalAccuracy;
@end
TLCoordinate TLCoordinatePerturb(TLCoordinate origCoord, TLCoordinateAccuracy accuracy) {
if (accuracy <= 0.0) return origCoord;
CGPoint randoms = TLRandomGaussian();
CGSize degreeSize = TLProjectionInfoMetersPerDegree(origCoord);
// two standardDeviations is 95.45% certainty
const double standardDeviations = 2.0;
double lonAdjustment = (randoms.x * accuracy) / (standardDeviations * degreeSize.width);
double latAdjustment = (randoms.y * accuracy) / (standardDeviations * degreeSize.height);
TLCoordinate unclampedCoordinate = TLCoordinateMake(origCoord.lat + latAdjustment,
origCoord.lon + lonAdjustment);
return TLCoordinateAdjustToRange(unclampedCoordinate);
}
// use this size only for approximations
CGSize TLProjectionInfoMetersPerDegree(TLCoordinate targetCoord) {
TLProjectionGeoidMeters equatorCircumference = 2.0 * M_PI * TLProjectionGeoidGetEquatorialRadius(TLProjectionGeoidWGS84);
TLProjectionGeoidMeters parallelCircumference = cos(targetCoord.lat * TLCoordinateDegreesToRadians) * equatorCircumference;
double meridianSpacing = parallelCircumference / TLProjectionInfoFullCircle;
double parallelSpacing = equatorCircumference / TLProjectionInfoFullCircle; // NOTE: this assumes a spherical earth
return CGSizeMake((CGFloat)meridianSpacing, (CGFloat)parallelSpacing);
}