From a68c7fae5359ab85fd457f3443530a923ca83451 Mon Sep 17 00:00:00 2001 From: Diana Shkolnikov Date: Mon, 4 May 2015 17:42:07 -0400 Subject: [PATCH 1/5] 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' ] } } ] From 31bed0d104c8a65a6ca13b1854ed9a143eadf46a Mon Sep 17 00:00:00 2001 From: Diana Shkolnikov Date: Tue, 5 May 2015 11:19:47 -0400 Subject: [PATCH 2/5] Change /reverse default size to 10 --- sanitiser/reverse.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) 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); From 0b44ba68c33c2b17511c98bda0d8b14693e0ce63 Mon Sep 17 00:00:00 2001 From: Diana Shkolnikov Date: Tue, 5 May 2015 11:21:00 -0400 Subject: [PATCH 3/5] forgot to add test to commit --- test/unit/sanitiser/reverse.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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(); }); }); From ed0b5113d5aa3d0921f8d665c7be6cb066cb85d9 Mon Sep 17 00:00:00 2001 From: Diana Shkolnikov Date: Tue, 5 May 2015 11:23:07 -0400 Subject: [PATCH 4/5] change for loop for foreach --- helper/geojsonify.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/helper/geojsonify.js b/helper/geojsonify.js index a8610136..ef998463 100644 --- a/helper/geojsonify.js +++ b/helper/geojsonify.js @@ -77,13 +77,11 @@ function search( docs, params ){ * @param {object} dest */ function copyProperties( source, props, dest ) { - var count = props.length; - for ( var i = 0; i < count; i++ ) { - var prop = props[i]; + props.forEach( function ( prop ) { if ( source.hasOwnProperty( prop ) ) { dest[prop] = source[prop]; } - } + }); } From 39e3672271c05b86911adc9e85043c574f11e626 Mon Sep 17 00:00:00 2001 From: Diana Shkolnikov Date: Thu, 7 May 2015 14:26:47 -0400 Subject: [PATCH 5/5] Fix property check --- helper/geojsonify.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helper/geojsonify.js b/helper/geojsonify.js index ef998463..b3486a3e 100644 --- a/helper/geojsonify.js +++ b/helper/geojsonify.js @@ -27,7 +27,7 @@ function search( docs, params ){ var geodata = docs.map( function( doc ) { // something went very wrong - if( !doc || !doc.center_point ) { + if( !doc || !doc.hasOwnProperty( 'center_point' ) ) { return warning(); }