コード例 #1
0
        public async Task <BusStopDetails> GetStopDataAsync(string StopRef, DateTime timeStart, DateTime timeEnd, bool forceRefresh = false)
        {
            //check if its in the buffer
            OpenArchive gtfsArchive = GetArchiveFromBuffer(this.archiveFileName);

            if (gtfsArchive == null)
            {
                string     completePath = this.fileMgr.FilePathRoot + this.archiveFileName;
                ZipArchive zipFile      = this.fileMgr.GetZipFile(this.archiveFileName);
                gtfsArchive = new OpenArchive(completePath, zipFile);
                this.bufferedArchives.Add(gtfsArchive);
            }

            //get stop id
            //find stop id from stop name or stop code
            StopModel stopObj = GetStopFromStopRef(gtfsArchive, StopRef);


            //get expected trips at stop
            List <TripTimesModel> trips = GetTripsFromStopId(gtfsArchive, stopObj.stop_id, timeStart, timeEnd);


            //fill in the details
            BusStopDetails stopDetails = new BusStopDetails();

            stopDetails.StopId        = stopObj.stop_id;
            stopDetails.StopRef       = stopObj.stop_code;
            stopDetails.StopPointName = stopObj.stop_name;
            stopDetails.busStopX      = stopObj.stop_lat;
            stopDetails.busStopY      = stopObj.stop_lon;

            foreach (TripTimesModel trip in trips)
            {
                //details from trip times
                VehicleJourney vehicleJourney  = new VehicleJourney();
                TimeSpan       startTime       = timeStart.TimeOfDay;
                TimeSpan       endTime         = timeEnd.TimeOfDay;
                TimeSpan       tripArrivalTime = trip.departure_time;
                DateTime       startOfDate     = timeStart.Date;
                startOfDate = startOfDate.Add(tripArrivalTime);

                vehicleJourney.TripId          = trip.trip_id;
                vehicleJourney.AimedArrival    = startOfDate;
                vehicleJourney.ConfidenceLevel = 3; //not real time


                //get more details from trip.csv
                TripModel tripDetails = GetTripFromTripId(gtfsArchive, trip.trip_id);
                vehicleJourney.DirrectionAway = tripDetails.direction_id == 1;

                //get more details from routes.csv
                RouteModel routeDetails = GetRouteFromRouteId(gtfsArchive, tripDetails.route_id);
                vehicleJourney.LineRef = routeDetails.route_short_name;

                stopDetails.IncomingVehicles.Add(vehicleJourney);
            }
            stopDetails.IncomingVehicles.Sort();
            return(stopDetails);
        }
コード例 #2
0
        protected RouteModel GetRouteFromRouteId(OpenArchive gtfsArchive, string routeId)
        {
            if (routeList == null)
            {
                using (Stream routeStream = gtfsArchive.zip.GetEntry("google_transit/routes.csv").Open())
                {
                    routeList = CsvSerializer.DeserializeFromStream <List <RouteModel> >(routeStream);
                }
            }

            RouteModel routeDetails = routeList.FirstOrDefault(o => o.route_id == routeId);

            return(routeDetails);
        }
コード例 #3
0
        protected TripModel GetTripFromTripId(OpenArchive gtfsArchive, int tripId)
        {
            if (tripList == null)
            {
                using (Stream tripStream = gtfsArchive.zip.GetEntry("google_transit/trips.csv").Open())
                {
                    tripList = CsvSerializer.DeserializeFromStream <List <TripModel> >(tripStream);
                }
            }

            TripModel tripDetails = tripList.FirstOrDefault(o => o.trip_id == tripId);

            return(tripDetails);
        }
コード例 #4
0
        protected StopModel GetStopFromStopRef(OpenArchive gtfsArchive, string stopRef)
        {
            if (stopsList == null)
            {
                using (Stream stopsStream = gtfsArchive.zip.GetEntry("google_transit/stops.csv").Open())
                {
                    this.stopsList = CsvSerializer.DeserializeFromStream <List <StopModel> >(stopsStream);
                }
            }

            StopModel stopObj = stopsList.FirstOrDefault(o => o.stop_code == stopRef);

            return(stopObj);
        }
