/// <summary> /// Gets the default is trip possible function. /// </summary> public static Func <uint, DateTime, bool> GetIsTripPossibleFunc(this TransitDb db) { var tripEnumerator = db.GetTripsEnumerator(); var schedulesSnumerator = db.GetSchedulesEnumerator(); return((tripId, day) => { if (tripEnumerator.MoveTo(tripId)) { if (schedulesSnumerator.MoveTo(tripEnumerator.ScheduleId)) { return schedulesSnumerator.DateIsSet(day); } } return false; }); }
/// <summary> /// Copies all core data stops, schedules, trips, and connections from the given transit db. /// </summary> public static void CopyFrom(this TransitDb db, TransitDb other) { if (db == null) { throw new ArgumentNullException("db"); } if (other == null) { throw new ArgumentNullException("other"); } if (other.ConnectionSorting == null) { throw new ArgumentException("A database can only be copied if connections are sorted."); } var stopIdsBlockSize = 1024 * 32; var stopIds = new MemoryArray <uint>(stopIdsBlockSize); var tripIdsBlockSize = 1024 * 32; var tripIds = new MemoryArray <uint>(tripIdsBlockSize); var scheduleIds = new Dictionary <uint, uint>(); var agencyIds = new Dictionary <uint, uint>(); // copy stops and keep id transformations. var stopsEnumerator = other.GetStopsEnumerator(); while (stopsEnumerator.MoveNext()) { var stopsMeta = other.StopAttributes.Get(stopsEnumerator.MetaId); var newMetaId = db.StopAttributes.Add(stopsMeta); var newStopId = db.AddStop(stopsEnumerator.Latitude, stopsEnumerator.Longitude, newMetaId); if (stopsEnumerator.Id >= stopIds.Length) { stopIds.Resize(stopIds.Length + stopIdsBlockSize); } stopIds[stopsEnumerator.Id] = newStopId; } // copy trips, copy schedules that have not been copied yet, and keep trip id transformations. var tripsEnumerator = other.GetTripsEnumerator(); var scheduleEnumerator = other.GetSchedulesEnumerator(); while (tripsEnumerator.MoveNext()) { var tripsMeta = other.TripAttributes.Get(tripsEnumerator.MetaId); var newMetaId = db.TripAttributes.Add(tripsMeta); uint newAgencyMetaId = uint.MaxValue; if (!agencyIds.TryGetValue(tripsEnumerator.AgencyId, out newAgencyMetaId)) { var agencyMeta = other.AgencyAttributes.Get(tripsEnumerator.AgencyId); newAgencyMetaId = db.AgencyAttributes.Add(agencyMeta); agencyIds.Add(tripsEnumerator.AgencyId, newAgencyMetaId); } uint newScheduleId = uint.MaxValue; if (!scheduleIds.TryGetValue(tripsEnumerator.ScheduleId, out newScheduleId)) { if (scheduleEnumerator.MoveTo(tripsEnumerator.ScheduleId)) { newScheduleId = scheduleEnumerator.CopyTo(db.SchedulesDb); scheduleIds[tripsEnumerator.ScheduleId] = newScheduleId; } } var newTripId = db.AddTrip(newScheduleId, newAgencyMetaId, newMetaId); if (tripsEnumerator.Id >= tripIds.Length) { tripIds.Resize(tripIds.Length + tripIdsBlockSize); } tripIds[tripsEnumerator.Id] = newTripId; } // copy connections. var connectionEnumerator = other.GetConnectionsEnumerator(other.ConnectionSorting.Value); while (connectionEnumerator.MoveNext()) { var newArrivalStop = stopIds[connectionEnumerator.ArrivalStop]; var newDepartureStop = stopIds[connectionEnumerator.DepartureStop]; var newTripId = tripIds[connectionEnumerator.TripId]; db.AddConnection(newDepartureStop, newArrivalStop, newTripId, connectionEnumerator.DepartureTime, connectionEnumerator.ArrivalTime); } // copy shapes. if (other.ShapesDb != null) { var shapesEnumerator = other.ShapesDb.GetEnumerator(); while (shapesEnumerator.MoveNext()) { var stop1 = stopIds[shapesEnumerator.Stop1]; var stop2 = stopIds[shapesEnumerator.Stop2]; db.ShapesDb.Add(stop1, stop2, shapesEnumerator.Shape); } } }