Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions import/source/whosonfirst/map/hierarchies.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ function sortHierarchy (hierarchy) {

function mapper (place, properties) {
const hierarchies = _.get(properties, 'wof:hierarchy', [])
const pt = spec.names.get(place.ontology.type)
const id = parseInt(place.identity.id, 10)

hierarchies.forEach((hierarchy, o) => {
// sort hierarchy and ensure self-reference exists
const sorted = sortHierarchy({ ...hierarchy, ...{ [`${pt.name}_id`]: id } })
// note: place.ontology.type is used directly (rather than looking it up
// via spec.names) so that records with a placetype not present in the
// spec file (eg. a WOF placetype spatial doesn't support yet) still get
// a self-reference instead of crashing - sortHierarchy() already treats
// unrecognised placetypes as rank 0.
const sorted = sortHierarchy({ ...hierarchy, ...{ [`${place.ontology.type}_id`]: id } })

let depth = 0
for (const key in sorted) {
Expand Down
22 changes: 22 additions & 0 deletions import/source/whosonfirst/map/hierarchies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,28 @@ tap.test('mapper: key ordering', (t) => {

t.end()
})
tap.test('mapper: record\'s own placetype not in spec (eg. postalregion) should not throw, ' +
'and should still get a self-reference', (t) => {
const p = new Place(new Identity('wof', '1864205377'), new Ontology('admin', 'postalregion'))
map(p, {
'wof:hierarchy': [
{
continent_id: 102191581,
country_id: 85633159,
empire_id: 136253055
}
]
})

t.same([
new Hierarchy(p.identity, new Identity('wof', '85633159'), 'wof:0', 0),
new Hierarchy(p.identity, new Identity('wof', '136253055'), 'wof:0', 1),
new Hierarchy(p.identity, new Identity('wof', '102191581'), 'wof:0', 2),
new Hierarchy(p.identity, p.identity, 'wof:0', 3)
], p.hierarchy)

t.end()
})
tap.test('mapper: sort hierarchy - unknown placetype', (t) => {
const p = new Place(new Identity('wof', '1729339019'), new Ontology('admin', 'locality'))
map(p, {
Expand Down