Skip to content

Commit 16de14b

Browse files
committed
feat(tags): map alternate names to individual fields
1 parent 01cb65d commit 16de14b

4 files changed

Lines changed: 160 additions & 256 deletions

File tree

schema/name_osm.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525

2626
var OSM_NAMING_SCHEMA = {
2727
'name': 'default',
28-
'loc_name': 'default',
29-
'alt_name': 'default',
30-
'short_name': 'default',
28+
'loc_name': 'alt',
29+
'alt_name': 'alt',
30+
'short_name': 'abbr',
3131

3232
// note: these aliases are currently disabled because they are not being used when querying
3333
// 'int_name': 'international',
@@ -38,14 +38,4 @@ var OSM_NAMING_SCHEMA = {
3838
// 'sorting_name': 'sorting'
3939
};
4040

41-
// this property is considered the 'primary name'
42-
// for label generation, the others are considered
43-
// secondary or 'aliases'.
44-
Object.defineProperty(OSM_NAMING_SCHEMA, '_primary', {
45-
value: 'name',
46-
enumerable: false,
47-
configurable: false,
48-
writable: false
49-
});
50-
5141
module.exports = OSM_NAMING_SCHEMA;

stream/tag_mapper.js

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,29 @@ var ADDRESS_SCHEMA = _.merge( {},
1717
require('../schema/address_karlsruhe')
1818
);
1919

20+
/**
21+
* convenience function for appending names:
22+
*
23+
* - selects 'setName' or 'setNameAlias' appropriately.
24+
* - splits on ';', a common OSM convention for multi values.
25+
* - applies name normalization via trim().
26+
*/
27+
const appendName = (doc, label, value) => {
28+
if (!_.isString(value) || !value.length) { return; }
29+
30+
value.split(';').forEach(name => {
31+
let initial = !doc.getName(label);
32+
let trimmed = trim(name);
33+
if (trimmed.length < 1){ return; }
34+
35+
if (initial) {
36+
doc.setName(label, trimmed);
37+
} else {
38+
doc.setNameAlias(label, trimmed);
39+
}
40+
});
41+
};
42+
2043
module.exports = function(){
2144

2245
var stream = through.obj( function( doc, enc, next ) {
@@ -29,40 +52,35 @@ module.exports = function(){
2952
return next( null, doc );
3053
}
3154

55+
// primary name tag: 'name'
56+
if (_.has(tags, 'name')) {
57+
appendName(doc, 'default', _.get(tags, 'name'));
58+
}
59+
3260
// Unfortunately we need to iterate over every tag,
3361
// so we only do the iteration once to save CPU.
3462
_.each(tags, (value, key) => {
63+
// primary field was mapped above
64+
if (key === 'name') { return; }
3565

3666
// Map localized names which begin with 'name:'
3767
// @ref: http://wiki.openstreetmap.org/wiki/Namespace#Language_code_suffix
38-
var suffix = getNameSuffix( key );
39-
if( suffix ){
40-
var val1 = trim( value );
41-
if( val1 ){
42-
doc.setName( suffix, val1 );
43-
}
68+
var suffix = getNameSuffix(key);
69+
if (suffix) {
70+
appendName(doc, suffix, value);
4471
}
4572

4673
// Map name data from our name mapping schema
47-
else if( _.has(NAME_SCHEMA, key) ){
48-
var val2 = trim( value );
49-
if( val2 ){
50-
if( key === NAME_SCHEMA._primary ){
51-
doc.setName( NAME_SCHEMA[key], val2 );
52-
} else if ( 'default' === NAME_SCHEMA[key] ) {
53-
doc.setNameAlias( NAME_SCHEMA[key], val2 );
54-
} else {
55-
doc.setName( NAME_SCHEMA[key], val2 );
56-
}
57-
}
74+
else if (_.has(NAME_SCHEMA, key)) {
75+
appendName(doc, _.get(NAME_SCHEMA, key), value);
5876
}
5977

6078
// Map address data from our address mapping schema
61-
else if( _.has(ADDRESS_SCHEMA, key) ){
62-
var val3 = trim( value );
63-
if( val3 ){
64-
let label = ADDRESS_SCHEMA[key];
65-
doc.setAddress(label, normalizeAddressField(label, val3));
79+
else if (_.has(ADDRESS_SCHEMA, key)) {
80+
let label = ADDRESS_SCHEMA[key];
81+
let trimmed = trim(value);
82+
if (trimmed.length > 0) {
83+
doc.setAddress(label, normalizeAddressField(label, trimmed));
6684
}
6785
}
6886
});
@@ -103,8 +121,8 @@ module.exports = function(){
103121
if( tags.hasOwnProperty('iata') ){
104122
var iata = trim( tags.iata );
105123
if( iata ){
106-
doc.setNameAlias( 'default', iata );
107-
doc.setNameAlias( 'default', `${iata} Airport` );
124+
appendName(doc, 'code', iata);
125+
appendName(doc, 'org', `${iata} Airport`);
108126
}
109127
}
110128
}

0 commit comments

Comments
 (0)