diff --git a/schema/name_osm.js b/schema/name_osm.js index 9a6b0bd3..453e299c 100644 --- a/schema/name_osm.js +++ b/schema/name_osm.js @@ -25,9 +25,9 @@ var OSM_NAMING_SCHEMA = { 'name': 'default', - 'loc_name': 'default', - 'alt_name': 'default', - 'short_name': 'default', + 'loc_name': 'alt', + 'alt_name': 'alt', + 'short_name': 'abbr', // note: these aliases are currently disabled because they are not being used when querying // 'int_name': 'international', @@ -38,14 +38,4 @@ var OSM_NAMING_SCHEMA = { // 'sorting_name': 'sorting' }; -// this property is considered the 'primary name' -// for label generation, the others are considered -// secondary or 'aliases'. -Object.defineProperty(OSM_NAMING_SCHEMA, '_primary', { - value: 'name', - enumerable: false, - configurable: false, - writable: false -}); - module.exports = OSM_NAMING_SCHEMA; diff --git a/stream/tag_mapper.js b/stream/tag_mapper.js index a670f315..a00cdd43 100644 --- a/stream/tag_mapper.js +++ b/stream/tag_mapper.js @@ -29,41 +29,32 @@ module.exports = function(){ return next( null, doc ); } + // primary name tag: 'name' + if (_.has(tags, 'name')) { + appendName(doc, 'default', _.get(tags, 'name')); + } + // Unfortunately we need to iterate over every tag, // so we only do the iteration once to save CPU. _.each(tags, (value, key) => { + // primary field was mapped above + if (key === 'name') { return; } // Map localized names which begin with 'name:' // @ref: http://wiki.openstreetmap.org/wiki/Namespace#Language_code_suffix - var suffix = getNameSuffix( key ); - if( suffix ){ - var val1 = trim( value ); - if( val1 ){ - doc.setName( suffix, val1 ); - } + var suffix = getNameSuffix(key); + if (suffix) { + appendName(doc, suffix, value); } // Map name data from our name mapping schema - else if( _.has(NAME_SCHEMA, key) ){ - var val2 = trim( value ); - if( val2 ){ - if( key === NAME_SCHEMA._primary ){ - doc.setName( NAME_SCHEMA[key], val2 ); - } else if ( 'default' === NAME_SCHEMA[key] ) { - doc.setNameAlias( NAME_SCHEMA[key], val2 ); - } else { - doc.setName( NAME_SCHEMA[key], val2 ); - } - } + else if (_.has(NAME_SCHEMA, key)) { + appendName(doc, _.get(NAME_SCHEMA, key), value); } // Map address data from our address mapping schema - else if( _.has(ADDRESS_SCHEMA, key) ){ - var val3 = trim( value ); - if( val3 ){ - let label = ADDRESS_SCHEMA[key]; - doc.setAddress(label, normalizeAddressField(label, val3)); - } + else if (_.has(ADDRESS_SCHEMA, key)) { + appendAddress(doc, ADDRESS_SCHEMA[key], value); } }); @@ -103,8 +94,8 @@ module.exports = function(){ if( tags.hasOwnProperty('iata') ){ var iata = trim( tags.iata ); if( iata ){ - doc.setNameAlias( 'default', iata ); - doc.setNameAlias( 'default', `${iata} Airport` ); + appendName(doc, 'code', iata); + appendName(doc, 'org', `${iata} Airport`); } } } @@ -166,3 +157,46 @@ function normalizeAddressField(key, value) { return value; } + +/** + * convenience function for appending names: + * + * - selects 'setName' or 'setNameAlias' appropriately. + * - splits on ';', a common OSM convention for multi values. + * - applies token normalization via trim(). + */ +const appendName = (doc, label, value) => { + if (!_.isString(value) || !value.length) { return; } + + value.split(';').forEach(name => { + let initial = !doc.getName(label); + let trimmed = trim(name); + if (trimmed.length < 1) { return; } + + if (initial) { + doc.setName(label, trimmed); + } else { + doc.setNameAlias(label, trimmed); + } + }); +}; + +/** + * convenience function for appending address components: + * + * - selects 'setAddress' or 'setAddressAlias' appropriately. + * - applies token normalization via trim() & normalizeAddressField() + */ +const appendAddress = (doc, label, value) => { + if (!_.isString(value) || !value.length) { return; } + + let initial = !doc.getAddress(label); + let trimmed = normalizeAddressField(label, trim(value)); + if (trimmed.length < 1) { return; } + + if (initial) { + doc.setAddress(label, trimmed); + } else { + doc.setAddressAlias(label, trimmed); + } +}; diff --git a/test/fixtures/combined_vancouver_queens.json b/test/fixtures/combined_vancouver_queens.json index 13db43d9..05a1ec97 100644 --- a/test/fixtures/combined_vancouver_queens.json +++ b/test/fixtures/combined_vancouver_queens.json @@ -856355,16 +856355,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Mountain Equipment Co-op (MEC)", - "MEC" - ] + "alt": "MEC", + "default": "Mountain Equipment Co-op (MEC)" }, "phrase": { - "default": [ - "Mountain Equipment Co-op (MEC)", - "MEC" - ] + "alt": "MEC", + "default": "Mountain Equipment Co-op (MEC)" }, "address_parts": { "number": "212", @@ -863850,16 +863846,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Caffé Brixton", - "Brixton Cafe" - ] + "alt": "Brixton Cafe", + "default": "Caffé Brixton" }, "phrase": { - "default": [ - "Caffé Brixton", - "Brixton Cafe" - ] + "alt": "Brixton Cafe", + "default": "Caffé Brixton" }, "address_parts": { "number": "212", @@ -869997,16 +869989,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "8 and a 1/2 Restaurant", - "Eight and a Half Restaurant" - ] + "alt": "Eight and a Half Restaurant", + "default": "8 and a 1/2 Restaurant" }, "phrase": { - "default": [ - "8 and a 1/2 Restaurant", - "Eight and a Half Restaurant" - ] + "alt": "Eight and a Half Restaurant", + "default": "8 and a 1/2 Restaurant" }, "center_point": { "lon": -123.101665, @@ -878546,16 +878534,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Fellowship Bell", - "Vancouver's Coal Harbour Fellowship Bell" - ] + "alt": "Vancouver's Coal Harbour Fellowship Bell", + "default": "Fellowship Bell" }, "phrase": { - "default": [ - "Fellowship Bell", - "Vancouver's Coal Harbour Fellowship Bell" - ] + "alt": "Vancouver's Coal Harbour Fellowship Bell", + "default": "Fellowship Bell" }, "center_point": { "lon": -123.13068, @@ -879720,16 +879704,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "KFC", - "Kentucky Fried Chicken" - ] + "alt": "Kentucky Fried Chicken", + "default": "KFC" }, "phrase": { - "default": [ - "KFC", - "Kentucky Fried Chicken" - ] + "alt": "Kentucky Fried Chicken", + "default": "KFC" }, "center_point": { "lon": -123.132197, @@ -892429,16 +892409,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "On Lok Restaurant & Wun Tun House", - "On Lok" - ] + "default": "On Lok Restaurant & Wun Tun House", + "abbr": "On Lok" }, "phrase": { - "default": [ - "On Lok Restaurant & Wun Tun House", - "On Lok" - ] + "default": "On Lok Restaurant & Wun Tun House", + "abbr": "On Lok" }, "address_parts": { "number": "2010", @@ -917669,16 +917645,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Raga", - "Classic East Indian Cuisone" - ] + "alt": "Classic East Indian Cuisone", + "default": "Raga" }, "phrase": { - "default": [ - "Raga", - "Classic East Indian Cuisone" - ] + "alt": "Classic East Indian Cuisone", + "default": "Raga" }, "address_parts": { "number": "1177", @@ -919117,16 +919089,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "University of British Columbia", - "UBC" - ] + "alt": "UBC", + "default": "University of British Columbia" }, "phrase": { - "default": [ - "University of British Columbia", - "UBC" - ] + "alt": "UBC", + "default": "University of British Columbia" }, "center_point": { "lon": -123.256728, @@ -919598,16 +919566,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Floatplane Terminal", - "Vancouver Harbour Water Airport" - ] + "alt": "Vancouver Harbour Water Airport", + "default": "Floatplane Terminal" }, "phrase": { - "default": [ - "Floatplane Terminal", - "Vancouver Harbour Water Airport" - ] + "alt": "Vancouver Harbour Water Airport", + "default": "Floatplane Terminal" }, "center_point": { "lon": -123.117614, @@ -923138,16 +923102,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Bentall 5", - "Bell Tower" - ] + "alt": "Bell Tower", + "default": "Bentall 5" }, "phrase": { - "default": [ - "Bentall 5", - "Bell Tower" - ] + "alt": "Bell Tower", + "default": "Bentall 5" }, "center_point": { "lon": -123.11812, @@ -924479,16 +924439,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Dollar Valu", - "Dollar Max" - ] + "alt": "Dollar Max", + "default": "Dollar Valu" }, "phrase": { - "default": [ - "Dollar Valu", - "Dollar Max" - ] + "alt": "Dollar Max", + "default": "Dollar Valu" }, "center_point": { "lon": -123.154562, @@ -927844,16 +927800,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Renaissance Hotel", - "p2b Bistro & Bar" - ] + "alt": "p2b Bistro & Bar", + "default": "Renaissance Hotel" }, "phrase": { - "default": [ - "Renaissance Hotel", - "p2b Bistro & Bar" - ] + "alt": "p2b Bistro & Bar", + "default": "Renaissance Hotel" }, "address_parts": { "number": "1133", @@ -928931,16 +928883,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Canada Place", - "Vancouver Convention Centre East" - ] + "alt": "Vancouver Convention Centre East", + "default": "Canada Place" }, "phrase": { - "default": [ - "Canada Place", - "Vancouver Convention Centre East" - ] + "alt": "Vancouver Convention Centre East", + "default": "Canada Place" }, "center_point": { "lon": -123.111928, @@ -930935,15 +930883,13 @@ "_type": "_doc", "data": { "name": { - "default": [ - "BC Place" - ], + "alt": "B.C. Place", + "default": "BC Place", "ru": "Би-Си Плэйс" }, "phrase": { - "default": [ - "BC Place" - ], + "alt": "B.C. Place", + "default": "BC Place", "ru": "Би-Си Плэйс" }, "center_point": { @@ -934663,16 +934609,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Veterans Club", - "ANAF # 298" - ] + "alt": "ANAF # 298", + "default": "Veterans Club" }, "phrase": { - "default": [ - "Veterans Club", - "ANAF # 298" - ] + "alt": "ANAF # 298", + "default": "Veterans Club" }, "address_parts": { "number": "3917", @@ -938050,16 +937992,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Belmont", - "Belmont Park" - ] + "alt": "Belmont Park", + "default": "Belmont" }, "phrase": { - "default": [ - "Belmont", - "Belmont Park" - ] + "alt": "Belmont Park", + "default": "Belmont" }, "center_point": { "lon": -73.727894, @@ -940730,16 +940668,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Totem Park", - "Enchanted Forest" - ] + "alt": "Enchanted Forest", + "default": "Totem Park" }, "phrase": { - "default": [ - "Totem Park", - "Enchanted Forest" - ] + "alt": "Enchanted Forest", + "default": "Totem Park" }, "center_point": { "lon": -123.254088, @@ -941567,16 +941501,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Guelph Park", - "Dude Chilling Park" - ] + "alt": "Dude Chilling Park", + "default": "Guelph Park" }, "phrase": { - "default": [ - "Guelph Park", - "Dude Chilling Park" - ] + "alt": "Dude Chilling Park", + "default": "Guelph Park" }, "center_point": { "lon": -123.095926, @@ -944774,16 +944704,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "TELUS World of Science", - "Science World" - ] + "alt": "Science World", + "default": "TELUS World of Science" }, "phrase": { - "default": [ - "TELUS World of Science", - "Science World" - ] + "alt": "Science World", + "default": "TELUS World of Science" }, "center_point": { "lon": -123.103746, @@ -945224,16 +945150,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Long Island Motor Parkway", - "Vanderbilt Motor Parkway" - ] + "alt": "Vanderbilt Motor Parkway", + "default": "Long Island Motor Parkway" }, "phrase": { - "default": [ - "Long Island Motor Parkway", - "Vanderbilt Motor Parkway" - ] + "alt": "Vanderbilt Motor Parkway", + "default": "Long Island Motor Parkway" }, "center_point": { "lon": -73.756592, @@ -945470,16 +945392,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Patrol Officer William T Gunn Junior Park", - "Detective William T. Gunn Park" - ] + "alt": "Detective William T. Gunn Park", + "default": "Patrol Officer William T Gunn Junior Park" }, "phrase": { - "default": [ - "Patrol Officer William T Gunn Junior Park", - "Detective William T. Gunn Park" - ] + "alt": "Detective William T. Gunn Park", + "default": "Patrol Officer William T Gunn Junior Park" }, "center_point": { "lon": -73.731892, @@ -946636,16 +946554,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Kitsilano War Memorial Community Centre", - "Kitsilano Community Centre" - ] + "alt": "Kitsilano Community Centre", + "default": "Kitsilano War Memorial Community Centre" }, "phrase": { - "default": [ - "Kitsilano War Memorial Community Centre", - "Kitsilano Community Centre" - ] + "alt": "Kitsilano Community Centre", + "default": "Kitsilano War Memorial Community Centre" }, "center_point": { "lon": -123.162065, @@ -948681,16 +948595,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Telus", - "The Boot" - ] + "alt": "The Boot", + "default": "Telus" }, "phrase": { - "default": [ - "Telus", - "The Boot" - ] + "alt": "The Boot", + "default": "Telus" }, "center_point": { "lon": -123.022033, @@ -955695,16 +955605,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Hugh Dempster Pavillion", - "Huge Dumpster" - ] + "alt": "Huge Dumpster", + "default": "Hugh Dempster Pavillion" }, "phrase": { - "default": [ - "Hugh Dempster Pavillion", - "Huge Dumpster" - ] + "alt": "Huge Dumpster", + "default": "Hugh Dempster Pavillion" }, "address_parts": { "number": "6245", @@ -955940,16 +955846,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "Dorothy Somerset Studios", - "Hut M-18" - ] + "alt": "Hut M-18", + "default": "Dorothy Somerset Studios" }, "phrase": { - "default": [ - "Dorothy Somerset Studios", - "Hut M-18" - ] + "alt": "Hut M-18", + "default": "Dorothy Somerset Studios" }, "center_point": { "lon": -123.254586, @@ -956051,16 +955953,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "B.C. Binning Studios", - "Hut M-17" - ] + "alt": "Hut M-17", + "default": "B.C. Binning Studios" }, "phrase": { - "default": [ - "B.C. Binning Studios", - "Hut M-17" - ] + "alt": "Hut M-17", + "default": "B.C. Binning Studios" }, "center_point": { "lon": -123.254968, @@ -956211,16 +956109,12 @@ "_type": "_doc", "data": { "name": { - "default": [ - "First Nations House Of Learning", - "First Nations Longhouse" - ] + "alt": "First Nations Longhouse", + "default": "First Nations House Of Learning" }, "phrase": { - "default": [ - "First Nations House Of Learning", - "First Nations Longhouse" - ] + "alt": "First Nations Longhouse", + "default": "First Nations House Of Learning" }, "center_point": { "lon": -123.257298, diff --git a/test/stream/tag_mapper.js b/test/stream/tag_mapper.js index 6e896089..287f6027 100644 --- a/test/stream/tag_mapper.js +++ b/test/stream/tag_mapper.js @@ -1,4 +1,4 @@ - +const _ = require('lodash'); const through = require('through2'); const mapper = require('../../stream/tag_mapper'); const fixtures = require('../fixtures/docs'); @@ -94,8 +94,9 @@ module.exports.tests.osm_names = function(test, common) { }); var stream = mapper(); stream.pipe( through.obj( function( doc, enc, next ){ - t.equal(doc.getName('default'), 'name', 'correctly mapped'); - t.deepEqual(doc.getNameAliases('default'), ['loc_name','alt_name','short_name'], 'correctly mapped'); + t.deepEqual(_.castArray(doc.name.default), ['name'], 'correctly mapped'); + t.deepEqual(_.castArray(doc.name.alt), ['loc_name','alt_name'], 'correctly mapped'); + t.deepEqual(_.castArray(doc.name.abbr), ['short_name'], 'correctly mapped'); // note: these aliases are currently disabled because they are not being used when querying // t.equal(doc.getName('national'), 'nat_name', 'correctly mapped'); @@ -248,8 +249,9 @@ module.exports.tests.airport_codes = function(test, common) { test('alias - airport codes', function(t) { var stream = mapper(); stream.pipe( through.obj( function( doc, enc, next ){ - t.equal(doc.getName('default'), 'test', 'correctly mapped'); - t.deepEqual(doc.getNameAliases('default'), ['FOO', 'FOO Airport'], 'correctly mapped'); + t.deepEqual(_.castArray(doc.name.default), ['test'], 'correctly mapped'); + t.deepEqual(_.castArray(doc.name.code), ['FOO'], 'correctly mapped'); + t.deepEqual(_.castArray(doc.name.org), ['FOO Airport'], 'correctly mapped'); t.end(); // test will fail if not called (or called twice). next(); }));