Skip to content

Instantly share code, notes, and snippets.

@juanpujol
Last active January 23, 2017 19:19
Show Gist options
  • Save juanpujol/dc2651b9ff72a8610fd29fb3c4be0a69 to your computer and use it in GitHub Desktop.
Save juanpujol/dc2651b9ff72a8610fd29fb3c4be0a69 to your computer and use it in GitHub Desktop.
MBTA Realtime example
const normalizedCollection = [];
newHook.result.mode.forEach((mode) => {
const feature = {
mode: mode.mode_name,
locality: 'Boston',
stateCode: 'MA',
geometry: {
type: 'Point',
coordinates: [],
},
};
mode.route.forEach((route) => {
// Set route
feature.route = route.route_id;
route.direction.forEach((direction) => {
// Set direction
feature.direction = direction.direction_name;
direction.trip.forEach((trip) => {
// Set tripId
feature.tripId = trip.trip_id;
const vehicle = trip.vehicle;
// Set vehicleId
feature.vehicle = vehicle.vehicle_id;
// Set vehicle coordinates
feature.geometry.coordinates = [
vehicle.vehicle_lon,
vehicle.vehicle_lat,
];
// Set vehicle time
const unixTime = moment.unix(Number(vehicle.vehicle_timestamp));
feature.time = moment.tz(unixTime, 'EST').format();
console.log(feature) // Logs OK.
normalizedCollection.push(feature) // sync problem
});
});
});
console.log(normalizedCollection);
});
{
"mode": [
{
"route_type": "0",
"mode_name": "Subway",
"route": [
{
"route_id": "Green-B",
"route_name": "Green Line B",
"direction": [
{
"direction_id": "0",
"direction_name": "Westbound",
"trip": [
{
"trip_id": "3697_1485185400_0",
"trip_name": "B train from Park Street to Boston College",
"trip_headsign": "Boston College",
"vehicle": {
"vehicle_id": "10011",
"vehicle_lat": "42.34027",
"vehicle_lon": "-71.16689",
"vehicle_bearing": "298",
"vehicle_timestamp": "1485186201",
"vehicle_label": "3697-3835"
}
},
{
"trip_id": "668_0",
"trip_name": "B train from Park Street to Boston College",
"trip_headsign": "Boston College",
"vehicle": {
"vehicle_id": "10078",
"vehicle_lat": "42.35556",
"vehicle_lon": "-71.06326",
"vehicle_bearing": "210",
"vehicle_timestamp": "1485186190",
"vehicle_label": "3645-3812"
}
},
{
"trip_id": "660_0",
"trip_name": "B train from Park Street to Boston College",
"trip_headsign": "Boston College",
"vehicle": {
"vehicle_id": "10076",
"vehicle_lat": "42.34978",
"vehicle_lon": "-71.07855",
"vehicle_bearing": "240",
"vehicle_timestamp": "1485186160",
"vehicle_label": "3869-3604"
}
},
{
"trip_id": "646_0",
"trip_name": "B train from Park Street to Boston College",
"trip_headsign": "Boston College",
"vehicle": {
"vehicle_id": "10165",
"vehicle_lat": "42.34962",
"vehicle_lon": "-71.1032",
"vehicle_bearing": "278",
"vehicle_timestamp": "1485186219",
"vehicle_label": "3658-3819"
}
},
{
"trip_id": "640_0",
"trip_name": "B train from Park Street to Boston College",
"trip_headsign": "Boston College",
"vehicle": {
"vehicle_id": "10158",
"vehicle_lat": "42.35141",
"vehicle_lon": "-71.11802",
"vehicle_bearing": "278",
"vehicle_timestamp": "1485186204",
"vehicle_label": "3608-3881"
}
},
{
"trip_id": "631_0",
"trip_name": "B train from Park Street to Boston College",
"trip_headsign": "Boston College",
"vehicle": {
"vehicle_id": "10147",
"vehicle_lat": "42.34901",
"vehicle_lon": "-71.13409",
"vehicle_bearing": "236",
"vehicle_timestamp": "1485186219",
"vehicle_label": "3686-3836"
}
},
{
"trip_id": "625_0",
"trip_name": "B train from Park Street to Boston College",
"trip_headsign": "Boston College",
"vehicle": {
"vehicle_id": "10142",
"vehicle_lat": "42.34845",
"vehicle_lon": "-71.13712",
"vehicle_bearing": "286",
"vehicle_timestamp": "1485186177",
"vehicle_label": "3687-3844"
}
}
]
},
{
"direction_id": "1",
"direction_name": "Eastbound",
"trip": [
{
"trip_id": "3713_1485185400_1",
"trip_name": "B train from Boston College to Park Street",
"trip_headsign": "Park Street",
"vehicle": {
"vehicle_id": "10026",
"vehicle_lat": "42.35554",
"vehicle_lon": "-71.06322",
"vehicle_bearing": "30",
"vehicle_timestamp": "1485186073",
"vehicle_label": "3713-3863"
}
},
{
"trip_id": "667_1",
"trip_name": "B train from Boston College to Park Street",
"trip_headsign": "Park Street",
"vehicle": {
"vehicle_id": "10044",
"vehicle_lat": "42.34018",
"vehicle_lon": "-71.16709",
"vehicle_bearing": "138",
"vehicle_timestamp": "1485186095",
"vehicle_label": "3610-3831"
}
},
{
"trip_id": "663_1",
"trip_name": "B train from Boston College to Park Street",
"trip_headsign": "Park Street",
"vehicle": {
"vehicle_id": "10107",
"vehicle_lat": "42.33816",
"vehicle_lon": "-71.15424",
"vehicle_bearing": "116",
"vehicle_timestamp": "1485186197",
"vehicle_label": "3627-3880"
}
},
{
"trip_id": "653_1",
"trip_name": "B train from Boston College to Park Street",
"trip_headsign": "Park Street",
"vehicle": {
"vehicle_id": "10102",
"vehicle_lat": "42.34413",
"vehicle_lon": "-71.14252",
"vehicle_bearing": "30",
"vehicle_timestamp": "1485186188",
"vehicle_label": "3603-3890"
}
},
{
"trip_id": "658_1",
"trip_name": "B train from Boston College to Park Street",
"trip_headsign": "Park Street",
"vehicle": {
"vehicle_id": "10233",
"vehicle_lat": "42.34872",
"vehicle_lon": "-71.13809",
"vehicle_bearing": "115",
"vehicle_timestamp": "1485186217",
"vehicle_label": "3664"
}
},
{
"trip_id": "657_1",
"trip_name": "B train from Boston College to Park Street",
"trip_headsign": "Park Street",
"vehicle": {
"vehicle_id": "10232",
"vehicle_lat": "42.34872",
"vehicle_lon": "-71.13809",
"vehicle_bearing": "113",
"vehicle_timestamp": "1485186204",
"vehicle_label": "3838"
}
},
{
"trip_id": "635_1",
"trip_name": "B train from Boston College to Park Street",
"trip_headsign": "Park Street",
"vehicle": {
"vehicle_id": "10037",
"vehicle_lat": "42.35207",
"vehicle_lon": "-71.12407",
"vehicle_bearing": "94",
"vehicle_timestamp": "1485186219",
"vehicle_label": "3643-3855"
}
},
{
"trip_id": "632_1",
"trip_name": "B train from Boston College to Park Street",
"trip_headsign": "Park Street",
"vehicle": {
"vehicle_id": "10019",
"vehicle_lat": "42.35115",
"vehicle_lon": "-71.11633",
"vehicle_bearing": "99",
"vehicle_timestamp": "1485186195",
"vehicle_label": "3653-3889"
}
},
{
"trip_id": "612_1",
"trip_name": "B train from Boston College to Park Street",
"trip_headsign": "Park Street",
"vehicle": {
"vehicle_id": "10093",
"vehicle_lat": "42.34891",
"vehicle_lon": "-71.09763",
"vehicle_bearing": "95",
"vehicle_timestamp": "1485186208",
"vehicle_label": "3850-3680"
}
},
{
"trip_id": "611_1",
"trip_name": "B train from Boston College to Park Street",
"trip_headsign": "Park Street",
"vehicle": {
"vehicle_id": "10077",
"vehicle_lat": "42.34896",
"vehicle_lon": "-71.09402",
"vehicle_bearing": "85",
"vehicle_timestamp": "1485186199",
"vehicle_label": "3864-3622"
}
}
]
}
]
},
{
"route_id": "Green-C",
"route_name": "Green Line C",
"direction": [
{
"direction_id": "0",
"direction_name": "Westbound",
"trip": [
{
"trip_id": "661_0",
"trip_name": "C train from North Station to Cleveland Circle",
"trip_headsign": "Cleveland Circle",
"vehicle": {
"vehicle_id": "10039",
"vehicle_lat": "42.35631",
"vehicle_lon": "-71.06277",
"vehicle_bearing": "210",
"vehicle_timestamp": "1485186198",
"vehicle_label": "3700-3800"
}
},
{
"trip_id": "671_0",
"trip_name": "C train from North Station to Cleveland Circle",
"trip_headsign": "Cleveland Circle",
"vehicle": {
"vehicle_id": "10002",
"vehicle_lat": "42.36543",
"vehicle_lon": "-71.0604",
"vehicle_bearing": "135",
"vehicle_timestamp": "1485186219",
"vehicle_label": "3845-3877"
}
},
{
"trip_id": "644_0",
"trip_name": "C train from North Station to Cleveland Circle",
"trip_headsign": "Cleveland Circle",
"vehicle": {
"vehicle_id": "10017",
"vehicle_lat": "42.34791",
"vehicle_lon": "-71.08631",
"vehicle_bearing": "285",
"vehicle_timestamp": "1485186202",
"vehicle_label": "3829-3617"
}
},
{
"trip_id": "637_0",
"trip_name": "C train from North Station to Cleveland Circle",
"trip_headsign": "Cleveland Circle",
"vehicle": {
"vehicle_id": "10164",
"vehicle_lat": "42.34509",
"vehicle_lon": "-71.11063",
"vehicle_bearing": "247",
"vehicle_timestamp": "1485186221",
"vehicle_label": "3852-3624"
}
},
{
"trip_id": "630_0",
"trip_name": "C train from North Station to Cleveland Circle",
"trip_headsign": "Cleveland Circle",
"vehicle": {
"vehicle_id": "10156",
"vehicle_lat": "42.34058",
"vehicle_lon": "-71.12717",
"vehicle_bearing": "241",
"vehicle_timestamp": "1485186203",
"vehicle_label": "3851-3614"
}
},
{
"trip_id": "622_0",
"trip_name": "C train from North Station to Cleveland Circle",
"trip_headsign": "Cleveland Circle",
"vehicle": {
"vehicle_id": "10083",
"vehicle_lat": "42.33984",
"vehicle_lon": "-71.12983",
"vehicle_bearing": "258",
"vehicle_timestamp": "1485186221",
"vehicle_label": "3636-3805"
}
}
]
},
{
"direction_id": "1",
"direction_name": "Eastbound",
"trip": [
{
"trip_id": "3822_1485185400_1",
"trip_name": "C train from Cleveland Circle to North Station",
"trip_headsign": "North Station",
"vehicle": {
"vehicle_id": "10043",
"vehicle_lat": "42.36607",
"vehicle_lon": "-71.061",
"vehicle_bearing": "315",
"vehicle_timestamp": "1485186182",
"vehicle_label": "3822-3715"
}
},
{
"trip_id": "662_1",
"trip_name": "C train from Cleveland Circle to North Station",
"trip_headsign": "North Station",
"vehicle": {
"vehicle_id": "10143",
"vehicle_lat": "42.33695",
"vehicle_lon": "-71.14558",
"vehicle_bearing": "74",
"vehicle_timestamp": "1485186182",
"vehicle_label": "3692-3804"
}
},
{
"trip_id": "651_1",
"trip_name": "C train from Cleveland Circle to North Station",
"trip_headsign": "North Station",
"vehicle": {
"vehicle_id": "10061",
"vehicle_lat": "42.34126",
"vehicle_lon": "-71.1249",
"vehicle_bearing": "70",
"vehicle_timestamp": "1485186219",
"vehicle_label": "3708-3868"
}
},
{
"trip_id": "633_1",
"trip_name": "C train from Cleveland Circle to North Station",
"trip_headsign": "North Station",
"vehicle": {
"vehicle_id": "10025",
"vehicle_lat": "42.34862",
"vehicle_lon": "-71.0976",
"vehicle_bearing": "60",
"vehicle_timestamp": "1485186187",
"vehicle_label": "3806-3813"
}
},
{
"trip_id": "623_1",
"trip_name": "C train from Cleveland Circle to North Station",
"trip_headsign": "North Station",
"vehicle": {
"vehicle_id": "10108",
"vehicle_lat": "42.34977",
"vehicle_lon": "-71.07825",
"vehicle_bearing": "60",
"vehicle_timestamp": "1485186178",
"vehicle_label": "3871-3652"
}
}
]
}
]
},
{
"route_id": "Green-D",
"route_name": "Green Line D",
"direction": [
{
"direction_id": "0",
"direction_name": "Westbound",
"trip": [
{
"trip_id": "3616_1485185400_0",
"trip_name": "D train from Government Center to Riverside",
"trip_headsign": "Riverside",
"vehicle": {
"vehicle_id": "10047",
"vehicle_lat": "42.33723",
"vehicle_lon": "-71.25208",
"vehicle_bearing": "305",
"vehicle_timestamp": "1485185748",
"vehicle_label": "3616-3818"
}
},
{
"trip_id": "3628_1485185400_0",
"trip_name": "D train from Government Center to Riverside",
"trip_headsign": "Riverside",
"vehicle": {
"vehicle_id": "10058",
"vehicle_lat": "42.33723",
"vehicle_lon": "-71.25208",
"vehicle_bearing": "302",
"vehicle_timestamp": "1485186208",
"vehicle_label": "3628-3876"
}
},
{
"trip_id": "656_0",
"trip_name": "D train from Government Center to Riverside",
"trip_headsign": "Riverside",
"vehicle": {
"vehicle_id": "10182",
"vehicle_lat": "42.35164",
"vehicle_lon": "-71.07172",
"vehicle_bearing": "240",
"vehicle_timestamp": "1485186202",
"vehicle_label": "3691-3842"
}
},
{
"trip_id": "641_0",
"trip_name": "D train from Government Center to Riverside",
"trip_headsign": "Riverside",
"vehicle": {
"vehicle_id": "10020",
"vehicle_lat": "42.33259",
"vehicle_lon": "-71.11699",
"vehicle_bearing": "236",
"vehicle_timestamp": "1485186212",
"vehicle_label": "3625-3882"
}
},
{
"trip_id": "634_0",
"trip_name": "D train from Government Center to Riverside",
"trip_headsign": "Riverside",
"vehicle": {
"vehicle_id": "10190",
"vehicle_lat": "42.33131",
"vehicle_lon": "-71.12456",
"vehicle_bearing": "262",
"vehicle_timestamp": "1485186219",
"vehicle_label": "3694-3801"
}
},
{
"trip_id": "621_0",
"trip_name": "D train from Government Center to Riverside",
"trip_headsign": "Riverside",
"vehicle": {
"vehicle_id": "10123",
"vehicle_lat": "42.3282",
"vehicle_lon": "-71.19504",
"vehicle_bearing": "235",
"vehicle_timestamp": "1485186210",
"vehicle_label": "3841-3682"
}
},
{
"trip_id": "617_0",
"trip_name": "D train from Government Center to Riverside",
"trip_headsign": "Riverside",
"vehicle": {
"vehicle_id": "10009",
"vehicle_lat": "42.32259",
"vehicle_lon": "-71.20535",
"vehicle_bearing": "219",
"vehicle_timestamp": "1485186219",
"vehicle_label": "3853-3717"
}
},
{
"trip_id": "610_0",
"trip_name": "D train from Government Center to Riverside",
"trip_headsign": "Riverside",
"vehicle": {
"vehicle_id": "10137",
"vehicle_lat": "42.33514",
"vehicle_lon": "-71.24778",
"vehicle_bearing": "303",
"vehicle_timestamp": "1485186210",
"vehicle_label": "3888-3607"
}
}
]
},
{
"direction_id": "1",
"direction_name": "Eastbound",
"trip": [
{
"trip_id": "3655_1485185400_1",
"trip_name": "D train from Riverside to Government Center",
"trip_headsign": "Government Center",
"vehicle": {
"vehicle_id": "10228",
"vehicle_lat": "42.35927",
"vehicle_lon": "-71.05946",
"vehicle_bearing": "35",
"vehicle_timestamp": "1485186189",
"vehicle_label": "3655-3816"
}
},
{
"trip_id": "670_1",
"trip_name": "D train from Riverside to Government Center",
"trip_headsign": "Government Center",
"vehicle": {
"vehicle_id": "10235",
"vehicle_lat": "42.33285",
"vehicle_lon": "-71.24337",
"vehicle_bearing": "124",
"vehicle_timestamp": "1485186202",
"vehicle_label": "3673-3874"
}
},
{
"trip_id": "666_1",
"trip_name": "D train from Riverside to Government Center",
"trip_headsign": "Government Center",
"vehicle": {
"vehicle_id": "10110",
"vehicle_lat": "42.31981",
"vehicle_lon": "-71.21838",
"vehicle_bearing": "122",
"vehicle_timestamp": "1485186213",
"vehicle_label": "3661-3861"
}
},
{
"trip_id": "647_1",
"trip_name": "D train from Riverside to Government Center",
"trip_headsign": "Government Center",
"vehicle": {
"vehicle_id": "10032",
"vehicle_lat": "42.33514",
"vehicle_lon": "-71.1482",
"vehicle_bearing": "76",
"vehicle_timestamp": "1485186192",
"vehicle_label": "3817-3676"
}
},
{
"trip_id": "638_1",
"trip_name": "D train from Riverside to Government Center",
"trip_headsign": "Government Center",
"vehicle": {
"vehicle_id": "10103",
"vehicle_lat": "42.33127",
"vehicle_lon": "-71.1266",
"vehicle_bearing": "97",
"vehicle_timestamp": "1485186206",
"vehicle_label": "3810-3642"
}
},
{
"trip_id": "629_1",
"trip_name": "D train from Riverside to Government Center",
"trip_headsign": "Government Center",
"vehicle": {
"vehicle_id": "10100",
"vehicle_lat": "42.34892",
"vehicle_lon": "-71.0943",
"vehicle_bearing": "85",
"vehicle_timestamp": "1485186200",
"vehicle_label": "3665-3821"
}
},
{
"trip_id": "620_1",
"trip_name": "D train from Riverside to Government Center",
"trip_headsign": "Government Center",
"vehicle": {
"vehicle_id": "10157",
"vehicle_lat": "42.35245",
"vehicle_lon": "-71.06483",
"vehicle_bearing": "20",
"vehicle_timestamp": "1485186186",
"vehicle_label": "3706-3892"
}
},
{
"trip_id": "642_1",
"trip_name": "D train from Reservoir to North Station",
"trip_headsign": "North Station",
"vehicle": {
"vehicle_id": "10223",
"vehicle_lat": "42.35986",
"vehicle_lon": "-71.05891",
"vehicle_bearing": "35",
"vehicle_timestamp": "1485186193",
"vehicle_label": "3626"
}
}
]
}
]
},
{
"route_id": "Green-E",
"route_name": "Green Line E",
"direction": [
{
"direction_id": "0",
"direction_name": "Westbound",
"trip": [
{
"trip_id": "664_0",
"trip_name": "E train from Lechmere to Heath Street",
"trip_headsign": "Heath Street",
"vehicle": {
"vehicle_id": "10062",
"vehicle_lat": "42.36234",
"vehicle_lon": "-71.05804",
"vehicle_bearing": "175",
"vehicle_timestamp": "1485186211",
"vehicle_label": "3705-3823"
}
},
{
"trip_id": "655_0",
"trip_name": "E train from Lechmere to Heath Street",
"trip_headsign": "Heath Street",
"vehicle": {
"vehicle_id": "10231",
"vehicle_lat": "42.35356",
"vehicle_lon": "-71.06432",
"vehicle_bearing": "200",
"vehicle_timestamp": "1485186188",
"vehicle_label": "3839-3668"
}
},
{
"trip_id": "636_0",
"trip_name": "E train from Lechmere to Heath Street",
"trip_headsign": "Heath Street",
"vehicle": {
"vehicle_id": "10177",
"vehicle_lat": "42.33791",
"vehicle_lon": "-71.09506",
"vehicle_bearing": "241",
"vehicle_timestamp": "1485186213",
"vehicle_label": "3884-3651"
}
},
{
"trip_id": "628_0",
"trip_name": "E train from North Station to Heath Street",
"trip_headsign": "Heath Street",
"vehicle": {
"vehicle_id": "10221",
"vehicle_lat": "42.33326",
"vehicle_lon": "-71.10899",
"vehicle_bearing": "266",
"vehicle_timestamp": "1485186207",
"vehicle_label": "3649-3825"
}
}
]
},
{
"direction_id": "1",
"direction_name": "Eastbound",
"trip": [
{
"trip_id": "3843_1485185400_1",
"trip_name": "E train from Heath Street to Lechmere",
"trip_headsign": "Lechmere",
"vehicle": {
"vehicle_id": "10187",
"vehicle_lat": "42.37108",
"vehicle_lon": "-71.07723",
"vehicle_bearing": "305",
"vehicle_timestamp": "1485186030",
"vehicle_label": "3843-3716"
}
},
{
"trip_id": "665_1",
"trip_name": "E train from Heath Street to Lechmere",
"trip_headsign": "Lechmere",
"vehicle": {
"vehicle_id": "10222",
"vehicle_lat": "42.33391",
"vehicle_lon": "-71.10533",
"vehicle_bearing": "54",
"vehicle_timestamp": "1485186169",
"vehicle_label": "3840-3670"
}
},
{
"trip_id": "659_1",
"trip_name": "E train from Heath Street to Lechmere",
"trip_headsign": "Lechmere",
"vehicle": {
"vehicle_id": "10096",
"vehicle_lat": "42.33546",
"vehicle_lon": "-71.1012",
"vehicle_bearing": "66",
"vehicle_timestamp": "1485186201",
"vehicle_label": "3635-3846"
}
},
{
"trip_id": "650_1",
"trip_name": "E train from Heath Street to Lechmere",
"trip_headsign": "Lechmere",
"vehicle": {
"vehicle_id": "10050",
"vehicle_lat": "42.34115",
"vehicle_lon": "-71.08692",
"vehicle_bearing": "55",
"vehicle_timestamp": "1485186210",
"vehicle_label": "3710-3848"
}
},
{
"trip_id": "639_1",
"trip_name": "E train from Heath Street to Lechmere",
"trip_headsign": "Lechmere",
"vehicle": {
"vehicle_id": "10067",
"vehicle_lat": "42.35157",
"vehicle_lon": "-71.07167",
"vehicle_bearing": "60",
"vehicle_timestamp": "1485186218",
"vehicle_label": "3828-3690"
}
},
{
"trip_id": "652_1",
"trip_name": "E train from Heath Street to Lechmere",
"trip_headsign": "Lechmere",
"vehicle": {
"vehicle_id": "10230",
"vehicle_lat": "42.35614",
"vehicle_lon": "-71.06243",
"vehicle_bearing": "30",
"vehicle_timestamp": "1485186094",
"vehicle_label": "3662-3834"
}
},
{
"trip_id": "624_1",
"trip_name": "E train from Heath Street to Lechmere",
"trip_headsign": "Lechmere",
"vehicle": {
"vehicle_id": "10016",
"vehicle_lat": "42.36563",
"vehicle_lon": "-71.06058",
"vehicle_bearing": "315",
"vehicle_timestamp": "1485186208",
"vehicle_label": "3866-3641"
}
}
]
}
]
}
]
},
{
"route_type": "3",
"mode_name": "Bus",
"route": [
{
"route_id": "70A",
"route_name": "70A",
"direction": [
{
"direction_id": "0",
"direction_name": "Outbound",
"trip": [
{
"trip_id": "32882937",
"trip_name": "10:08 am from Franklin St @ Sidney St to Lincoln St opp Silver Hill Ln",
"trip_headsign": "North Waltham",
"vehicle": {
"vehicle_id": "y0627",
"vehicle_lat": "42.3743209838867",
"vehicle_lon": "-71.209716796875",
"vehicle_bearing": "304",
"vehicle_speed": "0",
"vehicle_timestamp": "1485186167",
"vehicle_label": "0627"
}
}
]
}
]
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment