Browse Source

Merge pull request #843 from pelias/geonames-ids

Geonames ids should not be misrepresented in hierarchy
pull/375/merge v3.21.2
Diana Shkolnikov 8 years ago committed by GitHub
parent
commit
bf54e62dec
  1. 15
      middleware/normalizeParentIds.js
  2. 106
      test/unit/middleware/normalizeParentIds.js

15
middleware/normalizeParentIds.js

@ -32,7 +32,16 @@ function normalizeParentIds(place) {
if (place) {
placeTypes.forEach(function (placeType) {
if (place[placeType] && place[placeType].length > 0 && place[placeType][0]) {
place[placeType + '_gid'] = [ makeNewId(placeType, place[placeType + '_gid']) ];
var source = 'whosonfirst';
// looking forward to the day we can remove all geonames specific hacks, but until then...
// geonames sometimes has its own ids in the parent hierarchy, so it's dangerous to assume that
// it's always WOF ids and hardcode to that
if (place.source === 'geonames' && place.source_id === place[placeType + '_gid'][0]) {
source = place.source;
}
place[placeType + '_gid'] = [ makeNewId(source, placeType, place[placeType + '_gid']) ];
}
});
}
@ -48,12 +57,12 @@ function normalizeParentIds(place) {
* @param {number} id
* @return {string}
*/
function makeNewId(placeType, id) {
function makeNewId(source, placeType, id) {
if (!id) {
return;
}
var doc = new Document('whosonfirst', placeType, id);
var doc = new Document(source, placeType, id);
return doc.getGid();
}

106
test/unit/middleware/normalizeParentIds.js

@ -77,6 +77,112 @@ module.exports.tests.interface = function(test, common) {
});
});
test('Geonames ids do not override parent hierarchy with WOF equivalents', function(t) {
var input = {
data: [{
'parent': {
'country_id': [ '85633793' ],
'country': [ 'United States' ],
'country_a': [ 'USA' ],
'region_id': [ '85688579' ],
'region': [ 'Mississippi' ],
'region_a': [ 'MS' ]
},
'source': 'geonames',
'source_id': '4436296',
'layer': 'region',
'country': [ 'United States' ],
'country_a': [ 'USA' ],
'country_gid': [ '85633793' ],
'region': [ 'Mississippi' ],
'region_a': [ 'MS' ],
'region_gid': [ '85688579' ]
}]
};
var expected = {
data: [{
'parent': {
'country_id': [ '85633793' ],
'country': [ 'United States' ],
'country_a': [ 'USA' ],
'region_id': [ '85688579' ],
'region': [ 'Mississippi' ],
'region_a': [ 'MS' ]
},
'layer': 'region',
'source': 'geonames',
'source_id': '4436296',
'country': ['United States'],
'country_gid': ['whosonfirst:country:85633793'],
'country_a': ['USA'],
'region': ['Mississippi'],
'region_gid': ['whosonfirst:region:85688579'],
'region_a': ['MS']
}]
};
normalizer({}, input, function () {
t.deepEqual(input, expected);
t.end();
});
});
test('Geonames ids do not override parent hierarchy with WOF equivalents', function(t) {
var input = {
data: [{
'parent': {
'country_id': [ '85633793' ],
'country': [ 'United States' ],
'country_a': ['USA'],
'region_id': [ '4436296' ],
'region': [ 'Mississippi' ],
'region_a': [ 'MS' ]
},
'source': 'geonames',
'source_id': '4436296',
'layer': 'region',
'country': [ 'United States' ],
'country_a': [ 'USA' ],
'country_gid': ['85633793'],
'region': [ 'Mississippi' ],
'region_a': [ 'MS' ],
'region_gid': [ '4436296' ]
}]
};
var expected = {
data: [{
'parent': {
'country_id': [ '85633793' ],
'country': [ 'United States' ],
'country_a': [ 'USA' ],
'region_id': [ '4436296' ],
'region': [ 'Mississippi' ],
'region_a': [ 'MS' ]
},
'layer': 'region',
'source': 'geonames',
'source_id': '4436296',
'country': ['United States'],
'country_gid': ['whosonfirst:country:85633793'],
'country_a': ['USA'],
'region': ['Mississippi'],
'region_gid': ['geonames:region:4436296'],
'region_a': ['MS']
}]
};
normalizer({}, input, function () {
t.deepEqual(input, expected);
t.end();
});
});
};
module.exports.all = function (tape, common) {

Loading…
Cancel
Save