1.0.11 • Published 6 years ago

digitransit v1.0.11

Weekly downloads
-
License
ISC
Repository
-
Last release
6 years ago

Digitransit

Node.js module for Digitransit Routing API and Geocoding API. Module converts a Javascript object into a GraphQL query and returns results as a promise.

About

We created this module for Tripper to generate basic itineraries for daily allowance (päiväraha) and distance compensation (kilometrikorvaus) suggestions.

Tripper logo

Getting Started

Install module, generate query object for either routing or address search and enjoy results.

See test.js for a sample tripObject and addressObject that can be passed to the module.

For the options regarding travel modes etc. please, refer to the original Digitransit docs at https://digitransit.fi/en/developers/services-and-apis/1-routing-api/

Installation

Using npm:

$ npm install --save digitransit

Test

$ npm test

Use

const digitransit = require('digitransit');

// First routing, then addressSearch and last address lookup

// FIRST: Routing API
// Query where we leave from Tampere and go directly to Helsinki
// Modes are Transit and walking and we want to depart asap
// We only want one itinerary
// We could provide the waypoints in an array of objects with latitude (lat)
// and longitude (lon) for each stop in RIGHT order
const tripObject = {
  waypoints: [],
  from: {
    lat: 61.502659,
    lon: 23.779152,
  },
  to: {
    lat: 60.171570,
    lon: 24.945303,
  },
  itineraries: 1,
  modes: ['walk', 'transit'],
  arrivalTime: false,
  time: new Date(),
};

digitransit.routing(tripObject).then((response) => {
    console.log(response)
    /*
    Should be something like:
    {
      success: true,
      itineraries: [
        {
          legs: [
          { startTime: 1499165526000,
            endTime: 1499166150000,
            mode: 'WALK',
            duration: 624,
            distance: 774.9380000000001,
            agency: null },
          { startTime: 1499166151000,
            endTime: 1499171665000,
            mode: 'RAIL',
            duration: 5514,
            distance: 165864.96795957946,
            agency: { name: 'VR Osakeyhtiö' } },
          { startTime: 1499171666000,
            endTime: 1499171898000,
            mode: 'WALK',
            duration: 232,
            distance: 264.704,
            agency: null }
          ]
        }
      ]
    }
    */
}).catch((error) => {
  console.log(error);
});

// SECOND Address API
// Query an address in Tampere
// We want only one result (the best) and address names in Finnish
const addressObject = {
  text: 'Tammelan puistokatu 22, 33100 Tampere',
  size: 1,
  lang: 'fi'
};
digitransit.addressSearch(addressObject).then((response) => {
    console.log(response);
    /*
    Should be something like:
    {
      success: true,
      results:
       [
        {
          type: 'Feature',
          geometry: { type: 'Point', coordinates: [Object] },
          properties:
           { id: 'polyline:71199',
             gid: 'openstreetmap:street:polyline:71199',
             layer: 'street',
             source: 'openstreetmap',
             source_id: 'polyline:71199',
             name: 'Tammelan puistokatu',
             street: 'Tammelan puistokatu',
             postalcode: '33100',
             postalcode_gid: 'whosonfirst:postalcode:421475007',
             confidence: 0.9388888888888888,
             accuracy: 'centroid',
             country: 'Suomi',
             country_gid: 'whosonfirst:country:85633143',
             country_a: 'FIN',
             region: 'Pirkanmaa',
             region_gid: 'whosonfirst:region:85683099',
             localadmin: 'Tampere',
             localadmin_gid: 'whosonfirst:localadmin:907199231',
             locality: 'Tampere',
             locality_gid: 'whosonfirst:locality:101748431',
             neighbourhood: 'Tammela',
             neighbourhood_gid: 'whosonfirst:neighbourhood:1108728527',
             label: 'Tammelan puistokatu, Tampere' },
             bbox: [ 23.779277, 61.498954, 23.779724, 61.506702 ]
        }
      ]
    }
    */
}).catch((error) => {
  console.log(error);
});

// THIRD Address lookup
const coordinates = {
  lat: 61.502659,
  lon: 23.779152,
  size: 1,
  lang: 'fi',
};
digitransit.addressLookup(coordinates).then((response) => {
    console.log(response);
    /*
    Should be something like:
    {
      success: true,
      results:
        [
          {
            type: 'Feature',
            geometry: { type: 'Point', coordinates: [Object] },
            properties:
              {
                id: 'fi/pirkanmaa:103430467N-1',
                gid: 'openaddresses:address:fi/pirkanmaa:103430467N-1',
                layer: 'address',
                source: 'openaddresses',
                source_id: 'fi/pirkanmaa:103430467n-1',
                name: 'Tammelan Puistokatu 24',
                housenumber: '24',
                street: 'Tammelan Puistokatu',
                postalcode: '33100',
                postalcode_gid: 'whosonfirst:postalcode:421475007',
                confidence: 0.8,
                distance: 0.012,
                accuracy: 'point',
                country: 'Suomi',
                country_gid: 'whosonfirst:country:85633143',
                country_a: 'FIN',
                region: 'Pirkanmaa',
                region_gid: 'whosonfirst:region:85683099',
                localadmin: 'Tampere',
                localadmin_gid: 'whosonfirst:localadmin:907199231',
                locality: 'Tampere',
                locality_gid: 'whosonfirst:locality:101748431',
                neighbourhood: 'Tammela',
                neighbourhood_gid: 'whosonfirst:neighbourhood:1108728527',
                label: 'Tammelan Puistokatu 24, Tampere'
              }
            }
        ]
    }
    */
}).catch((error) => {
  console.log(error);
});

Changelog

1.0.10 = Added background infromation about the project 1.0.9 = Changed the query object in the sample from isArrivalTime to arrivalTime

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago