/// <summary> /// Adds a transfers db. /// </summary> public static void AddTransfersDb(this TransitDb db, Profiles.Profile profile, IAttributeCollection defaultProfile, float maxTimeInSeconds) { var transfersDb = new TransfersDb(db.StopsCount); var factor = profile.Factor(defaultProfile); // add all transfers. var enumerator1 = db.GetStopsEnumerator(); while (enumerator1.MoveNext()) { var enumerator2 = db.GetStopsEnumerator(); while (enumerator2.MoveNext()) { if (enumerator1.Id < enumerator2.Id) { var distance = Coordinate.DistanceEstimateInMeter(enumerator1.Latitude, enumerator1.Longitude, enumerator2.Latitude, enumerator2.Longitude); var time = (int)System.Math.Round(distance * factor.Value, 0); if (time < maxTimeInSeconds) { transfersDb.AddTransfer(enumerator1.Id, enumerator2.Id, time); } } } } db.AddTransfersDb(profile, transfersDb); }
/// <summary> /// Gets the meta-data for the given stop. /// </summary> public static IAttributeCollection GetStopMeta(this TransitDb db, uint stopId) { var enumerator = db.GetStopsEnumerator(); if (!enumerator.MoveTo(stopId)) { return(null); } return(db.StopAttributes.Get(enumerator.MetaId)); }
/// <summary> /// Searches for the first stop with some tags or based on some condition. /// </summary> public static uint SearchFirstStopsWithTags(this TransitDb db, Func <IAttributeCollection, bool> condition) { var stops = new HashSet <uint>(); var enumerator = db.GetStopsEnumerator(); enumerator.Reset(); while (enumerator.MoveNext()) { var stopTags = db.StopAttributes.Get(enumerator.MetaId); if (condition(stopTags)) { return(enumerator.Id); } } return(Constants.NoStopId); }
/// <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); } } }