A swiss public transit API (SBB and Google..)

Here we go.. After weeks of going back and forth between a crappy API (sbb.ch) with exact schedule and a nice API with wrong schedule (google.com), I finally merged them into one.

What are the capabilities of this API? Let’s make an example.

Given you entered from = “Feldstrasse 133, Zürich” and to = “Bern”.

  • It then checks sbb.ch first – No hit.
  • Then it checks google.com, which will return you a schedule with the next transit station from the address “feldstrasse+133, Zürich”

Ok, this is not a big deal.

In GottaGo, we often use (or almost everytime) the coordinates of a station. Let’s make another example.

Query: from=Triemli[8.495521,47.368052,0] and to=”Feldstrasse 133″.

The coordinates are then reverse geocoded, so that we get a city name.. Which is “Zürich” in this case.

  • The API will first check sbb.ch for “Zürich, Triemli” to “Feldstrasse 133″, which will be no hit.
  • It then checks sbb.ch for “Zürich, Triemli” to “Zürich, Feldstrasse 133″.

    Note that the city is also added to the other parameter since we had no hit in the first place and the city name did not yet occur in the second parameter “Feldstrasse 133″. But no hit again..

  • Then it checks google.com for “Zürich, Triemli” to “Feldstrasse 133″, which will be no hit, since there are a few more “Feldstrasse”s around in Switzerland.
  • Ok, now the last resort: Check google with “Zürich, Triemli” to “Zürich, Feldstrasse 133″ – Ahhh, finally a hit :)

Why all this? Because of POI and address-book search.

SBB.ch doesn’t really provide a proper API for using POIs and addresses. And google doesn’t provide the comfort of saying “Fribourg” is the Mainstation of “Fribourg” and not the center of Fribourg, and the schedule is sometimes wrong, especially during holidays.

This results in a comfortable API which we can use to make also very simples queries like: from “Current Location” to “Liip” in `GottaGo’.

The URL to this API? http://couch.codesofa.com/api/transit.xml?from=station[longitude,latitude,0]; station2[longitude,latitude,0]&to=station3; station4[longitude,latitude,0]

(The coordinates are optional, and you can enter as many stations as you like, separated by “;”)

Eg: couch.codesofa.com/api/transit.xml?from=Triemli[8.495521,47.368052,0]&to=Bern

 

Btw.: Latest Beta of `GottaGo’ is out. (An iPhone app for swiss public transit). If you want a preview, see: http://liip.to/applyforbeta

9 Comments to A swiss public transit API (SBB and Google..)

  1. August 6, 2008 at 2:38 pm Permalink
    sprain's Gravatar sprain

    how official ist this api? not afraid sbb will just shut it off in a few days?

  2. August 6, 2008 at 10:27 pm Permalink

    of course I am.. But I actually believe in those guys as they should be lucky, that someone did the dirty work for them ;) but there is a fallback to google to the official API.. So that should be fine..

  3. August 8, 2008 at 7:12 am Permalink

    Hey! GottaGo is great, but I noticed an inconsistency in the interface with the SDK guidelines.

    On the screen showing times, you use the arrow in a blue circle (sorry, I can’t remember the SDK name for it right now) to show you can navigate to the next page. However, this arrow should be used to indicate additional functionality for that table row.

    I.e. It should mean that clicking the row itself does one thing, but clicking the actual arrow will take you to an alternative screen (maybe settings or something). You can see this in use in the wi-fi settings page.

    In GottaGo, clicking on the row navigates to the next screen, but clicking the blue arrow actually does nothing at all (which is frustrating as clicking that side of the bar is typical behaviour for me).

    The standard arrow (no blue background) should be used to indicate that clicking the row will navigate to another screen (and doesn’t have its own hit area), so I think this should be used on both the time listings page, but also the ‘closest’ page.

    Russell

  4. August 8, 2008 at 7:41 am Permalink

    Hi Russel!
    Thanks for your detailed remark! You are absolutely right about it. It’s called accessory view (the arrow on the right) and it really should also be clickable. It may, however, do the same action as the row itself. But the fact that it’s not clickable is a bug – of course. And it’s addressed in the next release :)

  5. August 8, 2008 at 7:49 am Permalink

    Great :)

  6. August 8, 2008 at 1:54 pm Permalink
    Risotto's Gravatar Risotto

    I was about to code exactly the same API as yours. I’m glad you did it first :) . I was also going to write an app almost like GottaGo. Writing that app also let me wonder why t.f. the sbb.ch site is so ugly to parse. I was pretty upset that there is no official SOAP or XML-RPC web-service to query from. Looks like SBB is just a very lame company
    that doesn’t really care about it’s customers. After all, they are paid by the public! They really should be pressured to make that data available to everyone…

    Well, anyway, if you’re looking for some help to further develop the app, I’m more than willing to join you. I did some work with the SDK too and as I said, I was halfway through with it too :) . In my prototype I did a nice google map integration which would be great in GottaGo too. So ping me, if you’re interested!

  7. December 24, 2009 at 4:13 pm Permalink
    Lukas's Gravatar Lukas

    Hello, Very nice. I was desperatly looking for this kind of API. But it keeps saying “Please update GottaGo” at the last entry and the dates seem really weird. I was looking for GottaGo in the appstore but couldn’t find it. Is it still operational?

  8. December 25, 2009 at 1:27 am Permalink
    Marc's Gravatar Marc

    Hi Lukas

    This one is the old API, there is a “newer” one, and still operational:

    http://couch.codesofa.com/api/002/transit/.xml?ftype=station&from=winterthur&ttype=station&to=bern&fts=1249308316

    fts = from time stamp (departure time)
    tts = to time stamp (arrival time)

  9. February 2, 2011 at 11:25 pm Permalink

    I wonder if anyone has reverse-engineered the SBB iphone app’s API usage? I want to make a better SBB app. I can install a proxy and look, but maybe someone has done that already :)
    Drop me a line, xaphod@gmail.com

Dare to comment?