Skip to content

Instantly share code, notes, and snippets.

@TravelTime-Frontend
Last active February 21, 2023 08:52
Show Gist options
  • Save TravelTime-Frontend/a78a1dc715623abef61ed94b07678482 to your computer and use it in GitHub Desktop.
Save TravelTime-Frontend/a78a1dc715623abef61ed94b07678482 to your computer and use it in GitHub Desktop.
Point in polygon JavaScript
import 'dotenv/config';
import fs from 'fs';
import readline from 'readline';
import { TravelTimeClient } from 'traveltime-api';
const data = [];
const stream = fs.createReadStream('../data/customers.csv');
const reader = readline.createInterface({ input: stream });
const outputDir = './results';
const outputPath = `${outputDir}/reachable_customers.json`;
const traveltimeClient = new TravelTimeClient({
apiKey: process.env.TT_APP_KEY || 'YOUR_APP_ID',
applicationId: process.env.TT_APP_ID || 'YOUR_APP_KEY',
});
reader.on('line', (row) => { data.push(row.split(',')); });
reader.on('close', async () => {
data.shift();
const locations = data.map((row) => ({
id: row[0],
coords: {
lat: parseFloat(row[1]),
lng: parseFloat(row[2]),
},
}));
const departureLocation = locations[0];
const arrivalLocations = locations.slice(1);
const { results } = await traveltimeClient.timeFilter({
locations,
departure_searches: [
{
id: 'departure search example',
departure_location_id: departureLocation.id,
arrival_location_ids: arrivalLocations.map((location) => location.id),
transportation: { type: 'driving' },
travel_time: 1800,
departure_time: new Date().toISOString(),
properties: ['travel_time'],
},
],
});
console.log(`Number of unreachable customers - ${results[0].unreachable.length}`);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
fs.writeFileSync(outputPath, JSON.stringify(results[0].locations, null, 2));
console.log(`Done, results saved at: ${outputPath}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment