mirror of https://github.com/pelias/api.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.3 KiB
60 lines
1.3 KiB
9 years ago
|
/**
|
||
|
* helper sanitiser methods for geo parameters
|
||
|
*/
|
||
10 years ago
|
var util = require( 'util' );
|
||
10 years ago
|
|
||
|
/**
|
||
|
* Parse and validate bbox parameter
|
||
10 years ago
|
* bbox = bottom_left lon, bottom_left lat, top_right lon, top_right lat
|
||
10 years ago
|
* bbox = left, bottom, right, top
|
||
10 years ago
|
* bbox = min Longitude, min Latitude, max Longitude, max Latitude
|
||
10 years ago
|
*
|
||
|
* @param {object} clean
|
||
|
* @param {string} param
|
||
|
*/
|
||
|
function sanitize_bbox( clean, param ) {
|
||
|
if( !param ) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var bboxArr = param.split( ',' );
|
||
|
|
||
|
if( Array.isArray( bboxArr ) && bboxArr.length === 4 ) {
|
||
9 years ago
|
var bbox = bboxArr.map(parseFloat);
|
||
10 years ago
|
|
||
9 years ago
|
if (bbox.some(isNaN)) {
|
||
|
return;
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
clean.bbox = {
|
||
|
right: Math.max( bbox[0], bbox[2] ),
|
||
|
top: Math.max( bbox[1], bbox[3] ),
|
||
|
left: Math.min( bbox[0], bbox[2] ),
|
||
|
bottom: Math.min( bbox[1], bbox[3] )
|
||
|
};
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
/**
|
||
|
* Validate lat,lon values
|
||
|
*
|
||
|
* @param {string} coord lat|lon
|
||
|
* @param {object} clean
|
||
|
* @param {string} param
|
||
|
* @param {bool} latlon_is_required
|
||
|
*/
|
||
|
function sanitize_coord( coord, clean, param, latlon_is_required ) {
|
||
10 years ago
|
var value = parseFloat( param );
|
||
10 years ago
|
if ( !isNaN( value ) ) {
|
||
|
clean[coord] = value;
|
||
|
}
|
||
|
else if (latlon_is_required) {
|
||
10 years ago
|
throw new Error( util.format( 'missing param \'%s\'', coord ) );
|
||
10 years ago
|
}
|
||
|
}
|
||
9 years ago
|
|
||
|
module.exports = {
|
||
|
sanitize_bbox: sanitize_bbox,
|
||
|
sanitize_coord: sanitize_coord
|
||
|
};
|