ArcGIS REST API – Query Feature Service for Intercity Bus Data

arcgis-rest-api

I am currently trying to retrieve some data on intercity bus travel from (https://www.bts.gov/intercity-busing/data-maps-and-apps). The data is only available as an interactive map, but I need to retrieve it into something I can analyze in Python such as a .json or .csv. To this end I found a url that I can make queries at (https://geo.dot.gov/server/rest/services/IntercityBus/nbta_routes_20190120/FeatureServer/query) and found that this problem was similar to (Query Feature Service on ESRI ArcGIS REST API with Python Requests).

How can I use this query tool to retrieve all of the data? I think I need to figure out what keys work, because I tried entering "*" or similar "all" files but could not get any of the data returned.

Best Answer

Firstly, you need to go up one level from that URL (by clicking on the link at the top of it) to find yourself at the feature service base URL of:

https://geo.dot.gov/server/rest/services/IntercityBus/nbta_routes_20190120/FeatureServer

From there, you can see that it lists all of that layers in that feature service with a link for each of them. There is only one layer listed, so click on the link for that layer, which takes you to the layer's URL at:

https://geo.dot.gov/server/rest/services/IntercityBus/nbta_routes_20190120/FeatureServer/0

Now scroll down to the bottom of the page and click on the 'Query' link at the bottom of the page, which will take you to the query page for that layer at:

https://geo.dot.gov/server/rest/services/IntercityBus/nbta_routes_20190120/FeatureServer/0/query

Now you have a web page with a form that you can use to query the layer. You can set the output 'Format' (at the bottom) to 'JSON' (ESRI JSON) or 'GeoJSON' if that's the format you want. But leave it on 'HTML' while testing so that you can more easily view your results. Use the Query 'GET' button NOT the Query 'POST' button if you want to end up with a URL (in the resulting browser page) that you can easily use to repeat the query any time.

All the other options are up to you and your preferences. However, most (all?) feature services are configured to only return a limited number of results (usually capped at 1,000). So it is very unlikely that this feature service will let you download everything!

I usually set the following in the query form:

  • `Where: "1=1"
  • Out Fields: "*"
  • Return Geometry: "False"

And this seems to work for this Feature Layer, returning 1271 records.

I also found this URL:

https://geo.dot.gov/server/rest/services/IntercityBus/nbta_stops_20200409/FeatureServer/0/query

Entering the same values in this form returns 1092 records.