From 51f25c2b019f3566288dbafb725268abb24b01a2 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Tue, 23 Dec 2014 16:07:20 -0500 Subject: [PATCH] min/max lat/lon to form bbox from the input params --- sanitiser/_geo.js | 21 +++++++++++---------- test/unit/sanitiser/suggest.js | 10 +++++----- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/sanitiser/_geo.js b/sanitiser/_geo.js index fd720cfe..5e8baa64 100644 --- a/sanitiser/_geo.js +++ b/sanitiser/_geo.js @@ -45,23 +45,24 @@ function sanitize( req ){ clean.zoom = 10; // default } - // bbox + // bbox + // bbox = bottom_left lat, bottom_left lon, top_right lat, top_right lon + // bbox = left,bottom,right,top + // bbox = min Longitude , min Latitude , max Longitude , max Latitude if (params.bbox) { var bbox = []; var bboxArr = params.bbox.split(','); - if( Array.isArray(bboxArr) && bboxArr.length === 4 ){ - bboxArr.forEach(function(latlon, index) { + if( Array.isArray(bboxArr) && bboxArr.length === 4 ) { + bbox = bboxArr.filter(function(latlon, index) { latlon = parseFloat(latlon, 10); - if ( !(index % 2 === 0 ? is_invalid_lat(latlon) : is_invalid_lon(latlon)) ) { - bbox.push(latlon); - } + return !(index % 2 === 0 ? is_invalid_lat(latlon) : is_invalid_lon(latlon)); }); if (bbox.length === 4) { clean.bbox = { - top : bbox[0], - right : bbox[1], - bottom: bbox[2], - left : bbox[3] + top : Math.max(bbox[0], bbox[2]), + right : Math.max(bbox[1], bbox[3]), + bottom: Math.min(bbox[0], bbox[2]), + left : Math.min(bbox[1], bbox[3]) }; } else { return { diff --git a/test/unit/sanitiser/suggest.js b/test/unit/sanitiser/suggest.js index c85d11c9..9732b2d2 100644 --- a/test/unit/sanitiser/suggest.js +++ b/test/unit/sanitiser/suggest.js @@ -143,7 +143,7 @@ module.exports.tests.sanitize_bbox = function(test, common) { sanitize({ input: 'test', lat: 0, lon: 0, bbox: bbox }, function( err, clean ){ var expected = JSON.parse(JSON.stringify( defaultClean )); t.equal(err, undefined, 'no error'); - t.deepEqual(clean, expected, 'clean set correctly'); + t.deepEqual(clean, expected, 'falling back on 50km distance from centroid'); }); }); t.end(); @@ -156,10 +156,10 @@ module.exports.tests.sanitize_bbox = function(test, common) { return parseInt(i); }); expected.bbox = { - top : bboxArray[0], - right : bboxArray[1], - bottom: bboxArray[2], - left : bboxArray[3] + top : Math.max(bboxArray[0], bboxArray[2]), + right : Math.max(bboxArray[1], bboxArray[3]), + bottom: Math.min(bboxArray[0], bboxArray[2]), + left : Math.min(bboxArray[1], bboxArray[3]) }; t.equal(err, undefined, 'no error'); t.deepEqual(clean, expected, 'clean set correctly (' + bbox + ')');