diff --git a/helper/geojsonify.js b/helper/geojsonify.js index b4b57536..dfa177ca 100644 --- a/helper/geojsonify.js +++ b/helper/geojsonify.js @@ -8,11 +8,13 @@ var GeoJSON = require('geojson'), _ = require('lodash'); // Properties to be copied +// If a property is identified as a single string, assume it should be presented as a string in response +// If something other than string is desired, use the following structure: { name: 'category', type: 'array' } var DETAILS_PROPS = [ 'housenumber', 'street', 'postalcode', - 'confidence', + { name: 'confidence', type: 'default' }, 'distance', 'country', 'country_gid', @@ -40,10 +42,10 @@ var DETAILS_PROPS = [ 'borough_a', 'neighbourhood', 'neighbourhood_gid', - 'bounding_box' + { name: 'bounding_box', type: 'default' }, + { name: 'category', type: 'array' } ]; - function lookupSource(src) { return src.source; } @@ -217,26 +219,65 @@ function computeBBox(geojson, geojsonExtentPoints) { function copyProperties( source, props, dst ) { props.forEach( function ( prop ) { - if ( source.hasOwnProperty( prop ) ) { - - // array value, take first item in array (at this time only used for admin values) - if (source[prop] instanceof Array) { - if (source[prop].length === 0) { - return; - } - if (source[prop][0]) { - dst[prop] = source[prop][0]; - } + var property = { + name: prop.name || prop, + type: prop.type || 'string' + }; + + var value = null; + if ( source.hasOwnProperty( property.name ) ) { + + switch (property.type) { + case 'string': + value = getStringValue(source[property.name]); + break; + case 'array': + value = getArrayValue(source[property.name]); + break; + // default behavior is to copy property exactly as is + default: + value = source[property.name]; } - // simple value - else { - dst[prop] = source[prop]; + if (_.isNumber(value) || (value && !_.isEmpty(value))) { + dst[property.name] = value; } } }); } +function getStringValue(property) { + // isEmpty check works for all types of values: strings, arrays, objects + if (_.isEmpty(property)) { + return ''; + } + + if (_.isString(property)) { + return property; + } + + // array value, take first item in array (at this time only used for admin values) + if (_.isArray(property)) { + return property[0]; + } + + return _.toString(property); +} + + +function getArrayValue(property) { + // isEmpty check works for all types of values: strings, arrays, objects + if (_.isEmpty(property)) { + return ''; + } + + if (_.isArray(property)) { + return property; + } + + return [property]; +} + /** * Create a gid from a document * @TODO modify all importers to create separate source and layer fields to remove mapping diff --git a/test/unit/helper/geojsonify.js b/test/unit/helper/geojsonify.js index 6f3581c5..8ec97396 100644 --- a/test/unit/helper/geojsonify.js +++ b/test/unit/helper/geojsonify.js @@ -153,7 +153,11 @@ module.exports.tests.search = function(test, common) { 'neighbourhood': 'test3', 'housenumber': '13', 'street': 'Liverpool Road', - 'postalcode': 'N1 0RW' + 'postalcode': 'N1 0RW', + 'category': [ + 'food', + 'nightlife' + ] } }, { @@ -208,7 +212,11 @@ module.exports.tests.search = function(test, common) { 'county': 'New York', 'borough': 'Manhattan', 'locality': 'New York', - 'neighbourhood': 'Koreatown' + 'neighbourhood': 'Koreatown', + 'category': [ + 'tourism', + 'transport' + ] } } ] @@ -245,7 +253,7 @@ module.exports.tests.search = function(test, common) { 'default': 'East New York' }, 'source_id': '85816607', - 'category': [], + 'category': ['government'], '_id': '85816607', '_type': 'neighbourhood', '_score': 21.434, @@ -328,6 +336,7 @@ module.exports.tests.search = function(test, common) { 'source': 'whosonfirst', 'source_id': '85816607', 'name': 'East New York', + 'category': ['government'], 'confidence': 0.888, 'country': 'United States', 'country_gid': '85633793',