Browse Source

Compute distance for each result of reverse query

pull/244/head
Diana Shkolnikov 9 years ago
parent
commit
dd78f324ee
  1. 3
      helper/geojsonify.js
  2. 32
      middleware/distance.js
  3. 1
      package.json
  4. 2
      routes/v1.js
  5. 41
      test/unit/middleware/distance.js
  6. 1
      test/unit/run.js

3
helper/geojsonify.js

@ -19,7 +19,8 @@ var DETAILS_PROPS = [
'localadmin', 'localadmin',
'locality', 'locality',
'neighbourhood', 'neighbourhood',
'confidence' 'confidence',
'distance'
]; ];

32
middleware/distance.js

@ -0,0 +1,32 @@
var geolib = require('geolib');
function setup() {
return computeDistances;
}
function computeDistances(req, res, next) {
// do nothing if no result data set
if (!req.results || !req.results.data) {
return next();
}
if ( !(req.clean.hasOwnProperty('lat') && req.clean.hasOwnProperty('lon')) ) {
return next();
}
req.results.data.forEach(function (place) {
// the result of getDistance is in meters, so convert to kilometers
place.distance = geolib.getDistance(
{ latitude: req.clean.lat, longitude: req.clean.lon },
{ latitude: place.center_point.lat, longitude: place.center_point.lon }
) / 1000;
});
next();
}
module.exports = setup;

1
package.json

@ -41,6 +41,7 @@
"extend": "2.0.1", "extend": "2.0.1",
"geojson": "^0.2.1", "geojson": "^0.2.1",
"geojson-extent": "^0.3.1", "geojson-extent": "^0.3.1",
"geolib": "^2.0.18",
"geopipes-elasticsearch-backend": "^0.2.0", "geopipes-elasticsearch-backend": "^0.2.0",
"is-object": "^1.0.1", "is-object": "^1.0.1",
"markdown": "0.5.0", "markdown": "0.5.0",

2
routes/v1.js

@ -25,6 +25,7 @@ var controllers = {
/** ----------------------- controllers ----------------------- **/ /** ----------------------- controllers ----------------------- **/
var postProc = { var postProc = {
distances: require('../middleware/distance'),
confidenceScores: require('../middleware/confidenceScore'), confidenceScores: require('../middleware/confidenceScore'),
renamePlacenames: require('../middleware/renamePlacenames'), renamePlacenames: require('../middleware/renamePlacenames'),
geocodeJSON: require('../middleware/geocodeJSON'), geocodeJSON: require('../middleware/geocodeJSON'),
@ -70,6 +71,7 @@ function addRoutes(app, peliasConfig) {
sanitisers.reverse.middleware, sanitisers.reverse.middleware,
controllers.search(undefined, reverseQuery), controllers.search(undefined, reverseQuery),
// TODO: add confidence scores // TODO: add confidence scores
postProc.distances(),
postProc.renamePlacenames(), postProc.renamePlacenames(),
postProc.geocodeJSON(peliasConfig), postProc.geocodeJSON(peliasConfig),
postProc.sendJSON postProc.sendJSON

41
test/unit/middleware/distance.js

@ -0,0 +1,41 @@
var distance = require('../../../middleware/distance')();
module.exports.tests = {};
module.exports.tests.computeDistance = function(test, common) {
test('valid lat/lon and results', function(t) {
var req = {
clean: {
lat: 45,
lon: -77
},
results: {
data: [
{
center_point: {
lat: 40,
lon: -71
}
}
]
}
};
var expected = 742.348;
distance(req, null, function () {
t.equal(req.results.data[0].distance, expected, 'correct distance computed');
t.end();
});
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('[middleware] distance: ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

1
test/unit/run.js

@ -22,6 +22,7 @@ var tests = [
require('./helper/outputSchema'), require('./helper/outputSchema'),
require('./helper/types'), require('./helper/types'),
require('./sanitiser/_geo_common'), require('./sanitiser/_geo_common'),
require('./middleware/distance'),
]; ];
tests.map(function(t) { tests.map(function(t) {

Loading…
Cancel
Save