Skip to content

Commit

Permalink
implement haversine; test dan's images
Browse files Browse the repository at this point in the history
ref #10
  • Loading branch information
maxgrossman committed May 13, 2018
1 parent 4da2d9b commit 038416b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
15 changes: 11 additions & 4 deletions adapters/sequence/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions adapters/sequence/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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
},
Expand Down
3 changes: 1 addition & 2 deletions adapters/sequence/split.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 11 additions & 6 deletions helpers/index.js
Original file line number Diff line number Diff line change
@@ -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)

});
}
exports.flatten = (nestedElements) => flattener(nestedElements)
11 changes: 9 additions & 2 deletions test/adapters/sequencerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -45,4 +51,5 @@ describe('sequence', () => {

}
})
.timeout(10000000)
})

0 comments on commit 038416b

Please sign in to comment.