Skip to content

Commit

Permalink
v2.0
Browse files Browse the repository at this point in the history
- New GPX library, up to 20% faster conversion
- Fixed importing bugs that were crashing converter
  • Loading branch information
bionicl authored Apr 6, 2019
2 parents d5c57b0 + d714a39 commit 8996014
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 119 deletions.
5 changes: 3 additions & 2 deletions Arc-app-export-converter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@ public static void Main() {
Console.WriteLine("Places initialised");
}
SetupWeight();

DateTime startTime = DateTime.Now;
foreach (var item in ReturnFilePath()) {
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine();
Console.WriteLine("Opening file: " + item);
XmlReader xr = new XmlReader(item, true, weight);


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

// On finish
PlacesManager.SavePlaces();
Console.WriteLine(DateTime.Now - startTime);
}

static void SetupWeight() {
Expand Down
6 changes: 5 additions & 1 deletion ConverterLibrary/ConverterLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="GpxTools">
<HintPath>..\packages\GpxTools.macharius40.1.0.2\lib\netstandard2.0\GpxTools.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand All @@ -40,6 +43,7 @@
<Compile Include="JsonParser.cs" />
<Compile Include="PlacesManager.cs" />
<Compile Include="XmlReader.cs" />
<Compile Include="ElevationCalculator.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
4 changes: 4 additions & 0 deletions ConverterLibrary/ElevationCalculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using System;
public class ElevationCalculator {

}
2 changes: 1 addition & 1 deletion ConverterLibrary/JsonParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ static void WriteToFile(string json, string fileName) {
sw.Write(json);
sw.Close();
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine(string.Format("Json file created!"));
Console.WriteLine(string.Format("Json file created!\n") + fileName);
}
}

200 changes: 86 additions & 114 deletions ConverterLibrary/XmlReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Text;
using GpxTools;

