summaryrefslogtreecommitdiff
path: root/gpxunit.pas
blob: df5d34fe22c94bf140baac596e9a580c306ee555 (plain)
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
unit gpxunit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, mystringlistunit;

type
  tTrkPt = record
    lat,lon,ele,time: string;
  end;
  tTrack = record
    name:   string;
    trkPts: array of tTrkPt;
  end;
  tGpxInhalt = record
    kopf: string;
    trks: array of tTrack;
  end;
  tGpxFile = class
    prefix: tMyStringList;
    gpx:    array of tGpxInhalt;
    constructor create;
    destructor destroy; override;
    function loadFromFile(s: string): boolean;
    procedure saveToFile(s: string);
  end;

implementation

uses
  lowlevelunit, dialogs;

constructor tGpxFile.create;
begin
  inherited create;
  prefix:=tMyStringList.create;
  setLength(gpx,0);
end;

destructor tGpxFile.destroy;
begin
  prefix.free;
  setLength(gpx,0);
  inherited destroy;
end;

function tGpxFile.loadFromFile(s: string): boolean;
var
  f:   tMyStringList;
  m:   longint;
  t,u: string;
begin
  result:=false;
  prefix.clear;
  setLength(gpx,0);
  f:=tMyStringList.create;
  f.loadFromFile(s);
  f.splitLines('<','>');
  m:=0;
  t:='';
  f.rewind;
  while f.readln(t) do
    case m of
      0:
        if startetMit('<gpx ',t) then begin
          setLength(gpx,length(gpx)+1);
          setLength(gpx[length(gpx)-1].trks,0);
          gpx[length(gpx)-1].kopf:=erstesArgument(t,'>');
          inc(m);
        end
        else
          prefix.add(t);
      1:
        if t='<trk>' then begin
          setLength(gpx[length(gpx)-1].trks,length(gpx[length(gpx)-1].trks)+1);
          gpx[length(gpx)-1].trks[length(gpx[length(gpx)-1].trks)-1].name:='';
          setLength(gpx[length(gpx)-1].trks[length(gpx[length(gpx)-1].trks)-1].trkPts,0);
          inc(m);
        end
        else if t='</trk>' then
          dec(m);
      2:
        if t='<name>' then begin
          t:='';
          u:='';
          repeat
            t:=t+#10+u;
            if not f.readln(u) then
              exit;
          until u='</name>';
          delete(t,1,2);
          gpx[length(gpx)-1].trks[length(gpx[length(gpx)-1].trks)-1].name:=t;
        end
        else if t='<trkseg>' then
          inc(m)
        else if t='</trkseg>' then
          dec(m);
      3:
        if startetMit('<trkpt ',t) then begin
          setLength(gpx[length(gpx)-1].trks[length(gpx[length(gpx)-1].trks)-1].trkPts,
             length(gpx[length(gpx)-1].trks[length(gpx[length(gpx)-1].trks)-1].trkPts)+1);
          with gpx[length(gpx)-1].trks[length(gpx[length(gpx)-1].trks)-1].trkPts[
               length(gpx[length(gpx)-1].trks[length(gpx[length(gpx)-1].trks)-1].trkPts)-1] do begin
            erstesArgument(t,'lat="');
            lat:=erstesArgument(t,'"');
            erstesArgument(t,'lon="');
            lon:=erstesArgument(t,'"');
            repeat
              if not f.readln(t) then
                exit;
            until t='<ele>';
            t:='';
            u:='';
            repeat
              t:=t+#10+u;
              if not f.readln(u) then
                exit;
            until u='</ele>';
            delete(t,1,2);
            ele:=t;
            repeat
              if not f.readln(t) then
                exit;
            until t='<time>';
            t:='';
            u:='';
            repeat
              t:=t+#10+u;
              if not f.readln(u) then
                exit;
            until u='</time>';
            delete(t,1,2);
            time:=t;
          end;
        end
        else if pos('</trkseg>',t)>0 then
          dec(m);
    end{of Case};
  f.free;
  result:=true;
end;

procedure tGpxFile.saveToFile(s: string);
var
  f:     textFile;
  i,j,k: longint;
begin
  assignFile(f,s);
  rewrite(f);
  write(f,prefix.text);
  for i:=0 to length(gpx)-1 do begin
    writeln(f,'<gpx '+gpx[i].kopf+'>');
    for j:=0 to length(gpx[i].trks)-1 do begin
      writeln(f,' <trk>');
      while pos('<',gpx[i].trks[j].name)>0 do
        gpx[i].trks[j].name[pos('<',gpx[i].trks[j].name)]:='-';
      while pos('>',gpx[i].trks[j].name)>0 do
        gpx[i].trks[j].name[pos('>',gpx[i].trks[j].name)]:='+';
      writeln(f,'  <name>'+gpx[i].trks[j].name+'</name>');
      writeln(f,'  <trkseg>');
      for k:=0 to length(gpx[i].trks[j].trkPts)-1 do begin
        writeln(f,'   <trkpt lat="'+gpx[i].trks[j].trkPts[k].lat+
                  '" lon="'+gpx[i].trks[j].trkPts[k].lon+'">');
        writeln(f,'    <ele>'+gpx[i].trks[j].trkPts[k].ele+'</ele>');
        writeln(f,'    <time>'+gpx[i].trks[j].trkPts[k].time+'</time>');
        writeln(f,'   </trkpt>');
      end;
      writeln(f,'  </trkseg>');
      writeln(f,' </trk>');
    end;
    writeln(f,'</gpx>');
  end;
  closeFile(f);
end;

end.