diff --git a/middleware/normalizeParentIds.js b/middleware/normalizeParentIds.js index 3aa9a6a3a..e2a4430e8 100644 --- a/middleware/normalizeParentIds.js +++ b/middleware/normalizeParentIds.js @@ -37,6 +37,8 @@ function normalizeParentIds(place) { // The default values via lodash _.get is used only when the value is `undefined`, in our case it may be null. let source = _.get(place, `${placeType}_source[0]`) || 'whosonfirst'; + const layer = _.get(place, `${placeType}_layer[0]`, placeType); + const placetype_ids = _.get(place, `${placeType}_gid[0]`, null); // looking forward to the day we can remove all geonames specific hacks, but until then... @@ -46,7 +48,7 @@ function normalizeParentIds(place) { source = place.source; } - place[`${placeType}_gid`] = [ makeNewId(source, placeType, placetype_ids) ]; + place[`${placeType}_gid`] = [ makeNewId(source, layer, placetype_ids) ]; } }); } diff --git a/middleware/renamePlacenames.js b/middleware/renamePlacenames.js index 281988e0d..36b909d32 100644 --- a/middleware/renamePlacenames.js +++ b/middleware/renamePlacenames.js @@ -5,7 +5,7 @@ const PARENT_PROPS = require('../helper/placeTypes'); const ADDRESS_PROPS = [ { name: 'unit', newName: 'unit' }, { name: 'number', newName: 'housenumber' }, - { name: 'zip', newName: 'postalcode', transform: (value) => { return [value]; } }, + { name: 'zip', newName: 'postalcode', transform: (value) => { return [value]; }, addOriginalId: true }, { name: 'street', newName: 'street' } ]; @@ -63,6 +63,12 @@ function renameAddressProperty(place, prop) { else { place[prop.newName] = place.address_parts[prop.name]; } + + if (prop.addOriginalId) { + place[`${prop.newName}_gid`] = [place.source_id]; + place[`${prop.newName}_source`] = [place.source]; + place[`${prop.newName}_layer`] = [place.layer]; + } } module.exports = setup; diff --git a/test/unit/middleware/normalizeParentIds.js b/test/unit/middleware/normalizeParentIds.js index 6d51879f6..142a8433c 100644 --- a/test/unit/middleware/normalizeParentIds.js +++ b/test/unit/middleware/normalizeParentIds.js @@ -329,6 +329,59 @@ module.exports.tests.interface = function(test, common) { }); }); + + test('address zip may override postalcode parent hierarchy', function(t) { + var input = { + data: [{ + 'address_parts': { 'number': '815', 'street': 'Tennessee Street', 'zip': '94107.0' }, + 'parent': { + 'country_id': [ '85633793' ], + 'country': [ 'United States' ], + 'country_a': [ 'USA' ], + 'postalcode': [ '94107' ], + 'postalcode_id': [ '554784675' ], + }, + 'source': 'openaddresses', + 'source_id': 'us/ca/san_francisco:819b088d2837cf5d', + 'layer': 'address', + 'country': [ 'United States' ], + 'country_a': [ 'USA' ], + 'country_gid': ['85633793'], + 'postalcode': [ '94107.0' ], + 'postalcode_gid': [ 'us/ca/san_francisco:819b088d2837cf5d:819b088d2837cf5d' ], + 'postalcode_source': [ 'openaddresses' ], + 'postalcode_layer': [ 'address' ], + }] + }; + + var expected = { + data: [{ + 'address_parts': { 'number': '815', 'street': 'Tennessee Street', 'zip': '94107.0' }, + 'parent': { + 'country_id': [ '85633793' ], + 'country': [ 'United States' ], + 'country_a': [ 'USA' ], + 'postalcode': [ '94107' ], + 'postalcode_id': [ '554784675' ], + }, + 'source': 'openaddresses', + 'source_id': 'us/ca/san_francisco:819b088d2837cf5d', + 'layer': 'address', + 'country': [ 'United States' ], + 'country_a': [ 'USA' ], + 'country_gid': [ 'whosonfirst:country:85633793' ], + 'postalcode': [ '94107.0' ], + 'postalcode_gid': [ 'openaddresses:address:us/ca/san_francisco:819b088d2837cf5d:819b088d2837cf5d' ], + 'postalcode_source': [ 'openaddresses' ], + 'postalcode_layer': [ 'address' ], + }] + }; + + normalizer({}, input, function () { + t.deepEqual(input, expected); + t.end(); + }); + }); }; module.exports.all = function (tape, common) {