public class XmlTimeline {

Expand Down Expand Up @@ -33,12 +34,15 @@ public override string ToString() {
}

//Time
public DateTime ReturnDate() {
public DateTime? ReturnDate() {
DateTime tempDate = new DateTime();
if (type == TimelineItemType.activity)
tempDate = activity.startTime;
else
else {
if (!place.startTime.HasValue)
return null;
tempDate = place.startTime.Value;
}
return new DateTime(tempDate.Year, tempDate.Month, tempDate.Day, 12, 0, 0, tempDate.Kind);
}

Expand Down Expand Up @@ -76,13 +80,19 @@ public DateTime StartTime {
public class Coordinates {
public double lat;
public double lon;
public string ele = null;
public double? ele = null;
public DateTime? time = null;

public Coordinates(string lat, string lon) {
this.lat = Convert.ToDouble(lat);
this.lon = Convert.ToDouble(lon);
}
public Coordinates(double lat, double lon, double? ele, DateTime? time) {
this.lat = lat;
this.lon = lon;
this.ele = ele;
this.time = time;
}
public Coordinates(double lat, double lon) {
this.lat = lat;
this.lon = lon;
Expand All @@ -97,7 +107,7 @@ public class Place {
public Coordinates position;
public DateTime? startTime;
public DateTime? endTime;
public string ele;
public double? ele;
public string link;

public int Duration {
Expand All @@ -111,10 +121,11 @@ public int Duration {
}
}

public Place(Coordinates position, string name, DateTime? startTime = null, string ele = null, string link = null) {
public Place(Coordinates position, string name, DateTime? startTime = null, double? ele = null, string link = null) {
this.position = position;
this.startTime = startTime;
this.name = name;
this.ele = ele;
this.link = link;
}

Expand Down Expand Up @@ -245,12 +256,16 @@ public class XmlReader {
// Activity and places loading
public XmlReader(string path, bool isPath, float weight) {
this.weight = weight;

string allText = "";
if (isPath) {
allText = File.ReadAllText(path);
originalName = path.Replace(".gpx", "");
LoadFile(path);
} else {
LoadString(path);
}
} else
allText = path;
byte[] byteArray = Encoding.UTF8.GetBytes(allText);
MemoryStream stream = new MemoryStream(byteArray);
Load(stream);
}
public XmlReader(List<XmlTimeline.TimelineItem> timelineItems) {
for (int i = 0; i < 10; i++) {
Expand All @@ -262,119 +277,72 @@ public XmlReader(List<XmlTimeline.TimelineItem> timelineItems) {
SetXmlDate();
}

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

// Ignore first 2 lines
sr.ReadLine();
sr.ReadLine();

// Loop
timelineItems.Clear();
while (true) {
string line = sr.ReadLine().Replace("\t", "");
if (line.StartsWith("<wpt", StringComparison.Ordinal))
GetPlace(line, sr);
else if (line.StartsWith("<trk", StringComparison.Ordinal))
GetMove(sr);
else if (line.StartsWith("</gpx", StringComparison.Ordinal))
break;
};
sr.Close();
GpxReader gpxReader = new GpxReader(stream);
while (gpxReader.Read()) {
switch (gpxReader.ObjectType) {
case GpxObjectType.Metadata:
//gpxReader.Metadata;
break;
case GpxObjectType.WayPoint:
GetPlace(gpxReader.WayPoint);
break;
case GpxObjectType.Track:
GetMove(gpxReader.Track);
break;
}
}
SetStartEnd();
SetSummary();
SetXmlDate();

//Display();
}

public void LoadString(string text) {
MemoryStream mStrm = new MemoryStream(Encoding.UTF8.GetBytes(text));
StreamReader tempSteamR = new StreamReader(mStrm, System.Text.Encoding.UTF8, true);
Load(tempSteamR);
}
public void LoadFile(string path) {
StreamReader sr = new StreamReader(path);
Load(sr);
}

void GetPlace(string line, StreamReader sr) {
if (line.EndsWith("/>"))
return;
XmlTimeline.Coordinates location = HelpMethods.GetLatLon(line);

void GetPlace(GpxTools.Gpx.GpxWayPoint waypoint) {
// location
XmlTimeline.Coordinates location = new XmlTimeline.Coordinates(waypoint.Latitude, waypoint.Longitude);
// time
DateTime? startTime = null;

// time
string tempLine = sr.ReadLine().Replace("\t", "");
if (tempLine.StartsWith("<time>")) {
startTime = HelpMethods.ParseIso8601(
HelpMethods.LeaveCenterFromString(
tempLine,
"<time>",
"</time>"));
tempLine = sr.ReadLine().Replace("\t", "");
}

DateTime? startTime = waypoint.Time;
// ele
string ele = "";
tempLine = sr.ReadLine().Replace("\t", "");
if (tempLine.StartsWith("<ele>")) {
ele = HelpMethods.LeaveCenterFromString(tempLine, "<ele>", "</ele>");
tempLine = sr.ReadLine().Replace("\t", "");
}

double? ele = waypoint.Elevation;
// name (if exist)
string name = "";
if (tempLine.StartsWith("<name>")) {
name = HelpMethods.LeaveCenterFromString(tempLine.Replace("&amp;", "&").Replace("&lt;", "<").Replace("&gt;", ">").Replace("&quot;", "\"").Replace("&apos;", "'"), "<name>", "</name>");
tempLine = sr.ReadLine().Replace("\t", "");
}
string name = waypoint.Name;
string link = "";
if (tempLine.StartsWith("<link")) {
link = HelpMethods.LeaveCenterFromString(tempLine, "<link href=\"", "\" />");
tempLine = sr.ReadLine().Replace("\t", "");
}

if (waypoint.Links.Count > 0)
link = waypoint.Links[0].Href;
// If previous is place
if (timelineItems.Count >= 1 && timelineItems.Last().type == XmlTimeline.TimelineItemType.place)
timelineItems.Last().place.endTime = startTime;

//if (timelineItems.Count >= 1 && timelineItems.Last().type == XmlTimeline.TimelineItemType.activity)
// startTime = timelineItems.Last().activity.endTime;
if (timelineItems.Count >= 1 && timelineItems.Last().type == XmlTimeline.TimelineItemType.place) {
if (!timelineItems.Last().place.endTime.HasValue && startTime.HasValue)
timelineItems.Last().place.endTime = startTime;
else {
startTime = timelineItems.Last().place.startTime;
}
} else if (timelineItems.Count >= 1 && timelineItems.Last().type == XmlTimeline.TimelineItemType.activity && !startTime.HasValue)
startTime = timelineItems.Last().activity.endTime;
timelineItems.Add(new XmlTimeline.TimelineItem(new XmlTimeline.Place(location, name, startTime, ele, link)));
}
void GetMove(StreamReader sr) {
void GetMove(GpxTools.Gpx.GpxTrack track) {

// Type
string line = sr.ReadLine().Replace("\t", "");
if (line == "<trkseg />") {
sr.ReadLine();
return;
}
ActivityType type = ActivityType.car;
if (line.StartsWith("<type>", StringComparison.CurrentCulture)) {
line = HelpMethods.LeaveCenterFromString(line, "<type>", "</type>");
Enum.TryParse(line, out type);
Enum.TryParse(track.Type, out type);

// Track points
line = sr.ReadLine().Replace("\t", "");
}
if (line == "<trkseg />") {
sr.ReadLine();
return;
}
// Track points
List<XmlTimeline.Coordinates> coords = new List<XmlTimeline.Coordinates>();
while (true) {
line = sr.ReadLine().Replace("\t", "");
if (line == "</trkseg>")
break;
else {
AddWaypoint(line, sr, coords);
}
GpxTools.Gpx.GpxPointCollection<GpxTools.Gpx.GpxPoint> points = new GpxTools.Gpx.GpxPointCollection<GpxTools.Gpx.GpxPoint>();

points = track.ToGpxPoints();
foreach (var item in points) {
coords.Add(new XmlTimeline.Coordinates(item.Latitude, item.Longitude, item.Elevation, item.Time));
}

if (coords.Count >= 2) {
if (timelineItems.Count > 0 &&
timelineItems[timelineItems.Count - 1].type == XmlTimeline.TimelineItemType.activity &&
Expand All @@ -385,39 +353,40 @@ void GetMove(StreamReader sr) {
AddTimeToPreviousPlace(newActivity);
timelineItems.Add(new XmlTimeline.TimelineItem(newActivity));
AddTimeToPreviousPlace(newActivity);
//activitySummary[(int)type].Add(newActivity);
}
}
sr.ReadLine();
}
void AddWaypoint(string line, StreamReader sr, List<XmlTimeline.Coordinates> coords) {
XmlTimeline.Coordinates location = HelpMethods.GetLatLon(line);
location.ele = HelpMethods.LeaveCenterFromString(sr.ReadLine().Replace("\t", ""), "<ele>", "</ele>");
location.time = HelpMethods.ParseIso8601(
HelpMethods.LeaveCenterFromString(
sr.ReadLine().Replace("\t", ""),
"<time>",
"</time>"));
sr.ReadLine();
coords.Add(location);
}
void AddTimeToPreviousPlace(XmlTimeline.Activity activity) {
if (timelineItems.Count >= 1) {
if (timelineItems.Last().type == XmlTimeline.TimelineItemType.place)
timelineItems.Last().place.endTime = activity.startTime;
if (timelineItems.Count >= 2)
if (timelineItems[timelineItems.Count - 2].type == XmlTimeline.TimelineItemType.place && !timelineItems[timelineItems.Count - 2].place.endTime.HasValue)
timelineItems[timelineItems.Count - 2].place.endTime = activity.startTime;
}
}

// End calculations
void SetStartEnd() {
if (timelineItems.First().type == XmlTimeline.TimelineItemType.place && !timelineItems.First().place.startTime.HasValue) {
DateTime time = timelineItems.First().place.endTime.Value;
DateTime time = new DateTime();
if (timelineItems.First().place.endTime.HasValue)
time = timelineItems.First().place.endTime.Value;
else if (timelineItems[1].type == XmlTimeline.TimelineItemType.place && timelineItems[1].place.startTime.HasValue)
time = timelineItems[1].place.startTime.Value;
else if (timelineItems[1].type == XmlTimeline.TimelineItemType.activity)
time = timelineItems[1].activity.startTime;
DateTime newTime = new DateTime(time.Year, time.Month, time.Day, 0, 0, 0, time.Kind);
timelineItems.First().place.startTime = newTime;
}

if (timelineItems.Last().type == XmlTimeline.TimelineItemType.place && !timelineItems.Last().place.endTime.HasValue) {
DateTime time = timelineItems.Last().place.startTime.Value;
DateTime time = new DateTime();
if (timelineItems.Last().place.startTime.HasValue)
time = timelineItems.Last().place.startTime.Value;
else if (timelineItems[timelineItems.Count - 2].type == XmlTimeline.TimelineItemType.place && timelineItems[timelineItems.Count - 2].place.endTime.HasValue)
time = timelineItems[timelineItems.Count - 2].place.endTime.Value;
else if (timelineItems[timelineItems.Count - 2].type == XmlTimeline.TimelineItemType.activity)
time = timelineItems[timelineItems.Count - 2].activity.endTime;
DateTime newTime = new DateTime(time.Year, time.Month, time.Day, 23, 59, 59, time.Kind);
timelineItems.Last().place.endTime = newTime;
}
Expand Down Expand Up @@ -462,6 +431,7 @@ void Display() {
}
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("Lenght: " + summary.Length);
foreach (var item in summary) {
if (item.duration > 0)
Console.WriteLine(item.ToString());
Expand All @@ -488,7 +458,9 @@ public static List<XmlReader> Split(XmlReader xml) {
currentDate = item.ReturnDate();
}

if (currentDate == item.ReturnDate()) {
if (item.ReturnDate() == null) {

} if (currentDate == item.ReturnDate()) {
tempList.Add(item);
lastItem = item;
} else {
Expand Down
3 changes: 2 additions & 1 deletion ConverterLibrary/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net461" />
<package id="GpxTools.macharius40" version="1.0.2" targetFramework="net461" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net461" />
</packages>

0 comments on commit 8996014

Please sign in to comment.