Skip to content

Commit

Permalink
Add code
Browse files Browse the repository at this point in the history
  • Loading branch information
DJm00n committed Nov 23, 2017
1 parent 9314ea9 commit e627605
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 1 deletion.
171 changes: 170 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,184 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Json;
using MKCoolsoft.GPXLib;


namespace starlineonline2gpx
{
class Date
{
private static DateTime UnixEpoch =
new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static TimeZoneInfo TimeZone = TimeZoneInfo.Local;

public static long minSeconds = long.MaxValue;
public static long maxSeconds = long.MinValue;

public static DateTime DateTimeFromUnixTimestampSeconds(long seconds)
{
if (seconds < minSeconds) minSeconds = seconds;
if (seconds > maxSeconds) maxSeconds = seconds;
return UnixEpoch.AddSeconds(seconds);
}
}

class Program
{
static decimal Minlat = decimal.MaxValue;
static decimal Maxlat = decimal.MinValue;
static decimal Minlon = decimal.MaxValue;
static decimal Maxlon = decimal.MinValue;

static void Main(string[] args)
{
if (args.Length > 0 && File.Exists(args[0]))
{
var path = args[0];
using (StreamReader sr = new StreamReader(path))
{
var json = JsonValue.Parse(sr.ReadToEnd());

GPXLib gpx = new GPXLib();

var meta = json["meta"];
var data = json["data"];
var trackNum = 0;
var waypointNum = 0;
foreach(JsonValue elem in data)
{
string type = elem["type"];
switch(type)
{
case "TZ":
ParseTimezone(gpx, elem);
break;
case "TRACK":
ParseTrack(gpx, elem, ++trackNum);
break;
case "NO_SIGNAL":
ParseNoSignal(gpx, elem);
break;
case "WAYPOINT":
ParseWaypoint(gpx, elem, ++waypointNum);
break;
case "STOP":
ParseStop(gpx, elem);
break;
default:
throw new NotImplementedException();

}
}


gpx.Metadata.Name = "Starline GPX Track";

var startDate = Date.DateTimeFromUnixTimestampSeconds(Date.minSeconds);
var endDate = Date.DateTimeFromUnixTimestampSeconds(Date.maxSeconds);
gpx.Metadata.Desc = String.Format("Track from {0} to {1}", startDate, endDate);

gpx.Creator = "starlineonline2gpx converter";

gpx.Metadata.Bounds.Maxlat = Maxlat;
gpx.Metadata.Bounds.Minlat = Minlat;
gpx.Metadata.Bounds.Maxlon = Maxlon;
gpx.Metadata.Bounds.Minlon = Minlon;
gpx.Metadata.Time = startDate;
gpx.Metadata.TimeSpecified = true;

var author = new Person();
author.Link.Href = @"https://github.com/DJm00n/starlineonline2gpx";
author.Email.Id = @"dimitriy.ryazantcev";
author.Email.Domain = "gmail.com";
author.Name = @"Dimitriy Ryazantcev";

gpx.Metadata.Author = author;


gpx.SaveToFile(Path.ChangeExtension(path, "gpx"));
}
}
}

private static void TrackMinMax(decimal lat, decimal lon)
{
if (lat < Minlat) Minlat = lat;
if (lat > Maxlat) Maxlat = lat;

if (lon < Minlon) Minlon = lon;
if (lon > Maxlon) Maxlon = lon;
}

private static void ParseNoSignal(GPXLib gpx, JsonValue elem)
{
//throw new NotImplementedException();
}

private static void ParseStop(GPXLib gpx, JsonValue elem)
{
//throw new NotImplementedException();
}

private static void ParseWaypoint(GPXLib gpx, JsonValue elem, int waypointNum)
{
Wpt waypoint = new Wpt
{
Name = String.Format("Waypoint {0}", waypointNum),
Lat = elem["x"],
Lon = elem["y"],
Ele = elem["z"],
EleSpecified = elem.ContainsKey("z") && elem["z"] != 0,
Sat = Convert.ToString((int)elem["sat_qty"]),
Time = Date.DateTimeFromUnixTimestampSeconds(elem["t"]),
TimeSpecified = elem.ContainsKey("t")
};

gpx.WptList.Add(waypoint);
TrackMinMax(waypoint.Lat, waypoint.Lon);
}

private static void ParseTimezone(GPXLib gpx, JsonValue elem)
{
string displayName = "Local Time Zone";
string standardName = "Local Time";
TimeSpan offset = TimeSpan.FromSeconds(elem["time_shift"]);
TimeZoneInfo local = TimeZoneInfo.CreateCustomTimeZone(standardName, offset, displayName, standardName);
Date.TimeZone = local;
//TODO save this timezone somewhere in gpx file
}

private static void ParseTrack(GPXLib gpx, JsonValue elem, int trackNum)
{
Trkseg segment = new Trkseg();

foreach (JsonValue node in elem["nodes"])
{
Wpt wpt = new Wpt
{
Lat = node["x"],
Lon = node["y"],
Ele = node["z"],
EleSpecified = node.ContainsKey("z") && node["z"] != 0,
Sat = Convert.ToString((int)node["sat_qty"]),
Time = Date.DateTimeFromUnixTimestampSeconds(node["t"]),
TimeSpecified = node.ContainsKey("t")
};

segment.TrkptList.Add(wpt);
TrackMinMax(wpt.Lat, wpt.Lon);
}

Trk track = new Trk(String.Format("Track {0}", trackNum))
{
Number = trackNum.ToString()
};
track.TrksegList.Add(segment);

gpx.TrkList.Add(track);
}
}
}
5 changes: 5 additions & 0 deletions packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MKCoolsoft.GPXLib" version="1.0.2" targetFramework="net452" />
<package id="System.Json" version="4.4.0" targetFramework="net452" />
</packages>
9 changes: 9 additions & 0 deletions starlineonline2gpx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,16 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="MKCoolsoft.GPXLib, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\MKCoolsoft.GPXLib.1.0.2\lib\MKCoolsoft.GPXLib.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Json, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\System.Json.4.4.0\lib\netstandard1.0\System.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -48,6 +56,7 @@
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down

0 comments on commit e627605

Please sign in to comment.