Skip to content

Commit 8996014

Browse files
authored
v2.0
- New GPX library, up to 20% faster conversion - Fixed importing bugs that were crashing converter
2 parents d5c57b0 + d714a39 commit 8996014

File tree

6 files changed

+101
-119
lines changed

6 files changed

+101
-119
lines changed

Arc-app-export-converter/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,22 @@ public static void Main() {
1313
Console.WriteLine("Places initialised");
1414
}
1515
SetupWeight();
16-
16+
DateTime startTime = DateTime.Now;
1717
foreach (var item in ReturnFilePath()) {
1818
Console.ForegroundColor = ConsoleColor.DarkGray;
1919
Console.WriteLine();
2020
Console.WriteLine("Opening file: " + item);
2121
XmlReader xr = new XmlReader(item, true, weight);
2222

2323

24-
// Split into days
24+
//// Split into days
2525
List<XmlReader> daysInXml = XmlReader.Split(xr);
2626
JsonParser.Parse(daysInXml, xr.originalName + ".json", true);
2727
}
2828

2929
// On finish
3030
PlacesManager.SavePlaces();
31+
Console.WriteLine(DateTime.Now - startTime);
3132
}
3233

3334
static void SetupWeight() {

ConverterLibrary/ConverterLibrary.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
<ItemGroup>
3030
<Reference Include="System" />
3131
<Reference Include="Newtonsoft.Json">
32-
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
32+
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
33+
</Reference>
34+
<Reference Include="GpxTools">
35+
<HintPath>..\packages\GpxTools.macharius40.1.0.2\lib\netstandard2.0\GpxTools.dll</HintPath>
3336
</Reference>
3437
</ItemGroup>
3538
<ItemGroup>
@@ -40,6 +43,7 @@
4043
<Compile Include="JsonParser.cs" />
4144
<Compile Include="PlacesManager.cs" />
4245
<Compile Include="XmlReader.cs" />
46+
<Compile Include="ElevationCalculator.cs" />
4347
</ItemGroup>
4448
<ItemGroup>
4549
<None Include="packages.config" />
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using System;
2+
public class ElevationCalculator {
3+
4+
}

ConverterLibrary/JsonParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ static void WriteToFile(string json, string fileName) {
238238
sw.Write(json);
239239
sw.Close();
240240
Console.ForegroundColor = ConsoleColor.DarkGreen;
241-
Console.WriteLine(string.Format("Json file created!"));
241+
Console.WriteLine(string.Format("Json file created!\n") + fileName);
242242
}
243243
}
244244

ConverterLibrary/XmlReader.cs

Lines changed: 86 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Text;
6+
using GpxTools;
67

