@digitransit/

Polyline_Decode

Nodejs

Decodes a Google encoded polyline and prints the list of points

fork
loading
Files
  • index.js
  • package-lock.json

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.8448100806393435","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"Y29uc3QgcmVxdWVzdCA9IHJlcXVpcmUoJ3JlcXVlc3QnKTsKY29uc3QgcG9seWxpbmUgPSByZXF1aXJlKCdwb2x5bGluZScpOwoKdmFyIHJlcSA9IHsKICB1cmw6ICdodHRwczovL2FwaS5kaWdpdHJhbnNpdC5maS9yb3V0aW5nL3YxL3JvdXRlcnMvaHNsL2luZGV4L2dyYXBocWwnLAogIG1ldGhvZDogJ1BPU1QnLAogIGhlYWRlcnM6IHsgIkNvbnRlbnQtVHlwZSI6ICJhcHBsaWNhdGlvbi9ncmFwaHFsIiB9LAogIGJvZHk6IGB7IAogICAgICAgICAgcGxhbigKICAgICAgICAgICAgZnJvbToge2xhdDogNjAuMTg3OTAsIGxvbjogMjQuOTYxMjN9CiAgICAgICAgICAgIHRvOiB7bGF0OiA2MC4xNzA5MSwgbG9uOiAyNC45NDE5MH0KICAgICAgICAgICAgbnVtSXRpbmVyYXJpZXM6IDEKICAgICAgICAgICkgewogICAgICAgICAgICBpdGluZXJhcmllcyB7CiAgICAgICAgICAgICAgbGVncyB7CiAgICAgICAgICAgICAgICBmcm9tIHsKICAgICAgICAgICAgICAgICAgbmFtZQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgdG8gewogICAgICAgICAgICAgICAgICBuYW1lCiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBsZWdHZW9tZXRyeSB7CiAgICAgICAgICAgICAgICAgIGxlbmd0aAogICAgICAgICAgICAgICAgICBwb2ludHMKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9YAp9OwoKcmVxdWVzdChyZXEsIGZ1bmN0aW9uIChlcnJvciwgcmVzcG9uc2UsIGJvZHkpIHsKICBpZiAoIWVycm9yICYmIHJlc3BvbnNlLnN0YXR1c0NvZGUgPT0gMjAwKSB7CiAgICB2YXIgcGxhbiA9IEpTT04ucGFyc2UoYm9keSkuZGF0YS5wbGFuOwoKICAgIHZhciBpdGluZXJhcnkgPSBwbGFuLml0aW5lcmFyaWVzWzBdOwoKICAgIGl0aW5lcmFyeS5sZWdzLmZvckVhY2goZnVuY3Rpb24obGVnKSB7CiAgICAgIGNvbnNvbGUubG9nKCJQb2ludHMgZm9yIGxlZyAiK2xlZy5mcm9tLm5hbWUrIiAtPiAiK2xlZy50by5uYW1lKTsKICAgICAgdmFyIGxlZ0dlb21ldHJ5ID0gbGVnLmxlZ0dlb21ldHJ5OwoKICAgICAgLy9EZWNvZGUgcG9seWxpbmUgdXNpbmcgcG9seWxpbmUgbGlicmFyeQogICAgICB2YXIgcG9pbnRzID0gcG9seWxpbmUuZGVjb2RlKGxlZ0dlb21ldHJ5LnBvaW50cyk7CiAgICAgIHBvaW50cy5mb3JFYWNoKGZ1bmN0aW9uKHBvaW50KSB7CiAgICAgICAgY29uc29sZS5sb2cocG9pbnRbMF0rIiwiK3BvaW50WzFdKTsKICAgICAgfSk7CiAgICB9KTsKICB9Cn0pOw=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const request = require('request');
const polyline = require('polyline');

var req = {
  url: 'https://api.digitransit.fi/routing/v1/routers/hsl/index/graphql',
  method: 'POST',
  headers: { "Content-Type": "application/graphql" },
  body: `{ 
          plan(
            from: {lat: 60.18790, lon: 24.96123}
            to: {lat: 60.17091, lon: 24.94190}
            numItineraries: 1
          ) {
            itineraries {
              legs {
                from {
                  name
                }
                to {
                  name
                }
                legGeometry {
                  length
                  points
                }
              }
            }
          }
        }`
};

request(req, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    var plan = JSON.parse(body).data.plan;

    var itinerary = plan.itineraries[0];

    itinerary.legs.forEach(function(leg) {
      console.log("Points for leg "+leg.from.name+" -> "+leg.to.name);
      var legGeometry = leg.legGeometry;

      //Decode polyline using polyline library
      var points = polyline.decode(legGeometry.points);
      points.forEach(function(point) {
        console.log(point[0]+","+point[1]);
      });
    });
  }
});
node v10.16.0