ericandrewlewis / mta-subway-complexes

0 stars 1 forks source link

How to use destinationStationId for complexStationId lookup #1

Closed Elaniobro closed 4 years ago

Elaniobro commented 6 years ago

@ericandrewlewis Using your /mta-realtime-subway-departures I found something I would like carlification on. I am trying to use complexes.json as a lookup for destinationStationId name.

When looking at stations that have crosstown stops, I am not finding the ID. Using the G line as an example (court sq) I see this in the response of client.departures(242)

[ { complexId: 242, name: 'Fort Hamilton Pkwy', lines: [ { S: 
   [ { routeId: 'G', time: 1535389812, destinationStationId: '281' }] ] },

station 281 does not exist in your complexes.json file, is this intended? If so, how should one lookup station 281 to get the complexStationId of 606?

ericandrewlewis commented 6 years ago

When looking at stations that have crosstown stops, I am not finding the ID.

Do you mean the station ID? I think we could add stationId into the API result. Would that be useful?

station 281 does not exist in your complexes.json file, is this intended?

This is intended. A station is basically a platform. For example the 6 Av-14th Street stop has three stations: one for the L line (station 116), one for the FM line (station 229) and one for the 123 line (station 332). They are all part of the larger complex ID 601.

If so, how should one lookup station 281 to get the complexStationId of 606?

You can map station to complex ID by using the mta-subway-stations module.

Elaniobro commented 5 years ago

@ericandrewlewis, wanting to pick this back up.

I am sure that would be useful. I am trying to figure out how to best resolve station ID's. thoughts?

ericandrewlewis commented 4 years ago

Hey @Elaniobro - sorry for the delay getting back to you on this!

Given a response like the one I just fetched by calling client.departures(242):

{
  "complexId": 242,
  "name": "Fort Hamilton Pkwy",
  "lines": [
    {
      "name": "6th Av - Culver",
      "departures": {
        "S": [
          {
            "routeId": "G",
            "time": 1601839762,
            "destinationStationId": "243"
          },
          ...
        ],
        "N": [
          ...
        ]
      }
    }
  ]
}

We could extract a destinationStationId of 243.

We would then filter the stations exposed by the mta-subway-stations module:

const stations = require('mta-subway-stations');
const station = stations.find(station => station['Station ID'] === destinationStationId);

Then we can access the Complex ID for the related station:

console.log(station['Complex ID']);

Altogether:

const { createClient } = require('mta-realtime-subway-departures');
const stations = require('mta-subway-stations');
const complexes = require('mta-subway-complexes');
require('dotenv').config();

const client = createClient(process.env.MTA_API_KEY);

(async () => {
  try {
    let response = await client.departures(242);
    const destinationStationId = response.lines[0].departures.S[0].destinationStationId;
    const station = stations.find(station => station['Station ID'] === destinationStationId);
    const complex = complexes[station['Complex ID']];
    console.log(`The destination complex ID for the first train heading south from complex ID 242 is ${station['Complex ID']}, which is ${complex.name}.`);
    // The destination complex ID for the first train heading south from complex ID 242 is 243, which is called Church Av.

  } catch(e) {
    console.error(e);
    console.log('Did you set the MTA_API_KEY from https://api.mta.info ?')
    return;
  }
})();
Elaniobro commented 4 years ago

Thanks for the long awaited reply, truly well written and thought out. This will certainly be most appreciated by others who may wish to use your module.

I should have closed this ticket way back when I first submitted it. I pretty much did what you outlined and self resolved it.

🍻