entur / api-examples

Examples for calling APIs
https://developer.entur.org
European Union Public License 1.2
2 stars 0 forks source link

Search for stop by name #2

Closed ways closed 5 years ago

ways commented 5 years ago

Hi again.

I'm trying to search for a stop by name in NSR. The API has two examples. Search for place and search for stop.

Place:

{
  topographicPlace(query: "lijordet") {
    id
    name {
      value
    }
  }
}

This gives several hits, but not the stop I'm looking for: https://en-tur.no/nearby-stop-place-detail?id=NSR:StopPlace:3700

Stop: I can find stops by "id", but I can't tell how to specify string to search for here.

{
  stopPlace(id: "NSR:StopPlace:58277") {
    id
    keyValues {
      key
      values
    }
    name {
      value
    }
    ... on StopPlace {
      quays {
        id
        keyValues {
          key
          values
        }
        geometry {
          type
          coordinates
        }
      }
    }
  }
}

Edit: update second example used

draperunner commented 5 years ago

You should use the Geocoder API /autocomplete endpoint for searching up stop places and their ID before using the JourneyPlanner API to do the actual travel search.

So this call: GET https://api.entur.io/geocoder/v1/autocomplete?text=Lijordet&size=20&lang=no will give you search results in GeoJSON format for "Lijordet". The first feature has the category "metroStation" and the ID "NSR:StopPlace:3700". This is the ID you can use in the stopPlace graphql query.

draperunner commented 5 years ago

If you're using JavaScript, you might want to check out our SDK: https://github.com/entur/sdk

An example with the SDK:

import EnturService from '@entur/sdk'

const service = new EnturService({ clientName: 'awesomecompany-awesomeapp' })

async function getDeparturesFromPlace(placeName) {
    const [feature] = await service.getFeatures(placeName)
    const departures = await service.getStopPlaceDepartures(feature.properties.id)

    departures.forEach((departure) => {
        const { expectedDepartureTime, destinationDisplay, serviceJourney } = departure
        const { line } = serviceJourney.journeyPattern
        console.log(`${expectedDepartureTime} ${line.transportMode} ${line.publicCode} ${destinationDisplay.frontText}`)
    })
}

getDeparturesFromPlace('Lijordet')
ways commented 5 years ago

So this call: GET https://api.entur.io/geocoder/v1/autocomplete?text=Lijordet&size=20&lang=no will give you search results in GeoJSON format for "Lijordet". The first feature has the category "metroStation" and the ID "NSR:StopPlace:3700". This is the ID you can use in the stopPlace graphql query.

Thanks! Got it working at https://gitlab.com/larsfp/pentur