Example #1
0
            public int CompareTo(StopTimeList other)
            {
                if (Route != other.Route)
                {
                    return(Route.GetHashCode() - other.Route.GetHashCode());
                }

                if (Count != other.Count)
                {
                    return(Count - other.Count);
                }
                for (int i = 0; i < Count; i++)
                {
                    if (this[i].Item1 != other[i].Item1)
                    {
                        return(this[i].Item1.GetHashCode() - other[i].Item1.GetHashCode());
                    }
                    if (this[i].Item2 != other[i].Item2)
                    {
                        return(this[i].Item2.CompareTo(other[i].Item2));
                    }
                }
                //for (int i = 0; i < Count; i++)
                //    if (this[i].Item3 != other[i].Item3)
                //        throw new Exception("Wrong stop_headsign field (ambigousity)");
                return(0);
            }
Example #2
0
        private void convertStopTimesTable()
        {
            //sdb.RegisterTableType(typeof(TripTypeHeadsign));
            var set       = new SortedDictionary <StopTimeList, List <string> >();
            var timeDiffs = new List <int>();
            var seenTrips = new HashSet <string>();

            using (var table = gtfs.GetTable("stop_times"))
            {
                string       lastTrip  = null;
                StopTimeList curList   = null;
                TimeSpan     startTime = new TimeSpan();
                foreach (var record in table.Records)
                {
                    string trip          = record["trip_id"];
                    string arrivalTime   = record["arrival_time"];
                    string departureTime = record["departure_time"];
                    if (trip != lastTrip)
                    {
                        if (lastTrip != null)
                        {
                            if (!set.Keys.Contains(curList))
                            {
                                set.Add(curList, new List <String>());
                            }
                            set[curList].Add(lastTrip);
                        }
                        startTime = parseGtfsTime(arrivalTime);
                        getTrip[trip].StartTime = startTime;
                        curList  = new StopTimeList(getRouteOfTrip[trip]);
                        lastTrip = trip;
                        if (seenTrips.Contains(trip))
                        {
                            throw new InvalidDataException("Trip contained in an interrupted sequence.");
                        }
                        seenTrips.Add(trip);
                    }
                    TimeSpan curTime    = parseGtfsTime(arrivalTime);
                    TimeSpan curEndTime = parseGtfsTime(departureTime);
                    while (curTime < startTime)
                    {
                        curTime += TimeSpan.FromDays(1);
                    }
                    curList.Add(Tuple.Create(getStop[record["stop_id"]], curTime - startTime, record["stop_headsign"]));

                    timeDiffs.Add((int)(curEndTime - curTime).TotalMinutes);
                    calculatePercent(table);
                }
                if (lastTrip != null)
                {
                    if (!set.Keys.Contains(curList))
                    {
                        set.Add(curList, new List <String>());
                    }
                    set[curList].Add(lastTrip);
                }
            }
            foreach (var pair in set)
            {
                pair.Key.CreateLists(pair.Value.Select(str => getTrip[str]).ToList());
            }

            var getTripType = new Dictionary <StopList, TripType>();

            foreach (var stopList in StopTimeList.StopLists)
            {
                TripType tt = new TripType {
                    Route = stopList.Key.Route
                };
                tt.HeadsignEntries = new List <TripTypeHeadsign>();
                tt.Route.TripTypes = addToList(tt, tt.Route.TripTypes);
                sdb.AddEntity(tt);
                getTripType[stopList.Value] = tt;
                string prevName = tt.Route.Name;
                int    pos      = 0;
                foreach (var stopAndName in stopList.Key)
                {
                    StopEntry se = new StopEntry {
                        Stop = stopAndName.Item1, TripType = tt
                    };
                    tt.StopEntries = addToList(se, tt.StopEntries);
                    TTEntry te = new TTEntry {
                        Position = pos, Stop = stopAndName.Item1, TripType = tt
                    };
                    sdb.AddEntity(se);
                    sdb.AddEntity(te);
                    if (stopAndName.Item2 != null && stopAndName.Item2 != prevName)
                    {
                        TripTypeHeadsign he = new TripTypeHeadsign {
                            Headsign = stopAndName.Item2, StartIndex = (short)pos, TripType = tt
                        };
                        tt.HeadsignEntries = addToList(he, tt.HeadsignEntries);
                        //sdb.AddEntity(he);
                        prevName = stopAndName.Item2;
                    }
                    pos++;
                }
            }
            foreach (var timeList in StopTimeList.TimeLists)
            {
                TripTimeType ttt = new TripTimeType {
                    TripType = getTripType[timeList.Key.Base]
                };
                ttt.TripType.TripTimeTypes = addToList(ttt, ttt.TripType.TripTimeTypes);
                sdb.AddEntity(ttt);
                foreach (var timeEntry in timeList.Key)
                {
                    TimeEntry te = new TimeEntry {
                        Time = (short)timeEntry.TotalMinutes, TripTimeType = ttt
                    };
                    sdb.AddEntity(te);
                    ttt.TimeEntries = addToList(te, ttt.TimeEntries);
                }
                foreach (var trip in timeList.Key.Trips)
                {
                    trip.TripTimeType = ttt;
                    ttt.Trips         = addToList(trip, ttt.Trips);
                    //if (ttt.TripType.Shape == null)
                    //    ttt.TripType.Shape = getShapeOfTrip[trip];
                    //else if (ttt.TripType.Shape != getShapeOfTrip[trip])
                    //    throw new InvalidDataException();
                }
            }
            var emptyTrips = sdb.GetTable <Trip>().Where(t => t.TripTimeType == null).ToList();

            Log(0, "Trips missing from stop_times.txt count: " + emptyTrips.Count);
            sdb.RemoveEntityAll(emptyTrips);

            //Setting shapes to TripTypes
            int wrongShapeTrips = 0;

            foreach (var tt in sdb.GetTable <TripType>())
            {
                var tripAndShapes = tt.Trips.Select(trip => new { Trip = trip, Shape = getShapeOfTrip[trip] }).ToList();
                tt.Shape         = tripAndShapes.GroupBy(x => x.Shape).MaxBy(x => x.Key == nullShape ? int.MinValue : x.Count()).Key;
                wrongShapeTrips += tripAndShapes.Where(x => x.Shape != tt.Shape).Count();
            }
            log(0, "Shapes set to triptypes. Trips with wrong shape: " + wrongShapeTrips * 100 / getTrip.Count + "%");

            //Settings names to TripTypes
            int wrongNameTrips = 0;

            foreach (var tt in sdb.GetTable <TripType>())
            {
                var ttNames = tt.Trips.Select(trip => getNameOfTrip[trip]).Except(new string[] { null, "" }).ToList();
                if (ttNames.Any())
                {
                    tt.Name         = ttNames.MostFrequent();
                    wrongNameTrips += ttNames.Except(new string[] { tt.Name }).Count();
                }
            }
            log(0, "Names set to triptypes. Trips with wrong name: " + wrongNameTrips * 100 / getTrip.Count + "%");

            //setting unknown route names
            var unknownTripTypes = sdb.GetTable <TripType>().Where(tt => tt.Name == "" || tt.Name == null).ToList();

            foreach (var tt in unknownTripTypes)
            {
                if (tt.HeadsignEntries.Any())
                {
                    var names = new Dictionary <string, int>();
                    for (int i = 0; i < tt.HeadsignEntries.Count; i++)
                    {
                        var headsignEntry = tt.HeadsignEntries[i];
                        int size          = (i < tt.HeadsignEntries.Count - 1 ? tt.HeadsignEntries[i + 1].StartIndex : tt.StopEntries.Count) - headsignEntry.StartIndex;
                        int oldValue      = 0;
                        names.TryGetValue(headsignEntry.Headsign, out oldValue);
                        names[headsignEntry.Headsign] = oldValue + size;
                    }
                    tt.Name = names.MaxBy(n => n.Value).Key;
                }
                else
                {
                    var stoplist = sdb.GetTable <StopEntry>().Where(se => se.TripType == tt);
                    tt.Name = stoplist.Last().Stop.Name;
                }
            }
            var unknownRoutes = sdb.GetTable <Route>().Where(r => r.Name == "Unknown" || r.Name == "" || r.Name == null).ToList();

            foreach (var route in unknownRoutes)
            {
                var triptypes = sdb.GetTable <TripType>().Where(tt => tt.Route == route).ToList();
                if (triptypes.Count == 0)
                {
                    sdb.RemoveEntity(route);
                }
                else
                {
                    route.Name = triptypes.Where(tt => tt.Name != "" && tt.Name != null).GroupBy(tt => tt.Name).MaxBy(x => x.Sum(y => y.Trips.Count())).Key;
                }
            }

            log(0, "Average end-stop time difference: " + timeDiffs.Average() + ", max diff: " + timeDiffs.Max());

            log(0, "Compressing stop_times done! First compression efficiency: " + set.Average(x => x.Value.Count));
            long firstSize = set.Sum(x => x.Key.Count * (4 + 2));
            long totalSize = StopTimeList.StopLists.Sum(x => x.Key.Count * 4) + StopTimeList.TimeLists.Sum(x => x.Key.Count * 2);

            log(0, "Second total db compression rate: " + (totalSize * 100 / firstSize) + "%");
            log(0, "Stoplist number compression rate: " + (StopTimeList.StopLists.Count * 100 / set.Count) + "%");

            int changingCount  = sdb.GetTable <TripType>().Where(tt => tt.HeadsignEntries.Count > 1).Count();
            int totalTripCount = sdb.GetTable <TripType>().Count();

            log(0, string.Format("Triptype with changing name: {0}/{1} ({2}%)", changingCount, totalTripCount, changingCount * 100 / totalTripCount));
        }