-
-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support semi-colon delimited names #589
Changes from all commits
46168cc
8619656
fd8a752
2a0a319
a1e377b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,11 @@ | |
const _ = require('lodash'); | ||
const through = require('through2'); | ||
const peliasLogger = require('pelias-logger').get('openstreetmap'); | ||
const parseSemicolonDelimitedValues = require('../util/parseSemicolonDelimitedValues'); | ||
|
||
var LOCALIZED_NAME_KEYS = require('../config/localized_name_keys'); | ||
var NAME_SCHEMA = require('../schema/name_osm'); | ||
var ADDRESS_SCHEMA = _.merge( {}, | ||
const LOCALIZED_NAME_KEYS = require('../config/localized_name_keys'); | ||
const NAME_SCHEMA = require('../schema/name_osm'); | ||
const ADDRESS_SCHEMA = _.merge( {}, | ||
require('../schema/address_tiger'), | ||
require('../schema/address_osm'), | ||
require('../schema/address_naptan'), | ||
|
@@ -19,7 +20,7 @@ var ADDRESS_SCHEMA = _.merge( {}, | |
|
||
module.exports = function(){ | ||
|
||
var stream = through.obj( function( doc, enc, next ) { | ||
const stream = through.obj( function( doc, enc, next ) { | ||
|
||
try { | ||
|
||
|
@@ -35,34 +36,39 @@ module.exports = function(){ | |
|
||
// Map localized names which begin with 'name:' | ||
// @ref: http://wiki.openstreetmap.org/wiki/Namespace#Language_code_suffix | ||
var suffix = getNameSuffix( key ); | ||
if( suffix ){ | ||
var val1 = trim( value ); | ||
if( val1 ){ | ||
doc.setName( suffix, val1 ); | ||
} | ||
const langCode = getNameSuffix( key ); | ||
if( langCode ){ | ||
const langValues = parseSemicolonDelimitedValues( value ); | ||
langValues.forEach(( langValue, i ) => { | ||
if ( i === 0 ) { | ||
doc.setName( langCode, langValue ); | ||
} else { | ||
doc.setNameAlias( langCode, langValue ); | ||
} | ||
Comment on lines
+42
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah nice this is much cleaner. |
||
}); | ||
} | ||
|
||
// Map name data from our name mapping schema | ||
else if( _.has(NAME_SCHEMA, key) ){ | ||
var val2 = trim( value ); | ||
if( val2 ){ | ||
if( key === NAME_SCHEMA._primary ){ | ||
doc.setName( NAME_SCHEMA[key], val2 ); | ||
} else if ( 'default' === NAME_SCHEMA[key] ) { | ||
doc.setNameAlias( NAME_SCHEMA[key], val2 ); | ||
} else { | ||
doc.setName( NAME_SCHEMA[key], val2 ); | ||
const nameValues = parseSemicolonDelimitedValues( cleanString( value ) ); | ||
nameValues.forEach(( nameValue, i ) => { | ||
// For the primary name key 'name', ensure it is the first value | ||
if( 'name' === key && i === 0 ){ | ||
doc.setName(NAME_SCHEMA[key], nameValue); | ||
return; | ||
} | ||
} | ||
|
||
// Otherwise set as an alias | ||
doc.setNameAlias( NAME_SCHEMA[key], nameValue ); | ||
}); | ||
} | ||
|
||
// Map address data from our address mapping schema | ||
else if( _.has(ADDRESS_SCHEMA, key) ){ | ||
var val3 = trim( value ); | ||
if( val3 ){ | ||
let label = ADDRESS_SCHEMA[key]; | ||
doc.setAddress(label, normalizeAddressField(label, val3)); | ||
const addrValue = cleanString( value ); | ||
if( addrValue ){ | ||
const label = ADDRESS_SCHEMA[key]; | ||
doc.setAddress(label, normalizeAddressField(label, addrValue)); | ||
} | ||
} | ||
}); | ||
|
@@ -71,21 +77,22 @@ module.exports = function(){ | |
// other names which we could use as the default. | ||
if( !doc.getName('default') ){ | ||
|
||
var defaultName = | ||
_.get(tags, 'official_name') || | ||
_.get(tags, 'int_name') || | ||
_.get(tags, 'nat_name') || | ||
_.get(tags, 'reg_name') || | ||
doc.getName('en'); | ||
const defaultName = [ | ||
...parseSemicolonDelimitedValues(_.get(tags, 'official_name')), | ||
...parseSemicolonDelimitedValues(_.get(tags, 'int_name')), | ||
...parseSemicolonDelimitedValues(_.get(tags, 'nat_name')), | ||
...parseSemicolonDelimitedValues(_.get(tags, 'reg_name')), | ||
...parseSemicolonDelimitedValues(doc.getName('en')) | ||
].filter(Boolean); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More fanciness :P So just to confirm, this is because you can't use Might actually be a use case for |
||
|
||
// use one of the preferred name tags listed above | ||
if ( defaultName ){ | ||
doc.setName('default', defaultName); | ||
if ( defaultName.length ){ | ||
doc.setName('default', defaultName[0]); | ||
} | ||
|
||
// else try to use an available two-letter language name tag | ||
else { | ||
var keys = Object.keys(doc.name).filter(n => n.length === 2); | ||
const keys = Object.keys(doc.name).filter(n => n.length === 2); | ||
|
||
// unambiguous (there is only a single two-letter name tag) | ||
if ( keys.length === 1 ){ | ||
|
@@ -101,7 +108,7 @@ module.exports = function(){ | |
// Import airport codes as aliases | ||
if( tags.hasOwnProperty('aerodrome') || tags.hasOwnProperty('aeroway') ){ | ||
if( tags.hasOwnProperty('iata') ){ | ||
var iata = trim( tags.iata ); | ||
const iata = cleanString( tags.iata ); | ||
if( iata ){ | ||
doc.setNameAlias( 'default', iata ); | ||
doc.setNameAlias( 'default', `${iata} Airport` ); | ||
|
@@ -127,7 +134,7 @@ module.exports = function(){ | |
}; | ||
|
||
// Clean string of leading/trailing junk chars | ||
function trim( str ){ | ||
function cleanString( str ){ | ||
return _.trim( str, '#$%^*<>-=_{};:",./?\t\n\' ' ); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const parseSemicolonDelimitedValues = require('../../util/parseSemicolonDelimitedValues'); | ||
|
||
module.exports.tests = {}; | ||
|
||
// test exports | ||
module.exports.tests.smoke = function (test, common) { | ||
test('interface', t => { | ||
t.equal(typeof parseSemicolonDelimitedValues, 'function', 'function'); | ||
t.end(); | ||
}); | ||
test('parse - invalid', t => { | ||
t.deepEqual(parseSemicolonDelimitedValues(1), []); | ||
t.deepEqual(parseSemicolonDelimitedValues(['a']), []); | ||
t.deepEqual(parseSemicolonDelimitedValues([{'a': 'b'}]), []); | ||
t.deepEqual(parseSemicolonDelimitedValues(undefined), []); | ||
t.deepEqual(parseSemicolonDelimitedValues(null), []); | ||
t.deepEqual(parseSemicolonDelimitedValues(''), []); | ||
t.end(); | ||
}); | ||
test('parse - examples', t => { | ||
t.deepEqual(parseSemicolonDelimitedValues(''), []); | ||
t.deepEqual(parseSemicolonDelimitedValues(' '), []); | ||
t.deepEqual(parseSemicolonDelimitedValues(' ;; ; ; ; ; ;; ; ;; '), []); | ||
t.deepEqual(parseSemicolonDelimitedValues(' a; b ;;; ; '), ['a', 'b']); | ||
t.end(); | ||
}); | ||
}; | ||
|
||
module.exports.all = function (tape, common) { | ||
|
||
function test(name, testFunction) { | ||
return tape('parseSemicolonDelimitedValues: ' + name, testFunction); | ||
} | ||
|
||
for (var testCase in module.exports.tests) { | ||
module.exports.tests[testCase](test, common); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const _ = require('lodash'); | ||
|
||
// Split multi-value OSM tags into an Array | ||
// https://wiki.openstreetmap.org/wiki/Talk:Semi-colon_value_separator | ||
function parseSemicolonDelimitedValues(value) { | ||
return (_.isString(value) ? value : '') | ||
.split(';') | ||
.map(v => v.trim()) | ||
.filter(v => v.length); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. diff --git a/util/parseSemicolonDelimitedValues.js b/util/parseSemicolonDelimitedValues.js
index 08e21ac..39c123b 100644
--- a/util/parseSemicolonDelimitedValues.js
+++ b/util/parseSemicolonDelimitedValues.js
@@ -5,8 +5,8 @@ const _ = require('lodash');
function parseSemicolonDelimitedValues(value) {
return (_.isString(value) ? value : '')
.split(';')
- .map(Function.prototype.call, String.prototype.trim)
- .filter(Boolean);
+ .map(v => v.trim())
+ .filter(v => v.length);
}
module.exports = parseSemicolonDelimitedValues;
\ No newline at end of file |
||
module.exports = parseSemicolonDelimitedValues; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was kinda weird and overly fancy, I removed it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Over the years we've learned many things you can do with Javascript, but shouldn't :P