RMProjection.m
14 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//
// RMProjection.m
//
// Copyright (c) 2008-2012, Route-Me Contributors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#import "RMGlobalConstants.h"
#import "proj_api.h"
#import "RMProjection.h"
@implementation RMProjection
{
// This is actually a PROJ4 projPJ, but it is typed as void* so the proj_api doesn't have to be included
void *_internalProjection;
// the size of the earth, in projected units (meters, most often)
RMProjectedRect _planetBounds;
// hardcoded to YES in #initWithString:InBounds:
BOOL _projectionWrapsHorizontally;
}
@synthesize internalProjection = _internalProjection;
@synthesize planetBounds = _planetBounds;
@synthesize projectionWrapsHorizontally = _projectionWrapsHorizontally;
#pragma mark - Common projections
static RMProjection *_googleProjection = nil;
static RMProjection *_latitudeLongitudeProjection = nil;
+ (RMProjection *)googleProjection
{
if (_googleProjection)
{
return _googleProjection;
}
else
{
RMProjectedRect theBounds = RMProjectedRectMake(-20037508.34, -20037508.34, 20037508.34 * 2, 20037508.34 * 2);
_googleProjection = [[RMProjection alloc] initWithString:@"+title= Google Mercator EPSG:900913 +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs"
inBounds:theBounds];
return _googleProjection;
}
}
+ (RMProjection *)EPSGLatLong
{
if (_latitudeLongitudeProjection)
{
return _latitudeLongitudeProjection;
}
else
{
RMProjectedRect theBounds = RMProjectedRectMake(-kMaxLong, -kMaxLat, 360.0, kMaxLong);
_latitudeLongitudeProjection = [[RMProjection alloc] initWithString:@"+proj=latlong +ellps=WGS84"
inBounds:theBounds];
return _latitudeLongitudeProjection;
}
}
#pragma mark -
- (id)initWithString:(NSString *)proj4String inBounds:(RMProjectedRect)projectedBounds
{
if (!(self = [super init]))
return nil;
_internalProjection = pj_init_plus([proj4String UTF8String]);
if (_internalProjection == NULL)
{
RMLog(@"Unhandled error creating projection. String is %@", proj4String);
[self release];
return nil;
}
_planetBounds = projectedBounds;
_projectionWrapsHorizontally = YES;
return self;
}
- (id)initWithString:(NSString *)proj4String
{
RMProjectedRect theBounds;
theBounds = RMProjectedRectMake(0, 0, 0, 0);
return [self initWithString:proj4String inBounds:theBounds];
}
- (id)init
{
return [self initWithString:@"+proj=latlong +ellps=WGS84"];
}
- (void)dealloc
{
if (_internalProjection)
pj_free(_internalProjection);
[super dealloc];
}
- (RMProjectedPoint)wrapPointHorizontally:(RMProjectedPoint)aPoint
{
if (!_projectionWrapsHorizontally || _planetBounds.size.width == 0.0f || _planetBounds.size.height == 0.0f)
return aPoint;
while (aPoint.x < _planetBounds.origin.x)
aPoint.x += _planetBounds.size.width;
while (aPoint.x > (_planetBounds.origin.x + _planetBounds.size.width))
aPoint.x -= _planetBounds.size.width;
return aPoint;
}
- (RMProjectedPoint)constrainPointToBounds:(RMProjectedPoint)aPoint
{
if (_planetBounds.size.width == 0.0f || _planetBounds.size.height == 0.0f)
return aPoint;
[self wrapPointHorizontally:aPoint];
if (aPoint.y < _planetBounds.origin.y)
aPoint.y = _planetBounds.origin.y;
else if (aPoint.y > (_planetBounds.origin.y + _planetBounds.size.height))
aPoint.y = _planetBounds.origin.y + _planetBounds.size.height;
return aPoint;
}
- (RMProjectedPoint)coordinateToProjectedPoint:(CLLocationCoordinate2D)aLatLong
{
projUV uv = {
aLatLong.longitude * DEG_TO_RAD,
aLatLong.latitude * DEG_TO_RAD
};
projUV result = pj_fwd(uv, _internalProjection);
RMProjectedPoint result_point = {
result.u,
result.v,
};
return result_point;
}
- (CLLocationCoordinate2D)projectedPointToCoordinate:(RMProjectedPoint)aPoint
{
projUV uv = {
aPoint.x,
aPoint.y,
};
projUV result = pj_inv(uv, _internalProjection);
CLLocationCoordinate2D result_coordinate = {
result.v * RAD_TO_DEG,
result.u * RAD_TO_DEG,
};
return result_coordinate;
}
#pragma mark - UTM conversions
// This uses code by Chuck Gantz, found at http://www.gpsy.com/gpsinfo/geotoutm/
// It is limited to WGS84, have a look at the original source code if you need more.
//
// Source
// Defense Mapping Agency. 1987b. DMA Technical Report: Supplement to Department of Defense World Geodetic System
// 1984 Technical Report. Part I and II. Washington, DC: Defense Mapping Agency
//
#define deg2rad (M_PI / 180.0)
#define rad2deg (180.0 / M_PI)
// This routine determines the correct UTM letter designator for the given latitude.
// Returns 'Z' if latitude is outside the UTM limits of 84N to 80S
// Written by Chuck Gantz- chuck.gantz@globalstar.com
+ (NSString *)UTMLetterDesignatorForLatitude:(double)latitude
{
char letterDesignator;
if ((84 >= latitude) && (latitude >= 72)) letterDesignator = 'X';
else if ((72 > latitude) && (latitude >= 64)) letterDesignator = 'W';
else if ((64 > latitude) && (latitude >= 56)) letterDesignator = 'V';
else if ((56 > latitude) && (latitude >= 48)) letterDesignator = 'U';
else if ((48 > latitude) && (latitude >= 40)) letterDesignator = 'T';
else if ((40 > latitude) && (latitude >= 32)) letterDesignator = 'S';
else if ((32 > latitude) && (latitude >= 24)) letterDesignator = 'R';
else if ((24 > latitude) && (latitude >= 16)) letterDesignator = 'Q';
else if ((16 > latitude) && (latitude >= 8)) letterDesignator = 'P';
else if (( 8 > latitude) && (latitude >= 0)) letterDesignator = 'N';
else if (( 0 > latitude) && (latitude >= -8)) letterDesignator = 'M';
else if ((-8> latitude) && (latitude >= -16)) letterDesignator = 'L';
else if ((-16 > latitude) && (latitude >= -24)) letterDesignator = 'K';
else if ((-24 > latitude) && (latitude >= -32)) letterDesignator = 'J';
else if ((-32 > latitude) && (latitude >= -40)) letterDesignator = 'H';
else if ((-40 > latitude) && (latitude >= -48)) letterDesignator = 'G';
else if ((-48 > latitude) && (latitude >= -56)) letterDesignator = 'F';
else if ((-56 > latitude) && (latitude >= -64)) letterDesignator = 'E';
else if ((-64 > latitude) && (latitude >= -72)) letterDesignator = 'D';
else if ((-72 > latitude) && (latitude >= -80)) letterDesignator = 'C';
else letterDesignator = 'Z'; //This is here as an error flag to show that the Latitude is outside the UTM limits
return [NSString stringWithFormat:@"%c", letterDesignator];
}
// Converts latitude/longitude to UTM coordinates. Equations from USGS Bulletin 1532.
// East longitudes are positive, West longitudes are negative.
// North latitudes are positive, South latitudes are negative.
// Latitude and longitude are in decimal degrees.
// Written by Chuck Gantz - chuck.gantz@globalstar.com
+ (void)convertCoordinate:(CLLocationCoordinate2D)coordinate
toUTMZoneNumber:(int *)utmZoneNumber
utmZoneLetter:(NSString **)utmZoneLetter
isNorthernHemisphere:(BOOL *)isNorthernHemisphere
easting:(double *)easting
northing:(double *)northing
{
double a = 6378137.0;
double eccSquared = 0.00669438;
double k0 = 0.9996;
double longitudeOrigin, longitudeOriginRad;
double eccPrimeSquared;
double N, T, C, A, M;
// Make sure the longitude is between -180.00 .. 179.9
double longitudeTemp = (coordinate.longitude + 180.0) - (floor((coordinate.longitude + 180.0) / 360.0) * 360.0) - 180.0;
double latitudeRad = coordinate.latitude * deg2rad;
double longitudeRad = longitudeTemp * deg2rad;
*utmZoneNumber = floor((longitudeTemp + 180.0) / 6.0) + 1;
if (coordinate.latitude >= 56.0 && coordinate.latitude < 64.0 && longitudeTemp >= 3.0 && longitudeTemp < 12.0)
*utmZoneNumber = 32;
// Special zones for Svalbard
if (coordinate.latitude >= 72.0 && coordinate.latitude < 84.0)
{
if (longitudeTemp >= 0.0 && longitudeTemp < 9.0) *utmZoneNumber = 31;
else if (longitudeTemp >= 9.0 && longitudeTemp < 21.0) *utmZoneNumber = 33;
else if (longitudeTemp >= 21.0 && longitudeTemp < 33.0) *utmZoneNumber = 35;
else if (longitudeTemp >= 33.0 && longitudeTemp < 42.0) *utmZoneNumber = 37;
}
longitudeOrigin = (*utmZoneNumber - 1) * 6 - 180 + 3; //+3 puts origin in middle of zone
longitudeOriginRad = longitudeOrigin * deg2rad;
// Compute the UTM Zone from the latitude and longitude
NSString *utmLetterDesignator = [self UTMLetterDesignatorForLatitude:coordinate.latitude];
if (utmZoneLetter != NULL)
*utmZoneLetter = utmLetterDesignator;
if (isNorthernHemisphere != NULL)
{
char zoneLetterChar = [utmLetterDesignator UTF8String][0];
*isNorthernHemisphere = (zoneLetterChar >= 'N' && zoneLetterChar <= 'X');
}
eccPrimeSquared = (eccSquared) / (1.0 - eccSquared);
N = a / sqrt(1.0 - eccSquared * sin(latitudeRad) * sin(longitudeRad));
T = tan(latitudeRad) * tan(latitudeRad);
C = eccPrimeSquared * cos(latitudeRad) * cos(latitudeRad);
A = cos(latitudeRad) * (longitudeRad - longitudeOriginRad);
M = a * ((1.0 - eccSquared/4 - 3*eccSquared*eccSquared/64 - 5*eccSquared*eccSquared*eccSquared/256) * latitudeRad
- (3*eccSquared/8 + 3*eccSquared*eccSquared/32 + 45*eccSquared*eccSquared*eccSquared/1024) * sin(2*latitudeRad)
+ (15*eccSquared*eccSquared/256 + 45*eccSquared*eccSquared*eccSquared/1024) * sin(4*latitudeRad)
- (35*eccSquared*eccSquared*eccSquared/3072) * sin(6*latitudeRad));
*easting = (double)(k0*N*(A+(1-T+C)*A*A*A/6 + (5 - 18*T+T*T + 72*C - 58*eccPrimeSquared)*A*A*A*A*A / 120) + 500000.0);
*northing = (double)(k0 * (M + N*tan(latitudeRad) * (A*A/2 + (5 - T + 9*C + 4*C*C) * A*A*A*A/24 + (61 - 58*T + T*T + 600*C - 330*eccPrimeSquared) * A*A*A*A*A*A/720)));
if (coordinate.latitude < 0)
*northing += 10000000.0; //10000000 meter offset for southern hemisphere
}
// Converts UTM coords to latitude/longitude. Equations from USGS Bulletin 1532.
// East longitudes are positive, West longitudes are negative.
// North latitudes are positive, South latitudes are negative.
// Latitude and longitude are in decimal degrees.
// Written by Chuck Gantz - chuck.gantz@globalstar.com
+ (void)convertUTMZoneNumber:(int)utmZoneNumber
utmZoneLetter:(NSString *)utmZoneLetter
isNorthernHemisphere:(BOOL)isNorthernHemisphere
easting:(double)easting
northing:(double)northing
toCoordinate:(CLLocationCoordinate2D *)coordinate
{
double k0 = 0.9996;
double a = 6378137.0;
double eccSquared = 0.00669438;
double eccPrimeSquared;
double e1 = (1 - sqrt(1-eccSquared)) / (1 + sqrt(1-eccSquared));
double N1, T1, C1, R1, D, M;
double longitudeOrigin;
double mu, phi1, phi1Rad;
double x, y, latitude, longitude;
x = easting - 500000.0; // remove 500,000 meter offset for longitude
y = northing;
if (utmZoneLetter != nil)
{
char zoneLetter = [utmZoneLetter UTF8String][0];
if ((zoneLetter >= 'c' && zoneLetter <= 'm') || (zoneLetter >= 'C' && zoneLetter <= 'M'))
y -= 10000000.0; // remove 10,000,000 meter offset used for southern hemisphere
}
else if ( ! isNorthernHemisphere)
{
y -= 10000000.0; // remove 10,000,000 meter offset used for southern hemisphere
}
longitudeOrigin = (utmZoneNumber - 1)*6 - 180 + 3; //+3 puts origin in middle of zone
eccPrimeSquared = (eccSquared) / (1-eccSquared);
M = y / k0;
mu = M / (a * (1 - eccSquared/4 - 3*eccSquared*eccSquared/64 - 5*eccSquared*eccSquared*eccSquared/256));
phi1Rad = mu + (3*e1/2 - 27*e1*e1*e1/32) * sin(2*mu) + (21*e1*e1/16 - 55*e1*e1*e1*e1/32) * sin(4*mu) + (151*e1*e1*e1/96) * sin(6*mu);
phi1 = phi1Rad * rad2deg;
N1 = a / sqrt(1 - eccSquared*sin(phi1Rad)*sin(phi1Rad));
T1 = tan(phi1Rad) * tan(phi1Rad);
C1 = eccPrimeSquared * cos(phi1Rad) * cos(phi1Rad);
R1 = a * (1 - eccSquared) / pow(1 - eccSquared*sin(phi1Rad)*sin(phi1Rad), 1.5);
D = x / (N1 * k0);
latitude = phi1Rad - (N1 * tan(phi1Rad) / R1) * (D*D/2 - (5 + 3*T1 + 10*C1 - 4*C1*C1 - 9*eccPrimeSquared) * D*D*D*D/24 + (61 + 90*T1 + 298*C1 + 45*T1*T1 - 252*eccPrimeSquared - 3*C1*C1) * D*D*D*D*D*D/720);
latitude = latitude * rad2deg;
longitude = (D - (1 + 2*T1+C1) * D*D*D/6 + (5 - 2*C1 + 28*T1 - 3*C1*C1 + 8*eccPrimeSquared + 24*T1*T1) * D*D*D*D*D/120) / cos(phi1Rad);
longitude = longitudeOrigin + longitude * rad2deg;
(*coordinate).latitude = latitude;
(*coordinate).longitude = longitude;
}
@end