Browse Source

min/max lat/lon to form bbox from the input params

pull/60/head
Harish Krishna 10 years ago
parent
commit
3973039f19
  1. 21
      sanitiser/_geo.js
  2. 10
      test/unit/sanitiser/suggest.js

21
sanitiser/_geo.js

@ -45,23 +45,24 @@ function sanitize( req ){
clean.zoom = 10; // default 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) { if (params.bbox) {
var bbox = []; var bbox = [];
var bboxArr = params.bbox.split(','); var bboxArr = params.bbox.split(',');
if( Array.isArray(bboxArr) && bboxArr.length === 4 ){ if( Array.isArray(bboxArr) && bboxArr.length === 4 ) {
bboxArr.forEach(function(latlon, index) { bbox = bboxArr.filter(function(latlon, index) {
latlon = parseFloat(latlon, 10); latlon = parseFloat(latlon, 10);
if ( !(index % 2 === 0 ? is_invalid_lat(latlon) : is_invalid_lon(latlon)) ) { return !(index % 2 === 0 ? is_invalid_lat(latlon) : is_invalid_lon(latlon));
bbox.push(latlon);
}
}); });
if (bbox.length === 4) { if (bbox.length === 4) {
clean.bbox = { clean.bbox = {
top : bbox[0], top : Math.max(bbox[0], bbox[2]),
right : bbox[1], right : Math.max(bbox[1], bbox[3]),
bottom: bbox[2], bottom: Math.min(bbox[0], bbox[2]),
left : bbox[3] left : Math.min(bbox[1], bbox[3])
}; };
} else { } else {
return { return {

10
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 ){ sanitize({ input: 'test', lat: 0, lon: 0, bbox: bbox }, function( err, clean ){
var expected = JSON.parse(JSON.stringify( defaultClean )); var expected = JSON.parse(JSON.stringify( defaultClean ));
t.equal(err, undefined, 'no error'); 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(); t.end();
@ -156,10 +156,10 @@ module.exports.tests.sanitize_bbox = function(test, common) {
return parseInt(i); return parseInt(i);
}); });
expected.bbox = { expected.bbox = {
top : bboxArray[0], top : Math.max(bboxArray[0], bboxArray[2]),
right : bboxArray[1], right : Math.max(bboxArray[1], bboxArray[3]),
bottom: bboxArray[2], bottom: Math.min(bboxArray[0], bboxArray[2]),
left : bboxArray[3] left : Math.min(bboxArray[1], bboxArray[3])
}; };
t.equal(err, undefined, 'no error'); t.equal(err, undefined, 'no error');
t.deepEqual(clean, expected, 'clean set correctly (' + bbox + ')'); t.deepEqual(clean, expected, 'clean set correctly (' + bbox + ')');

Loading…
Cancel
Save