Package: gtfs-segments
GTFS SegmentsA fast and efficient library to generate bus stop spacingsTable of ContentsAbout The ProjectGetting StartedPrerequisitesInstallationUsageGet GTFS FilesGet GTFS SegmentsVisualize SpacingsPlot DistributionsRoadmapContributingLicenseContactAcknowledgmentsAbout The ProjectThegtfs-segmentsis a Python (3.9+) package that represents GTFS data forbusesin a concise tabular manner using segments. The distribution of bus stop spacings can be viewed by generating histograms. The stop spacings can be visualized at the network, route, or segment level. The segment data can be exported to well-known formats such as.csvor.geojsonfor further analysis. Additionally, the package provides commands to download the latest data from@mobility datasources.The package condenses the raw GTFS data by considering the services offered only on thebusiest day(in the data). More discussion on the interpretation of different weightings for stop spacings, and the process in which the package condenses information can be seen in ourarXiv paper. The usage of the package is detailed indocumentation. The stop spacings dataset containing over 540 transit providers in the US generated using this package can be found onHarvard Dataverse.(back to top)Getting StartedPrerequisitesThe major dependencies of this library are the following packages.numpyshapelypandasscipygeopandasmatplotlibcontextilyThe detailed list of package dependencies can be found inrequirements.txtInstallationOption AUse pip to install the package.pipinstallgtfs-segmentsℹ️ Windows users may have to download and install Microsoft Visual C++ distributions. Followtheseinstructions.📓Google Colab :You can install and use thegtfs-segmentsvia google colab. Here is atutorialto help you get started. Make a copy and get started with your work!OptionBClone the repogitclonehttps://github.com/UTEL-UIUC/gtfs_segments.gitInstall geopandas using the following code. Read moreherecondacreate-ngeo_env-cconda-forgepython=3.11geopandas
condaactivategeo_envInstall thegtfs_segmentspackagecdgtfs_segments
pythonsetup.pyinstall(back to top)Usageℹ️ For documentation, please refer to theDocumentationImport the package usingimportgtfs_segmentsGet GTFS FilesFetch all sourcesfromgtfs_segmentsimportfetch_gtfs_sourcesources_df=fetch_gtfs_source()sources_df.head()Fetch source by name/provider/statefromgtfs_segmentsimportfetch_gtfs_sourcesources_df=fetch_gtfs_source(place='Chicago')sources_dfAutomated Downloadfromgtfs_segmentsimportdownload_latest_datadownload_latest_data(sources_df,"output_folder")Manual DownloadDownload the GTFS.zipfiles from@transitfeedsor@mobility data.Get GTFS Segmentsfromgtfs_segmentsimportget_gtfs_segmentssegments_df=get_gtfs_segments("path_to_gtfs_zip_file")# [Optional] Run in parallel using multiple CPU coressegments_df=get_gtfs_segments("path_to_gtfs_zip_file",parallel=True)Alternatively, filter a specific agency by passingagency_idas a string or multiple agencies as list ["SFMTA",]segments_df = get_gtfs_segments("path_to_gtfs_zip_file",agency_id = "SFMTA")
segments_dfTable generated by gtfs-segments using data from San Francisco’s Muni system. Each row contains the following columns:segment_id: the segment's identifier, produced by gtfs-segmentsstop_id1: the identifier of the segment's beginning stop. The identifier is the same one the agency has chosen in the stops.txt file of its GTFS package.stop_id2: The identifier of the segment's ending stop.route_id: The same route ID listed in the agency's routes.txt file.direction_id: The route's direction identifier.traversals: The number of times the indicated route traverses the segment during the "measurement interval." The "measurement interval" chosen is the busiest day in the GTFS schedule: the day which has the most bus services running.distance: The length of the bus segment in meters.geometry: The segment's LINESTRING (a format for encoding geographic paths). All geometries are re-projected onto Mercator (EPSG:4326/WGS84) to maintain consistency.traversal_time: The time (in seconds) that it takes for the bus to traverse the segment.speed: The speed of the bus (in kmph) while traversing the segment. Default tonp.inf♾ in casetraversal_timeis zero.Each row does not represent one segment. Rather, each row maps to a combination of a segment, a route that includes that segment, and a direction. For instance, a segment included in eight routes will appear as eight rows, which will have the same information except for route_id and traversals (since some routes might traverse the segment more than others). This choice enables filtering by route and preserves how many times each route traverses each segment during the measurement interval. The direction identifier is used for very rare cases (mostly loops) in which a route visits the same two stops, in the same order, but in different directions.Visualize SpacingsVisualize stop spacings at network, route and segments levels along with basemaps and stop locations.ℹ️ For more information on visualization refer to theVisualization Tutorialℹ️ Alternatively, useview_spacings_interactiveto view the stop spacings interactively.fromgtfs_segmentsimportview_spacingsview_spacings(segments_df,route='8',segment='6364-3725-1',basemap=True)HeatmapView the heatmap of stop spacings ("distance" as metric). UseDivergingcolormaps to highlight narrow and wide spacings. Setlight_mode = Falsefor Dark mode.fromgtfs_segmentsimportview_heatmapf=view_heatmap(df,cmap='RdBu',light_mode=True)view_heatmap(df,cmap="YlOrRd",interactive=True,light_mode=False)Plot Distributionsfromgtfs_segmentsimportplot_histplot_hist(segments_df,max_spacing=1200)Optionally save figures usingplot_hist(segments_df,file_path="spacings_hist.png",save_fig=True)Summary StatisticsGet Network Summary Statsfromgtfs_segmentsimportsummary_statssummary_stats(segments_df,max_spacing=3000,export=True,file_path="summary.csv")Get Route Summary Statsfromgtfs_segmentsimportget_route_stats,get_bus_feedfeed=get_bus_feed('path_to_gtfs.zip')get_route_stats(feed)Here each row contains the following columns:route: Theroute_idfor the route of interestdirection: Thedirection_idof the routeroute_length: The total length of the route.Units: Kilometers (Km)total time: The total scheduled time to travel the whole route.Units: Hours (Hr)headway: The average headway between consecutive buses for the route. ANaNindicates only 1 trip.Units: Hours (Hr)peak_buses: The 15-minute interval where the route has the maximum number of buses concurrently running.average_speed: The average speed of the bus along the route.Units: Kmphn_bus_avg: The average number of buses concurrently runningbus_spacing: The average spacing (in distance) between consecutive buses.Units:Kilometers (Km)stop_spacing: The average distance between two consecutive stops.Units:Kilometers (Km)Download Segments DataDownload the data as either.csvor.geojsonfromgtfs_segmentsimportexport_segmentsexport_segments(segments_df,'filename',output_format='geojson')# Get csv without geometryexport_segments(segments_df,'filename',output_format='csv',geometry=False)(back to top)RoadmapackageAdd interactive visualization with foliumVisualize catchment areas for stopsLog trips that do not have shapesSee theopen issuesfor a full list of proposed features (and known issues).(back to top)LicenseDistributed under the MIT License. SeeLICENSE.txtfor more information.Citing gtfs-segmentsIf you use gtfs-segments in your research please use the following BibTeX entry:@software{devunuri_gtfs_segments,author={Devunuri, Saipraneeth and Lehe, Lewis},doi={10.5281/zenodo.10019419},month=Oct,title={{GTFS Segments: A fast and efficient library to generate bus stop spacings}},url={https://github.com/UTEL-UIUC/gtfs_segments},version={2.0.3},year={2023}}Citing stop spacings paperIf you use stop spacings paper in your research please use the following BibTeX entry:@misc{devunuri2022bus,title={Bus Stop Spacings Statistics: Theory and Evidence},author={Saipraneeth Devunuri and Shirin Qiam and Lewis Lehe and Ayush Pandey},year={2022},eprint={2208.04394},archivePrefix={arXiv},primaryClass={stat.ME}}(back to top)ContributingContributions are what makes the open-source community such an amazing place to learn, inspire, and create. Any contributions you make aregreatly appreciated.If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
Don't forget to give the project a star! Thanks again!Fork the ProjectCreate your Feature Branch (git checkout -b feature/AmazingFeature)Commit your Changes (git commit -m 'Add some AmazingFeature')Push to the Branch (git push origin feature/AmazingFeature)Open a Pull RequestFor more information refer toCONTRIBUTING.mdContactSaipraneeth Devunuri -@praneethDevunu1-sd37@illinois.eduProject Link:https://github.com/UTEL-UIUC/gtfs_segmentsAcknowledgmentsParts of the code use thePartridgelibraryDo check outgtfs_functionswhich was an inspiration for this projectShoutout toMobility Datafor compiling GTFS from around the globe and constantly maintaining them(back to top)