Browse Source

extracted labelGenerator to be injectable

pull/680/head
Stephen Hess 8 years ago
parent
commit
c14a1311f7
  1. 11
      helper/geojsonify.js
  2. 7
      middleware/geocodeJSON.js
  3. 56
      test/unit/helper/geojsonify.js

11
helper/geojsonify.js

@ -1,18 +1,17 @@
var GeoJSON = require('geojson');
var extent = require('geojson-extent');
var labelGenerator = require('./labelGenerator');
var logger = require('pelias-logger').get('api');
var type_mapping = require('./type_mapping');
var _ = require('lodash');
var addDetails = require('./geojsonify_place_details');
var addMetaData = require('./geojsonify_meta_data');
function geojsonifyPlaces( params, docs ){
function geojsonifyPlaces( params, docs, labelGenerator ){
// flatten & expand data for geojson conversion
var geodata = docs
.map(geojsonifyPlace.bind(null, params))
.map(geojsonifyPlace.bind(null, params, labelGenerator))
.filter( function( doc ){
return !!doc;
});
@ -35,7 +34,7 @@ function geojsonifyPlaces( params, docs ){
return geojson;
}
function geojsonifyPlace(params, place) {
function geojsonifyPlace(params, labelGenerator, place) {
// something went very wrong
if( !place || !place.hasOwnProperty( 'center_point' ) ) {
@ -47,7 +46,7 @@ function geojsonifyPlace(params, place) {
addMetaData(place, output);
addName(place, output);
addDetails(params, place, output);
addLabel(place, output);
addLabel(place, output, labelGenerator);
// map center_point for GeoJSON to work properly
@ -76,7 +75,7 @@ function addName(src, dst) {
* @param {object} src
* @param {object} dst
*/
function addLabel(src, dst) {
function addLabel(src, dst, labelGenerator) {
dst.label = labelGenerator(dst);
}

7
middleware/geocodeJSON.js

@ -11,11 +11,12 @@ var _ = require('lodash');
* @param {string} [basePath]
* @returns {middleware}
*/
function setup(peliasConfig, basePath) {
function setup(peliasConfig, basePath, labelGenerator) {
var opts = {
config: peliasConfig || require('pelias-config').generate().api,
basePath: basePath || '/'
basePath: basePath || '/',
labelGenerator: labelGenerator || require('../helper/labelGenerator')
};
function middleware(req, res, next) {
@ -73,7 +74,7 @@ function convertToGeocodeJSON(req, res, next, opts) {
res.body.geocoding.timestamp = new Date().getTime();
// convert docs to geojson and merge with geocoding block
extend(res.body, geojsonify(req.clean, res.data || []));
extend(res.body, geojsonify(req.clean, res.data || [], opts.labelGenerator));
next();
}

56
test/unit/helper/geojsonify.js

@ -5,15 +5,15 @@ module.exports.tests = {};
module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) {
t.equal(typeof geojsonify, 'function', 'search is a function');
t.equal(geojsonify.length, 2, 'accepts x arguments');
t.equal(typeof geojsonify, 'function', 'geojsonify is a function');
t.equal(geojsonify.length, 3, 'accepts x arguments');
t.end();
});
};
// ref: https://github.com/pelias/pelias/issues/84
module.exports.tests.earth = function(test, common) {
test('earth', function(t) {
var earth = [{
'_type': 'geoname',
'_id': '6295630',
@ -28,9 +28,12 @@ module.exports.tests.earth = function(test, common) {
}
}];
test('earth', function(t) {
var labelGenerator = function(doc) {
return 'label for 6295630';
};
t.doesNotThrow(function(){
geojsonify( {}, earth );
geojsonify( {}, earth, labelGenerator );
});
t.end();
});
@ -39,6 +42,7 @@ module.exports.tests.earth = function(test, common) {
module.exports.tests.geojsonify = function(test, common) {
test('geojsonify(doc)', function(t) {
var input = [
{
'_id': 'id1',
@ -143,7 +147,7 @@ module.exports.tests.geojsonify = function(test, common) {
'layer': 'layer1',
'source': 'source1',
'source_id': 'source_id_1',
'label': '\'Round Midnight Jazz and Blues Bar, test2, England, United Kingdom',
'label': 'label for id1',
'name': '\'Round Midnight Jazz and Blues Bar',
'country_a': 'GBR',
'country': 'United Kingdom',
@ -179,7 +183,7 @@ module.exports.tests.geojsonify = function(test, common) {
'layer': 'layer2',
'source': 'source2',
'source_id': 'source_id_2',
'label': 'Blues Cafe, test2, England, United Kingdom',
'label': 'label for id2',
'name': 'Blues Cafe',
'country_a': 'GBR',
'country': 'United Kingdom',
@ -208,7 +212,7 @@ module.exports.tests.geojsonify = function(test, common) {
'layer': 'venue',
'source': 'openstreetmap',
'source_id': 'source_id_3',
'label': 'Empire State Building, Manhattan, New York, NY, USA',
'label': 'label for node:34633854',
'name': 'Empire State Building',
'country_a': 'USA',
'country': 'United States',
@ -228,8 +232,20 @@ module.exports.tests.geojsonify = function(test, common) {
]
};
test('geojsonify(doc)', function(t) {
var json = geojsonify( {categories: 'foo'}, input );
var labelGenerator = function(doc) {
if (doc.id === 'id1') {
return 'label for id1';
}
if (doc.id === 'id2') {
return 'label for id2';
}
if (doc.id === 'node:34633854') {
return 'label for node:34633854';
}
};
var json = geojsonify( {categories: 'foo'}, input, labelGenerator );
t.deepEqual(json, expected, 'all docs mapped');
t.end();
@ -374,7 +390,7 @@ module.exports.tests.geojsonify = function(test, common) {
'localadmin_gid': '404521211',
'locality': 'New York',
'locality_gid': '85977539',
'label': 'East New York, Brooklyn, New York, NY, USA'
'label': 'label for 85816607'
},
'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588],
'geometry': {
@ -388,7 +404,13 @@ module.exports.tests.geojsonify = function(test, common) {
]
};
var json = geojsonify( {categories: 'foo'}, input );
var labelGenerator = function(doc) {
if (doc.id === '85816607') {
return 'label for 85816607';
}
};
var json = geojsonify( {categories: 'foo'}, input, labelGenerator );
t.deepEqual(json, expected, 'all wanted properties exposed');
t.end();
@ -434,7 +456,7 @@ module.exports.tests.categories = function (test, common) {
'source_id': '85816607',
'name': 'East New York',
'category': ['government'],
'label': 'East New York'
'label': 'label for 85816607'
},
'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588],
'geometry': {
@ -448,7 +470,13 @@ module.exports.tests.categories = function (test, common) {
]
};
var json = geojsonify( {categories: 'foo'}, input );
var labelGenerator = function(doc) {
if (doc.id === '85816607') {
return 'label for 85816607';
}
};
var json = geojsonify( {categories: 'foo'}, input, labelGenerator );
t.deepEqual(json, expected, 'all wanted properties exposed');
t.end();

Loading…
Cancel
Save