-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpx-writer.m
319 lines (293 loc) · 12.8 KB
/
gpx-writer.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
/*
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 <libxml/encoding.h>
#import <libxml/xmlwriter.h>
#import "gpx-api.h"
#import "gpx-writer.h"
#define ENCODING "UTF-8"
void write_metadata(GPX *gpx, xmlTextWriterPtr writer);
void write_waypoints(GPX *gpx, xmlTextWriterPtr writer);
void write_routes(GPX *gpx, xmlTextWriterPtr writer);
void write_tracks(GPX *gpx, xmlTextWriterPtr writer);
void write_path_header(PathHeader* header, xmlTextWriterPtr writer);
void write_waypoint(Waypoint* waypoint, xmlTextWriterPtr writer, NSString *tag);
void write_links(NSMutableArray *links, xmlTextWriterPtr writer);
void write_time(NSDate *time, xmlTextWriterPtr writer);
NSString* write_gpx_to_string(GPX *gpx)
{
xmlBufferPtr buffer = write_gpx(gpx);
if (!buffer) {
return NO;
}
NSString *ret_str = [[NSString alloc] initWithUTF8String:(char*)buffer->content];
if (ret_str) {
return ret_str;
}
return nil;
}
BOOL write_gpx_to_file(GPX *gpx, NSString *file)
{
xmlBufferPtr buffer = write_gpx(gpx);
if (!buffer) {
return NO;
}
FILE *fp ;
fp = fopen([file UTF8String], "w");
if (fp) {
int ret = xmlBufferDump(fp, buffer);
if (ret < 0) {
return NO;
}
} else {
return NO;
}
xmlBufferFree(buffer);
fclose(fp);
return YES;
}
xmlBufferPtr write_gpx(GPX *gpx)
{
if (!gpx) {
return nil;
}
xmlBufferPtr buffer = xmlBufferCreate();
if (buffer == NULL) {
NSLog(@"Error creating the xml buffer\n");
return nil;
}
xmlTextWriterPtr writer = xmlNewTextWriterMemory(buffer, 0);
if (writer == NULL) {
NSLog(@"Error creating the xml writer\n");
return nil;
}
int ret_code = xmlTextWriterStartDocument(writer, NULL, ENCODING, NULL);
if (ret_code < 0) {
NSLog(@"Error at xmlTextWriterStartDocument\n");
return nil;
}
ret_code = xmlTextWriterStartElement(writer, BAD_CAST "gpx");
if (ret_code < 0) {
NSLog(@"Error at xmlTextWriterStartElement\n");
return nil;
}
xmlTextWriterWriteAttribute(writer, BAD_CAST "version", BAD_CAST "1.1");
xmlTextWriterWriteAttribute(writer, BAD_CAST "creator", BAD_CAST "GPX-API");
xmlTextWriterWriteAttribute(writer, BAD_CAST "xmlns", BAD_CAST "http://www.topografix.com/GPX/1/1");
xmlTextWriterWriteAttribute(writer, BAD_CAST "xmlns:xsi", BAD_CAST "http://www.w3.org/2001/XMLSchema-instance");
xmlTextWriterWriteAttribute(writer, BAD_CAST "xsi:schemaLocation", BAD_CAST "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd");
write_metadata(gpx, writer);
write_waypoints(gpx, writer);
write_routes(gpx, writer);
write_tracks(gpx, writer);
xmlTextWriterEndElement(writer);
xmlTextWriterEndDocument(writer);
return buffer;
}
void write_metadata(GPX *gpx, xmlTextWriterPtr writer)
{
Metadata *metadata = gpx.metadata;
if (metadata) {
xmlTextWriterStartElement(writer, BAD_CAST "metadata");
if (metadata.name) {
xmlTextWriterWriteElement(writer, BAD_CAST "name", BAD_CAST [metadata.name UTF8String]);
}
if (metadata.desc) {
xmlTextWriterWriteElement(writer, BAD_CAST "desc", BAD_CAST [metadata.desc UTF8String]);
}
if (metadata.author) {
xmlTextWriterStartElement(writer, BAD_CAST "author");
xmlTextWriterWriteElement(writer, BAD_CAST "name", BAD_CAST [metadata.author.name UTF8String]);
if (metadata.author.email) {
xmlTextWriterStartElement(writer, BAD_CAST "email");
xmlTextWriterWriteAttribute(writer, BAD_CAST "id", BAD_CAST [metadata.author.email.user UTF8String]);
xmlTextWriterWriteAttribute(writer, BAD_CAST "domain", BAD_CAST [metadata.author.email.domain UTF8String]);
xmlTextWriterEndElement(writer);
}
if (metadata.author.link) {
xmlTextWriterStartElement(writer, BAD_CAST "link");
xmlTextWriterWriteAttribute(writer, BAD_CAST "href", BAD_CAST [[metadata.author.link.href absoluteString] UTF8String]);
if (metadata.author.link.text) {
xmlTextWriterWriteElement(writer, BAD_CAST "text", BAD_CAST [metadata.author.link.text UTF8String]);
}
if (metadata.author.link.type) {
xmlTextWriterWriteElement(writer, BAD_CAST "type", BAD_CAST [metadata.author.link.type UTF8String]);
}
xmlTextWriterEndElement(writer);
}
xmlTextWriterEndElement(writer);
}
if (metadata.copyright) {
xmlTextWriterStartElement(writer, BAD_CAST "copyright");
xmlTextWriterWriteAttribute(writer, BAD_CAST "author", BAD_CAST [metadata.copyright.author UTF8String]);
if (metadata.copyright.year) {
xmlTextWriterWriteElement(writer, BAD_CAST "year", BAD_CAST [metadata.copyright.year UTF8String]);
}
if (metadata.copyright.license) {
xmlTextWriterWriteElement(writer, BAD_CAST "license", BAD_CAST [[metadata.copyright.license absoluteString] UTF8String]);
}
xmlTextWriterEndElement(writer);
}
if (metadata.link) {
write_links(metadata.link, writer);
}
if (metadata.time) {
write_time(metadata.time, writer);
}
if (metadata.keywords) {
xmlTextWriterWriteElement(writer, BAD_CAST "keywords", BAD_CAST [metadata.keywords UTF8String]);
}
if (metadata.bounds) {
xmlTextWriterStartElement(writer, BAD_CAST "bounds");
xmlTextWriterWriteAttribute(writer, BAD_CAST "minlat", BAD_CAST [NSString stringWithFormat:@"%f", metadata.bounds.minlat]);
xmlTextWriterWriteAttribute(writer, BAD_CAST "minlon", BAD_CAST [NSString stringWithFormat:@"%f", metadata.bounds.minlon]);
xmlTextWriterWriteAttribute(writer, BAD_CAST "maxlat", BAD_CAST [NSString stringWithFormat:@"%f", metadata.bounds.maxlat]);
xmlTextWriterWriteAttribute(writer, BAD_CAST "maxlon", BAD_CAST [NSString stringWithFormat:@"%f", metadata.bounds.maxlon]);
xmlTextWriterEndElement(writer);
}
xmlTextWriterEndElement(writer);
}
}
void write_waypoints(GPX *gpx, xmlTextWriterPtr writer)
{
NSMutableArray *waypoints = gpx.waypoints;
for (Waypoint *waypoint in waypoints) {
write_waypoint(waypoint, writer, @"wpt");
}
}
void write_routes(GPX *gpx, xmlTextWriterPtr writer)
{
for (Route *route in gpx.routes) {
xmlTextWriterStartElement(writer, BAD_CAST "rte");
write_path_header(route, writer);
if (route.rtept) {
for (Waypoint *waypoint in route.rtept) {
write_waypoint(waypoint, writer, @"rtept");
}
}
xmlTextWriterEndElement(writer);
}
}
void write_tracks(GPX *gpx, xmlTextWriterPtr writer)
{
for (Trek *trek in gpx.treks) {
xmlTextWriterStartElement(writer, BAD_CAST "trk");
write_path_header(trek, writer);
if (trek.trekseg) {
for (TrekSegment *seg in trek.trekseg) {
xmlTextWriterStartElement(writer, BAD_CAST "trkseg");
for (Waypoint *waypoint in seg.trekpoints) {
write_waypoint(waypoint, writer, @"trkpt");
}
xmlTextWriterEndElement(writer);
}
}
xmlTextWriterEndElement(writer);
}
}
void write_path_header(PathHeader *header, xmlTextWriterPtr writer)
{
if (header.name) {
xmlTextWriterWriteElement(writer, BAD_CAST "name", BAD_CAST [header.name UTF8String]);
}
if (header.cmt) {
xmlTextWriterWriteElement(writer, BAD_CAST "cmt", BAD_CAST [header.cmt UTF8String]);
}
if (header.desc) {
xmlTextWriterWriteElement(writer, BAD_CAST "desc", BAD_CAST [header.desc UTF8String]);
}
if (header.src) {
xmlTextWriterWriteElement(writer, BAD_CAST "src", BAD_CAST [header.src UTF8String]);
}
write_links(header.link, writer);
if (header.number) {
xmlTextWriterWriteElement(writer, BAD_CAST "number", BAD_CAST [[NSString stringWithFormat:@"%i", header.number] UTF8String]);
}
if (header.type) {
xmlTextWriterWriteElement(writer, BAD_CAST "type", BAD_CAST [header.type UTF8String]);
}
}
void write_waypoint(Waypoint *waypoint, xmlTextWriterPtr writer, NSString *tag)
{
xmlTextWriterStartElement(writer, BAD_CAST [tag UTF8String]);
xmlTextWriterWriteAttribute(writer, BAD_CAST "lat", BAD_CAST [[NSString stringWithFormat:@"%f", waypoint.lat] UTF8String]);
xmlTextWriterWriteAttribute(writer, BAD_CAST "lon", BAD_CAST [[NSString stringWithFormat:@"%f", waypoint.lon] UTF8String]);
write_path_header(waypoint, writer);
if (waypoint.elev) {
xmlTextWriterWriteElement(writer, BAD_CAST "ele", BAD_CAST [[NSString stringWithFormat:@"%f", waypoint.elev] UTF8String]);
}
if (waypoint.time) {
write_time(waypoint.time, writer);
}
if ([waypoint getMagvar]) {
xmlTextWriterWriteElement(writer, BAD_CAST "magvar", BAD_CAST [[NSString stringWithFormat:@"%f", [waypoint getMagvar]] UTF8String]);
}
if (waypoint.geoidheight) {
xmlTextWriterWriteElement(writer, BAD_CAST "geoidheight", BAD_CAST [[NSString stringWithFormat:@"%f", waypoint.geoidheight] UTF8String]);
}
if (waypoint.sym) {
xmlTextWriterWriteElement(writer, BAD_CAST "sym", BAD_CAST [waypoint.sym UTF8String]);
}
if ([waypoint getFix]) {
xmlTextWriterWriteElement(writer, BAD_CAST "fix", BAD_CAST [[waypoint getFix] UTF8String]);
}
if (waypoint.sat) {
xmlTextWriterWriteElement(writer, BAD_CAST "sat", BAD_CAST [[NSString stringWithFormat:@"%i", waypoint.sat] UTF8String]);
}
if (waypoint.hdop) {
xmlTextWriterWriteElement(writer, BAD_CAST "hdop", BAD_CAST [[NSString stringWithFormat:@"%f", waypoint.hdop] UTF8String]);
}
if (waypoint.vdop) {
xmlTextWriterWriteElement(writer, BAD_CAST "vdop", BAD_CAST [[NSString stringWithFormat:@"%f", waypoint.vdop] UTF8String]);
}
if (waypoint.pdop) {
xmlTextWriterWriteElement(writer, BAD_CAST "pdop", BAD_CAST [[NSString stringWithFormat:@"%f", waypoint.pdop] UTF8String]);
}
if (waypoint.ageofdgpsdata) {
xmlTextWriterWriteElement(writer, BAD_CAST "ageofdgpsdata", BAD_CAST [[NSString stringWithFormat:@"%f", waypoint.ageofdgpsdata] UTF8String]);
}
if ([waypoint getDgpsid]) {
xmlTextWriterWriteElement(writer, BAD_CAST "dgpsid", BAD_CAST [[NSString stringWithFormat:@"%i", [waypoint getDgpsid]] UTF8String]);
}
xmlTextWriterEndElement(writer);
}
void write_links(NSMutableArray *links, xmlTextWriterPtr writer)
{
for (Link *link in links) {
xmlTextWriterStartElement(writer, BAD_CAST "link");
xmlTextWriterWriteAttribute(writer, BAD_CAST "href", BAD_CAST [[link.href absoluteString] UTF8String]);
if (link.text) {
xmlTextWriterWriteElement(writer, BAD_CAST "text", BAD_CAST [link.text UTF8String]);
}
if (link.type) {
xmlTextWriterWriteElement(writer, BAD_CAST "type", BAD_CAST [link.type UTF8String]);
}
xmlTextWriterEndElement(writer);
}
}
void write_time(NSDate *time, xmlTextWriterPtr writer)
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeStyle:NSDateFormatterFullStyle];
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
xmlTextWriterWriteElement(writer, BAD_CAST "time", BAD_CAST [[dateFormat stringFromDate:time] UTF8String]);
[dateFormat release];
}