Skip to content

Instantly share code, notes, and snippets.

@brookemckim
Created March 19, 2020 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brookemckim/bd14c62dedbbeb5f9d979de328fb1995 to your computer and use it in GitHub Desktop.
Save brookemckim/bd14c62dedbbeb5f9d979de328fb1995 to your computer and use it in GitHub Desktop.
const moment = require('moment');
const turf = require('@turf/turf');
const UUID = require("pure-uuid");
const K_ANON_VAL = 4;
const FUZZED_TIME_BUCKET = 15;
const FUZZED_LOCATION_PRECISION = 3;
const RANDOM_LATITUDE_RADIUS = 0.004;
const RANDOM_LONGITUDE_RADIUS = 0.005;
const generateTripUUID = (trip_id) => {
const uuid = new UUID(5, "ns:URL", trip_id);
return uuid
}
const generateFuzzedLocations = (coordinates) => {
const startLocation = coordinates[0].map(c => c.toFixed(FUZZED_LOCATION_PRECISION))
const endLocation = coordinates[coordinates.length - 1].map(c => c.toFixed(FUZZED_LOCATION_PRECISION))
return {
start: startLocation,
end: endLocation
}
}
const fuzzTimes = (minute, hour) => {
const fuzzedMinute = (((minute + (FUZZED_TIME_BUCKET / 2)) / FUZZED_TIME_BUCKET | 0) * FUZZED_TIME_BUCKET) % 60;;
const fuzzedHour = ((((minute / 105) + .5) | 0) + hour) % 24;
return [fuzzedHour, fuzzedMinute]
}
const calculateDistance = (coordinates) => {
return coordinates.reduce((totalForTrip, point, i, points) => {
let nextPoint = points[i + 1];
let pointDistance = 0;
if (nextPoint) {
pointDistance = turf.distance(turf.point(point), turf.point(nextPoint), { units: 'miles' });
}
return totalForTrip + pointDistance
}, 0)
}
const generateFuzzedDatetimes = (startedAt, endedAt) => {
const startDate = moment(startedAt);
const endDate = moment(endedAt);
const fuzzedStart = fuzzTimes(startDate.minute(), startDate.hour());
const fuzzedEnd = fuzzTimes(endDate.minute(), endDate.hour());
const startTime = moment(startDate).minute(fuzzedStart[1]).hour(fuzzedStart[0]);
const endTime = moment(endDate).minute(fuzzedEnd[1]).hour(fuzzedEnd[0]);
const tripDuration = startDate - endDate;
return {
startDate: startDate.format('Y-MM-DD'),
endDate: endDate.format('Y-MM-DD'),
startTime: startTime.format('HH:mm'),
endTime: endTime.format('HH:mm'),
tripDuration: parseInt(Math.abs(tripDuration / 1000 / 60)),
dayOfWeek: startDate.format('dddd'),
hourNum: startDate.format('H')
}
}
const generateFuzzedTripLocationKey = (startLatitude, startLongitude, endLatitude, endLongitude) => {
return `${startLatitude},${startLongitude},${endLatitude},${endLongitude}`
}
const generateRandomLocations = (startLatitude, startLongitude, endLatitude, endLongitude) => {
const randomNumberA = Math.random()
const randomNumberB = Math.random()
randomStartLongitude = parseFloat(fuzzedTrip.startLongitude) + (randomNumberB * RANDOM_LONGITUDE_RADIUS * Math.sin( 2 * Math.PI * randomNumberA / randomNumberB ));
randomEndLongitude = parseFloat(fuzzedTrip.endLongitude) + (randomNumberB * RANDOM_LONGITUDE_RADIUS * Math.sin( 2 * Math.PI * randomNumberA / randomNumberB ));
randomStartLatitude = parseFloat(fuzzedTrip.startLatitude) + (randomNumberB * RANDOM_LATITUDE_RADIUS * Math.cos( 2 * Math.PI * randomNumberA / randomNumberB ));
randomEndLatitude = parseFloat(fuzzedTrip.endLatitude) + (randomNumberB * RANDOM_LATITUDE_RADIUS * Math.cos( 2 * Math.PI * randomNumberA / randomNumberB ));
return {
startLongitude: randomStartLongitude.toFixed(FUZZED_LOCATION_PRECISION),
startLatitude: randomStartLatitude.toFixed(FUZZED_LOCATION_PRECISION),
endLatitude: randomEndLatitude.toFixed(FUZZED_LOCATION_PRECISION),
endLongitude: randomEndLongitude.toFixed(FUZZED_LOCATION_PRECISION)
}
}
{
"name": "fuzz-trips",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@turf/turf": "^5.1.6",
"moment": "^2.24.0",
"pure-uuid": "^1.6.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment