-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathupdate.js
More file actions
187 lines (170 loc) · 5.44 KB
/
Copy pathupdate.js
File metadata and controls
187 lines (170 loc) · 5.44 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const polyline = require('@mapbox/polyline');
const Papa = require('papaparse');
const fs = require('fs');
const fetch = require('node-fetch');
const here = require('./credentials.js');
let uber = Papa.parse(fs.readFileSync('./trips_data.csv', 'utf8'), {
header: true,
}).data;
let jump = Papa.parse(fs.readFileSync('./jump-trips.csv', 'utf8'), {
header: true,
}).data.filter((x) => x['Rental Status'] !== '');
// console.log(jump)
const currency = {
EUR: 1.11,
GBP: 1.29,
PLN: 0.26,
TRY: 0.17,
USD: 1,
};
//
//
// /*
// Data notes
//
// Distance: miles
// */
async function geocode(query) {
const url = `https://geocoder.ls.hereapi.com/6.2/geocode.json?apikey=${here.key}&searchtext=${query}`;
const data = await (await fetch(url)).json();
if (data.hasOwnProperty('Response') && data.Response.View.length > 0) {
return await data.Response.View[0].Result[0].Location
.NavigationPosition[0];
} else {
return 'error';
}
}
async function route(start, end, mode = 'car') {
const url = `https://route.ls.hereapi.com/routing/7.2/calculateroute.json?apikey=${here.key}&waypoint0=geo!${start.Latitude},${start.Longitude}&waypoint1=geo!${end.Latitude},${end.Longitude}&mode=fastest;${mode};traffic:disabled&routeattributes=shape`;
// console.log(url);
const data = await (await fetch(url)).json().catch((e) => console.log(url));
console.log(data);
if (data.hasOwnProperty('response')) {
if (data.response.hasOwnProperty('route')) {
const distance = await data.response.route[0].summary.distance;
const polyline = await data.response.route[0].shape.map((x) => [
Number(x.split(',')[1]),
Number(x.split(',')[0]),
]);
return {
routeError: false,
distance: distance,
polyline: polyline,
};
} else {
return {
routeError: true,
distance: '',
polyline: [],
};
}
} else {
return {
routeError: true,
distance: '',
polyline: [],
};
}
}
async function start() {
uber = uber
.filter((row) => row['Product Type'] !== 'UberEATS Marketplace')
.filter((row) => row['Trip or Order Status'] === 'COMPLETED');
const uberPromises = uber.map(async (row) => {
const startCoordinates = {
Latitude: Number(row['Begin Trip Lat']),
Longitude: Number(row['Begin Trip Lng']),
};
const endCoordinates = {
Latitude: Number(row['Dropoff Lat']),
Longitude: Number(row['Dropoff Lng']),
};
const { polyline, routeError } = await route(
startCoordinates,
endCoordinates
);
// routeError && console.log(routeError)
const mod = {
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: routeError ? 'error' : polyline,
},
properties: {},
};
mod.properties.cost =
currency[row['Fare Currency']] * Number(row['Fare Amount']);
mod.properties.provider = 'uber';
mod.properties.startDate = new Date(row['Begin Trip Time']);
mod.properties.distance = Number(row['Distance (miles)']);
mod.properties.startCoordinates = [
Number(row['Begin Trip Lng']),
Number(row['Begin Trip Lat']),
];
mod.properties.endCoordinates = [
Number(row['Dropoff Lng']),
Number(row['Dropoff Lat']),
];
return mod;
});
uber = await Promise.all(uberPromises);
const jumpPromises = jump.map(async (row) => {
// console.log(row);
// console.log(row['Begin Rental Address'])
const startGeocode = {
Latitude: row['Begin Rental Lat'],
Longitude: row['Begin Rental Lng'],
}; //await geocode(row['Begin Rental Address']);
// console.log(startGeocode);
const endGeocode = {
Latitude: row['Finish Rental Lat'],
Longitude: row['Finish Rental Lng'],
};
// console.log(endGeocode);
const { polyline, routeError } = await route(startGeocode, endGeocode);
routeError && console.log('jump error ' + routeError);
const mod = {
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: routeError ? 'error' : polyline,
},
properties: {},
};
mod.properties.cost = Number(row['Fare Amount'].toString().substring(1));
// console.log(mod.properties.cost);
mod.properties.provider = 'jump';
mod.properties.startDate = new Date(row['Begin Rental Time']);
mod.properties.distance = Number(row['Distance (miles)']);
mod.properties.startCoordinates = [
startGeocode.Longitude,
startGeocode.Latitude,
];
mod.properties.endCoordinates = [
endGeocode.Longitude,
endGeocode.Latitude,
];
return mod;
});
jump = await Promise.all(jumpPromises);
const old = require('../src/assets/data.json').features.filter(
(x) =>
x.properties.provider !== 'uber' && x.properties.provider !== 'jump'
);
// console.log(uber);
const output = [...uber, ...jump, ...old].filter(
(x) => x.geometry.coordinates !== 'error'
);
// console.log(output);
fs.writeFile(
'../src/updated.json',
JSON.stringify({
type: 'FeatureCollection',
features: output,
}),
function (err) {
console.log('File was saved!');
}
);
}
start();