78
public class XmlTimeline {
89

@@ -33,12 +34,15 @@ public override string ToString() {
3334
}
3435

3536
//Time
36-
public DateTime ReturnDate() {
37+
public DateTime? ReturnDate() {
3738
DateTime tempDate = new DateTime();
3839
if (type == TimelineItemType.activity)
3940
tempDate = activity.startTime;
40-
else
41+
else {
42+
if (!place.startTime.HasValue)
43+
return null;
4144
tempDate = place.startTime.Value;
45+
}
4246
return new DateTime(tempDate.Year, tempDate.Month, tempDate.Day, 12, 0, 0, tempDate.Kind);
4347
}
4448

@@ -76,13 +80,19 @@ public DateTime StartTime {
7680
public class Coordinates {
7781
public double lat;
7882
public double lon;
79-
public string ele = null;
83+
public double? ele = null;
8084
public DateTime? time = null;
8185

8286
public Coordinates(string lat, string lon) {
8387
this.lat = Convert.ToDouble(lat);
8488
this.lon = Convert.ToDouble(lon);
8589
}
90+
public Coordinates(double lat, double lon, double? ele, DateTime? time) {
91+
this.lat = lat;
92+
this.lon = lon;
93+
this.ele = ele;
94+
this.time = time;
95+
}
8696
public Coordinates(double lat, double lon) {
8797
this.lat = lat;
8898
this.lon = lon;
@@ -97,7 +107,7 @@ public class Place {
97107
public Coordinates position;
98108
public DateTime? startTime;
99109
public DateTime? endTime;
100-
public string ele;
110+
public double? ele;
101111
public string link;
102112

103113
public int Duration {
@@ -111,10 +121,11 @@ public int Duration {
111121
}
112122
}
113123

114-
public Place(Coordinates position, string name, DateTime? startTime = null, string ele = null, string link = null) {
124+
public Place(Coordinates position, string name, DateTime? startTime = null, double? ele = null, string link = null) {
115125
this.position = position;
116126
this.startTime = startTime;
117127
this.name = name;
128+
this.ele = ele;
118129
this.link = link;
119130
}
120131

@@ -245,12 +256,16 @@ public class XmlReader {
245256
// Activity and places loading
246257
public XmlReader(string path, bool isPath, float weight) {
247258
this.weight = weight;
259+
260+
string allText = "";
248261
if (isPath) {
262+
allText = File.ReadAllText(path);
249263
originalName = path.Replace(".gpx", "");
250-
LoadFile(path);
251-
} else {
252-
LoadString(path);
253-
}
264+
} else
265+
allText = path;
266+
byte[] byteArray = Encoding.UTF8.GetBytes(allText);
267+
MemoryStream stream = new MemoryStream(byteArray);
268+
Load(stream);
254269
}
255270
public XmlReader(List<XmlTimeline.TimelineItem> timelineItems) {
256271
for (int i = 0; i < 10; i++) {
@@ -262,119 +277,72 @@ public XmlReader(List<XmlTimeline.TimelineItem> timelineItems) {
262277
SetXmlDate();
263278
}
264279

265-
public void Load(StreamReader sr) {
280+
public void Load(Stream stream) {
266281
for (int i = 0; i < 10; i++) {
267282
activitySummary[i] = new List<XmlTimeline.Activity>();
268283
}
269284

270-
// Ignore first 2 lines
271-
sr.ReadLine();
272-
sr.ReadLine();
273-
274285
// Loop
275286
timelineItems.Clear();
276-
while (true) {
277-
string line = sr.ReadLine().Replace("\t", "");
278-
if (line.StartsWith("<wpt", StringComparison.Ordinal))
279-
GetPlace(line, sr);
280-
else if (line.StartsWith("<trk", StringComparison.Ordinal))
281-
GetMove(sr);
282-
else if (line.StartsWith("</gpx", StringComparison.Ordinal))
283-
break;
284-
};
285-
sr.Close();
287+
GpxReader gpxReader = new GpxReader(stream);
288+
while (gpxReader.Read()) {
289+
switch (gpxReader.ObjectType) {
290+
case GpxObjectType.Metadata:
291+
//gpxReader.Metadata;
292+
break;
293+
case GpxObjectType.WayPoint:
294+
GetPlace(gpxReader.WayPoint);
295+
break;
296+
case GpxObjectType.Track:
297+
GetMove(gpxReader.Track);
298+
break;
299+
}
300+
}
286301
SetStartEnd();
287302
SetSummary();
288303
SetXmlDate();
289304

290305
//Display();
291306
}
292307

293-
public void LoadString(string text) {
294-
MemoryStream mStrm = new MemoryStream(Encoding.UTF8.GetBytes(text));
295-
StreamReader tempSteamR = new StreamReader(mStrm, System.Text.Encoding.UTF8, true);
296-
Load(tempSteamR);
297-
}
298-
public void LoadFile(string path) {
299-
StreamReader sr = new StreamReader(path);
300-
Load(sr);
301-
}
302-
303-
void GetPlace(string line, StreamReader sr) {
304-
if (line.EndsWith("/>"))
305-
return;
306-
XmlTimeline.Coordinates location = HelpMethods.GetLatLon(line);
307-
308+
void GetPlace(GpxTools.Gpx.GpxWayPoint waypoint) {
309+
// location
310+
XmlTimeline.Coordinates location = new XmlTimeline.Coordinates(waypoint.Latitude, waypoint.Longitude);
308311
// time
309-
DateTime? startTime = null;
310-
311-
// time
312-
string tempLine = sr.ReadLine().Replace("\t", "");
313-
if (tempLine.StartsWith("<time>")) {
314-
startTime = HelpMethods.ParseIso8601(
315-
HelpMethods.LeaveCenterFromString(
316-
tempLine,
317-
"<time>",
318-
"</time>"));
319-
tempLine = sr.ReadLine().Replace("\t", "");
320-
}
321-
312+
DateTime? startTime = waypoint.Time;
322313
// ele
323-
string ele = "";
324-
tempLine = sr.ReadLine().Replace("\t", "");
325-
if (tempLine.StartsWith("<ele>")) {
326-
ele = HelpMethods.LeaveCenterFromString(tempLine, "<ele>", "</ele>");
327-
tempLine = sr.ReadLine().Replace("\t", "");
328-
}
329-
314+
double? ele = waypoint.Elevation;
330315
// name (if exist)
331-
string name = "";
332-
if (tempLine.StartsWith("<name>")) {
333-
name = HelpMethods.LeaveCenterFromString(tempLine.Replace("&amp;", "&").Replace("&lt;", "<").Replace("&gt;", ">").Replace("&quot;", "\"").Replace("&apos;", "'"), "<name>", "</name>");
334-
tempLine = sr.ReadLine().Replace("\t", "");
335-
}
316+
string name = waypoint.Name;
336317
string link = "";
337-
if (tempLine.StartsWith("<link")) {
338-
link = HelpMethods.LeaveCenterFromString(tempLine, "<link href=\"", "\" />");
339-
tempLine = sr.ReadLine().Replace("\t", "");
340-
}
341-
318+
if (waypoint.Links.Count > 0)
319+
link = waypoint.Links[0].Href;
342320
// If previous is place
343-
if (timelineItems.Count >= 1 && timelineItems.Last().type == XmlTimeline.TimelineItemType.place)
344-
timelineItems.Last().place.endTime = startTime;
345-
346-
//if (timelineItems.Count >= 1 && timelineItems.Last().type == XmlTimeline.TimelineItemType.activity)
347-
// startTime = timelineItems.Last().activity.endTime;
321+
if (timelineItems.Count >= 1 && timelineItems.Last().type == XmlTimeline.TimelineItemType.place) {
322+
if (!timelineItems.Last().place.endTime.HasValue && startTime.HasValue)
323+
timelineItems.Last().place.endTime = startTime;
324+
else {
325+
startTime = timelineItems.Last().place.startTime;
326+
}
327+
} else if (timelineItems.Count >= 1 && timelineItems.Last().type == XmlTimeline.TimelineItemType.activity && !startTime.HasValue)
328+
startTime = timelineItems.Last().activity.endTime;
348329
timelineItems.Add(new XmlTimeline.TimelineItem(new XmlTimeline.Place(location, name, startTime, ele, link)));
349330
}
350-
void GetMove(StreamReader sr) {
331+
void GetMove(GpxTools.Gpx.GpxTrack track) {
332+
351333
// Type
352-
string line = sr.ReadLine().Replace("\t", "");
353-
if (line == "<trkseg />") {
354-
sr.ReadLine();
355-
return;
356-
}
357334
ActivityType type = ActivityType.car;
358-
if (line.StartsWith("<type>", StringComparison.CurrentCulture)) {
359-
line = HelpMethods.LeaveCenterFromString(line, "<type>", "</type>");
360-
Enum.TryParse(line, out type);
335+
Enum.TryParse(track.Type, out type);
361336

362-
// Track points
363-
line = sr.ReadLine().Replace("\t", "");
364-
}
365-
if (line == "<trkseg />") {
366-
sr.ReadLine();
367-
return;
368-
}
337+
// Track points
369338
List<XmlTimeline.Coordinates> coords = new List<XmlTimeline.Coordinates>();
370-
while (true) {
371-
line = sr.ReadLine().Replace("\t", "");
372-
if (line == "</trkseg>")
373-
break;
374-
else {
375-
AddWaypoint(line, sr, coords);
376-
}
339+
GpxTools.Gpx.GpxPointCollection<GpxTools.Gpx.GpxPoint> points = new GpxTools.Gpx.GpxPointCollection<GpxTools.Gpx.GpxPoint>();
340+
341+
points = track.ToGpxPoints();
342+
foreach (var item in points) {
343+
coords.Add(new XmlTimeline.Coordinates(item.Latitude, item.Longitude, item.Elevation, item.Time));
377344
}
345+
378346
if (coords.Count >= 2) {
379347
if (timelineItems.Count > 0 &&
380348
timelineItems[timelineItems.Count - 1].type == XmlTimeline.TimelineItemType.activity &&
@@ -385,39 +353,40 @@ void GetMove(StreamReader sr) {
385353
AddTimeToPreviousPlace(newActivity);
386354
timelineItems.Add(new XmlTimeline.TimelineItem(newActivity));
387355
AddTimeToPreviousPlace(newActivity);
388-
//activitySummary[(int)type].Add(newActivity);
389356
}
390357
}
391-
sr.ReadLine();
392-
}
393-
void AddWaypoint(string line, StreamReader sr, List<XmlTimeline.Coordinates> coords) {
394-
XmlTimeline.Coordinates location = HelpMethods.GetLatLon(line);
395-
location.ele = HelpMethods.LeaveCenterFromString(sr.ReadLine().Replace("\t", ""), "<ele>", "</ele>");
396-
location.time = HelpMethods.ParseIso8601(
397-
HelpMethods.LeaveCenterFromString(
398-
sr.ReadLine().Replace("\t", ""),
399-
"<time>",
400-
"</time>"));
401-
sr.ReadLine();
402-
coords.Add(location);
403358
}
404359
void AddTimeToPreviousPlace(XmlTimeline.Activity activity) {
405360
if (timelineItems.Count >= 1) {
406361
if (timelineItems.Last().type == XmlTimeline.TimelineItemType.place)
407362
timelineItems.Last().place.endTime = activity.startTime;
363+
if (timelineItems.Count >= 2)
364+
if (timelineItems[timelineItems.Count - 2].type == XmlTimeline.TimelineItemType.place && !timelineItems[timelineItems.Count - 2].place.endTime.HasValue)
365+
timelineItems[timelineItems.Count - 2].place.endTime = activity.startTime;
408366
}
409367
}
410368

411369
// End calculations
412370
void SetStartEnd() {
413371
if (timelineItems.First().type == XmlTimeline.TimelineItemType.place && !timelineItems.First().place.startTime.HasValue) {
414-
DateTime time = timelineItems.First().place.endTime.Value;
372+
DateTime time = new DateTime();
373+
if (timelineItems.First().place.endTime.HasValue)
374+
time = timelineItems.First().place.endTime.Value;
375+
else if (timelineItems[1].type == XmlTimeline.TimelineItemType.place && timelineItems[1].place.startTime.HasValue)
376+
time = timelineItems[1].place.startTime.Value;
377+
else if (timelineItems[1].type == XmlTimeline.TimelineItemType.activity)
378+
time = timelineItems[1].activity.startTime;
415379
DateTime newTime = new DateTime(time.Year, time.Month, time.Day, 0, 0, 0, time.Kind);
416380
timelineItems.First().place.startTime = newTime;
417381
}
418-
419382
if (timelineItems.Last().type == XmlTimeline.TimelineItemType.place && !timelineItems.Last().place.endTime.HasValue) {
420-
DateTime time = timelineItems.Last().place.startTime.Value;
383+
DateTime time = new DateTime();
384+
if (timelineItems.Last().place.startTime.HasValue)
385+
time = timelineItems.Last().place.startTime.Value;
386+
else if (timelineItems[timelineItems.Count - 2].type == XmlTimeline.TimelineItemType.place && timelineItems[timelineItems.Count - 2].place.endTime.HasValue)
387+
time = timelineItems[timelineItems.Count - 2].place.endTime.Value;
388+
else if (timelineItems[timelineItems.Count - 2].type == XmlTimeline.TimelineItemType.activity)
389+
time = timelineItems[timelineItems.Count - 2].activity.endTime;
421390
DateTime newTime = new DateTime(time.Year, time.Month, time.Day, 23, 59, 59, time.Kind);
422391
timelineItems.Last().place.endTime = newTime;
423392
}
@@ -462,6 +431,7 @@ void Display() {
462431
}
463432
Console.WriteLine();
464433
Console.ForegroundColor = ConsoleColor.DarkGray;
434+
Console.WriteLine("Lenght: " + summary.Length);
465435
foreach (var item in summary) {
466436
if (item.duration > 0)
467437
Console.WriteLine(item.ToString());
@@ -488,7 +458,9 @@ public static List<XmlReader> Split(XmlReader xml) {
488458
currentDate = item.ReturnDate();
489459
}
490460

491-
if (currentDate == item.ReturnDate()) {
461+
if (item.ReturnDate() == null) {
462+
463+
} if (currentDate == item.ReturnDate()) {
492464
tempList.Add(item);
493465
lastItem = item;
494466
} else {

ConverterLibrary/packages.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net461" />
3+
<package id="GpxTools.macharius40" version="1.0.2" targetFramework="net461" />
4+
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net461" />
45
</packages>

0 commit comments

Comments
 (0)