diff --git a/helper/geojsonify.js b/helper/geojsonify.js index ec1e20f6..55bfc19d 100644 --- a/helper/geojsonify.js +++ b/helper/geojsonify.js @@ -3,23 +3,33 @@ var GeoJSON = require('geojson'), extent = require('geojson-extent'), outputGenerator = require('./outputGenerator'); -function search( docs, params ){ +// Properties to be copied when details=true +var DETAILS_PROPS = [ + 'alpha3', + 'admin0', + 'admin1', + 'admin1_abbr', + 'admin2', + 'local_admin', + 'locality', + 'neighborhood', + 'category', + 'address' +]; - // emit a warning if the doc format is invalid - // @note: if you see this error, fix it ASAP! - function warning(){ - console.error( 'error: invalid doc', __filename ); - return false; // remove offending doc from results - } + +function search( docs, params ){ var details = params ? params.details : {}; details = details === true || details === 1; // flatten & expand data for geojson conversion - var geodata = docs.map( function( doc ){ + var geodata = docs.map( function( doc ) { // something went very wrong - if( !doc ) { return warning(); } + if( !doc || !doc.hasOwnProperty( 'center_point' ) ) { + return warning(); + } var output = {}; @@ -28,7 +38,6 @@ function search( docs, params ){ output.layer = doc._type; // map center_point - if( !doc.center_point ) { return warning(); } output.lat = parseFloat( doc.center_point.lat ); output.lng = parseFloat( doc.center_point.lon ); @@ -37,15 +46,7 @@ function search( docs, params ){ if( !doc.name || !doc.name.default ) { return warning(); } output.name = doc.name.default; - // map admin values - if( doc.alpha3 ){ output.alpha3 = doc.alpha3; } - if( doc.admin0 ){ output.admin0 = doc.admin0; } - if( doc.admin1 ){ output.admin1 = doc.admin1; } - if( doc.admin1_abbr ){ output.admin1_abbr = doc.admin1_abbr; } - if( doc.admin2 ){ output.admin2 = doc.admin2; } - if( doc.local_admin ){ output.local_admin = doc.local_admin; } - if( doc.locality ){ output.locality = doc.locality; } - if( doc.neighborhood ){ output.neighborhood = doc.neighborhood; } + copyProperties( doc, DETAILS_PROPS, output ); } // generate region-specific text string @@ -77,4 +78,32 @@ function search( docs, params ){ return geojson; } +/** + * Copy specified properties from source to dest. + * Ignore missing properties. + * + * @param {object} source + * @param {[]} props + * @param {object} dest + */ +function copyProperties( source, props, dest ) { + props.forEach( function ( prop ) { + if ( source.hasOwnProperty( prop ) ) { + dest[prop] = source[prop]; + } + }); +} + + +/** + * emit a warning if the doc format is invalid + * + * @note: if you see this error, fix it ASAP! + */ +function warning( doc ) { + console.error( 'error: invalid doc', __filename, doc ); + return false; // remove offending doc from results +} + + module.exports.search = search; \ No newline at end of file diff --git a/sanitiser/reverse.js b/sanitiser/reverse.js index 52e211b6..780b1c01 100644 --- a/sanitiser/reverse.js +++ b/sanitiser/reverse.js @@ -7,10 +7,7 @@ var _sanitize = require('../sanitiser/_sanitize'), }, layers: require('../sanitiser/_layers'), details: require('../sanitiser/_details'), - size: function( req ) { - var size = require('../sanitiser/_size'); - return size(req, 1); - }, + size: require('../sanitiser/_size'), categories: function ( req ) { var categories = require('../sanitiser/_categories'); return categories(req); diff --git a/test/unit/helper/geojsonify.js b/test/unit/helper/geojsonify.js index 831c24cc..4f026439 100644 --- a/test/unit/helper/geojsonify.js +++ b/test/unit/helper/geojsonify.js @@ -67,7 +67,11 @@ module.exports.tests.search = function(test, common) { '\'round midnight jazz and blues bar' ], 'output': 'osmnode:2208150035' - } + }, + 'category': [ + 'food', + 'nightlife' + ] }, { '_id': 'id2', @@ -119,7 +123,11 @@ module.exports.tests.search = function(test, common) { 'empire state building' ], 'output': 'osmway:34633854' - } + }, + 'category': [ + 'tourism', + 'transport' + ] } ]; @@ -148,7 +156,13 @@ module.exports.tests.search = function(test, common) { 'admin2': 'Angel', 'local_admin': 'test1', 'locality': 'test2', - 'neighborhood': 'test3' + 'neighborhood': 'test3', + 'category': [ 'food', 'nightlife' ], + 'address': { + 'number': '13', + 'street': 'Liverpool Road', + 'zip': 'N1 0RW' + } } }, { @@ -196,7 +210,8 @@ module.exports.tests.search = function(test, common) { 'admin2': 'New York', 'local_admin': 'Manhattan', 'locality': 'New York', - 'neighborhood': 'Koreatown' + 'neighborhood': 'Koreatown', + 'category': [ 'tourism', 'transport' ] } } ] diff --git a/test/unit/sanitiser/reverse.js b/test/unit/sanitiser/reverse.js index c28eeb03..6dbcda3a 100644 --- a/test/unit/sanitiser/reverse.js +++ b/test/unit/sanitiser/reverse.js @@ -8,7 +8,7 @@ var suggest = require('../../../sanitiser/reverse'), layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', 'locality', 'local_admin', 'osmaddress', 'openaddresses' ], lon: 0, - size: 1, + size: 10, details: true, categories: [] }, @@ -108,7 +108,7 @@ module.exports.tests.sanitize_lon = function(test, common) { module.exports.tests.sanitize_size = function(test, common) { test('invalid size value', function(t) { sanitize({ size: 'a', input: 'test', lat: 0, lon: 0 }, function( err, clean ){ - t.equal(clean.size, 1, 'default size set'); + t.equal(clean.size, 10, 'default size set'); t.end(); }); });