Journeys API

ITS Factory Developer Wikistä
Versio hetkellä 6. lokakuuta 2014 kello 01.27 – tehnyt Juha.lundan (keskustelu | muokkaukset)

This is a workspace for Journeys API which allows developers and clients to access public transport lines, routes, stops and schedules. All material on this page are draft material and subject to significant change.

A beta-phase API is available at http://data.itsfactory.fi/journeys/api/1/lines please see examples on this page for queries.

Concept

Journeys API allows developers and clients to access following information via REST API

  • Routes on which public transport services operate
  • Lines, or in other words grouping of routes. For example Line 2, which runs from Rauhaniemi to Pyyninkintori in Tampere
  • Journeys made by public transport vehicles on certain line and route at certain day and time
  • Stops where public transport vehicles stop during their journeys

The API allows accessing these concepts as hierarchy of objects (see later) as well as performing searches.

API Entities

The API is built on top of NeTEx entity model (http://user47094.vs.easily.co.uk/netex/), from which the entities in this API are derived. Following picture outlines the entities and their relationships.

API Entities 1.1.png

In the picture above, entities are drawn with purple color. Boxes with blue color are properties of the entities, but drawn out to explain the concepts. Lines in the picture represent UML-styled relations. Lines with filled diamond represent relation with ownership rule: the entity which has a diamond shape attached to it, owns the entity or property at other end of the line. Lines with empty diamond represent relation reference rule: the entity which has an empty diamond shape attached to it, is referred by entity or property at other end of the line. For example ROUTE owns JOURNEY PATTERN, and JOURNEY references a STOP POINT. Numbers at the end of lines are multiplicity rules, which tell how many entities or properties are included in the relation. If number is not present at the end of a line, it is assumed that number one would exist at the end of that line. For example: LINE and ROUTE share a relation. In this relation, a ROUTE relates to one LINE, whereas a LINE relates to zero or more ROUTEs.

Routes

ROUTE entity describes a path through (a road or a rail) network, which is used by a public transport service (such as bus or a tram). ROUTE has typically one or more projections. Projection is a way to describe a ROUTE on selected canvas. For example a route could be presented on a map by drawing it as a line on it. Another way would be project a ROUTE for example to OpenStreetMap by using OpenStreetMap roads to present the ROUTE. Journeys API currently supports only projecting the ROUTE as coordinate shape. Following picture is taken from Tampere "reittikartta" and outlines a ROUTE through Tampere.

Journ Line 2.png

Line

LINE entity is a grouping of ROUTES that is generally known to the public with a common name, such as "Line 2". ROUTES on the same LINE usually follow similar ROUTEs.

Journey, Journey Pattern, DayType, DayTypeException and StopPoint

JOURNEY describes public transport vehicle's run on a given ROUTE through the city. Typically the run occurs at the same time on multiple days during a week. For example a certain JOURNEY on given ROUTE could start 10:00 AM on Mondays, Tuesdays, Wednesdays, Thursdays and Fridays. DAY TYPE, attached to a JOURNEY, classifies the conditions on which kind of days the JOURNEY occurs. Conditions can be week day names, but other kind of conditions can apply too (such as "busy day" or "all weekdays"). DAY TYPE EXCEPTION provides mechanism to temporarily deviate from conditions imposed by a DAY TYPE. For example if a JOURNEY would occur on all weekdays, public holidays could be modeled as DAY TYPE EXCEPTIONS. In other words we could say "This JOURNEY runs on every week day except 21.5.2014, because this day is Labor Day".

On a given time on a given DAY TYPE or a given ROUTE, the JOURNEY stops on certain STOP POINTS from where passengers can board or alight the vehicle. A CALL groups together the STOP POINT where the vehicle stops, and the time when the vehicle stops on that STOP POINT. Since the vehicle stops multiple times during a JOURNEY, a CALL also contains the order of the stop. CALLs on a certain JOURNEY follow a JOURNEY PATTERN. JOURNEY PATTERN is in essence a sequence of STOP POINTS on which the JOURNEY stops. In other words: ROUTE describes the vehicle's path through road network, and a JOURNEY PATTERN "overlays" it with a sequence of STOP POINTs. Consider a ROUTE as continuous drawn line and STOP POINTs as drawn circles on top of the line. STOP POINTs must follow the drawn line since those have to be on the vehicle's route. Single ROUTE can, however, have multiple JOURNEY PATTERNs: for example one for express line (which skips some of the stops) and one for the normal line which stops on every STOP POINT.

Queries and Responses

REST styled API

Journeys API follows RESTful design pattern. This means that all entities returned by the API have an unique URL via which the entity can be accessed at all times. The returned entities refer to each other via these URLs, and each entity URL is always included in the entity itself as "href" property. Following is a fragment from STOP POINT entity response:

...
"body" : [
    {
    "url" : "http://data.itsfactory.fi/journeys/api/1/stop-points/0001",
    "location" : "61.49751,23.76151",
    "name" : "Keskustori M",
    "shortName" : "0001",
    "tariffZone" : "1",
    "muncipality" : {
      "url" : "http://data.itsfactory.fi/journeys/api/1/muncipalities/837",
      "shortName" : "837",
      "name" : "Tampere"
    }
  }
]
...

The STOP POINT itself has a url property which points to the STOP POINT entity itself. A client can use this URL access this entity at any time. Nested objects may also include urls as in case of muncipality inside the stopPoint.

API-Wide URL parameters

Indent

Indent formats the server response to more human readable format.

http://data.itsfactory.fi/journeys/api/1/lines?indent=yes

Response structure

Journeys API response is structured as follows:

{
    "status" : "success",
    "data" : {
        "headers" : {
            "paging" : {
                "startIndex" : 0,
                "pageSize" : 1,
                "moreData" : false
            }
        },
        "body": [
            ...
        ]
    }
}

The response has headers and data elements. Data element contains the actual response data, and its content varies depending on the request made by the client. Headers contain metadata-like information about the response. startIndex tells the index of the first returned element, pageSize tells how many items were returned and moreData tells if the client should make another request to fetch the additional data. This approach is called "paging". The client can specify startIndex parameter to the request URL to specify the starting index of an entity list, for example:

http://data.itsfactory.fi/journeys/api/1/lines?startIndex=10

If omitted, the Journeys API assumes startIndex to be zero.

In case of a malformed request, such as wrongly formatted coordinate strings, server responds with a fail message:

{
  "status" : "fail",
  "data" : {
    "title" : "Coordinates",
    "message" : "Illegal format: must be latitude,longitude"
  }
}

In internal server error cases, server responds with similar response, except the status is "error":

{
  "status" : "error",
  "data" : {
    "message" : "Guru Meditation"
  }
}

Entity Queries

About Query Parameters

Journeys API allows the client user URL parameters in queries. You can find out valid query parameters for each entity on this page. A client can issue none or all query parameters in a query. For example a client could query:

http://data.itsfactory.fi/journeys/api/1/lines

or

http://data.itsfactory.fi/journeys/api/1/lines?name=13&description=hermia

by using multiple query parameters.

Query Parameter Reference

Below you can find allowed query parameters by Journeys API url.

api/1/lines
	- id : String
	- description : String

api/1/routes
	- lineId : String
	- name : String

api/1/journey-patterns
	- lineId : String
	- name : String
	- firstStopPointId : String
	- lastStopPointId : String
	- stopPointId : String

api/1/journeys
	- lineId : String
	- routeId : String
	- journeyPatternId : String
	- dayTypes : comma separated list of: monday, tuesday, wednesday, friday, saturday, sunday
	- departureTime : hh:mm
	- arrivalTime : hh:mm
	- firstStopPointId : String
	- lastStopPointId : String
	- stopPointId : String

api/1/stop-points
	- name: String 
	- location: lat,lon or lat1,lon1:lat2,lon2 (upper left corner of a box : lower right corner of a box)
	- tariffZone : one of: 1,2,3,S (http://joukkoliikenne.tampere.fi/fi/muutokset-tampereen-seudun-joukkoliikenteessa-30.6.2014/tariffijarjestelma-ja-vyohykejako.html)

api/1/muncipalities
        - shortName: String
        - name: String

About IDs

Each entity in Journeys API has an id (the url field ends to it), for example in line id "2" in url

http://data.itsfactory.fi/journeys/api/1/lines/2
  • LINE ids relate to "known" line names by the scheduling, for example line Y4 has id Y4. This id is unlikely to change.
  • STOP POINT ids relate to "known" stop point numbers by the scheduling, for example stop point Keskustori M has number 0001 which is also the id of the stop point. This id is unlikely to change.

ROUTEs, JOURNEYs, MUNCIPALITIES and JOURNEY PATTERNs have ids which are likely change over time. These ids should be stored in the client only for short periods of time.

Lines

A client can access specific LINE'S details by issuing request at http://data.itsfactory.fi/journeys/api/1/lines/13

This request would produce following response:

{
    "status" : "success",
    "data" : {
        "headers" : {
            "paging" : {
                "startIndex" : 0,
                "pageSize" : 1,
                "moreData" : false
            }
        },
        "body": [
            {
                "href" : "http://data.itsfactory.fi/journeys/api/1/lines/123",
                "name" : "13",
                "description" : "Hermia - Lukonmäki - Keskustori - Tesoma - Lamminpää - Ylöjärvi"
            }
        ]
    }
}

A client can search LINES by issuing for example following requests:

Routes

A client can access specific ROUTE'S details by issuing request at http://data.itsfactory.fi/journeys/api/1/routes/288

This request would produce following response:

{
    "url" : "http://data.itsfactory.fi/journeys/api/1/routes/288",
    "lineUrl" : "http://data.itsfactory.fi/journeys/api/1/lines/50",
    "name" : "Lempäälä - Koskipuisto H",
    "journeyPatterns" : [ {
      "url" : "http://data.itsfactory.fi/journeys/api/1/journey-patterns/507",
      "originStop" : "http://data.itsfactory.fi/journeys/api/1/stop-points/7559",
      "destinationStop" : "http://data.itsfactory.fi/journeys/api/1/stop-points/0517",
      "name" : "Lempäälä - Koskipuisto H"
    } ],
    "journeys" : [ {
      "url" : "http://data.itsfactory.fi/journeys/api/1/journeys/8726",
      "journeyPatternUrl" : "http://data.itsfactory.fi/journeys/api/1/journey-patterns/507",
      "departureTime" : "07:10:00",
      "arrivalTime" : "07:52:30",
      "dayTypes" : [ "friday" ],
      "dayTypeExceptions" : [ {
        "from" : "2015-04-30",
        "to" : "2015-04-30",
        "runs" : "yes"
      } ]
    }, {
      "url" : "http://data.itsfactory.fi/journeys/api/1/journeys/8728",
      "journeyPatternUrl" : "http://data.itsfactory.fi/journeys/api/1/journey-patterns/507",
      "departureTime" : "07:10:00",
      "arrivalTime" : "07:52:30",
      "dayTypes" : [ "monday", "tuesday", "wednesday", "thursday" ],
      "dayTypeExceptions" : [ {
        "from" : "2015-05-14",
        "to" : "2015-05-14",
        "runs" : "no"
      } ]
    } ],
    "geographicCoordinateProjection" : "6131429,2375268:-349,183:-259,177:-334,61:-225,-67 ..."
  }

In addition to its own url, a ROUTE contains url to the LINE under which it is running. ROUTE has a name. ROUTE also has references to JOURNEY PATTERNs and JOURNEYs operating on it. geographicCoordinateProjection contains information on how to draw the route on a map. This field is encoded to save bandwidth and the client must decode the fields value. The field takes form:

number,number:number,number etc.

These are encoded coordinates, in a form of:

lat1,lon1:delta lat2,delta lon2:delta lat3,delta lon3

a client would decode the field by substracting delta lon2 from lon1 as well as substracting delta lat2 from lat1 and dividing result with 1e5. This would result in a coordinate pair from which delta lat3 and delta lon3 could again be substracted and so on. First lat1 and lon1 should be just divided with 1e5. For example:

First : separated value is 6131429,2375268. First pair of coordinates can be obtained by dividing comma separated values with 1e5. Therefore first coordinate pair would be

lat1: 6131429 / 100000 = 61.31429
lon1: 2375268 / 100000 = 23.75268

==> lat1,lon2 = 61.31429,23.75268

Second coordinate pair would be obtained by substracting:

lat2: (6131429 - (-349)) = 6131778 => 6131778 / 100000 = 61.31778
lon2: (2375268 - 183) = 2375085 =>  2375085 / 100000 = 23.75085

==> lat2,lon2 = 61.31778,23.75085

Respectively, third coordinate pair would be obtained by substracting:

lat3: (6131778 - (-259)) = 6132037 => 6132037 / 100000 = 61.32037
lon3: (2375085 - 177) = 2374908 =>  2374908 / 100000 = 23.74908

==> lat3,lon3 = 61.32037,23.74908

And so on.

Journeys

A client can access specific JOURNEY'S details by issuing request at http://data.itsfactory.fi/journeys/api/1/journeys/2010

This request would produce following response:

{
    "url" : "http://data.itsfactory.fi/journeys/api/1/journeys/2010",
    "activityUrl" : "http://data.itsfactory.fi/journeys/api/1/vehicle-activity?journeyRef=3_0010_3615_0002",
    "routeUrl" : "http://data.itsfactory.fi/journeys/api/1/routes/398",
    "lineUrl" : "http://data.itsfactory.fi/journeys/api/1/lines/3",
    "journeyPatternUrl" : "http://data.itsfactory.fi/journeys/api/1/journey-patterns/333",
    "departureTime" : "00:10:00",
    "arrivalTime" : "00:38:00",
    "dayTypes" : [ "monday" ],
    "dayTypeExceptions" : [ {
      "from" : "2015-04-04",
      "to" : "2015-04-04",
      "runs" : "yes"
    } ],
    "calls" : [ {
      "arrivalTime" : "00:10:00",
      "departureTime" : "00:10:00",
      "stopPoint" : {
        "url" : "http://data.itsfactory.fi/journeys/api/1/stop-points/0002",
        "location" : "61.49756,23.76148",
        "name" : "Keskustori L",
        "shortName" : "0002",
        "tariffZone" : "1",
        "muncipality" : {
          "url" : "http://data.itsfactory.fi/journeys/api/1/muncipalities/837",
          "shortName" : "837",
          "name" : "Tampere"
        }
      }
    }, {
      "arrivalTime" : "00:11:00",
      "departureTime" : "00:11:00",
      "stopPoint" : {
        "url" : "http://data.itsfactory.fi/journeys/api/1/stop-points/0500",
        "location" : "61.49794,23.76558",
        "name" : "Koskipuisto C",
        "shortName" : "0500",
        "tariffZone" : "1",
        "muncipality" : {
          "url" : "http://data.itsfactory.fi/journeys/api/1/muncipalities/837",
          "shortName" : "837",
          "name" : "Tampere"
        }
      }
    }
    ...
    ]
  }

JOURNEY element contains property called "call". A call is NeTEx element which binds together journeys arrival and departure times with STOP POINT information. For example the JOURNEY in the response has three calls: first call tells that the JOURNEY start time and that it starts from STOP POINT with id 222.

A client can search JOURNEYS by issuing following requests:

Journey Patterns

A client can access specific JOURNEY PATTERNS details by issuing request at http://data.itsfactory.fi/journeys/api/1/joourney-patterns/260

This request would produce following response:

{
    "url" : "http://data.itsfactory.fi/journeys/api/1/journey-patterns/260",
    "routeUrl" : "http://data.itsfactory.fi/journeys/api/1/routes/327",
    "lineUrl" : "http://data.itsfactory.fi/journeys/api/1/lines/71",
    "originStop" : "http://data.itsfactory.fi/journeys/api/1/stop-points/1592",
    "destinationStop" : "http://data.itsfactory.fi/journeys/api/1/stop-points/8524",
    "name" : "Kalkku - Nokian asema C",
    "stopPoints" : [ {
      "url" : "http://data.itsfactory.fi/journeys/api/1/stop-points/1592",
      "location" : "61.49475,23.56963",
      "name" : "Kalkku",
      "shortName" : "1592",
      "tariffZone" : "1",
      "muncipality" : {
        "url" : "http://data.itsfactory.fi/journeys/api/1/muncipalities/837",
        "shortName" : "837",
        "name" : "Tampere"
      }
    }, {
      "url" : "http://data.itsfactory.fi/journeys/api/1/stop-points/8549",
      "location" : "61.49445,23.56352",
      "name" : "Välimaantie",
      "shortName" : "8549",
      "tariffZone" : "2",
      "muncipality" : {
        "url" : "http://data.itsfactory.fi/journeys/api/1/muncipalities/536",
        "shortName" : "536",
        "name" : "Nokia"
      }
    }, {
      "url" : "http://data.itsfactory.fi/journeys/api/1/stop-points/8547",
      "location" : "61.49267,23.55689",
      "name" : "Vihnuskallionkatu",
      "shortName" : "8547",
      "tariffZone" : "2",
      "muncipality" : {
        "url" : "http://data.itsfactory.fi/journeys/api/1/muncipalities/536",
        "shortName" : "536",
        "name" : "Nokia"
      }
    }
    ...
    ],
    "journeys" : [ {
      "url" : "http://data.itsfactory.fi/journeys/api/1/journeys/2040",
      "journeyPatternUrl" : "http://data.itsfactory.fi/journeys/api/1/journey-patterns/260",
      "departureTime" : "06:10:00",
      "arrivalTime" : "06:21:00",
      "dayTypes" : [ "friday" ],
      "dayTypeExceptions" : [ {
        "from" : "2015-05-01",
        "to" : "2015-05-01",
        "runs" : "no"
      } ]
    }, {
      "url" : "http://data.itsfactory.fi/journeys/api/1/journeys/2374",
      "journeyPatternUrl" : "http://data.itsfactory.fi/journeys/api/1/journey-patterns/260",
      "departureTime" : "06:10:00",
      "arrivalTime" : "06:21:00",
      "dayTypes" : [ "monday", "tuesday", "wednesday", "thursday" ],
      "dayTypeExceptions" : [ {
        "from" : "2015-05-14",
        "to" : "2015-05-14",
        "runs" : "no"
      } ]
    }
    ...
    ]
  }

A JOURNEY PATTERN contains references to STOP POINTs (which form the JOURNEY PATTERN) as well as references to JOURNEYs which run on this JOURNEY PATTERN.

Stop Points

A client can access specific STOP POINTS details by issuing request at http://data.itsfactory.fi/journeys/api/1/stop-points/0001

This request would produce following response:

{
    "url" : "http://data.itsfactory.fi/journeys/api/1/stop-points/0001",
    "location" : "61.49751,23.76151",
    "name" : "Keskustori M",
    "shortName" : "0001",
    "tariffZone" : "1",
    "muncipality" : {
      "url" : "http://data.itsfactory.fi/journeys/api/1/muncipalities/837",
      "shortName" : "837",
      "name" : "Tampere"
    }
  }

A client can search STOP POINTS by issuing following requests:

About Tariff Zones

Journeys API returns tariff zones as tariff zone ids defined by City of Tampere. Valid tariff zone ids are:

  • "1" for Tariff Zone 1
  • "2" for Tariff Zone 2
  • "3" for Tariff Zone 3
  • "S" for Tariff Zone S ("Seutuliikenne", regional transport)

Please see http://joukkoliikenne.tampere.fi/fi/muutokset-tampereen-seudun-joukkoliikenteessa-30.6.2014/tariffijarjestelma-ja-vyohykejako.html (in finnish) for more details on tariff zones.

Muncipalities

A client can access specific muncipalities details by issuing request at http://data.itsfactory.fi/journeys/api/1/muncipalities/837

This request would produce following response:

{
  "url": "http://data.itsfactory.fi/journeys/api/1/muncipalities/837",
  "shortName": "837",
  "name": "Tampere"
}

Vehicle Activity

Journeys API allows clients to access real-time location of vehicles moving through the street network. To do this, client would make a HTTP GET request to url

http://data.itsfactory.fi/journeys/api/1/vehicle-activity