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. 430
      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();
}

430
test/unit/helper/geojsonify.js

@ -5,32 +5,35 @@ 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',
'source': 'whosonfirst',
'layer': 'continent',
'name': {
'default': 'Earth'
},
'center_point': {
'lon': 0,
'lat': 0
}
}];
var earth = [{
'_type': 'geoname',
'_id': '6295630',
'source': 'whosonfirst',
'layer': 'continent',
'name': {
'default': 'Earth'
},
'center_point': {
'lon': 0,
'lat': 0
}
}];
var labelGenerator = function(doc) {
return 'label for 6295630';
};
test('earth', function(t) {
t.doesNotThrow(function(){
geojsonify( {}, earth );
geojsonify( {}, earth, labelGenerator );
});
t.end();
});
@ -39,197 +42,210 @@ module.exports.tests.earth = function(test, common) {
module.exports.tests.geojsonify = function(test, common) {
var input = [
{
'_id': 'id1',
'_type': 'layer1',
'source': 'source1',
'source_id': 'source_id_1',
'layer': 'layer1',
'center_point': {
'lat': 51.5337144,
'lon': -0.1069716
},
'name': {
'default': '\'Round Midnight Jazz and Blues Bar'
},
'housenumber': '13',
'street': 'Liverpool Road',
'postalcode': 'N1 0RW',
'country_a': 'GBR',
'country': 'United Kingdom',
'dependency': 'dependency name',
'region': 'Islington',
'region_a': 'ISL',
'macroregion': 'England',
'county': 'Angel',
'localadmin': 'test1',
'locality': 'test2',
'neighbourhood': 'test3',
'category': [
'food',
'nightlife'
]
},
{
'_id': 'id2',
'_type': 'layer2',
'source': 'source2',
'source_id': 'source_id_2',
'layer': 'layer2',
'name': {
'default': 'Blues Cafe'
},
'center_point': {
'lat': '51.517806',
'lon': '-0.101795'
},
'country_a': 'GBR',
'country': 'United Kingdom',
'dependency': 'dependency name',
'region': 'City And County Of The City Of London',
'region_a': 'COL',
'macroregion': 'England',
'county': 'Smithfield',
'localadmin': 'test1',
'locality': 'test2',
'neighbourhood': 'test3'
},
{
'_id': 'node:34633854',
'_type': 'venue',
'source': 'openstreetmap',
'source_id': 'source_id_3',
'layer': 'venue',
'name': {
'default': 'Empire State Building'
},
'center_point': {
'lat': '40.748432',
'lon': '-73.985656'
},
'country_a': 'USA',
'country': 'United States',
'dependency': 'dependency name',
'region': 'New York',
'region_a': 'NY',
'county': 'New York',
'borough': 'Manhattan',
'locality': 'New York',
'neighbourhood': 'Koreatown',
'category': [
'tourism',
'transport'
]
}
];
var expected = {
'type': 'FeatureCollection',
'bbox': [ -73.985656, 40.748432, -0.101795, 51.5337144 ],
'features': [
test('geojsonify(doc)', function(t) {
var input = [
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [
-0.1069716,
51.5337144
]
'_id': 'id1',
'_type': 'layer1',
'source': 'source1',
'source_id': 'source_id_1',
'layer': 'layer1',
'center_point': {
'lat': 51.5337144,
'lon': -0.1069716
},
'properties': {
'id': 'id1',
'gid': 'source1:layer1:id1',
'layer': 'layer1',
'source': 'source1',
'source_id': 'source_id_1',
'label': '\'Round Midnight Jazz and Blues Bar, test2, England, United Kingdom',
'name': '\'Round Midnight Jazz and Blues Bar',
'country_a': 'GBR',
'country': 'United Kingdom',
'dependency': 'dependency name',
'macroregion': 'England',
'region': 'Islington',
'region_a': 'ISL',
'county': 'Angel',
'localadmin': 'test1',
'locality': 'test2',
'neighbourhood': 'test3',
'housenumber': '13',
'street': 'Liverpool Road',
'postalcode': 'N1 0RW',
'category': [
'food',
'nightlife'
]
}
'name': {
'default': '\'Round Midnight Jazz and Blues Bar'
},
'housenumber': '13',
'street': 'Liverpool Road',
'postalcode': 'N1 0RW',
'country_a': 'GBR',
'country': 'United Kingdom',
'dependency': 'dependency name',
'region': 'Islington',
'region_a': 'ISL',
'macroregion': 'England',
'county': 'Angel',
'localadmin': 'test1',
'locality': 'test2',
'neighbourhood': 'test3',
'category': [
'food',
'nightlife'
]
},
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [
-0.101795,
51.517806
]
'_id': 'id2',
'_type': 'layer2',
'source': 'source2',
'source_id': 'source_id_2',
'layer': 'layer2',
'name': {
'default': 'Blues Cafe'
},
'properties': {
'id': 'id2',
'gid': 'source2:layer2:id2',
'layer': 'layer2',
'source': 'source2',
'source_id': 'source_id_2',
'label': 'Blues Cafe, test2, England, United Kingdom',
'name': 'Blues Cafe',
'country_a': 'GBR',
'country': 'United Kingdom',
'dependency': 'dependency name',
'macroregion': 'England',
'region': 'City And County Of The City Of London',
'region_a': 'COL',
'county': 'Smithfield',
'localadmin': 'test1',
'locality': 'test2',
'neighbourhood': 'test3'
}
'center_point': {
'lat': '51.517806',
'lon': '-0.101795'
},
'country_a': 'GBR',
'country': 'United Kingdom',
'dependency': 'dependency name',
'region': 'City And County Of The City Of London',
'region_a': 'COL',
'macroregion': 'England',
'county': 'Smithfield',
'localadmin': 'test1',
'locality': 'test2',
'neighbourhood': 'test3'
},
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [
-73.985656,
40.748432
]
'_id': 'node:34633854',
'_type': 'venue',
'source': 'openstreetmap',
'source_id': 'source_id_3',
'layer': 'venue',
'name': {
'default': 'Empire State Building'
},
'center_point': {
'lat': '40.748432',
'lon': '-73.985656'
},
'country_a': 'USA',
'country': 'United States',
'dependency': 'dependency name',
'region': 'New York',
'region_a': 'NY',
'county': 'New York',
'borough': 'Manhattan',
'locality': 'New York',
'neighbourhood': 'Koreatown',
'category': [
'tourism',
'transport'
]
}
];
var expected = {
'type': 'FeatureCollection',
'bbox': [ -73.985656, 40.748432, -0.101795, 51.5337144 ],
'features': [
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [
-0.1069716,
51.5337144
]
},
'properties': {
'id': 'id1',
'gid': 'source1:layer1:id1',
'layer': 'layer1',
'source': 'source1',
'source_id': 'source_id_1',
'label': 'label for id1',
'name': '\'Round Midnight Jazz and Blues Bar',
'country_a': 'GBR',
'country': 'United Kingdom',
'dependency': 'dependency name',
'macroregion': 'England',
'region': 'Islington',
'region_a': 'ISL',
'county': 'Angel',
'localadmin': 'test1',
'locality': 'test2',
'neighbourhood': 'test3',
'housenumber': '13',
'street': 'Liverpool Road',
'postalcode': 'N1 0RW',
'category': [
'food',
'nightlife'
]
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [
-0.101795,
51.517806
]
},
'properties': {
'id': 'id2',
'gid': 'source2:layer2:id2',
'layer': 'layer2',
'source': 'source2',
'source_id': 'source_id_2',
'label': 'label for id2',
'name': 'Blues Cafe',
'country_a': 'GBR',
'country': 'United Kingdom',
'dependency': 'dependency name',
'macroregion': 'England',
'region': 'City And County Of The City Of London',
'region_a': 'COL',
'county': 'Smithfield',
'localadmin': 'test1',
'locality': 'test2',
'neighbourhood': 'test3'
}
},
'properties': {
'id': 'node:34633854',
'gid': 'openstreetmap:venue:node:34633854',
'layer': 'venue',
'source': 'openstreetmap',
'source_id': 'source_id_3',
'label': 'Empire State Building, Manhattan, New York, NY, USA',
'name': 'Empire State Building',
'country_a': 'USA',
'country': 'United States',
'dependency': 'dependency name',
'region': 'New York',
'region_a': 'NY',
'county': 'New York',
'borough': 'Manhattan',
'locality': 'New York',
'neighbourhood': 'Koreatown',
'category': [
'tourism',
'transport'
]
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [
-73.985656,
40.748432
]
},
'properties': {
'id': 'node:34633854',
'gid': 'openstreetmap:venue:node:34633854',
'layer': 'venue',
'source': 'openstreetmap',
'source_id': 'source_id_3',
'label': 'label for node:34633854',
'name': 'Empire State Building',
'country_a': 'USA',
'country': 'United States',
'dependency': 'dependency name',
'region': 'New York',
'region_a': 'NY',
'county': 'New York',
'borough': 'Manhattan',
'locality': 'New York',
'neighbourhood': 'Koreatown',
'category': [
'tourism',
'transport'
]
}
}
]
};
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';
}
]
};
test('geojsonify(doc)', function(t) {
var json = geojsonify( {categories: 'foo'}, input );
};
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