Browse Source

moved assignLabels to fire before geojsonify

pull/680/head
Stephen Hess 8 years ago
parent
commit
beb309ca07
  1. 1
      helper/geojsonify.js
  2. 6
      middleware/assignLabels.js
  3. 2
      routes/v1.js
  4. 30
      test/unit/helper/geojsonify.js
  5. 64
      test/unit/middleware/assignLabels.js

1
helper/geojsonify.js

@ -46,6 +46,7 @@ function geojsonifyPlace(params, labelGenerator, place) {
addMetaData(place, output);
addName(place, output);
addDetails(params, place, output);
output.label = place.label;
// map center_point for GeoJSON to work properly
// these should not show up in the final feature properties

6
middleware/assignLabels.js

@ -9,12 +9,12 @@ function setup(labelGenerator) {
function assignLabel(req, res, next, labelGenerator) {
// do nothing if there's nothing to process
if (!res || !res.body || !res.body.features) {
if (!res || !res.data) {
return next();
}
res.body.features.forEach(function (feature) {
feature.properties.label = labelGenerator(feature.properties);
res.data.forEach(function (result) {
result.label = labelGenerator(result.parent);
});
next();

2
routes/v1.js

@ -88,8 +88,8 @@ function addRoutes(app, peliasConfig) {
postProc.renamePlacenames(),
postProc.parseBoundingBox(),
postProc.normalizeParentIds(),
postProc.geocodeJSON(peliasConfig, base),
postProc.assignLabels(require('pelias-labels')),
postProc.geocodeJSON(peliasConfig, base),
postProc.sendJSON
]),
autocomplete: createRouter([

30
test/unit/helper/geojsonify.js

@ -69,7 +69,8 @@ module.exports.tests.geojsonify = function(test, common) {
'category': [
'food',
'nightlife'
]
],
'label': 'label for id id1'
},
{
'_id': 'id2',
@ -93,7 +94,8 @@ module.exports.tests.geojsonify = function(test, common) {
'county': 'Smithfield',
'localadmin': 'test1',
'locality': 'test2',
'neighbourhood': 'test3'
'neighbourhood': 'test3',
'label': 'label for id id2'
},
{
'_id': 'node:34633854',
@ -120,7 +122,8 @@ module.exports.tests.geojsonify = function(test, common) {
'category': [
'tourism',
'transport'
]
],
'label': 'label for id node:34633854'
}
];
@ -160,7 +163,8 @@ module.exports.tests.geojsonify = function(test, common) {
'category': [
'food',
'nightlife'
]
],
'label': 'label for id id1'
}
},
{
@ -188,7 +192,8 @@ module.exports.tests.geojsonify = function(test, common) {
'county': 'Smithfield',
'localadmin': 'test1',
'locality': 'test2',
'neighbourhood': 'test3'
'neighbourhood': 'test3',
'label': 'label for id id2'
}
},
{
@ -219,7 +224,8 @@ module.exports.tests.geojsonify = function(test, common) {
'category': [
'tourism',
'transport'
]
],
'label': 'label for id node:34633854'
}
}
]
@ -330,7 +336,8 @@ module.exports.tests.geojsonify = function(test, common) {
null
],
'neighbourhood': [],
'neighbourhood_gid': []
'neighbourhood_gid': [],
'label': 'label for id 85816607'
}
];
@ -369,7 +376,8 @@ module.exports.tests.geojsonify = function(test, common) {
'county_gid': '102082361',
'localadmin_gid': '404521211',
'locality': 'New York',
'locality_gid': '85977539'
'locality_gid': '85977539',
'label': 'label for id 85816607'
},
'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588],
'geometry': {
@ -411,7 +419,8 @@ module.exports.tests.categories = function (test, common) {
'default': 'East New York'
},
'source_id': '85816607',
'category': ['government']
'category': ['government'],
'label': 'label for id 85816607'
}
];
@ -428,7 +437,8 @@ module.exports.tests.categories = function (test, common) {
'source': 'whosonfirst',
'source_id': '85816607',
'name': 'East New York',
'category': ['government']
'category': ['government'],
'label': 'label for id 85816607'
},
'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588],
'geometry': {

64
test/unit/middleware/assignLabels.js

@ -15,7 +15,7 @@ module.exports.tests.serialization = function(test, common) {
});
test('res without body should not throw an exception', function(t) {
test('res without data should not throw an exception', function(t) {
var assignLabels = require('../../../middleware/assignLabels')(function(){});
function testIt() {
@ -27,24 +27,12 @@ module.exports.tests.serialization = function(test, common) {
});
test('res.body without features should not throw an exception', function(t) {
var assignLabels = require('../../../middleware/assignLabels')(function(){});
function testIt() {
assignLabels({ body: {} }, {}, function() {});
}
t.doesNotThrow(testIt, 'an exception should not have been thrown');
t.end();
});
test('labels should be assigned to all results', function(t) {
var labelGenerator = function(properties) {
if (properties.id === 1) {
var labelGenerator = function(result) {
if (result.id === 1) {
return 'label 1';
}
if (properties.id === 2) {
if (result.id === 2) {
return 'label 2';
}
@ -53,39 +41,35 @@ module.exports.tests.serialization = function(test, common) {
var assignLabels = require('../../../middleware/assignLabels')(labelGenerator);
var input = {
body: {
features: [
data: [
{
properties: {
parent: {
id: 1
}
},
{
properties: {
parent: {
id: 2
}
}
]
}
};
var expected = {
body: {
features: [
data: [
{
properties: {
id: 1,
parent: {
id: 1
},
label: 'label 1'
}
},
{
properties: {
id: 2,
parent: {
id: 2
},
label: 'label 2'
}
}
]
}
};
assignLabels({}, input, function () {
@ -97,36 +81,32 @@ module.exports.tests.serialization = function(test, common) {
test('no explicit labelGenerator supplied should use pelias-labels module', function(t) {
var assignLabels = proxyquire('../../../middleware/assignLabels', {
'pelias-labels': function(properties) {
if (properties.id === 1) {
'pelias-labels': function(result) {
if (result.id === 1) {
return 'label 1';
}
}
})();
var input = {
body: {
features: [
data: [
{
properties: {
parent: {
id: 1
}
}
]
}
};
var expected = {
body: {
features: [
data: [
{
properties: {
id: 1,
parent: {
id: 1
},
label: 'label 1'
}
}
]
}
};
assignLabels({}, input, function () {

Loading…
Cancel
Save