From a68c7fae5359ab85fd457f3443530a923ca83451 Mon Sep 17 00:00:00 2001 From: Diana Shkolnikov Date: Mon, 4 May 2015 17:42:07 -0400 Subject: [PATCH] Add categories and address properties to result doc --- helper/geojsonify.js | 69 ++++++++++++++++++++++++---------- test/unit/helper/geojsonify.js | 23 ++++++++++-- 2 files changed, 69 insertions(+), 23 deletions(-) diff --git a/helper/geojsonify.js b/helper/geojsonify.js index 299bb4bb..a8610136 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.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 @@ -67,4 +68,34 @@ 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 ) { + var count = props.length; + for ( var i = 0; i < count; i++ ) { + var prop = props[i]; + 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/test/unit/helper/geojsonify.js b/test/unit/helper/geojsonify.js index 9f1aca4c..6e965b49 100644 --- a/test/unit/helper/geojsonify.js +++ b/test/unit/helper/geojsonify.js @@ -43,7 +43,11 @@ module.exports.tests.search = function(test, common) { '\'round midnight jazz and blues bar' ], 'output': 'osmnode:2208150035' - } + }, + 'category': [ + 'food', + 'nightlife' + ] }, { '_id': 'id2', @@ -95,7 +99,11 @@ module.exports.tests.search = function(test, common) { 'empire state building' ], 'output': 'osmway:34633854' - } + }, + 'category': [ + 'tourism', + 'transport' + ] } ]; @@ -124,7 +132,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' + } } }, { @@ -172,7 +186,8 @@ module.exports.tests.search = function(test, common) { 'admin2': 'New York', 'local_admin': 'Manhattan', 'locality': 'New York', - 'neighborhood': 'Koreatown' + 'neighborhood': 'Koreatown', + 'category': [ 'tourism', 'transport' ] } } ]