Exemplo n.º 1
0
        /// <summary>
        /// Reads a GTFS zip archive stream and converts it into a <see cref="GtfsFeed"/>.
        /// </summary>
        /// <param name="stream">A zip archive <see cref="Stream"/> containing General Transit Feet Specification data.</param>
        /// <param name="options">
        /// Use this to specify which files you want to extract from the GTFS. Defaults to <see cref="GtfsFileOptions.All"/>.
        /// You should specify this parameter if you know that you will only need certain tables out of the GTFS.
        /// </param>
        /// <returns>A <see cref="GtfsFeed"/> representation of the contents of <paramref name="stream"/>.</returns>
        public static async Task <GtfsFeed> ReadGtfsAsync(this Stream stream, GtfsFileOptions options = GtfsFileOptions.All)
        {
            if (options == GtfsFileOptions.None)
            {
                throw new ArgumentException("The \"options\" parameter cannot be \"None\".");
            }

            GtfsFeed feed;

            using (var zip = new System.IO.Compression.ZipArchive(stream, ZipArchiveMode.Read, false))
            {
                feed = new GtfsFeed
                {
                    Agency         = options.HasFlag(GtfsFileOptions.Agency) ? await zip.ParseCsv <Agency>("agency.txt", true) : null,
                    Stops          = options.HasFlag(GtfsFileOptions.Stops) ? (await zip.ParseCsv <Stop>("stops.txt", true)).ToFeatureCollection() : null,
                    Routes         = options.HasFlag(GtfsFileOptions.Routes) ? await zip.ParseCsv <Route>("routes.txt", true) : null,
                    Trips          = options.HasFlag(GtfsFileOptions.Trips) ? await zip.ParseCsv <Trip>("trips.txt", true) : null,
                    StopTimes      = options.HasFlag(GtfsFileOptions.StopTimes) ? await zip.ParseCsv <StopTime>("stop_times.txt", true) : null,
                    Calendar       = options.HasFlag(GtfsFileOptions.Calendar) ? await zip.ParseCsv <Calendar>("calendar.txt", true) : null,
                    CalendarDates  = options.HasFlag(GtfsFileOptions.CalendarDates) ? await zip.ParseCsv <CalendarDate>("calendar_dates.txt") : null,
                    FareAttributes = options.HasFlag(GtfsFileOptions.FareAttributes) ? await zip.ParseCsv <FareAttribute>("fare_attributes.txt") : null,
                    FareRules      = options.HasFlag(GtfsFileOptions.FareRules) ? await zip.ParseCsv <FareRule>("fare_rules.txt") : null,
                    Shapes         = options.HasFlag(GtfsFileOptions.Shapes) ? (await zip.ParseCsv <Shape>("shapes.txt")).ToFeatureCollection() : null,
                    Frequencies    = options.HasFlag(GtfsFileOptions.Frequencies) ? await zip.ParseCsv <Frequency>("frequencies.txt") : null,
                    Transfers      = options.HasFlag(GtfsFileOptions.Transfers) ? await zip.ParseCsv <Transfer>("transfers.txt") : null
                };

                var feedInfo = options.HasFlag(GtfsFileOptions.FeedInfo) ? await zip.ParseCsv <FeedInfo>("feed_info.txt") : null;

                feed.FeedInfo = feedInfo != null?feedInfo.FirstOrDefault() : null;
            }

            return(feed);
        }