Skip to content
Snippets Groups Projects
Commit 92e5de8a authored by Knuiman, Bart's avatar Knuiman, Bart
Browse files

Remove GTFS

parent 69f8ac65
No related branches found
No related tags found
No related merge requests found
using Sylvan.Data.Csv;
using System.IO.Compression;
using System.Reflection;
namespace WanderOV
{
public enum TransportType
{
Bus,
Tram,
Ferry,
Train,
Unknown
}
public class Line
{
public string owner;
public string linePlanningNumber;
public string publicNumber; // What is shown on display front Bus/Tram. String because might C11.
public string fromName;
public string toName;
public TransportType transportType;
}
public class Destination
{
public string owner;
public string destCode;
public string mainDest;
public string destDetail;
}
public class Stop
{
public float lat;
public float lon;
public string name;
}
public class Trip
{
public string owner;
public string journeyCode;
public Line line;
public Destination dest;
public List<Stop> stops;
}
public class GTFS
{
string curZipPath;
string curEntry;
// Some codes appear to start with a character, therefore we use strings instead of ints.
Dictionary<int, Stop> stops = new Dictionary<int, Stop>();
Dictionary<string, Line> lines = new Dictionary<string, Line>();
Dictionary<string, Destination> destinations = new Dictionary<string, Destination>();
Dictionary<string, Trip> trips = new Dictionary<string, Trip>();
public void PrintStats()
{
Console.WriteLine( $"Stops {stops.Count}" );
Console.WriteLine( $"Lines {lines.Count}" );
Console.WriteLine( $"Destinations {destinations.Count}" );
Console.WriteLine( $"Trips {trips.Count}" );
}
static string MakeKey( CsvDataReader csv, out string owner, out string code )
{
owner = csv.GetString( 3 );
code = csv.GetString( 4 );
return string.Join( ':', owner, code );
}
void LoadLine( CsvDataReader csv )
{
var key = MakeKey( csv, out var owner, out var code );
if (!lines.ContainsKey( key ))
{
var line = new Line();
line.owner = owner;
line.linePlanningNumber = code;
line.publicNumber = csv.GetString( 5 );
string lineName = csv.GetString( 6 );
string [] fromTo = lineName.Split( '-' );
if (fromTo!=null)
{
if (fromTo.Length > 0)
line.fromName = fromTo[0];
if (fromTo.Length > 1)
line.toName = fromTo[1];
}
line.transportType = csv.GetString( 9 ) switch
{
"BUS" => TransportType.Bus,
"TRAM" => TransportType.Tram,
"TRAIN" => TransportType.Train,
"FERRY" => TransportType.Ferry,
_ => TransportType.Unknown
};
lines.Add( key, line );
}
else
{
Console.WriteLine($"Found duplicate line {code} in {curZipPath} and file {curEntry}");
}
}
void LoadDest( CsvDataReader csv )
{
var key = MakeKey( csv, out var owner, out var code );
if (!destinations.ContainsKey( key ))
{
var dest = new Destination();
dest.owner = owner;
dest.destCode = code;
dest.mainDest = csv.GetString( 6 );
dest.destDetail = csv.GetString( 7 );
destinations.Add( key, dest );
}
else
{
// It appears that multiple files have the same destination encoded, this happes because of region traffic. Ignore it.
// Console.WriteLine( $"Found duplicate destination {code} in {curZipPath} and file {curEntry}" );
}
}
void LoadStop( CsvDataReader csv )
{
var key = MakeKey( csv, out var owner, out var code );
if (!stops.ContainsKey( key ))
{
var stop = new Stop();
stop.owner = owner;
stop.pointCode = code;
stop.rdx = csv.GetInt32( 8 );
stop.rdy = csv.GetInt32( 9 );
var desc = csv.GetString( 11 );
string[] cityStop = desc.Split( ',' );
if ( cityStop.Length > 1 )
{
stop.name = cityStop[1];
stop.city = cityStop[0];
}
else if ( cityStop.Length > 0 )
{
stop.name = cityStop[0];
}
stops.Add( key, stop );
}
else
{
// Console.WriteLine( $"Found duplicate stop {pointCode} in {curZipPath} and file {curEntry}" );
}
}
void LoadJourney( CsvDataReader csv )
{
var key = MakeKey( csv, out var owner, out var code );
if (!journeys.TryGetValue( key, out var journey ))
{
journey = new Journey();
journey.owner = owner;
journey.journeyCode = code;
destinations.TryGetValue( csv.GetString( 10 ), out journey.dest );
lines.TryGetValue( csv.GetString( 4 ), out journey.line );
journey.stops = new List<Stop>();
journeys.Add( key , journey );
}
stops.TryGetValue( csv.GetString( 8 ), out var stop );
journey.stops.Add( stop );
}
void LoadFile(string tag, ZipArchiveEntry entry, Action<CsvDataReader> cb )
{
if (entry.Name.StartsWith( tag ) && entry.Name.EndsWith( "TMI" ) && !entry.Name.EndsWith("DTMI")/*Dead*/)
{
curEntry = entry.Name;
using var sr = new StreamReader( entry.Open() );
using var csv = CsvDataReader.Create( sr );
while (csv.Read()) { cb( csv ); }
}
}
public void LoadAll(string folder)
{
string strExeFilePath = Assembly.GetExecutingAssembly().Location;
string strWorkPath = Path.GetDirectoryName(strExeFilePath);
string searchPath = Path.Combine( strWorkPath, folder );
if (!Directory.Exists( searchPath ))
{
Console.WriteLine( searchPath + " does not exist, nothing loaded." );
return;
}
var zippedTmiArchives = Directory.GetFiles( searchPath, "*.zip", SearchOption.AllDirectories );
foreach ( var zipPath in zippedTmiArchives)
{
curZipPath = Path.GetFileName( zipPath );
if (zipPath == null)
continue;
Console.WriteLine( "Loaded " + Path.GetFileName( zipPath ));
// try
{
using var archive = ZipFile.OpenRead( zipPath );
foreach (ZipArchiveEntry entry in archive.Entries)
{
LoadFile( "LINE", entry, LoadLine );
LoadFile( "DEST", entry, LoadDest );
LoadFile( "POINT", entry, LoadStop );
}
var jopali = archive.Entries.Where(e => e.Name.Contains( "JOPATILI" ) );
foreach( ZipArchiveEntry entry in jopali )
{
LoadFile( "JOPATILI", entry, LoadJourney );
}
}
// catch { }
}
}
}
}
\ No newline at end of file
......@@ -15,10 +15,6 @@
<None Remove="Code\GTFS.cs_" />
</ItemGroup>
<ItemGroup>
<Resource Include="Code\OVParsers\GTFS.cs_" />
</ItemGroup>
<ItemGroup>
<None Include="..\.editorconfig" Link=".editorconfig" />
<None Include="Code\OVParsers\KV1.cs" />
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment