Which station is a vehicle stopped at?

104 views
Skip to first unread message

Alan Kilian

unread,
Feb 9, 2021, 4:47:34 PM2/9/21
to MBTA Developers

I'm new to using this API and I have some beginner questions I'm sure many of you can help me with.

I'm building an LED sign showing part of the Redline subway and I would like to display trains on the LEDs.

I'm using a very resource-constrained microcontroller, so this project has been on hold for a few years since I could not parse the old PROTOBUF-style responses.

I'm VERY happy to see the new JSON-style API. I think I will be able to now query for train locations on the Redline and display them appropriately on my sign.

I am trying to understand the structure of the queries and responses and I'm a little lost as a beginner.

I see that I can query the location of all the Redline trains using this: Query
That's AWESOME!

I started out trying to use the Latitude and Longitude to locate a particular subway stop but I think there's a better way right?

Now I'm looking at vehicles that have a "current_status": "STOPPED_AT" and I'm trying to figure out what stop they are stopped at.

I see that the relationships/stop/data/id has a string, but I'm unsure how to determine which stop that is?

From /vehicles, here's a train that is stooped:
    {
      "attributes": {
        "current_status": "STOPPED_AT",
        "direction_id": 1,
        "latitude": 42.35526,
        "longitude": -71.06016
      },
      "id": "R-5469174F",
      "links": {
        "self": "/vehicles/R-5469174F"
      },
      "relationships": {
        "route": {
          "data": {
            "id": "Red",
            "type": "route"
          }
        },
        "stop": {
          "data": {
            "id": "70078",
            "type": "stop"
          }
        },
        "trip": {
          "data": {
            "id": "46386250",
            "type": "trip"
          }
        }
      },
      "type": "vehicle"
    },
   
From /stops this the the closest match for Latitude,Longitude:
      "attributes": {
        "address": "Washington St and Summer St, Boston, MA",
        "at_street": null,
        "description": null,
        "latitude": 42.355518,
        "location_type": 1,
        "longitude": -71.060225,
        "municipality": "Boston",
        "name": "Downtown Crossing",
        "on_street": null,
        "platform_code": null,
        "platform_name": null,
        "vehicle_type": null,
        "wheelchair_boarding": 1
      },
      "id": "place-dwnxg",
      "links": {
        "self": "/stops/place-dwnxg"
      },
      "relationships": {
        "child_stops": {},
        "facilities": {
          "links": {
            "related": "/facilities/?filter[stop]=place-dwnxg"
          }
        },
        "parent_station": {
          "data": null
        },
        "recommended_transfers": {},
        "zone": {
          "data": null
        }
      },
      "type": "stop"
    },

I'm not finding anything in the result from the /vehicle endpoint that will allow me to match up with the information from the /stops endpoint.

Can you help a beginner get going with this?

Thank you all for your help in advance. I know it's hard to get a newcomer going with complex APIs such as these.

Developer at MBTA

unread,
Feb 19, 2021, 12:22:44 PM2/19/21
to MBTA Developers
Hi Alan,

> I see that the relationships/stop/data/id has a string, but I'm unsure how to determine which stop that is?

This is the ID of the stop the vehicle is associated with (via the `current_status` field). You could get the attributes of stops by requesting single stops from the `/stops/:id` endpoint, using the `/stops` endpoint with a `filter[id]` to request several at once, or (often the most efficient method) including them in the `/vehicles` request itself using `include=stop`. See the documentation for the `include` parameter on the `/vehicles` endpoint for more details.

If you want to get a "station" for these IDs, you'll also need to follow the `parent_station` relationship on the stop. The stop associated with a vehicle is what's called a "child stop", which is an individual stopping location for vehicles — so for example there are at least two child stops (platforms) at most subway stations. If there are multiple child stops that are treated as a single "stop" (e.g. "Downtown Crossing" contains many train platforms), those stops will have a `parent_station` representing this higher-level stop. Note that multiple levels of relationships can be included: a `/vehicles` request with `include=stop.parent_station` will get you the parent stations for the stops for the vehicles you request.

"Attributes", "relationships", and "includes" are features of the JSON:API standard, which our API conforms to. If possible, we recommend using a JSON:API library for your language to make it easier to navigate the data and relationships.

—Developer@MBTA
Reply all
Reply to author
Forward
0 new messages