-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpx-api.m
487 lines (416 loc) · 10.6 KB
/
gpx-api.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
Copyright (c) 2012, Bryce Groff
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. 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 <Foundation/Foundation.h>
#import "gpx-api.h"
#pragma mark Link
@implementation Link
@synthesize href;
@synthesize text;
@synthesize type;
- (id)init
{
NSAssert(NO, @"You must use the initWithHref to initialize this object");
return nil;
}
- (id) initWithHref: (NSURL*) inHref {
self = [super init];
if(self) {
NSAssert(href == nil, @"inHref must be initialized before use");
href = [inHref retain];
}
return(self);
}
- (void) dumpLink {
NSLog(@"Link: href: %@\n", href);
if (text) {
NSLog(@"\ttext: %@\n", text);
}
if (type) {
NSLog(@"\ttype: %@\n", type);
}
}
- (void) dealloc
{
[href release];
[text release];
[type release];
[super dealloc];
}
@end
#pragma mark Email
@implementation Email
@synthesize user;
@synthesize domain;
- (id) init {
NSAssert(NO, @"You must use the initWithUserAndDomain to initialize this object");
return nil;
}
- (id) initWithUserAndDomain:(NSString *)inUser :(NSString *)inDomain {
self = [super init];
if(self) {
NSAssert(inUser != nil, @"inUser must be initialized before use");
NSAssert(inDomain != nil, @"inUser must be initialized before use");
user = [inUser retain];
domain = [inDomain retain];
}
return(self);
}
- (void) dumpEmail {
NSLog(@"%@@%@\n", user, domain);
}
- (void) dealloc
{
[user release];
[domain release];
[super dealloc];
}
@end
#pragma mark Person
@implementation Person
@synthesize name;
@synthesize email;
@synthesize link;
- (void) dumpAuthor {
if (name) {NSLog(@"name: %@\n", name);}
if (email) {[email dumpEmail];}
if (link) {[link dumpLink];}
}
- (void) dealloc {
[name release];
[email release];
[link release];
[super dealloc];
}
@end
#pragma mark Copyright
@implementation Copyright
@synthesize author;
@synthesize year;
@synthesize license;
- (id) init {
NSAssert(NO, @"You must use the initWithValues to initialize this object");
return nil;
}
-(id) initWithValues:(NSString *)inAuthor :(NSString *)inYear :(NSURL *)inLicense {
self = [super init];
if(self) {
NSAssert(inAuthor != nil, @"inAuthor must be initialized before use");
author = [inAuthor retain];
year = [inYear retain];
license = [inLicense retain];
}
return(self);
}
- (void) dumpCopyright {
NSLog(@"author: %@\n", author);
if (year) {NSLog(@"year: %@\n", year);}
if (license) {NSLog(@"license: %@\n", license);}
}
- (void) dealloc
{
[author release];
[year release];
[license release];
[super dealloc];
}
@end
#pragma mark Bounds
@implementation Bounds
@synthesize minlat;
@synthesize minlon;
@synthesize maxlat;
@synthesize maxlon;
@end
#pragma mark Metadata
@implementation Metadata
@synthesize name;
@synthesize desc;
@synthesize author;
@synthesize copyright;
@synthesize link;
@synthesize time;
@synthesize keywords;
@synthesize bounds;
- (void) addLink:(Link *)newLink {
if (link == nil) {
link = [[NSMutableArray alloc] init];
}
[link addObject:newLink];
}
- (void) dumpMetadata {
NSLog(@"Metadata:\n");
if (name) {NSLog(@"name: %@\n", name);}
if (desc) {NSLog(@"description: %@\n", desc);}
if (author) {[author dumpAuthor];}
if (copyright) {[copyright dumpCopyright];}
if (link) {
for (NSUInteger i = 0; i < link.count; i++) {
Link *l = [link objectAtIndex:i];
[l dumpLink];
}
}
if (time) {NSLog(@"time: %@\n", time);}
if (keywords) {NSLog(@"keywords: %@\n", keywords);}
if (bounds) {NSLog(@"minlat: %f, minlon: %f, maxlat: %f, maxlon: %f\n", bounds.minlat, bounds.minlon, bounds.maxlat, bounds.maxlon);}
}
- (void) dealloc {
[name release];
[desc release];
[author release];
[copyright release];
[link release];
[time release];
[keywords release];
[bounds release];
[super dealloc];
}
@end
#pragma mark PathHeader
@implementation PathHeader
@synthesize name;
@synthesize cmt;
@synthesize desc;
@synthesize src;
@synthesize link;
@synthesize number;
@synthesize type;
- (void)addLink:(Link *)newLink {
if (link == nil) {
link = [[NSMutableArray alloc] init];
}
[link addObject:newLink];
}
- (void) dumpPathHeader {
if (name) {NSLog(@"name: %@\n", name);}
if (cmt) {NSLog(@"comment: %@\n", cmt);}
if (desc) {NSLog(@"description: %@\n", desc);}
if (src) {NSLog(@"source: %@\n", src);}
if (link) {for(Link *l in link) {[l dumpLink];}}
if (number) {NSLog(@"number: %i\n", number);}
if (type) {NSLog(@"type: %@\n", type);}
}
- (void) dealloc {
[name release];
[cmt release];
[desc release];
[src release];
[link release];
[type release];
}
@end
#pragma mark Waypoint
@implementation Waypoint
@synthesize lat;
@synthesize lon;
@synthesize elev;
@synthesize time;
@synthesize geoidheight;
@synthesize sym;
@synthesize sat;
@synthesize hdop;
@synthesize vdop;
@synthesize pdop;
@synthesize ageofdgpsdata;
- (BOOL)setMagvar:(float)inMagvar {
if (inMagvar >= 0.0 && inMagvar <= 360.0) {
magvar = inMagvar;
return YES;
}
return NO;
}
- (float) getMagvar {
return magvar;
}
- (BOOL)setFix:(NSString *)inFix {
BOOL isOption = ([inFix caseInsensitiveCompare:@"none"] || [inFix caseInsensitiveCompare:@"2d"] ||
[inFix caseInsensitiveCompare:@"3d"] || [inFix caseInsensitiveCompare:@"dgps"] ||
[inFix caseInsensitiveCompare:@"pps"]);
if (isOption) {
NSLog(@"Fix must be one of the following: none, 2d, 3d, dgps, pps");
return NO;
}
fix = [inFix retain];
return YES;
}
- (NSString*) getFix {
return fix;
}
- (BOOL)setDgpsid:(unsigned int)inDgpsid {
if (inDgpsid <= 1023) {
NSLog(@"The dgpsid must be less than 1023");
return NO;
}
dgpsid = inDgpsid;
return YES;
}
- (unsigned int) getDgpsid {
return dgpsid;
}
- (id) init {
NSAssert(NO, @"You must use the initWithValues to initialize this object");
return nil;
}
- (id) initWithValues:(float)inLat :(float)inLon {
self = [super init];
if(self) {
lat = inLat;
lon = inLon;
}
return(self);
}
- (void) dumpWaypoint {
NSLog(@"Waypoint: lat: %f, lon: %f\n", lat, lon);
if (elev) {NSLog(@"\tElevation: %f\n", elev);}
if (time) {NSLog(@"\tTime: %@\n", time);}
[super dumpPathHeader];
if (magvar) {NSLog(@"\tMagvar: %f\n", magvar);}
if (sym) {NSLog(@"\tSymbol: %@\n", sym);}
if (fix) {NSLog(@"\tfix: %@\n", fix);}
if (dgpsid) {NSLog(@"\tStationID: %i\n", dgpsid);}
}
- (void) dealloc {
[time release];
[sym release];
[super dealloc];
}
@end
#pragma mark Route
@implementation Route
@synthesize rtept;
- (void)addWaypoint:(Waypoint *)waypoint {
if (rtept == nil) {
rtept = [[NSMutableArray alloc] init];
}
[rtept addObject:waypoint];
}
- (void) dumpRoute {
NSLog(@"Route:\n");
[super dumpPathHeader];
if (rtept) {for(Waypoint *p in rtept){[p dumpWaypoint];}}
}
- (void) dealloc {
[rtept release];
[super dealloc];
}
@end
#pragma mark TrekSegment
@implementation TrekSegment
@synthesize trekpoints;
- (void)addWaypoint:(Waypoint *)waypoint {
if (trekpoints == nil) {
trekpoints = [[NSMutableArray alloc] init];
}
[trekpoints addObject:waypoint];
}
- (void) dumpTrekSegment {
NSLog(@"Trek Segment\n");
if (trekpoints) {for(Waypoint *p in trekpoints){[p dumpWaypoint];}}
}
- (void) dealloc {
[trekpoints release];
[super dealloc];
}
@end
#pragma mark Trek
@implementation Trek
@synthesize trekseg;
- (void)addTrekseg:(TrekSegment *)segment {
if (trekseg == nil) {
trekseg = [[NSMutableArray alloc] init];
}
[trekseg addObject:segment];
}
- (void) dumpTrek {
NSLog(@"Trek:\n");
[super dumpPathHeader];
if (trekseg) {for(TrekSegment *s in trekseg){[s dumpTrekSegment];}}
}
- (void) dealloc {
[trekseg release];
[super dealloc];
}
@end
#pragma mark GPX
@implementation GPX
@synthesize metadata;
@synthesize waypoints;
@synthesize routes;
@synthesize treks;
- (id) init {
self = [super init];
if(self) {
version = [[NSString alloc] initWithString:@"1.1"];
creator = [[NSString alloc] initWithString:@"gpx-api version 1.0"];
}
return(self);
}
- (void) addWaypoint:(Waypoint *)waypoint {
if (waypoints == nil) {
waypoints = [[NSMutableArray alloc] init];
}
[waypoints addObject:waypoint];
}
- (void) addRoute:(Route *)route {
if (routes == nil) {
routes = [[NSMutableArray alloc] init];
}
[routes addObject:route];
}
- (void) addTrek:(Trek *)trek {
if (treks == nil) {
treks = [[NSMutableArray alloc] init];
}
[treks addObject:trek];
}
- (void) dumpGPX {
if (metadata) {
[metadata dumpMetadata];
}
if (waypoints) {
for (Waypoint *waypoint in waypoints) {
[waypoint dumpWaypoint];
}
}
if (routes) {
for (Route *route in routes) {
[route dumpRoute];
}
}
if (treks) {
for (Trek *trek in treks) {
[trek dumpTrek];
}
}
}
- (NSString*) xmlString {
return write_gpx_to_string(self);
}
- (void) dealloc {
[version release];
[creator release];
[metadata release];
[waypoints release];
[routes release];
[treks release];
[super dealloc];
}
@end