Skip to content

Instantly share code, notes, and snippets.

@brecke
Created October 27, 2013 14:56
Show Gist options
  • Select an option

  • Save brecke/7183228 to your computer and use it in GitHub Desktop.

Select an option

Save brecke/7183228 to your computer and use it in GitHub Desktop.

Revisions

  1. brecke created this gist Oct 27, 2013.
    100 changes: 100 additions & 0 deletions find_routes.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    // find SMTUC routes by coordinates

    allPoints = {
    "Pólo I (UC)": "-8.424174785614014,40.20796711699945",
    "Pólo II (UC)": "-8.415849208831787,40.2186240266062",
    "Jardim Botânico": "-8.420548439025879,40.206134690235245",
    "Praça da República": "-8.419668674468994,40.20949112422609",
    "Arcos do Jardim": "-8.420119285583496,40.206459463231106",
    "HUC": "-8.413639068603516,40.21848698479009",
    "Cruz de Celas": "-8.413327932357788,40.21345004307392",
    "Olivais": "-8.4037846326828,40.217778558308545",
    "Hospital Pediátrico": "-8.41500699520111,40.22287714961117",
    "Estádio": "-8.40729296207428,40.20138700265597",
    "Estação Nova": "-8.432232141494751,40.20923996623667",
    "Rodovária": "-8.436421969688467,40.21618753454799",
    "Estação Velha": "-8.439894259556695,40.22410469534916",
    "Parque da Cidade": "-8.428895473480225,40.20654827508496",
    "Largo da Portagem": "-8.429378271102905,40.20727884443912",
    "Sé Nova": "-8.42424272852304,40.209282189778385",
    "Sé Velha": "-8.427338844769224,40.20888275448279",
    "Museu Machado de Castro": "-8.422350883483887,40.20742633200469",
    "Convento de Santa Clara": "-8.433691039875157,40.20341332673922",
    "Estádio Universitário": "-8.432443352488804,40.20591252076246",
    "Quinta das Lágrimas": "-8.432457447052002,40.199297649893715",
    "Fórum Coimbra": "-8.443644940853119,40.21277868440866",
    "Dolce Vita": "-8.410114645957947,40.20492362914641",
    "Coimbra Shopping": "-8.407502174377441,40.19494768675609",
    "Penedo da Saudade": "-8.41478705406189,40.2054598047443",
    "Mercado D. Pedro V": "-8.427797108888626,40.21158916616087",
    "Mercado do Calhabé": "-8.413629864717223,40.203435860867465",
    "ESEC": "-8.406311161912981,40.205538671779415",
    "ISEC": "-8.410248290603022,40.19334391916367",
    "IPN": "-8.413875102996826,40.19220884028267",
    "TAGV": "-8.420687913894653,40.20967138087912",
    "Portugal dos Pequeninos": "-8.434699773788452,40.20317364509356",
    "Pátio da Inquisação": "-8.42796676712544,40.21223588951336",
    "Baixa": "-8.429367542266846,40.21153946705412",
    "Loja do Cidadão": "-8.432011391628748,40.21043746630625",
    "Vale das Flores": "-8.406193256378174,40.1947084473279",
    "Elísio de Moura": "-8.398390710353851,40.20983748901594",
    "Igreja de S. José": "-8.410098552703857,40.20286348326487",
    "Igreja de Sta. Cruz": "-8.428863286972046,40.211564046792134",
    "CHC": "-8.461189270019531,40.19569187402477"
    }

    myKey = "";
    agency_id = "2";
    range = 0.2; // 100m
    offset = 0;
    url = 'https://api.ost.pt/routes';
    standardRoutes = ['5', '7', '7T', '14', '14T', '24', '24T', '34', '38'];

    var request = require('request');

    for (var eachPOIname in allPoints) {
    if (allPoints.hasOwnProperty(eachPOIname)) {
    eachPOIcoords = allPoints[eachPOIname];
    routes = doRequest(eachPOIname, eachPOIcoords, function(eachPOIname, routes) {
    console.log("No POI " + eachPOIname + " passam as linhas: " + routes);

    intersectedRoutes = [];
    for (var i = 0; i < standardRoutes.length; i++) {
    if (routes.indexOf(standardRoutes[i]) >= 0) {
    console.log(" -> Encontrei a " + standardRoutes[i]);
    intersectedRoutes.push(standardRoutes[i]);
    }
    }
    console.log(" -> Confirma-se que aqui passam: " + intersectedRoutes + "\n");
    });
    }
    }

    function doRequest(POIname, pointCoordinates, callback) {
    request({
    url: url,
    qs: {
    key: myKey,
    center: pointCoordinates,
    range: range,
    agency_id: agency_id,
    offset: offset
    },
    json: true
    },
    function (error, response, body) {
    if(response.statusCode != 200) {
    console.log(response.statusCode + " oops!");
    }
    else if (!error && response.statusCode == 200) {
    routes = [];
    for (var i = 0; i < body['Objects'].length; i++) {
    // console.log("-> " + );
    routes.push(body['Objects'][i]['route_short_name']);
    }

    // console.log(routes);
    callback(POIname, routes);
    }
    })
    }