mirror of https://github.com/pelias/api.git
Diana Shkolnikov
9 years ago
11 changed files with 291 additions and 113 deletions
@ -0,0 +1,11 @@
|
||||
module.exports = [ |
||||
'country', |
||||
'macroregion', |
||||
'region', |
||||
'macrocounty', |
||||
'county', |
||||
'localadmin', |
||||
'locality', |
||||
'borough', |
||||
'neighbourhood' |
||||
]; |
@ -0,0 +1,56 @@
|
||||
var logger = require('pelias-logger').get('api'); |
||||
var Document = require('pelias-model').Document; |
||||
|
||||
var placeTypes = require('../helper/placeTypes'); |
||||
|
||||
/** |
||||
* Convert WOF integer ids to Pelias formatted ids that can be used by the /place endpoint. |
||||
* This should probably be moved to the import pipeline once we are happy with the way this works. |
||||
*/ |
||||
|
||||
function setup() { |
||||
return function (req, res, next) { |
||||
// do nothing if no result data set
|
||||
if (!res || !res.data) { |
||||
return next(); |
||||
} |
||||
|
||||
res.data = res.data.map(normalizeParentIds); |
||||
|
||||
next(); |
||||
}; |
||||
} |
||||
|
||||
/** |
||||
* Update all parent ids in the admin hierarchy |
||||
* |
||||
* @param {object} place |
||||
* @return {object} |
||||
*/ |
||||
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']) ]; |
||||
} |
||||
}); |
||||
} |
||||
|
||||
return place; |
||||
} |
||||
|
||||
/** |
||||
* Generate a valid Pelias ids from placetype and WOF id. |
||||
* Assumes all of the incoming ids are WOF ids. |
||||
* |
||||
* @param {string} placeType |
||||
* @param {number} id |
||||
* @return {string} |
||||
*/ |
||||
function makeNewId(placeType, id) { |
||||
var doc = new Document('whosonfirst', placeType, id); |
||||
return doc.getGid(); |
||||
} |
||||
|
||||
module.exports = setup; |
@ -0,0 +1,91 @@
|
||||
var normalizer = require('../../../middleware/normalizeParentIds')(); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('WOF ids converted to Pelias ids', function(t) { |
||||
|
||||
var input = { |
||||
data: [{ |
||||
'parent': { |
||||
'country': ['United States'], // these shouldn't change
|
||||
'country_id': ['85633793'], |
||||
'country_a': ['USA'] |
||||
}, |
||||
'country': ['United States'], |
||||
'country_gid': ['85633793'], |
||||
'country_a': ['USA'], |
||||
'macroregion': ['MacroRegion Name'], |
||||
'macroregion_gid': ['foobar'], |
||||
'macroregion_a': ['MacroRegion Abbreviation'], |
||||
'region': ['New York'], |
||||
'region_gid': ['85688543'], |
||||
'region_a': ['NY'], |
||||
'macrocounty': ['MacroCounty Name'], |
||||
'macrocounty_gid': ['~~~~~'], |
||||
'macrocounty_a': ['MacroCounty Abbreviation'], |
||||
'county': ['Kings County'], |
||||
'county_gid': ['102082361'], |
||||
'county_a': [null], |
||||
'localadmin': ['Brooklyn'], |
||||
'localadmin_gid': ['404521211'], |
||||
'localadmin_a': [null], |
||||
'locality': ['Some Locality'], |
||||
'locality_gid': ['85977539'], |
||||
'locality_a': [null], |
||||
'neighbourhood': [], |
||||
'neighbourhood_gid': [] |
||||
}] |
||||
}; |
||||
|
||||
var expected = { |
||||
data: [{ |
||||
'parent': { |
||||
'country': ['United States'], |
||||
'country_id': ['85633793'], |
||||
'country_a': ['USA'] |
||||
}, |
||||
'country': ['United States'], |
||||
'country_gid': ['whosonfirst:country:85633793'], |
||||
'country_a': ['USA'], |
||||
'macroregion': ['MacroRegion Name'], |
||||
'macroregion_gid': ['whosonfirst:macroregion:foobar'], |
||||
'macroregion_a': ['MacroRegion Abbreviation'], |
||||
'region': ['New York'], |
||||
'region_gid': ['whosonfirst:region:85688543'], |
||||
'region_a': ['NY'], |
||||
'macrocounty': ['MacroCounty Name'], |
||||
'macrocounty_gid': ['whosonfirst:macrocounty:~~~~~'], |
||||
'macrocounty_a': ['MacroCounty Abbreviation'], |
||||
'county': ['Kings County'], |
||||
'county_gid': ['whosonfirst:county:102082361'], |
||||
'county_a': [null], |
||||
'localadmin': ['Brooklyn'], |
||||
'localadmin_gid': ['whosonfirst:localadmin:404521211'], |
||||
'localadmin_a': [null], |
||||
'locality': ['Some Locality'], |
||||
'locality_gid': ['whosonfirst:locality:85977539'], |
||||
'locality_a': [null], |
||||
'neighbourhood': [], |
||||
'neighbourhood_gid': [] |
||||
}] |
||||
}; |
||||
|
||||
normalizer({}, input, function () { |
||||
t.deepEqual(input, expected); |
||||
t.end(); |
||||
}); |
||||
|
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('[middleware] normalizeParentIds: ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue