summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gpxunit.pas179
1 files changed, 179 insertions, 0 deletions
diff --git a/gpxunit.pas b/gpxunit.pas
new file mode 100644
index 0000000..a0ca3cb
--- /dev/null
+++ b/gpxunit.pas
@@ -0,0 +1,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);
+ writeln(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.