From 038416bbaf0874bc6b7949171d045fa4217ebe53 Mon Sep 17 00:00:00 2001 From: Max Grossman Date: Sun, 13 May 2018 15:09:50 -0500 Subject: [PATCH] implement haversine; test dan's images ref #10 --- adapters/sequence/helpers.js | 15 +++++++++++---- adapters/sequence/index.js | 6 +++--- adapters/sequence/split.js | 3 +-- helpers/index.js | 17 +++++++++++------ test/adapters/sequencerSpec.js | 11 +++++++++-- 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/adapters/sequence/helpers.js b/adapters/sequence/helpers.js index e6c0022..18995f0 100644 --- a/adapters/sequence/helpers.js +++ b/adapters/sequence/helpers.js @@ -13,10 +13,17 @@ const uuidv4 = require('uuid/v4'); * @return {number} euclidean distance between two points */ exports.calcDistance = (metaLoc, nextMetaLoc) => { - return Math.sqrt( - Math.pow(nextMetaLoc.lon - metaLoc.lon, 2) + - Math.pow(nextMetaLoc.lat - metaLoc.lat, 2) - ) + var R = 6371e3, + latRadians = [metaLoc.lat, nextMetaLoc.lat].map(lat => lat * (Math.PI / 180)), + latDiffs = (latRadians[0] - latRadians[1]) * (Math.PI / 180), + lonDiffs = (metaLoc.lon - nextMetaLoc.lon) * (Math.PI / 180), + + a = Math.pow(Math.sin(lonDiffs / 2), 2) + + (Math.cos(latRadians[0]) * Math.cos(latRadians[1]) * Math.pow(lonDiffs/2, 2)), + + c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); + + return R * c; } /** diff --git a/adapters/sequence/index.js b/adapters/sequence/index.js index f0f7c0b..960cd79 100644 --- a/adapters/sequence/index.js +++ b/adapters/sequence/index.js @@ -3,7 +3,7 @@ const fs = require('fs-extra'); const path = require('path'); const buildSequences = require('./build'); -const { flatten } = require('../../helpers'); +const flatten = require('../../helpers').flatten; Promise = require('bluebird') @@ -25,8 +25,8 @@ module.exports = (paths, cutDist, cutTime, cutSize) => { .then(async (images) => { try { const params = { - maxDist: cutDist || 0.2, - minDist: 0.1, + maxDist: cutDist || 300, + minDist: 1, maxDetla: cutTime || 120, size: cutSize || 0 }, diff --git a/adapters/sequence/split.js b/adapters/sequence/split.js index 64e1837..be08456 100644 --- a/adapters/sequence/split.js +++ b/adapters/sequence/split.js @@ -30,8 +30,7 @@ module.exports = (metas, params) => { sortedMetas.slice(0, pelIndex).forEach((meta, i) => { const partnerMeta = sortedMetas[i + 1], distance = calcDistance(meta.loc, partnerMeta.loc), - tooClose = false; - // tooClose = distance < minDist; + tooClose = distance < minDist; // ... if image is not too close to its partner, add it to a sequence. if (!tooClose) { diff --git a/helpers/index.js b/helpers/index.js index 1d5ad5e..49d7a96 100644 --- a/helpers/index.js +++ b/helpers/index.js @@ -1,14 +1,19 @@ 'use strict'; +// total wizard! thanks! +// https://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays-in-javascripts +const isArray = (elementInQuestion) => Array.isArray(elementInQuestion); +const flattener = (elementsToFlatten) => { + return elementsToFlatten.reduce((flatElements, element) => { + return flatElements.concat(isArray(element) ? flattener(element) : element); + }, []); +} + + /** * flattens array of nested elements * * @param {array} nested array of (potentially nested array) elements * @return {array} flattened array of elements in nested */ -exports.flatten = (nested) => { - return nested.reduce((flatt, el) => { - return flatt.concat(Array.isArray(el) ? flatten(el) : el) - - }); -} \ No newline at end of file +exports.flatten = (nestedElements) => flattener(nestedElements) \ No newline at end of file diff --git a/test/adapters/sequencerSpec.js b/test/adapters/sequencerSpec.js index c1abbe9..ba88d67 100644 --- a/test/adapters/sequencerSpec.js +++ b/test/adapters/sequencerSpec.js @@ -33,10 +33,16 @@ describe('sequence', () => { }) it ('given a path of images, generates a list of sequence objects', async () => { try { - const paths = ['/testData/exif-gps-samples'].map(p => process.cwd() + p), + const paths = [ + '/testData/exif-gps-samples', + '/testData/danbjoseph' + ].map(p => process.cwd() + p), sequences = await sequenceAdapter(paths), validation = Joi.validate(sequences, sequencesSchema); - + Object.keys(sequences).forEach(s => { + console.log(s); + console.log(sequences[s].length) + }) expect(validation.value).to.be.eql(sequences) expect(validation.error).to.be.null; @@ -45,4 +51,5 @@ describe('sequence', () => { } }) + .timeout(10000000) }) \ No newline at end of file