コード例 #5
0
        async public Task <bool> IsUpdateAvaliable()
        {
            try
            {
                //check if its in the buffer
                OpenArchive gtfsArchive = GetArchiveFromBuffer(this.archiveFileName);
                if (gtfsArchive == null)
                {
                    string     completePath = this.fileMgr.FilePathRoot + this.archiveFileName;
                    ZipArchive zipFile      = this.fileMgr.GetZipFile(this.archiveFileName);
                    gtfsArchive = new OpenArchive(completePath, zipFile);
                    this.bufferedArchives.Add(gtfsArchive);
                }

                //done by checking if the feed version number is different
                List <FeedInfo> feedInfos = null;
                using (Stream infoStream = gtfsArchive.zip.GetEntry("google_transit/feed_info.csv").Open())
                {
                    feedInfos = CsvSerializer.DeserializeFromStream <List <FeedInfo> >(infoStream);
                }

                FeedInfo feedInfo     = feedInfos.First();
                int      localVersion = feedInfo.feed_version;

                //compare this local version with the online version
                byte[] feedFile = await DownloadFile(this.FeedInfoUrl);

                string          feedString      = Encoding.UTF8.GetString(feedFile);
                List <FeedInfo> onlineFeedInfos = CsvSerializer.DeserializeFromString <List <FeedInfo> >(feedString);
                FeedInfo        onlineFeedInfo  = onlineFeedInfos.First();
                int             onlineVersion   = onlineFeedInfo.feed_version;

                return(onlineVersion != localVersion);
            }
            catch
            {
                //failed somewhere with reading local file
                //return true so caller will update local version
                return(true);
            }
        }
コード例 #6
0
        protected List <TripTimesModel> GetTripsFromStopId(OpenArchive gtfsArchive, int stopId, DateTime timeStart, DateTime timeEnd)
        {
            if (timeStart > timeEnd)
            {
                throw new ArgumentException("The end time must be greater than or equal to the start time");
            }
            else if (timeEnd.Subtract(timeStart).TotalDays > 1.0)
            {
                throw new ArgumentException("The start and end dates must be less than 1 day appart");
            }
            if (this.timeStringList == null)
            {
                this.timeStringList = new List <StringEntry>();
                using (Stream timeStream = gtfsArchive.zip.GetEntry("google_transit/stop_times.csv").Open())
                {
                    StreamReader timeReader = new StreamReader(timeStream);
                    //skip header
                    timeReader.ReadLine();
                    while (timeReader.EndOfStream == false)
                    {
                        string timeLine = timeReader.ReadLine();
                        this.timeStringList.Add(new StringEntry(timeLine));
                    }
                }
            }

            //binary search this.timeStringList for the valid entries
            StringEntry stopKey = new StringEntry(stopId.ToString());
            int         index   = this.timeStringList.BinarySearch(stopKey);

            if (index < 0 || index > this.timeStringList.Count())
            {
                throw new KeyNotFoundException("Failed to find a entry for the stop id:" + stopId.ToString() + " in stop_times.csv");
            }

            //find the start of this section
            while (this.timeStringList[index - 1].CompareTo(stopKey) == 0)
            {
                --index;
            }

            List <TripTimesModel> tripsAtStop = new List <TripTimesModel>();

            //populate the return entries
            while (this.timeStringList[index].CompareTo(stopKey) == 0)
            {
                TripTimesModel tripTime = new TripTimesModel();
                string         entry    = this.timeStringList[index].Entry;
                string[]       entries  = entry.Split(',');
                tripTime.stop_id        = int.Parse(entries[0]);
                tripTime.departure_time = GetTimeSpanFromString(entries[1]);
                tripTime.trip_id        = int.Parse(entries[2]);
                bool isInRange = IsTimeInRange(ref tripTime, timeStart, timeEnd);
                if (isInRange)
                {
                    IsTimeInRange(ref tripTime, timeStart, timeEnd);
                    tripsAtStop.Add(tripTime);
                }
                ++index;
            }

            return(tripsAtStop);
        }