/// <summary> /// Sorts the stops. /// </summary> public void Sort(Action <uint, uint> switchConnections) { if (_nextId > 0) { // sort stops, assume all stops are filled-in. QuickSort.Sort((stop) => { var latitude = StopsDb.DecodeSingle(_data[stop * SIZE + 0]); var longitude = StopsDb.DecodeSingle(_data[stop * SIZE + 1]); return(Itinero.Algorithms.Search.Hilbert.HilbertCurve.HilbertDistance(latitude, longitude, HilbertExtensions.DefaultHilbertSteps)); }, (stop1, stop2) => { var stop10 = _data[stop1 * SIZE + 0]; var stop11 = _data[stop1 * SIZE + 1]; var stop12 = _data[stop1 * SIZE + 2]; _data[stop1 * SIZE + 0] = _data[stop2 * SIZE + 0]; _data[stop1 * SIZE + 1] = _data[stop2 * SIZE + 1]; _data[stop1 * SIZE + 2] = _data[stop2 * SIZE + 2]; _data[stop2 * SIZE + 0] = stop10; _data[stop2 * SIZE + 1] = stop11; _data[stop2 * SIZE + 2] = stop12; if (switchConnections != null) { switchConnections((uint)stop1, (uint)stop2); } }, 0, _nextId - 1); } }
/// <summary> /// Creates a new transit db. /// </summary> public TransitDb() { _agencyAttributes = new AttributesIndex(AttributesIndexMode.ReverseStringIndex); _connectionsDb = new ConnectionsDb(); _stopsDb = new StopsDb(); _stopAttributes = new AttributesIndex(AttributesIndexMode.ReverseStringIndex); _tripsDb = new TripsDb(); _tripAttributes = new AttributesIndex(AttributesIndexMode.ReverseStringIndex); _transfersDbs = new Dictionary <string, TransfersDb>(); _schedulesDb = new SchedulesDb(); _shapesDb = new ShapesDb(); }
/// <summary> /// Deserializes from stream. /// </summary> public static TransitDb Deserialize(Stream stream) { var version = stream.ReadByte(); if (version > 2) { throw new Exception("Cannot deserialize db, version # doesn't match."); } // read agencies attributes. var agencyAttributes = AttributesIndex.Deserialize(new LimitedStream(stream), true); // read connections db. var connectionsDb = ConnectionsDb.Deserialize(stream); // read schedules db. var schedulesDb = SchedulesDb.Deserialize(stream); // read stop attributes. var stopAttributes = AttributesIndex.Deserialize(new LimitedStream(stream), true); // read stop db. var stopsDb = StopsDb.Deserialize(stream); // read transfer db's. var transferDbsCount = stream.ReadByte(); var transferDbs = new Dictionary <string, TransfersDb>(); for (var i = 0; i < transferDbsCount; i++) { var profileName = stream.ReadWithSizeString(); var transferDb = TransfersDb.Deserialize(stream); transferDbs.Add(profileName, transferDb); } // read trip attributes. var tripAttributes = AttributesIndex.Deserialize(new LimitedStream(stream), true); // write trips db. var tripsDb = TripsDb.Deserialize(stream); ShapesDb shapesDb = null; if (version != 1) { // write shapes db. shapesDb = ShapesDb.Deserialize(stream); } return(new TransitDb(agencyAttributes, connectionsDb, schedulesDb, stopAttributes, stopsDb, transferDbs, tripAttributes, tripsDb, shapesDb)); }
/// <summary> /// Creates a new transit db. /// </summary> private TransitDb(AttributesIndex agencyAttributes, ConnectionsDb connectionsDb, SchedulesDb schedulesDb, AttributesIndex stopAttributes, StopsDb stopsDb, Dictionary <string, TransfersDb> transferDbs, AttributesIndex tripAttributes, TripsDb tripsDb, ShapesDb shapesDb) { _agencyAttributes = agencyAttributes; _connectionsDb = connectionsDb; _schedulesDb = schedulesDb; _stopAttributes = stopAttributes; _stopsDb = stopsDb; _transfersDbs = transferDbs; _tripAttributes = tripAttributes; _tripsDb = tripsDb; _shapesDb = shapesDb; }
/// <summary> /// Adds a new stop. /// </summary> public uint Add(float latitude, float longitude, uint metaId) { var id = _nextId; _nextId++; var size = _data.Length; while ((id * SIZE + SIZE) > size) { size += BLOCK_SIZE; } if (size != _data.Length) { _data.Resize(size); } _data[id * SIZE + 0] = StopsDb.Encode(latitude); _data[id * SIZE + 1] = StopsDb.Encode(longitude); _data[id * SIZE + 2] = metaId; return(id); }