-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from pelias/osm_housenumber_parsing
osm: parse delimited house number lists
- Loading branch information
Showing
4 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
var through = require('through2'); | ||
|
||
// valid delimiters | ||
var DELIMITER_REGEX = /[,;]/; | ||
|
||
function streamFactory(){ | ||
return through.obj( function( json, _, next ){ | ||
|
||
// no-op, this record doesn't contain a delimited list of house numbers | ||
if( !json.tags || !json.tags.hasOwnProperty('addr:housenumber') || !json.tags['addr:housenumber'].match( DELIMITER_REGEX ) ){ | ||
this.push( json ); | ||
return next(); | ||
} | ||
|
||
// split delimited list in to array of members | ||
var housenumbers = json.tags['addr:housenumber'].split( DELIMITER_REGEX ); | ||
|
||
// remove empty members | ||
housenumbers = housenumbers.filter( function( e ){ return e; }); | ||
|
||
// deduplicate array | ||
housenumbers = housenumbers.filter( function( value, index, array ) { | ||
return array.indexOf( value ) === index; | ||
}); | ||
|
||
// iterate over housenumbers in list | ||
housenumbers.forEach( function( num ){ | ||
|
||
// create a copy with the house number changed | ||
var copy = JSON.parse( JSON.stringify( json ) ); | ||
copy.tags['addr:housenumber'] = num.trim(); | ||
|
||
// push each copy downstream | ||
this.push( copy ); | ||
|
||
}, this); | ||
|
||
// more | ||
next(); | ||
|
||
}); | ||
} | ||
|
||
module.exports = streamFactory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
|
||
var through = require('through2'), | ||
delimited_ranges = require('../../../stream/osm/delimited_ranges'); | ||
|
||
module.exports.street = {}; | ||
|
||
module.exports.street.noop = function(test) { | ||
test('missing housenumber', function(t) { | ||
var json = { no: 'housenumber' }; | ||
var records = stream( json, function( records ){ | ||
t.equal( records.length, 1 ); | ||
t.deepEqual( records[0], json ); | ||
t.end(); | ||
}); | ||
}); | ||
test('housenumber does not contain delimiter', function(t) { | ||
var json = { tags: { 'addr:housenumber': '1A' } }; | ||
var records = stream( json, function( records ){ | ||
t.equal( records.length, 1 ); | ||
t.deepEqual( records[0], json ); | ||
t.end(); | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports.street.range = function(test) { | ||
test('split on comma', function(t) { | ||
var json = { tags: { 'addr:housenumber': '1A,2B' }, misc: 'value' }; | ||
var records = stream( json, function( records ){ | ||
t.equal( records.length, 2 ); | ||
t.equal( records[0].tags['addr:housenumber'], '1A' ); | ||
t.equal( records[0].misc, 'value', 'preserve values' ); | ||
t.equal( records[1].tags['addr:housenumber'], '2B' ); | ||
t.equal( records[1].misc, 'value', 'preserve values' ); | ||
t.end(); | ||
}); | ||
}); | ||
test('split on semi-colon', function(t) { | ||
var json = { tags: { 'addr:housenumber': '1A;2B' }, misc: 'value' }; | ||
var records = stream( json, function( records ){ | ||
t.equal( records.length, 2 ); | ||
t.equal( records[0].tags['addr:housenumber'], '1A' ); | ||
t.equal( records[0].misc, 'value', 'preserve values' ); | ||
t.equal( records[1].tags['addr:housenumber'], '2B' ); | ||
t.equal( records[1].misc, 'value', 'preserve values' ); | ||
t.end(); | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports.street.range_with_whitespace = function(test) { | ||
test('superfluous whitespace', function(t) { | ||
var json = { tags: { 'addr:housenumber': ' 1A, 2B ' }, misc: 'value' }; | ||
var records = stream( json, function( records ){ | ||
t.equal( records.length, 2 ); | ||
t.equal( records[0].tags['addr:housenumber'], '1A' ); | ||
t.equal( records[0].misc, 'value', 'preserve values' ); | ||
t.equal( records[1].tags['addr:housenumber'], '2B' ); | ||
t.equal( records[1].misc, 'value', 'preserve values' ); | ||
t.end(); | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports.street.empty_members = function(test) { | ||
test('remove empty members', function(t) { | ||
var json = { tags: { 'addr:housenumber': '1A,,2B;' }, misc: 'value' }; | ||
var records = stream( json, function( records ){ | ||
t.equal( records.length, 2 ); | ||
t.equal( records[0].tags['addr:housenumber'], '1A' ); | ||
t.equal( records[0].misc, 'value', 'preserve values' ); | ||
t.equal( records[1].tags['addr:housenumber'], '2B' ); | ||
t.equal( records[1].misc, 'value', 'preserve values' ); | ||
t.end(); | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports.street.duplicates = function(test) { | ||
test('remove duplicates', function(t) { | ||
var json = { tags: { 'addr:housenumber': '1A,1A,2B' }, misc: 'value' }; | ||
var records = stream( json, function( records ){ | ||
t.equal( records.length, 2 ); | ||
t.equal( records[0].tags['addr:housenumber'], '1A' ); | ||
t.equal( records[0].misc, 'value', 'preserve values' ); | ||
t.equal( records[1].tags['addr:housenumber'], '2B' ); | ||
t.equal( records[1].misc, 'value', 'preserve values' ); | ||
t.end(); | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports.all = function (tape) { | ||
|
||
function test(name, testFunction) { | ||
return tape('delimited_ranges: ' + name, testFunction); | ||
} | ||
|
||
for( var testCase in module.exports.street ){ | ||
module.exports.street[testCase](test); | ||
} | ||
}; | ||
|
||
// generic stream test runner | ||
function stream( json, cb ){ | ||
|
||
var xform = delimited_ranges(); | ||
var records = []; | ||
|
||
var collect = function( chunk, _, next ){ | ||
records.push( chunk ); | ||
next(); | ||
}; | ||
|
||
var assert = function( next ){ | ||
cb( records ); | ||
next(); | ||
}; | ||
|
||
xform.pipe( through.obj( collect, assert )); | ||
xform.write( json ); | ||
xform.end(); | ||
} |