-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(popularity_bands): add post/popularity.js script
- Loading branch information
1 parent
05baaac
commit eb2bdf0
Showing
5 changed files
with
103 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,33 @@ | ||
/** | ||
* Minimum popularity post-processing script ensures that every document | ||
* meets a minimum popularity value based on the layer to which it belongs. | ||
* | ||
* For more info see: | ||
* - https://github.com/pelias/openstreetmap/pull/493#issuecomment-503019975 | ||
* - https://github.com/pelias/openstreetmap/blob/master/stream/popularity_mapper.js | ||
*/ | ||
|
||
const _ = require('lodash'); | ||
|
||
const MIN_POPULARITY_MAP = { | ||
postalcode: 9000 | ||
}; | ||
|
||
function popularity(doc) { | ||
let minValue = _.get(MIN_POPULARITY_MAP, doc.getLayer()); | ||
|
||
// only apply to layers listed in the mapping above | ||
if (!_.isFinite(minValue)) { return; } | ||
|
||
// avoid setting a popularity of 0 (or less) | ||
if (minValue <= 0) { return; } | ||
|
||
// skip updating records where the current popularity exceeds the minimum | ||
let currentValue = doc.getPopularity(); | ||
if (_.isFinite(currentValue) && currentValue >= minValue) { return; } | ||
|
||
// set the minimum popularity for this layer | ||
doc.setPopularity(minValue); | ||
} | ||
|
||
module.exports = popularity; |
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,66 @@ | ||
const Document = require('../../Document'); | ||
const popularity = require('../../post/popularity'); | ||
|
||
module.exports.tests = {}; | ||
|
||
const MIN_POSTCODE_POPULARITY = 9000; | ||
module.exports.tests.postalcode = function (test) { | ||
test('postalcode - empty', function (t) { | ||
var doc = new Document('mysource', 'postalcode', 'myid'); | ||
|
||
popularity(doc); | ||
t.deepEquals(doc.getPopularity(), MIN_POSTCODE_POPULARITY); | ||
|
||
t.end(); | ||
}); | ||
test('postalcode - zero', function (t) { | ||
var doc = new Document('mysource', 'postalcode', 'myid'); | ||
doc.setPopularity(0); | ||
|
||
popularity(doc); | ||
t.deepEquals(doc.getPopularity(), MIN_POSTCODE_POPULARITY); | ||
|
||
t.end(); | ||
}); | ||
test('postalcode - above minimum', function (t) { | ||
var doc = new Document('mysource', 'postalcode', 'myid'); | ||
doc.setPopularity(9999999); | ||
|
||
popularity(doc); | ||
t.deepEquals(doc.getPopularity(), 9999999); | ||
|
||
t.end(); | ||
}); | ||
test('postalcode - below minimum', function (t) { | ||
var doc = new Document('mysource', 'postalcode', 'myid'); | ||
doc.setPopularity(100); | ||
|
||
popularity(doc); | ||
t.deepEquals(doc.getPopularity(), MIN_POSTCODE_POPULARITY); | ||
|
||
t.end(); | ||
}); | ||
}; | ||
|
||
// layers not listed in MIN_POPULARITY_MAP should result in a no-op | ||
module.exports.tests.noop = function (test) { | ||
test('other layers - no-op', function (t) { | ||
var doc = new Document('mysource', 'mylayer', 'myid'); | ||
|
||
popularity(doc); | ||
t.deepEquals(doc.getPopularity(), undefined); | ||
|
||
t.end(); | ||
}); | ||
}; | ||
|
||
module.exports.all = function (tape, common) { | ||
|
||
function test(name, testFunction) { | ||
return tape('post/popularity: ' + name, testFunction); | ||
} | ||
|
||
for (var testCase in module.exports.tests) { | ||
module.exports.tests[testCase](test, common); | ||
} | ||
}; |
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