7
7
const _ = require ( 'lodash' ) ;
8
8
const through = require ( 'through2' ) ;
9
9
const peliasLogger = require ( 'pelias-logger' ) . get ( 'openstreetmap' ) ;
10
+ const parseSemicolonDelimitedValues = require ( '../util/parseSemicolonDelimitedValues' ) ;
10
11
11
12
const LOCALIZED_NAME_KEYS = require ( '../config/localized_name_keys' ) ;
12
13
const NAME_SCHEMA = require ( '../schema/name_osm' ) ;
@@ -37,29 +38,34 @@ module.exports = function(){
37
38
// @ref : http://wiki.openstreetmap.org/wiki/Namespace#Language_code_suffix
38
39
const langCode = getNameSuffix ( key ) ;
39
40
if ( langCode ) {
40
- const langValue = trim ( value ) ;
41
- if ( langValue ) {
42
- doc . setName ( langCode , langValue ) ;
43
- }
41
+ const langValues = parseSemicolonDelimitedValues ( value ) ;
42
+ langValues . forEach ( ( langValue , i ) => {
43
+ if ( i === 0 ) {
44
+ doc . setName ( langCode , langValue ) ;
45
+ } else {
46
+ doc . setNameAlias ( langCode , langValue ) ;
47
+ }
48
+ } ) ;
44
49
}
45
50
46
51
// Map name data from our name mapping schema
47
52
else if ( _ . has ( NAME_SCHEMA , key ) ) {
48
- const nameValue = trim ( value ) ;
49
- if ( nameValue ) {
50
- if ( 'name' === key ) {
51
- doc . setName ( NAME_SCHEMA [ key ] , nameValue ) ;
52
- } else if ( 'default' === NAME_SCHEMA [ key ] ) {
53
- doc . setNameAlias ( NAME_SCHEMA [ key ] , nameValue ) ;
54
- } else {
55
- doc . setName ( NAME_SCHEMA [ key ] , nameValue ) ;
53
+ const nameValues = parseSemicolonDelimitedValues ( cleanString ( value ) ) ;
54
+ nameValues . forEach ( ( nameValue , i ) => {
55
+ // For the primary name key 'name', ensure it is the first value
56
+ if ( 'name' === key && i === 0 ) {
57
+ doc . setName ( NAME_SCHEMA [ key ] , nameValue ) ;
58
+ return ;
56
59
}
57
- }
60
+
61
+ // Otherwise set as an alias
62
+ doc . setNameAlias ( NAME_SCHEMA [ key ] , nameValue ) ;
63
+ } ) ;
58
64
}
59
65
60
66
// Map address data from our address mapping schema
61
67
else if ( _ . has ( ADDRESS_SCHEMA , key ) ) {
62
- const addrValue = trim ( value ) ;
68
+ const addrValue = cleanString ( value ) ;
63
69
if ( addrValue ) {
64
70
const label = ADDRESS_SCHEMA [ key ] ;
65
71
doc . setAddress ( label , normalizeAddressField ( label , addrValue ) ) ;
@@ -71,16 +77,17 @@ module.exports = function(){
71
77
// other names which we could use as the default.
72
78
if ( ! doc . getName ( 'default' ) ) {
73
79
74
- const defaultName =
75
- _ . get ( tags , 'official_name' ) ||
76
- _ . get ( tags , 'int_name' ) ||
77
- _ . get ( tags , 'nat_name' ) ||
78
- _ . get ( tags , 'reg_name' ) ||
79
- doc . getName ( 'en' ) ;
80
+ const defaultName = [
81
+ ...parseSemicolonDelimitedValues ( _ . get ( tags , 'official_name' ) ) ,
82
+ ...parseSemicolonDelimitedValues ( _ . get ( tags , 'int_name' ) ) ,
83
+ ...parseSemicolonDelimitedValues ( _ . get ( tags , 'nat_name' ) ) ,
84
+ ...parseSemicolonDelimitedValues ( _ . get ( tags , 'reg_name' ) ) ,
85
+ ...parseSemicolonDelimitedValues ( doc . getName ( 'en' ) )
86
+ ] . filter ( Boolean ) ;
80
87
81
88
// use one of the preferred name tags listed above
82
- if ( defaultName ) {
83
- doc . setName ( 'default' , defaultName ) ;
89
+ if ( defaultName . length ) {
90
+ doc . setName ( 'default' , defaultName [ 0 ] ) ;
84
91
}
85
92
86
93
// else try to use an available two-letter language name tag
@@ -101,7 +108,7 @@ module.exports = function(){
101
108
// Import airport codes as aliases
102
109
if ( tags . hasOwnProperty ( 'aerodrome' ) || tags . hasOwnProperty ( 'aeroway' ) ) {
103
110
if ( tags . hasOwnProperty ( 'iata' ) ) {
104
- const iata = trim ( tags . iata ) ;
111
+ const iata = cleanString ( tags . iata ) ;
105
112
if ( iata ) {
106
113
doc . setNameAlias ( 'default' , iata ) ;
107
114
doc . setNameAlias ( 'default' , `${ iata } Airport` ) ;
@@ -127,7 +134,7 @@ module.exports = function(){
127
134
} ;
128
135
129
136
// Clean string of leading/trailing junk chars
130
- function trim ( str ) {
137
+ function cleanString ( str ) {
131
138
return _ . trim ( str , '#$%^*<>-=_{};:",./?\t\n\' ' ) ;
132
139
}
133
140
0 commit comments