Skip to content

Commit 952ed84

Browse files
committed
feat(wof): Use shared getDefaultName from wof package
This is a followup to pelias/whosonfirst#566 which moves us to a common implementation for getting default names from Who's on First records.
1 parent 8bf22ba commit 952ed84

3 files changed

Lines changed: 57 additions & 27 deletions

File tree

import/source/whosonfirst/config/generic.js

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,28 @@
11
const _ = require('lodash')
2+
const getDefaultName = require('pelias-whosonfirst').getDefaultName
23

34
/**
45
* This file provides some convenience functions for finding the 'generic name'
56
* and 'generic abbreviation' of a WOF document.
67
*
7-
* This should probably be a lot easier than it is, and there seems to be some
8-
* inconsistency within the Pelias codebase about which fields to prefer in
9-
* certain cases.
8+
* getName() used to have its own hand-ported copy of wof-admin-lookup's old
9+
* name-selection logic (see git history), maintained as "a best-effort attempt
10+
* to honour the field mappings... used [in] pelias/wof-admin-lookup" - which,
11+
* being a manual port, had drifted out of sync and was missing the longname/
12+
* qualifier-preferred-allowlist logic wof-admin-lookup and placeholder now
13+
* share. It now delegates to that same shared implementation directly instead
14+
* of maintaining a fourth independent copy.
1015
*
11-
* I've made a best-effort attempt to honour the field mappings which are (at time of writing)
12-
* being used the pelias/wof-admin-lookup module.
13-
* The idea is that this repo can be used as a drop-in replacement for wof-admin-lookup.
14-
*
15-
* Prior work:
16-
* [N1] https://github.com/pelias/wof-admin-lookup/blob/e7ea48af6eb5b2b88886dd4b4f71a81e6e38696a/src/pip/components/getDefaultName.js
17-
* [A1] https://github.com/pelias/wof-admin-lookup/blob/d9abfe32ed40184bd657df463e2faeb6ff2f7326/src/pip/components/extractFields.js#L44-L51
18-
* [A2] https://github.com/pelias/whosonfirst/blob/fee549816a8a29fc5c3daccc66129677f8d552d6/src/components/extractFields.js#L154
16+
* NOTE: while pelias-whosonfirst's getDefaultName support is still in progress,
17+
* this package is resolved via `npm link` to a local checkout rather than a
18+
* published npm version. Once pelias-whosonfirst publishes a release with
19+
* getDefaultName, bump the version constraint in package.json and remove the link.
1920
*/
2021

2122
// convenience function to find a generic name for the place
2223
function getName (properties) {
23-
const placeType = _.get(properties, 'wof:placetype')
24-
const ISOcountry = _.get(properties, 'iso:country')
25-
const quattroAlt = _.get(properties, 'qs:a2_alt')
26-
const label = _.get(properties, 'wof:label')
27-
const name = _.get(properties, 'wof:name')
28-
29-
// this US-county specific logic was ported from [A1]
30-
if (ISOcountry === 'US' && placeType === 'county' && quattroAlt) {
31-
return quattroAlt.trim()
32-
}
33-
34-
// use label
35-
if (label) { return label.trim() }
36-
37-
// use name
38-
if (name) { return name.trim() }
24+
const name = getDefaultName(properties)
25+
return typeof name === 'string' ? name.trim() : name
3926
}
4027

4128
// convenience function to find a generic abbreviation for the place

import/source/whosonfirst/map/names.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,48 @@ tap.test('mapper: wof:name - use qs:a2_alt for USA counties', (t) => {
5858
t.equal(p.name[0].name, 'example3')
5959
t.end()
6060
})
61+
tap.test('mapper: wof:name - label:eng_x_preferred_longname wins over qs:a2_alt for USA counties', (t) => {
62+
let p = new Place()
63+
map(p, {
64+
'iso:country': 'US',
65+
'wof:placetype': 'county',
66+
'wof:name': 'Kings',
67+
'label:eng_x_preferred_longname': ['Kings County'],
68+
'qs:a2_alt': 'a different qs:a2_alt value'
69+
})
70+
71+
t.equal(p.name.length, 1)
72+
t.equal(p.name[0].name, 'Kings County')
73+
t.end()
74+
})
75+
tap.test('mapper: wof:name - longname is used for French counties too (qualifier-preferred ' +
76+
'allowlist is not English-only)', (t) => {
77+
let p = new Place()
78+
map(p, {
79+
'iso:country': 'FR',
80+
'wof:placetype': 'county',
81+
'wof:name': 'Montmarault',
82+
'label:eng_x_preferred_longname': ['Montmarault Canton']
83+
})
84+
85+
t.equal(p.name.length, 1)
86+
t.equal(p.name[0].name, 'Montmarault Canton')
87+
t.end()
88+
})
89+
tap.test('mapper: wof:name - longname is NOT used outside the qualifier-preferred allowlist ' +
90+
'(eg. Greater London stays Greater London, not the ceremonial-county longname)', (t) => {
91+
let p = new Place()
92+
map(p, {
93+
'iso:country': 'GB',
94+
'wof:placetype': 'macrocounty',
95+
'wof:name': 'Greater London',
96+
'label:eng_x_preferred_longname': ['Greater London Ceremonial County']
97+
})
98+
99+
t.equal(p.name.length, 1)
100+
t.equal(p.name[0].name, 'Greater London')
101+
t.end()
102+
})
61103

62104
// generic abbreviation
63105
tap.test('mapper: wof:abbreviation - prefer wof:shortcode over wof:abbreviation', (t) => {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"morgan": "^1.9.1",
4848
"pelias-config": "^6.0.0",
4949
"pelias-logger": "^1.4.1",
50+
"pelias-whosonfirst": "^8.5.0",
5051
"split2": "^3.1.1",
5152
"through2": "^3.0.1",
5253
"turf-point": "^2.0.1",

0 commit comments

Comments
 (0)