From c14a1311f7463682c66cd75426142d838c5d98ca Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Wed, 5 Oct 2016 16:21:35 -0400 Subject: [PATCH 01/18] extracted labelGenerator to be injectable --- helper/geojsonify.js | 11 +- middleware/geocodeJSON.js | 7 +- test/unit/helper/geojsonify.js | 430 ++++++++++++++++++--------------- 3 files changed, 238 insertions(+), 210 deletions(-) diff --git a/helper/geojsonify.js b/helper/geojsonify.js index 9371d0c7..df2a874d 100644 --- a/helper/geojsonify.js +++ b/helper/geojsonify.js @@ -1,18 +1,17 @@ var GeoJSON = require('geojson'); var extent = require('geojson-extent'); -var labelGenerator = require('./labelGenerator'); var logger = require('pelias-logger').get('api'); var type_mapping = require('./type_mapping'); var _ = require('lodash'); var addDetails = require('./geojsonify_place_details'); var addMetaData = require('./geojsonify_meta_data'); -function geojsonifyPlaces( params, docs ){ +function geojsonifyPlaces( params, docs, labelGenerator ){ // flatten & expand data for geojson conversion var geodata = docs - .map(geojsonifyPlace.bind(null, params)) + .map(geojsonifyPlace.bind(null, params, labelGenerator)) .filter( function( doc ){ return !!doc; }); @@ -35,7 +34,7 @@ function geojsonifyPlaces( params, docs ){ return geojson; } -function geojsonifyPlace(params, place) { +function geojsonifyPlace(params, labelGenerator, place) { // something went very wrong if( !place || !place.hasOwnProperty( 'center_point' ) ) { @@ -47,7 +46,7 @@ function geojsonifyPlace(params, place) { addMetaData(place, output); addName(place, output); addDetails(params, place, output); - addLabel(place, output); + addLabel(place, output, labelGenerator); // map center_point for GeoJSON to work properly @@ -76,7 +75,7 @@ function addName(src, dst) { * @param {object} src * @param {object} dst */ -function addLabel(src, dst) { +function addLabel(src, dst, labelGenerator) { dst.label = labelGenerator(dst); } diff --git a/middleware/geocodeJSON.js b/middleware/geocodeJSON.js index 442e017e..6fded96d 100644 --- a/middleware/geocodeJSON.js +++ b/middleware/geocodeJSON.js @@ -11,11 +11,12 @@ var _ = require('lodash'); * @param {string} [basePath] * @returns {middleware} */ -function setup(peliasConfig, basePath) { +function setup(peliasConfig, basePath, labelGenerator) { var opts = { config: peliasConfig || require('pelias-config').generate().api, - basePath: basePath || '/' + basePath: basePath || '/', + labelGenerator: labelGenerator || require('../helper/labelGenerator') }; function middleware(req, res, next) { @@ -73,7 +74,7 @@ function convertToGeocodeJSON(req, res, next, opts) { res.body.geocoding.timestamp = new Date().getTime(); // convert docs to geojson and merge with geocoding block - extend(res.body, geojsonify(req.clean, res.data || [])); + extend(res.body, geojsonify(req.clean, res.data || [], opts.labelGenerator)); next(); } diff --git a/test/unit/helper/geojsonify.js b/test/unit/helper/geojsonify.js index ef7da91e..f7a4e99c 100644 --- a/test/unit/helper/geojsonify.js +++ b/test/unit/helper/geojsonify.js @@ -5,32 +5,35 @@ module.exports.tests = {}; module.exports.tests.interface = function(test, common) { test('valid interface', function(t) { - t.equal(typeof geojsonify, 'function', 'search is a function'); - t.equal(geojsonify.length, 2, 'accepts x arguments'); + t.equal(typeof geojsonify, 'function', 'geojsonify is a function'); + t.equal(geojsonify.length, 3, 'accepts x arguments'); t.end(); }); }; // ref: https://github.com/pelias/pelias/issues/84 module.exports.tests.earth = function(test, common) { + test('earth', function(t) { + var earth = [{ + '_type': 'geoname', + '_id': '6295630', + 'source': 'whosonfirst', + 'layer': 'continent', + 'name': { + 'default': 'Earth' + }, + 'center_point': { + 'lon': 0, + 'lat': 0 + } + }]; - var earth = [{ - '_type': 'geoname', - '_id': '6295630', - 'source': 'whosonfirst', - 'layer': 'continent', - 'name': { - 'default': 'Earth' - }, - 'center_point': { - 'lon': 0, - 'lat': 0 - } - }]; + var labelGenerator = function(doc) { + return 'label for 6295630'; + }; - test('earth', function(t) { t.doesNotThrow(function(){ - geojsonify( {}, earth ); + geojsonify( {}, earth, labelGenerator ); }); t.end(); }); @@ -39,197 +42,210 @@ module.exports.tests.earth = function(test, common) { module.exports.tests.geojsonify = function(test, common) { - var input = [ - { - '_id': 'id1', - '_type': 'layer1', - 'source': 'source1', - 'source_id': 'source_id_1', - 'layer': 'layer1', - 'center_point': { - 'lat': 51.5337144, - 'lon': -0.1069716 - }, - 'name': { - 'default': '\'Round Midnight Jazz and Blues Bar' - }, - 'housenumber': '13', - 'street': 'Liverpool Road', - 'postalcode': 'N1 0RW', - 'country_a': 'GBR', - 'country': 'United Kingdom', - 'dependency': 'dependency name', - 'region': 'Islington', - 'region_a': 'ISL', - 'macroregion': 'England', - 'county': 'Angel', - 'localadmin': 'test1', - 'locality': 'test2', - 'neighbourhood': 'test3', - 'category': [ - 'food', - 'nightlife' - ] - }, - { - '_id': 'id2', - '_type': 'layer2', - 'source': 'source2', - 'source_id': 'source_id_2', - 'layer': 'layer2', - 'name': { - 'default': 'Blues Cafe' - }, - 'center_point': { - 'lat': '51.517806', - 'lon': '-0.101795' - }, - 'country_a': 'GBR', - 'country': 'United Kingdom', - 'dependency': 'dependency name', - 'region': 'City And County Of The City Of London', - 'region_a': 'COL', - 'macroregion': 'England', - 'county': 'Smithfield', - 'localadmin': 'test1', - 'locality': 'test2', - 'neighbourhood': 'test3' - }, - { - '_id': 'node:34633854', - '_type': 'venue', - 'source': 'openstreetmap', - 'source_id': 'source_id_3', - 'layer': 'venue', - 'name': { - 'default': 'Empire State Building' - }, - 'center_point': { - 'lat': '40.748432', - 'lon': '-73.985656' - }, - 'country_a': 'USA', - 'country': 'United States', - 'dependency': 'dependency name', - 'region': 'New York', - 'region_a': 'NY', - 'county': 'New York', - 'borough': 'Manhattan', - 'locality': 'New York', - 'neighbourhood': 'Koreatown', - 'category': [ - 'tourism', - 'transport' - ] - } - ]; - - var expected = { - 'type': 'FeatureCollection', - 'bbox': [ -73.985656, 40.748432, -0.101795, 51.5337144 ], - 'features': [ + test('geojsonify(doc)', function(t) { + var input = [ { - 'type': 'Feature', - 'geometry': { - 'type': 'Point', - 'coordinates': [ - -0.1069716, - 51.5337144 - ] + '_id': 'id1', + '_type': 'layer1', + 'source': 'source1', + 'source_id': 'source_id_1', + 'layer': 'layer1', + 'center_point': { + 'lat': 51.5337144, + 'lon': -0.1069716 }, - 'properties': { - 'id': 'id1', - 'gid': 'source1:layer1:id1', - 'layer': 'layer1', - 'source': 'source1', - 'source_id': 'source_id_1', - 'label': '\'Round Midnight Jazz and Blues Bar, test2, England, United Kingdom', - 'name': '\'Round Midnight Jazz and Blues Bar', - 'country_a': 'GBR', - 'country': 'United Kingdom', - 'dependency': 'dependency name', - 'macroregion': 'England', - 'region': 'Islington', - 'region_a': 'ISL', - 'county': 'Angel', - 'localadmin': 'test1', - 'locality': 'test2', - 'neighbourhood': 'test3', - 'housenumber': '13', - 'street': 'Liverpool Road', - 'postalcode': 'N1 0RW', - 'category': [ - 'food', - 'nightlife' - ] - } + 'name': { + 'default': '\'Round Midnight Jazz and Blues Bar' + }, + 'housenumber': '13', + 'street': 'Liverpool Road', + 'postalcode': 'N1 0RW', + 'country_a': 'GBR', + 'country': 'United Kingdom', + 'dependency': 'dependency name', + 'region': 'Islington', + 'region_a': 'ISL', + 'macroregion': 'England', + 'county': 'Angel', + 'localadmin': 'test1', + 'locality': 'test2', + 'neighbourhood': 'test3', + 'category': [ + 'food', + 'nightlife' + ] }, { - 'type': 'Feature', - 'geometry': { - 'type': 'Point', - 'coordinates': [ - -0.101795, - 51.517806 - ] + '_id': 'id2', + '_type': 'layer2', + 'source': 'source2', + 'source_id': 'source_id_2', + 'layer': 'layer2', + 'name': { + 'default': 'Blues Cafe' }, - 'properties': { - 'id': 'id2', - 'gid': 'source2:layer2:id2', - 'layer': 'layer2', - 'source': 'source2', - 'source_id': 'source_id_2', - 'label': 'Blues Cafe, test2, England, United Kingdom', - 'name': 'Blues Cafe', - 'country_a': 'GBR', - 'country': 'United Kingdom', - 'dependency': 'dependency name', - 'macroregion': 'England', - 'region': 'City And County Of The City Of London', - 'region_a': 'COL', - 'county': 'Smithfield', - 'localadmin': 'test1', - 'locality': 'test2', - 'neighbourhood': 'test3' - } + 'center_point': { + 'lat': '51.517806', + 'lon': '-0.101795' + }, + 'country_a': 'GBR', + 'country': 'United Kingdom', + 'dependency': 'dependency name', + 'region': 'City And County Of The City Of London', + 'region_a': 'COL', + 'macroregion': 'England', + 'county': 'Smithfield', + 'localadmin': 'test1', + 'locality': 'test2', + 'neighbourhood': 'test3' }, { - 'type': 'Feature', - 'geometry': { - 'type': 'Point', - 'coordinates': [ - -73.985656, - 40.748432 - ] + '_id': 'node:34633854', + '_type': 'venue', + 'source': 'openstreetmap', + 'source_id': 'source_id_3', + 'layer': 'venue', + 'name': { + 'default': 'Empire State Building' + }, + 'center_point': { + 'lat': '40.748432', + 'lon': '-73.985656' + }, + 'country_a': 'USA', + 'country': 'United States', + 'dependency': 'dependency name', + 'region': 'New York', + 'region_a': 'NY', + 'county': 'New York', + 'borough': 'Manhattan', + 'locality': 'New York', + 'neighbourhood': 'Koreatown', + 'category': [ + 'tourism', + 'transport' + ] + } + ]; + + var expected = { + 'type': 'FeatureCollection', + 'bbox': [ -73.985656, 40.748432, -0.101795, 51.5337144 ], + 'features': [ + { + 'type': 'Feature', + 'geometry': { + 'type': 'Point', + 'coordinates': [ + -0.1069716, + 51.5337144 + ] + }, + 'properties': { + 'id': 'id1', + 'gid': 'source1:layer1:id1', + 'layer': 'layer1', + 'source': 'source1', + 'source_id': 'source_id_1', + 'label': 'label for id1', + 'name': '\'Round Midnight Jazz and Blues Bar', + 'country_a': 'GBR', + 'country': 'United Kingdom', + 'dependency': 'dependency name', + 'macroregion': 'England', + 'region': 'Islington', + 'region_a': 'ISL', + 'county': 'Angel', + 'localadmin': 'test1', + 'locality': 'test2', + 'neighbourhood': 'test3', + 'housenumber': '13', + 'street': 'Liverpool Road', + 'postalcode': 'N1 0RW', + 'category': [ + 'food', + 'nightlife' + ] + } + }, + { + 'type': 'Feature', + 'geometry': { + 'type': 'Point', + 'coordinates': [ + -0.101795, + 51.517806 + ] + }, + 'properties': { + 'id': 'id2', + 'gid': 'source2:layer2:id2', + 'layer': 'layer2', + 'source': 'source2', + 'source_id': 'source_id_2', + 'label': 'label for id2', + 'name': 'Blues Cafe', + 'country_a': 'GBR', + 'country': 'United Kingdom', + 'dependency': 'dependency name', + 'macroregion': 'England', + 'region': 'City And County Of The City Of London', + 'region_a': 'COL', + 'county': 'Smithfield', + 'localadmin': 'test1', + 'locality': 'test2', + 'neighbourhood': 'test3' + } }, - 'properties': { - 'id': 'node:34633854', - 'gid': 'openstreetmap:venue:node:34633854', - 'layer': 'venue', - 'source': 'openstreetmap', - 'source_id': 'source_id_3', - 'label': 'Empire State Building, Manhattan, New York, NY, USA', - 'name': 'Empire State Building', - 'country_a': 'USA', - 'country': 'United States', - 'dependency': 'dependency name', - 'region': 'New York', - 'region_a': 'NY', - 'county': 'New York', - 'borough': 'Manhattan', - 'locality': 'New York', - 'neighbourhood': 'Koreatown', - 'category': [ - 'tourism', - 'transport' - ] + { + 'type': 'Feature', + 'geometry': { + 'type': 'Point', + 'coordinates': [ + -73.985656, + 40.748432 + ] + }, + 'properties': { + 'id': 'node:34633854', + 'gid': 'openstreetmap:venue:node:34633854', + 'layer': 'venue', + 'source': 'openstreetmap', + 'source_id': 'source_id_3', + 'label': 'label for node:34633854', + 'name': 'Empire State Building', + 'country_a': 'USA', + 'country': 'United States', + 'dependency': 'dependency name', + 'region': 'New York', + 'region_a': 'NY', + 'county': 'New York', + 'borough': 'Manhattan', + 'locality': 'New York', + 'neighbourhood': 'Koreatown', + 'category': [ + 'tourism', + 'transport' + ] + } } + ] + }; + + var labelGenerator = function(doc) { + if (doc.id === 'id1') { + return 'label for id1'; + } + if (doc.id === 'id2') { + return 'label for id2'; + } + if (doc.id === 'node:34633854') { + return 'label for node:34633854'; } - ] - }; - test('geojsonify(doc)', function(t) { - var json = geojsonify( {categories: 'foo'}, input ); + }; + + var json = geojsonify( {categories: 'foo'}, input, labelGenerator ); t.deepEqual(json, expected, 'all docs mapped'); t.end(); @@ -374,7 +390,7 @@ module.exports.tests.geojsonify = function(test, common) { 'localadmin_gid': '404521211', 'locality': 'New York', 'locality_gid': '85977539', - 'label': 'East New York, Brooklyn, New York, NY, USA' + 'label': 'label for 85816607' }, 'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588], 'geometry': { @@ -388,7 +404,13 @@ module.exports.tests.geojsonify = function(test, common) { ] }; - var json = geojsonify( {categories: 'foo'}, input ); + var labelGenerator = function(doc) { + if (doc.id === '85816607') { + return 'label for 85816607'; + } + }; + + var json = geojsonify( {categories: 'foo'}, input, labelGenerator ); t.deepEqual(json, expected, 'all wanted properties exposed'); t.end(); @@ -434,7 +456,7 @@ module.exports.tests.categories = function (test, common) { 'source_id': '85816607', 'name': 'East New York', 'category': ['government'], - 'label': 'East New York' + 'label': 'label for 85816607' }, 'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588], 'geometry': { @@ -448,7 +470,13 @@ module.exports.tests.categories = function (test, common) { ] }; - var json = geojsonify( {categories: 'foo'}, input ); + var labelGenerator = function(doc) { + if (doc.id === '85816607') { + return 'label for 85816607'; + } + }; + + var json = geojsonify( {categories: 'foo'}, input, labelGenerator ); t.deepEqual(json, expected, 'all wanted properties exposed'); t.end(); From 248b1299c80cf2f0c257f65401b1c726d95124d9 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Fri, 7 Oct 2016 12:03:10 -0400 Subject: [PATCH 02/18] added post-processing step to generate labels injects label generation function for easier unit testing moved label generation code to separate package --- helper/geojsonify.js | 12 - helper/labelGenerator.js | 83 ------ helper/labelSchema.js | 78 ------ middleware/assignLabels.js | 23 ++ middleware/geocodeJSON.js | 5 +- package.json | 1 + routes/v1.js | 8 +- test/unit/helper/geojsonify.js | 46 +--- test/unit/helper/labelGenerator_AUS.js | 201 --------------- test/unit/helper/labelGenerator_CAN.js | 205 --------------- test/unit/helper/labelGenerator_GBR.js | 201 --------------- test/unit/helper/labelGenerator_USA.js | 263 -------------------- test/unit/helper/labelGenerator_default.js | 200 --------------- test/unit/helper/labelGenerator_examples.js | 127 ---------- test/unit/helper/labelSchema.js | 45 ---- test/unit/middleware/assignLabels.js | 107 ++++++++ test/unit/run.js | 8 +- 17 files changed, 147 insertions(+), 1466 deletions(-) delete mode 100644 helper/labelGenerator.js delete mode 100644 helper/labelSchema.js create mode 100644 middleware/assignLabels.js delete mode 100644 test/unit/helper/labelGenerator_AUS.js delete mode 100644 test/unit/helper/labelGenerator_CAN.js delete mode 100644 test/unit/helper/labelGenerator_GBR.js delete mode 100644 test/unit/helper/labelGenerator_USA.js delete mode 100644 test/unit/helper/labelGenerator_default.js delete mode 100644 test/unit/helper/labelGenerator_examples.js delete mode 100644 test/unit/helper/labelSchema.js create mode 100644 test/unit/middleware/assignLabels.js diff --git a/helper/geojsonify.js b/helper/geojsonify.js index df2a874d..7144d318 100644 --- a/helper/geojsonify.js +++ b/helper/geojsonify.js @@ -46,8 +46,6 @@ function geojsonifyPlace(params, labelGenerator, place) { addMetaData(place, output); addName(place, output); addDetails(params, place, output); - addLabel(place, output, labelGenerator); - // map center_point for GeoJSON to work properly // these should not show up in the final feature properties @@ -69,16 +67,6 @@ function addName(src, dst) { dst.name = src.name.default; } -/** - * Add region-specific label string - * - * @param {object} src - * @param {object} dst - */ -function addLabel(src, dst, labelGenerator) { - dst.label = labelGenerator(dst); -} - /** * Add bounding box * diff --git a/helper/labelGenerator.js b/helper/labelGenerator.js deleted file mode 100644 index 34112157..00000000 --- a/helper/labelGenerator.js +++ /dev/null @@ -1,83 +0,0 @@ -var _ = require('lodash'), - schemas = require('./labelSchema'); - -module.exports = function( record ){ - var schema = getSchema(record.country_a); - - // in virtually all cases, this will be the `name` field - var labelParts = getInitialLabel(record); - - // iterate the schema - for (var field in schema) { - var valueFunction = schema[field]; - labelParts.push(valueFunction(record)); - } - - // retain only things that are truthy - labelParts = _.compact(labelParts); - - // third, dedupe and join with a comma and return - return dedupeNameAndFirstLabelElement(labelParts).join(', '); - -}; - -function dedupeNameAndFirstLabelElement(labelParts) { - // only dedupe if a result has more than a name (the first label part) - if (labelParts.length > 1) { - // first, dedupe the name and 1st label array elements - // this is used to ensure that the `name` and first admin hierarchy elements aren't repeated - // eg - `["Lancaster", "Lancaster", "PA", "United States"]` -> `["Lancaster", "PA", "United States"]` - var deduped = _.uniq([labelParts.shift(), labelParts.shift()]); - - // second, unshift the deduped parts back onto the labelParts - labelParts.unshift.apply(labelParts, deduped); - - } - - return labelParts; - -} - -function getSchema(country_a) { - if (country_a && country_a.length && schemas[country_a]) { - return schemas[country_a]; - } - - return schemas.default; - -} - -// helper function that sets a default label for non-US/CA regions and countries -function getInitialLabel(record) { - if (isRegion(record.layer) && - isGeonamesOrWhosOnFirst(record.source) && - isUSAOrCAN(record.country_a)) { - return []; - } - - if (isCountry(record.layer)) { - return []; - } - - return [record.name]; - -} - -// this can go away once geonames is no longer supported -// https://github.com/pelias/wof-admin-lookup/issues/49 -function isCountry(layer) { - return 'country' === layer; -} - -function isRegion(layer) { - return 'region' === layer; -} - -function isUSAOrCAN(country_a) { - return 'USA' === country_a || 'CAN' === country_a; -} - -function isGeonamesOrWhosOnFirst(source) { - return 'geonames' === source || 'whosonfirst' === source; - -} diff --git a/helper/labelSchema.js b/helper/labelSchema.js deleted file mode 100644 index ec752b6e..00000000 --- a/helper/labelSchema.js +++ /dev/null @@ -1,78 +0,0 @@ -var _ = require('lodash'); - -module.exports = { - 'default': { - 'local': getFirstProperty(['locality', 'localadmin']), - 'country': getFirstProperty(['country']) - }, - 'GBR': { - 'local': getFirstProperty(['locality', 'localadmin']), - 'regional': getFirstProperty(['macroregion']), - 'country': getFirstProperty(['country']) - }, - 'USA': { - 'borough': getFirstProperty(['borough']), - 'local': getFirstProperty(['locality', 'localadmin', 'county']), - 'regional': getRegionalValue, - 'country': getUSACountryValue - }, - 'AUS': { - 'local' : getFirstProperty(['locality', 'localadmin']), - 'regional' : getRegionalValue, - 'country': getFirstProperty(['country']) - }, - 'CAN': { - 'local': getFirstProperty(['locality']), // no localadmins in CAN - 'regional': getRegionalValue, - 'country': getFirstProperty(['country']) - } -}; - -// find the first field of record that has a non-empty value that's not already in labelParts -function getFirstProperty(fields) { - return function(record) { - for (var i = 0; i < fields.length; i++) { - var fieldValue = record[fields[i]]; - - if (!_.isEmpty(fieldValue)) { - return fieldValue; - } - - } - - }; - -} - -// this function is exclusively used for figuring out which field to use for states/provinces -// 1. if a state/province is the most granular bit of info entered, the label should contain -// the full state/province name, eg: Pennsylvania, USA and Ontario, CA -// 2. otherwise, the state/province abbreviation should be used, eg: Lancaster, PA, USA and Bruce, ON, CA -// 3. if the abbreviation isn't available, use the full state/province name -function getRegionalValue(record) { - if ('region' === record.layer && record.region) { - // return full state name when state is the most granular piece of info - return record.region; - - } else if (record.region_a) { - // otherwise just return the region code when available - return record.region_a; - - } else if (record.region) { - // return the full name when there's no region code available - return record.region; - - } - -} - -// this function returns the full name of a country if the result is in the -// country layer (for "United States" record). It returns the abbreviation -// otherwise (eg - Lancaster, PA, USA). -function getUSACountryValue(record) { - if ('country' === record.layer && record.country) { - return record.country; - } - - return record.country_a; -} diff --git a/middleware/assignLabels.js b/middleware/assignLabels.js new file mode 100644 index 00000000..1b2aa109 --- /dev/null +++ b/middleware/assignLabels.js @@ -0,0 +1,23 @@ +function setup(labelGenerator) { + function middleware(req, res, next) { + return assignLabel(req, res, next, labelGenerator); + } + + return middleware; +} + +function assignLabel(req, res, next, labelGenerator) { + + // do nothing if there's nothing to process + if (!res || !res.body || !res.body.features) { + return next(); + } + + res.body.features.forEach(function (feature) { + feature.properties.label = labelGenerator(feature.properties); + }); + + next(); +} + +module.exports = setup; diff --git a/middleware/geocodeJSON.js b/middleware/geocodeJSON.js index 6fded96d..d3b62c92 100644 --- a/middleware/geocodeJSON.js +++ b/middleware/geocodeJSON.js @@ -15,8 +15,7 @@ function setup(peliasConfig, basePath, labelGenerator) { var opts = { config: peliasConfig || require('pelias-config').generate().api, - basePath: basePath || '/', - labelGenerator: labelGenerator || require('../helper/labelGenerator') + basePath: basePath || '/' }; function middleware(req, res, next) { @@ -74,7 +73,7 @@ function convertToGeocodeJSON(req, res, next, opts) { res.body.geocoding.timestamp = new Date().getTime(); // convert docs to geojson and merge with geocoding block - extend(res.body, geojsonify(req.clean, res.data || [], opts.labelGenerator)); + extend(res.body, geojsonify(req.clean, res.data || [] )); next(); } diff --git a/package.json b/package.json index 0443f632..2504b234 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "morgan": "1.7.0", "pelias-categories": "1.1.0", "pelias-config": "2.3.0", + "pelias-labels": "1.2.0", "pelias-logger": "0.1.0", "pelias-model": "4.3.0", "pelias-query": "8.8.0", diff --git a/routes/v1.js b/routes/v1.js index 4e867766..59936d57 100644 --- a/routes/v1.js +++ b/routes/v1.js @@ -46,7 +46,8 @@ var postProc = { geocodeJSON: require('../middleware/geocodeJSON'), sendJSON: require('../middleware/sendJSON'), parseBoundingBox: require('../middleware/parseBBox'), - normalizeParentIds: require('../middleware/normalizeParentIds') + normalizeParentIds: require('../middleware/normalizeParentIds'), + assignLabels: require('../middleware/assignLabels') }; /** @@ -88,6 +89,7 @@ function addRoutes(app, peliasConfig) { postProc.parseBoundingBox(), postProc.normalizeParentIds(), postProc.geocodeJSON(peliasConfig, base), + postProc.assignLabels(require('pelias-labels')), postProc.sendJSON ]), autocomplete: createRouter([ @@ -102,6 +104,7 @@ function addRoutes(app, peliasConfig) { postProc.parseBoundingBox(), postProc.normalizeParentIds(), postProc.geocodeJSON(peliasConfig, base), + postProc.assignLabels(require('pelias-labels')), postProc.sendJSON ]), reverse: createRouter([ @@ -119,6 +122,7 @@ function addRoutes(app, peliasConfig) { postProc.parseBoundingBox(), postProc.normalizeParentIds(), postProc.geocodeJSON(peliasConfig, base), + postProc.assignLabels(require('pelias-labels')), postProc.sendJSON ]), nearby: createRouter([ @@ -136,6 +140,7 @@ function addRoutes(app, peliasConfig) { postProc.parseBoundingBox(), postProc.normalizeParentIds(), postProc.geocodeJSON(peliasConfig, base), + postProc.assignLabels(require('pelias-labels')), postProc.sendJSON ]), place: createRouter([ @@ -147,6 +152,7 @@ function addRoutes(app, peliasConfig) { postProc.parseBoundingBox(), postProc.normalizeParentIds(), postProc.geocodeJSON(peliasConfig, base), + postProc.assignLabels(require('pelias-labels')), postProc.sendJSON ]), status: createRouter([ diff --git a/test/unit/helper/geojsonify.js b/test/unit/helper/geojsonify.js index f7a4e99c..1035901b 100644 --- a/test/unit/helper/geojsonify.js +++ b/test/unit/helper/geojsonify.js @@ -28,12 +28,8 @@ module.exports.tests.earth = function(test, common) { } }]; - var labelGenerator = function(doc) { - return 'label for 6295630'; - }; - t.doesNotThrow(function(){ - geojsonify( {}, earth, labelGenerator ); + geojsonify( {}, earth ); }); t.end(); }); @@ -147,7 +143,6 @@ module.exports.tests.geojsonify = function(test, common) { 'layer': 'layer1', 'source': 'source1', 'source_id': 'source_id_1', - 'label': 'label for id1', 'name': '\'Round Midnight Jazz and Blues Bar', 'country_a': 'GBR', 'country': 'United Kingdom', @@ -183,7 +178,6 @@ module.exports.tests.geojsonify = function(test, common) { 'layer': 'layer2', 'source': 'source2', 'source_id': 'source_id_2', - 'label': 'label for id2', 'name': 'Blues Cafe', 'country_a': 'GBR', 'country': 'United Kingdom', @@ -212,7 +206,6 @@ module.exports.tests.geojsonify = function(test, common) { 'layer': 'venue', 'source': 'openstreetmap', 'source_id': 'source_id_3', - 'label': 'label for node:34633854', 'name': 'Empire State Building', 'country_a': 'USA', 'country': 'United States', @@ -232,20 +225,7 @@ module.exports.tests.geojsonify = function(test, common) { ] }; - var labelGenerator = function(doc) { - if (doc.id === 'id1') { - return 'label for id1'; - } - if (doc.id === 'id2') { - return 'label for id2'; - } - if (doc.id === 'node:34633854') { - return 'label for node:34633854'; - } - - }; - - var json = geojsonify( {categories: 'foo'}, input, labelGenerator ); + var json = geojsonify( {categories: 'foo'}, input ); t.deepEqual(json, expected, 'all docs mapped'); t.end(); @@ -389,8 +369,7 @@ module.exports.tests.geojsonify = function(test, common) { 'county_gid': '102082361', 'localadmin_gid': '404521211', 'locality': 'New York', - 'locality_gid': '85977539', - 'label': 'label for 85816607' + 'locality_gid': '85977539' }, 'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588], 'geometry': { @@ -404,13 +383,7 @@ module.exports.tests.geojsonify = function(test, common) { ] }; - var labelGenerator = function(doc) { - if (doc.id === '85816607') { - return 'label for 85816607'; - } - }; - - var json = geojsonify( {categories: 'foo'}, input, labelGenerator ); + var json = geojsonify( {categories: 'foo'}, input ); t.deepEqual(json, expected, 'all wanted properties exposed'); t.end(); @@ -455,8 +428,7 @@ module.exports.tests.categories = function (test, common) { 'source': 'whosonfirst', 'source_id': '85816607', 'name': 'East New York', - 'category': ['government'], - 'label': 'label for 85816607' + 'category': ['government'] }, 'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588], 'geometry': { @@ -470,13 +442,7 @@ module.exports.tests.categories = function (test, common) { ] }; - var labelGenerator = function(doc) { - if (doc.id === '85816607') { - return 'label for 85816607'; - } - }; - - var json = geojsonify( {categories: 'foo'}, input, labelGenerator ); + var json = geojsonify( {categories: 'foo'}, input ); t.deepEqual(json, expected, 'all wanted properties exposed'); t.end(); diff --git a/test/unit/helper/labelGenerator_AUS.js b/test/unit/helper/labelGenerator_AUS.js deleted file mode 100644 index e5617ee5..00000000 --- a/test/unit/helper/labelGenerator_AUS.js +++ /dev/null @@ -1,201 +0,0 @@ - -var generator = require('../../../helper/labelGenerator'); - -module.exports.tests = {}; - -module.exports.tests.interface = function(test, common) { - test('interface', function(t) { - t.equal(typeof generator, 'function', 'valid function'); - t.end(); - }); -}; - -module.exports.tests.australia = function(test, common) { - test('venue', function(t) { - var doc = { - 'name': 'venue name', - 'layer': 'venue', - 'housenumber': 'house number', - 'street': 'street name', - 'neighbourhood': 'neighbourhood name', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'AUS', - 'country': 'Australia' - }; - t.equal(generator(doc),'venue name, locality name, region name, Australia'); - t.end(); - }); - - test('localadmin value should be used when locality is not available', function(t) { - var doc = { - 'name': 'venue name', - 'layer': 'venue', - 'housenumber': 'house number', - 'street': 'street name', - 'neighbourhood': 'neighbourhood name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'AUS', - 'country': 'Australia' - }; - t.equal(generator(doc),'venue name, localadmin name, region name, Australia'); - t.end(); - }); - - test('street', function(t) { - var doc = { - 'name': 'house number street name', - 'layer': 'address', - 'housenumber': 'house number', - 'street': 'street name', - 'neighbourhood': 'neighbourhood name', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'AUS', - 'country': 'Australia' - }; - t.equal(generator(doc),'house number street name, locality name, region name, Australia'); - t.end(); - }); - - test('neighbourhood', function(t) { - var doc = { - 'name': 'neighbourhood name', - 'layer': 'neighbourhood', - 'neighbourhood': 'neighbourhood name', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'AUS', - 'country': 'Australia' - }; - t.equal(generator(doc),'neighbourhood name, locality name, region name, Australia'); - t.end(); - }); - - test('locality', function(t) { - var doc = { - 'name': 'locality name', - 'layer': 'locality', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'AUS', - 'country': 'Australia' - }; - t.equal(generator(doc),'locality name, region name, Australia'); - t.end(); - }); - - test('localadmin', function(t) { - var doc = { - 'name': 'localadmin name', - 'layer': 'localadmin', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'AUS', - 'country': 'Australia' - }; - t.equal(generator(doc),'localadmin name, region name, Australia'); - t.end(); - }); - - test('county', function(t) { - var doc = { - 'name': 'county name', - 'layer': 'county', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'AUS', - 'country': 'Australia' - }; - t.equal(generator(doc),'county name, region name, Australia'); - t.end(); - }); - - test('macrocounty', function(t) { - var doc = { - 'name': 'macrocounty name', - 'layer': 'macrocounty', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'AUS', - 'country': 'Australia' - }; - t.equal(generator(doc),'macrocounty name, region name, Australia'); - t.end(); - }); - - test('region', function(t) { - var doc = { - 'name': 'region name', - 'layer': 'region', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'AUS', - 'country': 'Australia' - }; - t.equal(generator(doc),'region name, Australia'); - t.end(); - }); - - test('macroregion', function(t) { - var doc = { - 'name': 'macroregion name', - 'layer': 'macroregion', - 'macroregion': 'macroregion name', - 'country_a': 'AUS', - 'country': 'Australia' - }; - t.equal(generator(doc),'macroregion name, Australia'); - t.end(); - }); - - test('country', function(t) { - var doc = { - 'name': 'Australia', - 'layer': 'country', - 'postalcode': 'postalcode', - 'country_a': 'AUS', - 'country': 'Australia' - }; - t.equal(generator(doc),'Australia'); - t.end(); - }); - -}; - -module.exports.all = function (tape, common) { - - function test(name, testFunction) { - return tape('label generator (AUS): ' + name, testFunction); - } - - for( var testCase in module.exports.tests ){ - module.exports.tests[testCase](test, common); - } -}; diff --git a/test/unit/helper/labelGenerator_CAN.js b/test/unit/helper/labelGenerator_CAN.js deleted file mode 100644 index 814a9315..00000000 --- a/test/unit/helper/labelGenerator_CAN.js +++ /dev/null @@ -1,205 +0,0 @@ -var generator = require('../../../helper/labelGenerator'); - -module.exports.tests = {}; - -module.exports.tests.interface = function(test, common) { - test('interface', function(t) { - t.equal(typeof generator, 'function', 'valid function'); - t.end(); - }); -}; - -module.exports.tests.canada = function(test, common) { - test('venue', function(t) { - var doc = { - 'name': 'venue name', - 'layer': 'venue', - 'housenumber': 'house number', - 'street': 'street name', - 'neighbourhood': 'neighbourhood name', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'CAN', - 'country': 'Canada' - }; - t.equal(generator(doc),'venue name, locality name, region abbr, Canada'); - t.end(); - }); - - test('street', function(t) { - var doc = { - 'name': 'house number street name', - 'layer': 'address', - 'housenumber': 'house number', - 'street': 'street name', - 'neighbourhood': 'neighbourhood name', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'CAN', - 'country': 'Canada' - }; - t.equal(generator(doc),'house number street name, locality name, region abbr, Canada'); - t.end(); - }); - - test('neighbourhood', function(t) { - var doc = { - 'name': 'neighbourhood name', - 'layer': 'neighbourhood', - 'neighbourhood': 'neighbourhood name', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'CAN', - 'country': 'Canada' - }; - t.equal(generator(doc),'neighbourhood name, locality name, region abbr, Canada'); - t.end(); - }); - - test('locality', function(t) { - var doc = { - 'name': 'locality name', - 'layer': 'locality', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'CAN', - 'country': 'Canada' - }; - t.equal(generator(doc),'locality name, region abbr, Canada'); - t.end(); - }); - - test('localadmin', function(t) { - var doc = { - 'name': 'localadmin name', - 'layer': 'localadmin', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'CAN', - 'country': 'Canada' - }; - t.equal(generator(doc),'localadmin name, region abbr, Canada'); - t.end(); - }); - - test('county', function(t) { - var doc = { - 'name': 'county name', - 'layer': 'county', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'CAN', - 'country': 'Canada' - }; - t.equal(generator(doc),'county name, region abbr, Canada'); - t.end(); - }); - - test('macrocounty', function(t) { - var doc = { - 'name': 'macrocounty name', - 'layer': 'macrocounty', - 'macrocounty': 'macrocounty name', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'CAN', - 'country': 'Canada' - }; - t.equal(generator(doc),'macrocounty name, region abbr, Canada'); - t.end(); - }); - - test('region', function(t) { - var doc = { - 'name': 'region name', - 'layer': 'region', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'CAN', - 'country': 'Canada' - }; - t.equal(generator(doc),'region name, Canada'); - t.end(); - }); - - test('macroregion', function(t) { - var doc = { - 'name': 'macroregion name', - 'layer': 'macroregion', - 'macroregion': 'macroregion name', - 'country_a': 'CAN', - 'country': 'Canada' - }; - t.equal(generator(doc),'macroregion name, Canada'); - t.end(); - }); - - test('country', function(t) { - var doc = { - 'name': 'Canada', - 'layer': 'country', - 'country_a': 'CAN', - 'country': 'Canada' - }; - t.equal(generator(doc),'Canada'); - t.end(); - }); - - test('region should be used when region_a is not available', function(t) { - var doc = { - 'name': 'locality name', - 'layer': 'region', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'CAN', - 'country': 'Canada' - }; - t.equal(generator(doc),'locality name, region name, Canada', 'region should be used'); - t.end(); - }); - -}; - -module.exports.all = function (tape, common) { - - function test(name, testFunction) { - return tape('label generator (CAN): ' + name, testFunction); - } - - for( var testCase in module.exports.tests ){ - module.exports.tests[testCase](test, common); - } -}; diff --git a/test/unit/helper/labelGenerator_GBR.js b/test/unit/helper/labelGenerator_GBR.js deleted file mode 100644 index 44fe0d59..00000000 --- a/test/unit/helper/labelGenerator_GBR.js +++ /dev/null @@ -1,201 +0,0 @@ - -var generator = require('../../../helper/labelGenerator'); - -module.exports.tests = {}; - -module.exports.tests.interface = function(test, common) { - test('interface', function(t) { - t.equal(typeof generator, 'function', 'valid function'); - t.end(); - }); -}; - -module.exports.tests.united_kingdom = function(test, common) { - test('venue', function(t) { - var doc = { - 'name': 'venue name', - 'layer': 'venue', - 'housenumber': 'house number', - 'street': 'street name', - 'neighbourhood': 'neighbourhood name', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'GBR', - 'country': 'United Kingdom' - }; - t.equal(generator(doc),'venue name, locality name, macroregion name, United Kingdom'); - t.end(); - }); - - test('localadmin value should be used when locality is not available', function(t) { - var doc = { - 'name': 'venue name', - 'layer': 'venue', - 'housenumber': 'house number', - 'street': 'street name', - 'neighbourhood': 'neighbourhood name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'GBR', - 'country': 'United Kingdom' - }; - t.equal(generator(doc),'venue name, localadmin name, macroregion name, United Kingdom'); - t.end(); - }); - - test('street', function(t) { - var doc = { - 'name': 'house number street name', - 'layer': 'address', - 'housenumber': 'house number', - 'street': 'street name', - 'neighbourhood': 'neighbourhood name', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'GBR', - 'country': 'United Kingdom' - }; - t.equal(generator(doc),'house number street name, locality name, macroregion name, United Kingdom'); - t.end(); - }); - - test('neighbourhood', function(t) { - var doc = { - 'name': 'neighbourhood name', - 'layer': 'neighbourhood', - 'neighbourhood': 'neighbourhood name', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'GBR', - 'country': 'United Kingdom' - }; - t.equal(generator(doc),'neighbourhood name, locality name, macroregion name, United Kingdom'); - t.end(); - }); - - test('locality', function(t) { - var doc = { - 'name': 'locality name', - 'layer': 'locality', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'GBR', - 'country': 'United Kingdom' - }; - t.equal(generator(doc),'locality name, macroregion name, United Kingdom'); - t.end(); - }); - - test('localadmin', function(t) { - var doc = { - 'name': 'localadmin name', - 'layer': 'localadmin', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'GBR', - 'country': 'United Kingdom' - }; - t.equal(generator(doc),'localadmin name, macroregion name, United Kingdom'); - t.end(); - }); - - test('county', function(t) { - var doc = { - 'name': 'county name', - 'layer': 'county', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'GBR', - 'country': 'United Kingdom' - }; - t.equal(generator(doc),'county name, macroregion name, United Kingdom'); - t.end(); - }); - - test('macrocounty', function(t) { - var doc = { - 'name': 'macrocounty name', - 'layer': 'macrocounty', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'GBR', - 'country': 'United Kingdom' - }; - t.equal(generator(doc),'macrocounty name, macroregion name, United Kingdom'); - t.end(); - }); - - test('region', function(t) { - var doc = { - 'name': 'region name', - 'layer': 'region', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'GBR', - 'country': 'United Kingdom' - }; - t.equal(generator(doc),'region name, macroregion name, United Kingdom'); - t.end(); - }); - - test('macroregion', function(t) { - var doc = { - 'name': 'macroregion name', - 'layer': 'macroregion', - 'macroregion': 'macroregion name', - 'country_a': 'GBR', - 'country': 'United Kingdom' - }; - t.equal(generator(doc),'macroregion name, United Kingdom'); - t.end(); - }); - - test('country', function(t) { - var doc = { - 'name': 'United Kingdom', - 'layer': 'country', - 'postalcode': 'postalcode', - 'country_a': 'GBR', - 'country': 'United Kingdom' - }; - t.equal(generator(doc),'United Kingdom'); - t.end(); - }); - -}; - -module.exports.all = function (tape, common) { - - function test(name, testFunction) { - return tape('label generator (GBR): ' + name, testFunction); - } - - for( var testCase in module.exports.tests ){ - module.exports.tests[testCase](test, common); - } -}; diff --git a/test/unit/helper/labelGenerator_USA.js b/test/unit/helper/labelGenerator_USA.js deleted file mode 100644 index 3097bcca..00000000 --- a/test/unit/helper/labelGenerator_USA.js +++ /dev/null @@ -1,263 +0,0 @@ -var generator = require('../../../helper/labelGenerator'); - -module.exports.tests = {}; - -module.exports.tests.interface = function(test, common) { - test('interface', function(t) { - t.equal(typeof generator, 'function', 'valid function'); - t.end(); - }); -}; - -module.exports.tests.united_states = function(test, common) { - test('venue', function(t) { - var doc = { - 'name': 'venue name', - 'layer': 'venue', - 'housenumber': 'house number', - 'street': 'street name', - 'neighbourhood': 'neighbourhood name', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'USA', - 'country': 'United States' - }; - t.equal(generator(doc),'venue name, locality name, region abbr, USA'); - t.end(); - }); - - test('localadmin value should be used when there is no locality', function(t) { - var doc = { - 'name': 'venue name', - 'layer': 'venue', - 'housenumber': 'house number', - 'street': 'street name', - 'neighbourhood': 'neighbourhood name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'USA', - 'country': 'United States' - }; - t.equal(generator(doc),'venue name, localadmin name, region abbr, USA'); - t.end(); - }); - -test('county value should be used when there is no localadmin', function(t) { - var doc = { - 'name': 'venue name', - 'layer': 'venue', - 'housenumber': 'house number', - 'street': 'street name', - 'neighbourhood': 'neighbourhood name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'USA', - 'country': 'United States' - }; - t.equal(generator(doc),'venue name, county name, region abbr, USA'); - t.end(); - }); - - test('street', function(t) { - var doc = { - 'name': 'house number street name', - 'layer': 'address', - 'housenumber': 'house number', - 'street': 'street name', - 'neighbourhood': 'neighbourhood name', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'USA', - 'country': 'United States' - }; - t.equal(generator(doc),'house number street name, locality name, region abbr, USA'); - t.end(); - }); - - test('neighbourhood', function(t) { - var doc = { - 'name': 'neighbourhood name', - 'layer': 'neighbourhood', - 'neighbourhood': 'neighbourhood name', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'USA', - 'country': 'United States' - }; - t.equal(generator(doc),'neighbourhood name, locality name, region abbr, USA'); - t.end(); - }); - - test('venue in borough', function(t) { - var doc = { - 'name': 'venue name', - 'layer': 'borough', - 'neighbourhood': 'neighbourhood name', - 'borough': 'borough name', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'USA', - 'country': 'United States' - }; - t.equal(generator(doc),'venue name, borough name, locality name, region abbr, USA'); - t.end(); - }); - - test('locality', function(t) { - var doc = { - 'name': 'locality name', - 'layer': 'locality', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'USA', - 'country': 'United States' - }; - t.equal(generator(doc),'locality name, region abbr, USA'); - t.end(); - }); - - test('localadmin', function(t) { - var doc = { - 'name': 'localadmin name', - 'layer': 'localadmin', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'USA', - 'country': 'United States' - }; - t.equal(generator(doc),'localadmin name, region abbr, USA'); - t.end(); - }); - - test('county', function(t) { - var doc = { - 'name': 'county name', - 'layer': 'county', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'USA', - 'country': 'United States' - }; - t.equal(generator(doc),'county name, region abbr, USA'); - t.end(); - }); - - test('macrocounty', function(t) { - var doc = { - 'name': 'macrocounty name', - 'layer': 'macrocounty', - 'macrocounty': 'macrocounty name', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'USA', - 'country': 'United States' - }; - t.equal(generator(doc),'macrocounty name, region abbr, USA'); - t.end(); - }); - - test('region', function(t) { - var doc = { - 'name': 'region name', - 'layer': 'region', - 'region_a': 'region abbr', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'USA', - 'country': 'United States' - }; - t.equal(generator(doc),'region name, USA'); - t.end(); - }); - - test('macroregion', function(t) { - var doc = { - 'name': 'macroregion name', - 'layer': 'macroregion', - 'macroregion': 'macroregion name', - 'country_a': 'USA', - 'country': 'United States' - }; - t.equal(generator(doc),'macroregion name, USA'); - t.end(); - }); - - test('country', function(t) { - var doc = { - 'name': 'United States', - 'layer': 'country', - 'country_a': 'USA', - 'country': 'United States' - }; - t.equal(generator(doc),'United States'); - t.end(); - }); - - test('region should be used when region_a is not available', function(t) { - var doc = { - 'name': 'locality name', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'USA', - 'country': 'United States' - }; - t.equal(generator(doc),'locality name, region name, USA', 'region should be used'); - t.end(); - }); - -}; - -module.exports.all = function (tape, common) { - - function test(name, testFunction) { - return tape('label generator (USA): ' + name, testFunction); - } - - for( var testCase in module.exports.tests ){ - module.exports.tests[testCase](test, common); - } -}; diff --git a/test/unit/helper/labelGenerator_default.js b/test/unit/helper/labelGenerator_default.js deleted file mode 100644 index 4679f136..00000000 --- a/test/unit/helper/labelGenerator_default.js +++ /dev/null @@ -1,200 +0,0 @@ - -var generator = require('../../../helper/labelGenerator'); - -module.exports.tests = {}; - -module.exports.tests.interface = function(test, common) { - test('interface', function(t) { - t.equal(typeof generator, 'function', 'valid function'); - t.end(); - }); -}; - -module.exports.tests.default_country = function(test, common) { - test('venue', function(t) { - var doc = { - 'name': 'venue name', - 'layer': 'venue', - 'housenumber': 'house number', - 'street': 'street name', - 'neighbourhood': 'neighbourhood name', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'country code', - 'country': 'country name' - }; - t.equal(generator(doc),'venue name, locality name, country name'); - t.end(); - }); - - test('localadmin value should be used when locality is not available', function(t) { - var doc = { - 'name': 'venue name', - 'layer': 'venue', - 'housenumber': 'house number', - 'street': 'street name', - 'neighbourhood': 'neighbourhood name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'country code', - 'country': 'country name' - }; - t.equal(generator(doc),'venue name, localadmin name, country name'); - t.end(); - }); - - test('street', function(t) { - var doc = { - 'name': 'house number street name', - 'layer': 'address', - 'housenumber': 'house number', - 'street': 'street name', - 'neighbourhood': 'neighbourhood name', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'country code', - 'country': 'country name' - }; - t.equal(generator(doc),'house number street name, locality name, country name'); - t.end(); - }); - - test('neighbourhood', function(t) { - var doc = { - 'name': 'neighbourhood name', - 'layer': 'neighbourhood', - 'neighbourhood': 'neighbourhood name', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'country code', - 'country': 'country name' - }; - t.equal(generator(doc),'neighbourhood name, locality name, country name'); - t.end(); - }); - - test('locality', function(t) { - var doc = { - 'name': 'locality name', - 'layer': 'locality', - 'locality': 'locality name', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'country code', - 'country': 'country name' - }; - t.equal(generator(doc),'locality name, country name'); - t.end(); - }); - - test('localadmin', function(t) { - var doc = { - 'name': 'localadmin name', - 'layer': 'localadmin', - 'localadmin': 'localadmin name', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'country code', - 'country': 'country name' - }; - t.equal(generator(doc),'localadmin name, country name'); - t.end(); - }); - - test('county', function(t) { - var doc = { - 'name': 'county name', - 'layer': 'county', - 'county': 'county name', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'country code', - 'country': 'country name' - }; - t.equal(generator(doc),'county name, country name'); - t.end(); - }); - - test('macrocounty', function(t) { - var doc = { - 'name': 'macrocounty name', - 'layer': 'macrocounty', - 'macrocounty': 'macrocounty name', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'country code', - 'country': 'country name' - }; - t.equal(generator(doc),'macrocounty name, country name'); - t.end(); - }); - - test('region', function(t) { - var doc = { - 'name': 'region name', - 'layer': 'region', - 'region': 'region name', - 'macroregion': 'macroregion name', - 'country_a': 'country code', - 'country': 'country name' - }; - t.equal(generator(doc),'region name, country name'); - t.end(); - }); - - test('macroregion', function(t) { - var doc = { - 'name': 'macroregion name', - 'layer': 'macroregion', - 'macroregion': 'macroregion name', - 'country_a': 'country code', - 'country': 'country name' - }; - t.equal(generator(doc),'macroregion name, country name'); - t.end(); - }); - - test('country layer labels should only use the `country` field and not the `name`', function(t) { - var doc = { - 'name': 'source country name', - 'layer': 'country', - 'country_a': 'country code', - 'country': 'hierarchy country name' - }; - t.equal(generator(doc),'hierarchy country name'); - t.end(); - }); - -}; - -module.exports.all = function (tape, common) { - - function test(name, testFunction) { - return tape('label generator: ' + name, testFunction); - } - - for( var testCase in module.exports.tests ){ - module.exports.tests[testCase](test, common); - } -}; diff --git a/test/unit/helper/labelGenerator_examples.js b/test/unit/helper/labelGenerator_examples.js deleted file mode 100644 index 3207df86..00000000 --- a/test/unit/helper/labelGenerator_examples.js +++ /dev/null @@ -1,127 +0,0 @@ -var generator = require('../../../helper/labelGenerator'); - -module.exports.tests = {}; - -module.exports.tests.interface = function(test, common) { - test('interface', function(t) { - t.equal(typeof generator, 'function', 'valid function'); - t.end(); - }); -}; - -module.exports.tests.canada = function(test, common) { - test('venue', function(t) { - var doc = { - 'name': 'Tim Horton\'s', - 'layer': 'venue', - 'housenumber': '1', - 'street': 'Main St', - 'neighbourhood': 'College Heights', - 'locality': 'Thunder Bay', - 'region_a': 'ON', - 'region': 'Ontario', - 'country_a': 'CAN', - 'country': 'Canada' - }; - t.equal(generator(doc),'Tim Horton\'s, Thunder Bay, ON, Canada'); - t.end(); - }); - - test('address', function(t) { - var doc = { - 'name': '1 Main St', - 'layer': 'venue', - 'housenumber': '1', - 'street': 'Main St', - 'locality': 'Truth or Consequences', - 'region_a': 'NM', - 'region': 'New Mexico', - 'country_a': 'USA', - 'country': 'United States' - }; - t.equal(generator(doc),'1 Main St, Truth or Consequences, NM, USA'); - t.end(); - }); -}; - -module.exports.tests.france = function(test, common) { - test('eiffel tower', function(t) { - var doc = { - 'name': 'Tour Eiffel', - 'layer': 'venue', - 'neighbourhood': 'Quartier du Gros-Caillou', - 'locality': 'Paris', - 'region': 'Paris', - 'country_a': 'FRA', - 'country': 'France' - }; - t.equal(generator(doc),'Tour Eiffel, Paris, France'); - t.end(); - }); - - test('France street address', function(t) { - var doc = { - 'name': '74 rue de rivoli', - 'layer': 'address', - 'housenumber': '74', - 'street': 'Rue de Rivoli', - 'neighbourhood': 'Quartier Saint-Merri', - 'locality': 'Paris', - 'region': 'Paris', - 'country_a': 'FRA', - 'country': 'France' - }; - t.equal(generator(doc),'74 rue de rivoli, Paris, France'); - t.end(); - }); - - test('France neighbourhood', function(t) { - var doc = { - 'name': 'Grange aux Belles Terrage', - 'layer': 'neighbourhood', - 'neighbourhood': 'Grange aux Belles Terrage', - 'locality': 'Paris', - 'region': 'Paris', - 'country_a': 'FRA', - 'country': 'France' - }; - t.equal(generator(doc),'Grange aux Belles Terrage, Paris, France'); - t.end(); - }); - - test('Luxembourg (the city) in Luxembourg', function(t) { - var doc = { - 'name': 'Luxembourg', - 'layer': 'locality', - 'locality': 'Luxembourg', - 'country_a': 'LUX', - 'country': 'Luxembourg' - }; - // console.error(generator(doc)); - t.equal(generator(doc),'Luxembourg, Luxembourg'); - t.end(); - }); - -}; - -module.exports.tests.name_only = function(test, common) { - test('name-only results (no admin fields) should not include extraneous comma', function(t) { - var doc = { - 'name': 'Result name', - }; - t.equal(generator(doc),'Result name'); - t.end(); - }); - -}; - -module.exports.all = function (tape, common) { - - function test(name, testFunction) { - return tape('label generator (CAN): ' + name, testFunction); - } - - for( var testCase in module.exports.tests ){ - module.exports.tests[testCase](test, common); - } -}; diff --git a/test/unit/helper/labelSchema.js b/test/unit/helper/labelSchema.js deleted file mode 100644 index d18e7682..00000000 --- a/test/unit/helper/labelSchema.js +++ /dev/null @@ -1,45 +0,0 @@ -var schemas = require('../../../helper/labelSchema'); -var alpha3 = require('../mock/alpha3.json'); - -module.exports.tests = {}; - -module.exports.tests.interface = function(test, common) { - test('interface', function(t) { - t.equal(typeof schemas, 'object', 'valid object'); - t.equal(schemas.hasOwnProperty('default'), true, 'has default defined'); - t.end(); - }); -}; - -module.exports.tests.supported_countries = function(test, common) { - test('supported countries', function(t) { - var supported_countries = Object.keys(schemas); - - t.notEquals(supported_countries.indexOf('USA'), -1); - t.notEquals(supported_countries.indexOf('CAN'), -1); - t.notEquals(supported_countries.indexOf('GBR'), -1); - t.notEquals(supported_countries.indexOf('AUS'), -1); - t.notEquals(supported_countries.indexOf('default'), -1); - t.equals(supported_countries.length, 5); - - t.equals(Object.keys(schemas.USA).length, 4); - t.equals(Object.keys(schemas.CAN).length, 3); - t.equals(Object.keys(schemas.GBR).length, 3); - t.equals(Object.keys(schemas.AUS).length, 3); - t.equals(Object.keys(schemas.default).length, 2); - - t.end(); - - }); -}; - -module.exports.all = function (tape, common) { - - function test(name, testFunction) { - return tape('schemas: ' + name, testFunction); - } - - for( var testCase in module.exports.tests ){ - module.exports.tests[testCase](test, common); - } -}; diff --git a/test/unit/middleware/assignLabels.js b/test/unit/middleware/assignLabels.js new file mode 100644 index 00000000..34d689c3 --- /dev/null +++ b/test/unit/middleware/assignLabels.js @@ -0,0 +1,107 @@ +module.exports.tests = {}; + +module.exports.tests.serialization = function(test, common) { + test('undefined res should not throw an exception', function(t) { + var assignLabels = require('../../../middleware/assignLabels')(function(){}); + + function testIt() { + assignLabels(undefined, {}, function() {}); + } + + t.doesNotThrow(testIt, 'an exception should not have been thrown'); + t.end(); + + }); + + test('res without body should not throw an exception', function(t) { + var assignLabels = require('../../../middleware/assignLabels')(function(){}); + + function testIt() { + assignLabels({}, {}, function() {}); + } + + t.doesNotThrow(testIt, 'an exception should not have been thrown'); + t.end(); + + }); + + test('res.body without features should not throw an exception', function(t) { + var assignLabels = require('../../../middleware/assignLabels')(function(){}); + + function testIt() { + assignLabels({ body: {} }, {}, function() {}); + } + + t.doesNotThrow(testIt, 'an exception should not have been thrown'); + t.end(); + + }); + + test('labels should be assigned to all results', function(t) { + var labelGenerator = function(properties) { + if (properties.id === 1) { + return 'label 1'; + } + if (properties.id === 2) { + return 'label 2'; + } + + }; + + var assignLabels = require('../../../middleware/assignLabels')(labelGenerator); + + var input = { + body: { + features: [ + { + properties: { + id: 1 + } + }, + { + properties: { + id: 2 + } + } + ] + } + }; + + var expected = { + body: { + features: [ + { + properties: { + id: 1, + label: 'label 1' + } + }, + { + properties: { + id: 2, + label: 'label 2' + } + } + ] + } + }; + + assignLabels({}, input, function () { + t.deepEqual(input, expected); + t.end(); + }); + + }); + +}; + +module.exports.all = function (tape, common) { + + function test(name, testFunction) { + return tape('[middleware] assignLabels: ' + name, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; diff --git a/test/unit/run.js b/test/unit/run.js index 043bb131..3a8114f0 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -14,18 +14,12 @@ var tests = [ require('./controller/search'), require('./helper/diffPlaces'), require('./helper/geojsonify'), - require('./helper/labelGenerator_examples'), - require('./helper/labelGenerator_default'), - require('./helper/labelGenerator_CAN'), - require('./helper/labelGenerator_AUS'), - require('./helper/labelGenerator_GBR'), - require('./helper/labelGenerator_USA'), - require('./helper/labelSchema'), require('./helper/logging'), require('./helper/type_mapping'), require('./helper/sizeCalculator'), require('./middleware/access_log'), require('./middleware/accuracy'), + require('./middleware/assignLabels'), require('./middleware/confidenceScore'), require('./middleware/confidenceScoreFallback'), require('./middleware/confidenceScoreReverse'), From 4ee440224f5513b35594cc9f09110cd9c8369226 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Mon, 10 Oct 2016 13:58:07 -0400 Subject: [PATCH 03/18] bumped labels version to 1.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2504b234..a21cbe51 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "morgan": "1.7.0", "pelias-categories": "1.1.0", "pelias-config": "2.3.0", - "pelias-labels": "1.2.0", + "pelias-labels": "^1.2.0", "pelias-logger": "0.1.0", "pelias-model": "4.3.0", "pelias-query": "8.8.0", From 9b8276392440615ab11110007ce69e9e5fa83b3e Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Mon, 10 Oct 2016 14:00:47 -0400 Subject: [PATCH 04/18] removed extraneous parameter --- middleware/geocodeJSON.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/middleware/geocodeJSON.js b/middleware/geocodeJSON.js index d3b62c92..0d5217f9 100644 --- a/middleware/geocodeJSON.js +++ b/middleware/geocodeJSON.js @@ -11,7 +11,7 @@ var _ = require('lodash'); * @param {string} [basePath] * @returns {middleware} */ -function setup(peliasConfig, basePath, labelGenerator) { +function setup(peliasConfig, basePath) { var opts = { config: peliasConfig || require('pelias-config').generate().api, From 94eaafd39a7d30cf65a87fea8caa4ea81f99c58e Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Mon, 10 Oct 2016 15:12:04 -0400 Subject: [PATCH 05/18] setup labelGenerator to have a default --- middleware/assignLabels.js | 2 +- test/unit/middleware/assignLabels.js | 43 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/middleware/assignLabels.js b/middleware/assignLabels.js index 1b2aa109..8ee52acc 100644 --- a/middleware/assignLabels.js +++ b/middleware/assignLabels.js @@ -1,6 +1,6 @@ function setup(labelGenerator) { function middleware(req, res, next) { - return assignLabel(req, res, next, labelGenerator); + return assignLabel(req, res, next, labelGenerator || require('pelias-labels')); } return middleware; diff --git a/test/unit/middleware/assignLabels.js b/test/unit/middleware/assignLabels.js index 34d689c3..305f99f6 100644 --- a/test/unit/middleware/assignLabels.js +++ b/test/unit/middleware/assignLabels.js @@ -1,3 +1,5 @@ +var proxyquire = require('proxyquire').noCallThru(); + module.exports.tests = {}; module.exports.tests.serialization = function(test, common) { @@ -93,6 +95,47 @@ module.exports.tests.serialization = function(test, common) { }); + test('no explicit labelGenerator supplied should use pelias-labels module', function(t) { + var assignLabels = proxyquire('../../../middleware/assignLabels', { + 'pelias-labels': function(properties) { + if (properties.id === 1) { + return 'label 1'; + } + } + })(); + + var input = { + body: { + features: [ + { + properties: { + id: 1 + } + } + ] + } + }; + + var expected = { + body: { + features: [ + { + properties: { + id: 1, + label: 'label 1' + } + } + ] + } + }; + + assignLabels({}, input, function () { + t.deepEqual(input, expected); + t.end(); + }); + + }); + }; module.exports.all = function (tape, common) { From beb309ca076ee4fd1fd433daaf2f220efb034a8f Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Mon, 10 Oct 2016 16:17:16 -0400 Subject: [PATCH 06/18] moved assignLabels to fire before geojsonify --- helper/geojsonify.js | 1 + middleware/assignLabels.js | 6 +- routes/v1.js | 2 +- test/unit/helper/geojsonify.js | 30 +++++--- test/unit/middleware/assignLabels.js | 108 +++++++++++---------------- 5 files changed, 69 insertions(+), 78 deletions(-) diff --git a/helper/geojsonify.js b/helper/geojsonify.js index 7144d318..99d148dd 100644 --- a/helper/geojsonify.js +++ b/helper/geojsonify.js @@ -46,6 +46,7 @@ function geojsonifyPlace(params, labelGenerator, place) { addMetaData(place, output); addName(place, output); addDetails(params, place, output); + output.label = place.label; // map center_point for GeoJSON to work properly // these should not show up in the final feature properties diff --git a/middleware/assignLabels.js b/middleware/assignLabels.js index 8ee52acc..d10cad69 100644 --- a/middleware/assignLabels.js +++ b/middleware/assignLabels.js @@ -9,12 +9,12 @@ function setup(labelGenerator) { function assignLabel(req, res, next, labelGenerator) { // do nothing if there's nothing to process - if (!res || !res.body || !res.body.features) { + if (!res || !res.data) { return next(); } - res.body.features.forEach(function (feature) { - feature.properties.label = labelGenerator(feature.properties); + res.data.forEach(function (result) { + result.label = labelGenerator(result.parent); }); next(); diff --git a/routes/v1.js b/routes/v1.js index 59936d57..99a54a96 100644 --- a/routes/v1.js +++ b/routes/v1.js @@ -88,8 +88,8 @@ function addRoutes(app, peliasConfig) { postProc.renamePlacenames(), postProc.parseBoundingBox(), postProc.normalizeParentIds(), - postProc.geocodeJSON(peliasConfig, base), postProc.assignLabels(require('pelias-labels')), + postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), autocomplete: createRouter([ diff --git a/test/unit/helper/geojsonify.js b/test/unit/helper/geojsonify.js index 1035901b..22e65b75 100644 --- a/test/unit/helper/geojsonify.js +++ b/test/unit/helper/geojsonify.js @@ -69,7 +69,8 @@ module.exports.tests.geojsonify = function(test, common) { 'category': [ 'food', 'nightlife' - ] + ], + 'label': 'label for id id1' }, { '_id': 'id2', @@ -93,7 +94,8 @@ module.exports.tests.geojsonify = function(test, common) { 'county': 'Smithfield', 'localadmin': 'test1', 'locality': 'test2', - 'neighbourhood': 'test3' + 'neighbourhood': 'test3', + 'label': 'label for id id2' }, { '_id': 'node:34633854', @@ -120,7 +122,8 @@ module.exports.tests.geojsonify = function(test, common) { 'category': [ 'tourism', 'transport' - ] + ], + 'label': 'label for id node:34633854' } ]; @@ -160,7 +163,8 @@ module.exports.tests.geojsonify = function(test, common) { 'category': [ 'food', 'nightlife' - ] + ], + 'label': 'label for id id1' } }, { @@ -188,7 +192,8 @@ module.exports.tests.geojsonify = function(test, common) { 'county': 'Smithfield', 'localadmin': 'test1', 'locality': 'test2', - 'neighbourhood': 'test3' + 'neighbourhood': 'test3', + 'label': 'label for id id2' } }, { @@ -219,7 +224,8 @@ module.exports.tests.geojsonify = function(test, common) { 'category': [ 'tourism', 'transport' - ] + ], + 'label': 'label for id node:34633854' } } ] @@ -330,7 +336,8 @@ module.exports.tests.geojsonify = function(test, common) { null ], 'neighbourhood': [], - 'neighbourhood_gid': [] + 'neighbourhood_gid': [], + 'label': 'label for id 85816607' } ]; @@ -369,7 +376,8 @@ module.exports.tests.geojsonify = function(test, common) { 'county_gid': '102082361', 'localadmin_gid': '404521211', 'locality': 'New York', - 'locality_gid': '85977539' + 'locality_gid': '85977539', + 'label': 'label for id 85816607' }, 'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588], 'geometry': { @@ -411,7 +419,8 @@ module.exports.tests.categories = function (test, common) { 'default': 'East New York' }, 'source_id': '85816607', - 'category': ['government'] + 'category': ['government'], + 'label': 'label for id 85816607' } ]; @@ -428,7 +437,8 @@ module.exports.tests.categories = function (test, common) { 'source': 'whosonfirst', 'source_id': '85816607', 'name': 'East New York', - 'category': ['government'] + 'category': ['government'], + 'label': 'label for id 85816607' }, 'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588], 'geometry': { diff --git a/test/unit/middleware/assignLabels.js b/test/unit/middleware/assignLabels.js index 305f99f6..2fae4678 100644 --- a/test/unit/middleware/assignLabels.js +++ b/test/unit/middleware/assignLabels.js @@ -15,7 +15,7 @@ module.exports.tests.serialization = function(test, common) { }); - test('res without body should not throw an exception', function(t) { + test('res without data should not throw an exception', function(t) { var assignLabels = require('../../../middleware/assignLabels')(function(){}); function testIt() { @@ -27,24 +27,12 @@ module.exports.tests.serialization = function(test, common) { }); - test('res.body without features should not throw an exception', function(t) { - var assignLabels = require('../../../middleware/assignLabels')(function(){}); - - function testIt() { - assignLabels({ body: {} }, {}, function() {}); - } - - t.doesNotThrow(testIt, 'an exception should not have been thrown'); - t.end(); - - }); - test('labels should be assigned to all results', function(t) { - var labelGenerator = function(properties) { - if (properties.id === 1) { + var labelGenerator = function(result) { + if (result.id === 1) { return 'label 1'; } - if (properties.id === 2) { + if (result.id === 2) { return 'label 2'; } @@ -53,39 +41,35 @@ module.exports.tests.serialization = function(test, common) { var assignLabels = require('../../../middleware/assignLabels')(labelGenerator); var input = { - body: { - features: [ - { - properties: { - id: 1 - } - }, - { - properties: { - id: 2 - } + data: [ + { + parent: { + id: 1 } - ] - } + }, + { + parent: { + id: 2 + } + } + ] }; var expected = { - body: { - features: [ - { - properties: { - id: 1, - label: 'label 1' - } + data: [ + { + parent: { + id: 1 }, - { - properties: { - id: 2, - label: 'label 2' - } - } - ] - } + label: 'label 1' + }, + { + parent: { + id: 2 + }, + label: 'label 2' + } + ] }; assignLabels({}, input, function () { @@ -97,36 +81,32 @@ module.exports.tests.serialization = function(test, common) { test('no explicit labelGenerator supplied should use pelias-labels module', function(t) { var assignLabels = proxyquire('../../../middleware/assignLabels', { - 'pelias-labels': function(properties) { - if (properties.id === 1) { + 'pelias-labels': function(result) { + if (result.id === 1) { return 'label 1'; } } })(); var input = { - body: { - features: [ - { - properties: { - id: 1 - } + data: [ + { + parent: { + id: 1 } - ] - } + } + ] }; var expected = { - body: { - features: [ - { - properties: { - id: 1, - label: 'label 1' - } - } - ] - } + data: [ + { + parent: { + id: 1 + }, + label: 'label 1' + } + ] }; assignLabels({}, input, function () { From c8d32ddd1437efe65b76e67b6f14435c04e9737a Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Mon, 10 Oct 2016 16:21:05 -0400 Subject: [PATCH 07/18] moved `require`'d pelias-labels to "global" --- middleware/assignLabels.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/middleware/assignLabels.js b/middleware/assignLabels.js index d10cad69..26d15e29 100644 --- a/middleware/assignLabels.js +++ b/middleware/assignLabels.js @@ -1,6 +1,8 @@ +var defaultLabelGenerator = require('pelias-labels'); + function setup(labelGenerator) { function middleware(req, res, next) { - return assignLabel(req, res, next, labelGenerator || require('pelias-labels')); + return assignLabel(req, res, next, labelGenerator || defaultLabelGenerator); } return middleware; From 34d989301836105e3aaf06c0299e29584c112fa8 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Wed, 12 Oct 2016 12:14:10 -0400 Subject: [PATCH 08/18] removed unused parameter --- helper/geojsonify.js | 6 +++--- test/unit/helper/geojsonify.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/helper/geojsonify.js b/helper/geojsonify.js index 99d148dd..ce431d12 100644 --- a/helper/geojsonify.js +++ b/helper/geojsonify.js @@ -7,11 +7,11 @@ var _ = require('lodash'); var addDetails = require('./geojsonify_place_details'); var addMetaData = require('./geojsonify_meta_data'); -function geojsonifyPlaces( params, docs, labelGenerator ){ +function geojsonifyPlaces( params, docs ){ // flatten & expand data for geojson conversion var geodata = docs - .map(geojsonifyPlace.bind(null, params, labelGenerator)) + .map(geojsonifyPlace.bind(null, params)) .filter( function( doc ){ return !!doc; }); @@ -34,7 +34,7 @@ function geojsonifyPlaces( params, docs, labelGenerator ){ return geojson; } -function geojsonifyPlace(params, labelGenerator, place) { +function geojsonifyPlace(params, place) { // something went very wrong if( !place || !place.hasOwnProperty( 'center_point' ) ) { diff --git a/test/unit/helper/geojsonify.js b/test/unit/helper/geojsonify.js index 22e65b75..809b1ae9 100644 --- a/test/unit/helper/geojsonify.js +++ b/test/unit/helper/geojsonify.js @@ -6,7 +6,7 @@ module.exports.tests = {}; module.exports.tests.interface = function(test, common) { test('valid interface', function(t) { t.equal(typeof geojsonify, 'function', 'geojsonify is a function'); - t.equal(geojsonify.length, 3, 'accepts x arguments'); + t.equal(geojsonify.length, 2, 'accepts x arguments'); t.end(); }); }; From 942a8ff0a3599c59ce031f6e18dbd010fc2654bc Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Wed, 12 Oct 2016 12:17:49 -0400 Subject: [PATCH 09/18] removed redundant for pelias-labels --- routes/v1.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/routes/v1.js b/routes/v1.js index 99a54a96..570ee6c1 100644 --- a/routes/v1.js +++ b/routes/v1.js @@ -88,7 +88,7 @@ function addRoutes(app, peliasConfig) { postProc.renamePlacenames(), postProc.parseBoundingBox(), postProc.normalizeParentIds(), - postProc.assignLabels(require('pelias-labels')), + postProc.assignLabels(), postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), @@ -104,7 +104,7 @@ function addRoutes(app, peliasConfig) { postProc.parseBoundingBox(), postProc.normalizeParentIds(), postProc.geocodeJSON(peliasConfig, base), - postProc.assignLabels(require('pelias-labels')), + postProc.assignLabels(), postProc.sendJSON ]), reverse: createRouter([ @@ -122,7 +122,7 @@ function addRoutes(app, peliasConfig) { postProc.parseBoundingBox(), postProc.normalizeParentIds(), postProc.geocodeJSON(peliasConfig, base), - postProc.assignLabels(require('pelias-labels')), + postProc.assignLabels(), postProc.sendJSON ]), nearby: createRouter([ @@ -140,7 +140,7 @@ function addRoutes(app, peliasConfig) { postProc.parseBoundingBox(), postProc.normalizeParentIds(), postProc.geocodeJSON(peliasConfig, base), - postProc.assignLabels(require('pelias-labels')), + postProc.assignLabels(), postProc.sendJSON ]), place: createRouter([ @@ -152,7 +152,7 @@ function addRoutes(app, peliasConfig) { postProc.parseBoundingBox(), postProc.normalizeParentIds(), postProc.geocodeJSON(peliasConfig, base), - postProc.assignLabels(require('pelias-labels')), + postProc.assignLabels(), postProc.sendJSON ]), status: createRouter([ From d35f40f3724c916754b9218d292c3c40055280c4 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Wed, 19 Oct 2016 17:03:55 -0400 Subject: [PATCH 10/18] switched `label` to property-to-copy --- helper/geojsonify.js | 1 - helper/geojsonify_place_details.js | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/helper/geojsonify.js b/helper/geojsonify.js index ce431d12..1f173b9d 100644 --- a/helper/geojsonify.js +++ b/helper/geojsonify.js @@ -46,7 +46,6 @@ function geojsonifyPlace(params, place) { addMetaData(place, output); addName(place, output); addDetails(params, place, output); - output.label = place.label; // map center_point for GeoJSON to work properly // these should not show up in the final feature properties diff --git a/helper/geojsonify_place_details.js b/helper/geojsonify_place_details.js index b936cd41..44e827d5 100644 --- a/helper/geojsonify_place_details.js +++ b/helper/geojsonify_place_details.js @@ -41,6 +41,7 @@ var DETAILS_PROPS = [ { name: 'neighbourhood', type: 'string' }, { name: 'neighbourhood_gid', type: 'string' }, { name: 'bounding_box', type: 'default' }, + { name: 'label', type: 'string' }, { name: 'category', type: 'array', condition: checkCategoryParam } ]; From ff8fdf486fe3ff67ba89fd519810d248a5ff0fb3 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Wed, 19 Oct 2016 17:04:18 -0400 Subject: [PATCH 11/18] moved label assign to before geocodeJSON for all routes --- routes/v1.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/routes/v1.js b/routes/v1.js index 570ee6c1..6c5afd58 100644 --- a/routes/v1.js +++ b/routes/v1.js @@ -103,8 +103,8 @@ function addRoutes(app, peliasConfig) { postProc.renamePlacenames(), postProc.parseBoundingBox(), postProc.normalizeParentIds(), - postProc.geocodeJSON(peliasConfig, base), postProc.assignLabels(), + postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), reverse: createRouter([ @@ -121,8 +121,8 @@ function addRoutes(app, peliasConfig) { postProc.renamePlacenames(), postProc.parseBoundingBox(), postProc.normalizeParentIds(), - postProc.geocodeJSON(peliasConfig, base), postProc.assignLabels(), + postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), nearby: createRouter([ @@ -139,8 +139,8 @@ function addRoutes(app, peliasConfig) { postProc.renamePlacenames(), postProc.parseBoundingBox(), postProc.normalizeParentIds(), - postProc.geocodeJSON(peliasConfig, base), postProc.assignLabels(), + postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), place: createRouter([ @@ -151,8 +151,8 @@ function addRoutes(app, peliasConfig) { postProc.renamePlacenames(), postProc.parseBoundingBox(), postProc.normalizeParentIds(), - postProc.geocodeJSON(peliasConfig, base), postProc.assignLabels(), + postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), status: createRouter([ From 78cc4a5de3fd21ead605cd3941662f9ab67c1b8a Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Wed, 19 Oct 2016 17:38:17 -0400 Subject: [PATCH 12/18] Add boundary.rect handling to query/autocomplete --- query/autocomplete.js | 16 ++++- ...ocomplete_linguistic_bbox_san_francisco.js | 71 +++++++++++++++++++ test/unit/query/autocomplete.js | 20 ++++++ 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 test/unit/fixture/autocomplete_linguistic_bbox_san_francisco.js diff --git a/query/autocomplete.js b/query/autocomplete.js index d0e766f3..951962be 100644 --- a/query/autocomplete.js +++ b/query/autocomplete.js @@ -49,6 +49,7 @@ query.score( peliasQuery.view.population( views.pop_subquery ) ); // non-scoring hard filters query.filter( peliasQuery.view.sources ); query.filter( peliasQuery.view.layers ); +query.filter( peliasQuery.view.boundary_rect ); // -------------------------------- @@ -109,6 +110,19 @@ function generateQuery( clean ){ }); } + // boundary rect + if( check.number(clean['boundary.rect.min_lat']) && + check.number(clean['boundary.rect.max_lat']) && + check.number(clean['boundary.rect.min_lon']) && + check.number(clean['boundary.rect.max_lon']) ){ + vs.set({ + 'boundary:rect:top': clean['boundary.rect.max_lat'], + 'boundary:rect:right': clean['boundary.rect.max_lon'], + 'boundary:rect:bottom': clean['boundary.rect.min_lat'], + 'boundary:rect:left': clean['boundary.rect.min_lon'] + }); + } + // run the address parser if( clean.parsed_text ){ textParser( clean.parsed_text, vs ); @@ -120,4 +134,4 @@ function generateQuery( clean ){ }; } -module.exports = generateQuery; \ No newline at end of file +module.exports = generateQuery; diff --git a/test/unit/fixture/autocomplete_linguistic_bbox_san_francisco.js b/test/unit/fixture/autocomplete_linguistic_bbox_san_francisco.js new file mode 100644 index 00000000..5e47e603 --- /dev/null +++ b/test/unit/fixture/autocomplete_linguistic_bbox_san_francisco.js @@ -0,0 +1,71 @@ +module.exports = { + 'query': { + 'bool': { + 'must': [{ + 'constant_score': { + 'query': { + 'match': { + 'name.default': { + 'analyzer': 'peliasQueryPartialToken', + 'boost': 100, + 'query': 'test', + 'type': 'phrase', + 'operator': 'and', + 'slop': 3 + } + } + } + } + }], + 'should':[{ + 'function_score': { + 'query': { + 'match_all': {} + }, + 'max_boost': 20, + 'score_mode': 'first', + 'boost_mode': 'replace', + 'functions': [{ + 'field_value_factor': { + 'modifier': 'log1p', + 'field': 'popularity', + 'missing': 1 + }, + 'weight': 1 + }] + } + },{ + 'function_score': { + 'query': { + 'match_all': {} + }, + 'max_boost': 20, + 'score_mode': 'first', + 'boost_mode': 'replace', + 'functions': [{ + 'field_value_factor': { + 'modifier': 'log1p', + 'field': 'population', + 'missing': 1 + }, + 'weight': 3 + }] + } + }], + 'filter': [{ + 'geo_bounding_box': { + 'type': 'indexed', + 'center_point': { + 'top': 37.83239, + 'right': -122.35698, + 'bottom': 37.70808, + 'left': -122.51489 + } + } + }] + } + }, + 'sort': [ '_score' ], + 'size': 20, + 'track_scores': true +}; diff --git a/test/unit/query/autocomplete.js b/test/unit/query/autocomplete.js index 64b6c447..3903d468 100644 --- a/test/unit/query/autocomplete.js +++ b/test/unit/query/autocomplete.js @@ -189,6 +189,26 @@ module.exports.tests.query = function(test, common) { t.deepEqual(compiled.body, expected, 'autocomplete: valid boundary.country query'); t.end(); }); + + test('autocomplete + bbox around San Francisco', function(t) { + var query = generate({ + text: 'test', + 'boundary.rect.max_lat': 37.83239, + 'boundary.rect.max_lon': -122.35698, + 'boundary.rect.min_lat': 37.70808, + 'boundary.rect.min_lon': -122.51489, + tokens: ['test'], + tokens_complete: [], + tokens_incomplete: ['test'] + }); + + var compiled = JSON.parse( JSON.stringify( query ) ); + var expected = require('../fixture/autocomplete_linguistic_bbox_san_francisco'); + + t.deepEqual(compiled.type, 'autocomplete', 'query type set'); + t.deepEqual(compiled.body, expected, 'autocomplete_linguistic_focus_null_island'); + t.end(); + }); }; module.exports.all = function (tape, common) { From 7f3e7df505db3e3468a468872ff2cd5bcccf1f3d Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Wed, 19 Oct 2016 18:19:38 -0400 Subject: [PATCH 13/18] Add boundary.rect to geo_autocomplete sanitizer --- sanitizer/_geo_autocomplete.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sanitizer/_geo_autocomplete.js b/sanitizer/_geo_autocomplete.js index 04fc0b0d..45c2f921 100644 --- a/sanitizer/_geo_autocomplete.js +++ b/sanitizer/_geo_autocomplete.js @@ -1,5 +1,6 @@ var geo_common = require ('./_geo_common'); var LAT_LON_IS_REQUIRED = false; +var RECT_IS_REQUIRED = false; // validate inputs, convert types and apply defaults module.exports = function sanitize( raw, clean ){ @@ -9,6 +10,7 @@ module.exports = function sanitize( raw, clean ){ try { geo_common.sanitize_point( 'focus.point', clean, raw, LAT_LON_IS_REQUIRED ); + geo_common.sanitize_rect( 'boundary.rect', clean, raw, RECT_IS_REQUIRED ); } catch (err) { messages.errors.push( err.message ); From 5be06ac1fae02c6471f560f2b7e8070a7aef66ee Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Thu, 20 Oct 2016 11:24:25 -0400 Subject: [PATCH 14/18] removed whitespace --- middleware/geocodeJSON.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/middleware/geocodeJSON.js b/middleware/geocodeJSON.js index 0d5217f9..442e017e 100644 --- a/middleware/geocodeJSON.js +++ b/middleware/geocodeJSON.js @@ -73,7 +73,7 @@ function convertToGeocodeJSON(req, res, next, opts) { res.body.geocoding.timestamp = new Date().getTime(); // convert docs to geojson and merge with geocoding block - extend(res.body, geojsonify(req.clean, res.data || [] )); + extend(res.body, geojsonify(req.clean, res.data || [])); next(); } From 5f701100278911aa39d5a4266cb3adfb8b03c38f Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Thu, 20 Oct 2016 12:02:11 -0400 Subject: [PATCH 15/18] Add autocomplete boundary.rect ciao test --- .../autocomplete/boundary_rect_valid.coffee | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 test/ciao/autocomplete/boundary_rect_valid.coffee diff --git a/test/ciao/autocomplete/boundary_rect_valid.coffee b/test/ciao/autocomplete/boundary_rect_valid.coffee new file mode 100644 index 00000000..7e1c0403 --- /dev/null +++ b/test/ciao/autocomplete/boundary_rect_valid.coffee @@ -0,0 +1,37 @@ + +#> focus point +path: '/v1/autocomplete?text=cairo&boundary.rect.min_lat=30&boundary.rect.max_lat=32&boundary.rect.min_lon=29&boundary.rect.max_lon=31' + +#? 200 ok +response.statusCode.should.be.equal 200 +response.should.have.header 'charset', 'utf8' +response.should.have.header 'content-type', 'application/json; charset=utf-8' + +#? valid geocoding block +should.exist json.geocoding +should.exist json.geocoding.version +should.exist json.geocoding.attribution +should.exist json.geocoding.query +should.exist json.geocoding.engine +should.exist json.geocoding.engine.name +should.exist json.geocoding.engine.author +should.exist json.geocoding.engine.version +should.exist json.geocoding.timestamp + +#? valid geojson +json.type.should.be.equal 'FeatureCollection' +json.features.should.be.instanceof Array + +#? expected errors +should.not.exist json.geocoding.errors + +#? expected warnings +should.not.exist json.geocoding.warnings + +#? inputs +json.geocoding.query['text'].should.eql 'cairo' +json.geocoding.query['size'].should.eql 10 +json.geocoding.query['boundary.rect.min_lat'].should.eql 30 +json.geocoding.query['boundary.rect.max_lat'].should.eql 32 +json.geocoding.query['boundary.rect.min_lon'].should.eql 29 +json.geocoding.query['boundary.rect.max_lon'].should.eql 31 From 2e592335bce58444ffc05f092dcc1415c35c4573 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Thu, 20 Oct 2016 12:58:49 -0400 Subject: [PATCH 16/18] bumped labels module version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a21cbe51..cf1f8525 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "morgan": "1.7.0", "pelias-categories": "1.1.0", "pelias-config": "2.3.0", - "pelias-labels": "^1.2.0", + "pelias-labels": "^1.3.0", "pelias-logger": "0.1.0", "pelias-model": "4.3.0", "pelias-query": "8.8.0", From d230367f02a12146f0473c39d0e13a1262dabc7e Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Thu, 20 Oct 2016 12:59:02 -0400 Subject: [PATCH 17/18] changed the variable passed to the label generator --- middleware/assignLabels.js | 2 +- test/unit/middleware/assignLabels.js | 24 ++++++------------------ 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/middleware/assignLabels.js b/middleware/assignLabels.js index 26d15e29..1f072ddb 100644 --- a/middleware/assignLabels.js +++ b/middleware/assignLabels.js @@ -16,7 +16,7 @@ function assignLabel(req, res, next, labelGenerator) { } res.data.forEach(function (result) { - result.label = labelGenerator(result.parent); + result.label = labelGenerator(result); }); next(); diff --git a/test/unit/middleware/assignLabels.js b/test/unit/middleware/assignLabels.js index 2fae4678..26e32718 100644 --- a/test/unit/middleware/assignLabels.js +++ b/test/unit/middleware/assignLabels.js @@ -43,14 +43,10 @@ module.exports.tests.serialization = function(test, common) { var input = { data: [ { - parent: { - id: 1 - } + id: 1 }, { - parent: { - id: 2 - } + id: 2 } ] }; @@ -58,15 +54,11 @@ module.exports.tests.serialization = function(test, common) { var expected = { data: [ { - parent: { - id: 1 - }, + id: 1, label: 'label 1' }, { - parent: { - id: 2 - }, + id: 2, label: 'label 2' } ] @@ -91,9 +83,7 @@ module.exports.tests.serialization = function(test, common) { var input = { data: [ { - parent: { - id: 1 - } + id: 1 } ] }; @@ -101,9 +91,7 @@ module.exports.tests.serialization = function(test, common) { var expected = { data: [ { - parent: { - id: 1 - }, + id: 1, label: 'label 1' } ] From ba1c43241d92a365bcf6b4f8c1126eebd54e057a Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Thu, 20 Oct 2016 15:51:19 -0400 Subject: [PATCH 18/18] bumped labels version to 1.4.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cf1f8525..3511e9aa 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "morgan": "1.7.0", "pelias-categories": "1.1.0", "pelias-config": "2.3.0", - "pelias-labels": "^1.3.0", + "pelias-labels": "1.4.0", "pelias-logger": "0.1.0", "pelias-model": "4.3.0", "pelias-query": "8.8.0",