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

7
middleware/geocodeJSON.js

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

430
test/unit/helper/geojsonify.js

@ -5,32 +5,35 @@ module.exports.tests = {};
module.exports.tests.interface = function(test, common) { module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) { test('valid interface', function(t) {
t.equal(typeof geojsonify, 'function', 'search is a function'); t.equal(typeof geojsonify, 'function', 'geojsonify is a function');
t.equal(geojsonify.length, 2, 'accepts x arguments'); t.equal(geojsonify.length, 3, 'accepts x arguments');
t.end(); t.end();
}); });
}; };
// ref: https://github.com/pelias/pelias/issues/84 // ref: https://github.com/pelias/pelias/issues/84
module.exports.tests.earth = function(test, common) { 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 = [{ var labelGenerator = function(doc) {
'_type': 'geoname', return 'label for 6295630';
'_id': '6295630', };
'source': 'whosonfirst',
'layer': 'continent',
'name': {
'default': 'Earth'
},
'center_point': {
'lon': 0,
'lat': 0
}
}];
test('earth', function(t) {
t.doesNotThrow(function(){ t.doesNotThrow(function(){
geojsonify( {}, earth ); geojsonify( {}, earth, labelGenerator );
}); });
t.end(); t.end();
}); });
@ -39,197 +42,210 @@ module.exports.tests.earth = function(test, common) {
module.exports.tests.geojsonify = function(test, common) { module.exports.tests.geojsonify = function(test, common) {
var input = [ test('geojsonify(doc)', function(t) {
{ 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': [
{ {
'type': 'Feature', '_id': 'id1',
'geometry': { '_type': 'layer1',
'type': 'Point', 'source': 'source1',
'coordinates': [ 'source_id': 'source_id_1',
-0.1069716, 'layer': 'layer1',
51.5337144 'center_point': {
] 'lat': 51.5337144,
'lon': -0.1069716
}, },
'properties': { 'name': {
'id': 'id1', 'default': '\'Round Midnight Jazz and Blues Bar'
'gid': 'source1:layer1:id1', },
'layer': 'layer1', 'housenumber': '13',
'source': 'source1', 'street': 'Liverpool Road',
'source_id': 'source_id_1', 'postalcode': 'N1 0RW',
'label': '\'Round Midnight Jazz and Blues Bar, test2, England, United Kingdom', 'country_a': 'GBR',
'name': '\'Round Midnight Jazz and Blues Bar', 'country': 'United Kingdom',
'country_a': 'GBR', 'dependency': 'dependency name',
'country': 'United Kingdom', 'region': 'Islington',
'dependency': 'dependency name', 'region_a': 'ISL',
'macroregion': 'England', 'macroregion': 'England',
'region': 'Islington', 'county': 'Angel',
'region_a': 'ISL', 'localadmin': 'test1',
'county': 'Angel', 'locality': 'test2',
'localadmin': 'test1', 'neighbourhood': 'test3',
'locality': 'test2', 'category': [
'neighbourhood': 'test3', 'food',
'housenumber': '13', 'nightlife'
'street': 'Liverpool Road', ]
'postalcode': 'N1 0RW',
'category': [
'food',
'nightlife'
]
}
}, },
{ {
'type': 'Feature', '_id': 'id2',
'geometry': { '_type': 'layer2',
'type': 'Point', 'source': 'source2',
'coordinates': [ 'source_id': 'source_id_2',
-0.101795, 'layer': 'layer2',
51.517806 'name': {
] 'default': 'Blues Cafe'
}, },
'properties': { 'center_point': {
'id': 'id2', 'lat': '51.517806',
'gid': 'source2:layer2:id2', 'lon': '-0.101795'
'layer': 'layer2', },
'source': 'source2', 'country_a': 'GBR',
'source_id': 'source_id_2', 'country': 'United Kingdom',
'label': 'Blues Cafe, test2, England, United Kingdom', 'dependency': 'dependency name',
'name': 'Blues Cafe', 'region': 'City And County Of The City Of London',
'country_a': 'GBR', 'region_a': 'COL',
'country': 'United Kingdom', 'macroregion': 'England',
'dependency': 'dependency name', 'county': 'Smithfield',
'macroregion': 'England', 'localadmin': 'test1',
'region': 'City And County Of The City Of London', 'locality': 'test2',
'region_a': 'COL', 'neighbourhood': 'test3'
'county': 'Smithfield',
'localadmin': 'test1',
'locality': 'test2',
'neighbourhood': 'test3'
}
}, },
{ {
'type': 'Feature', '_id': 'node:34633854',
'geometry': { '_type': 'venue',
'type': 'Point', 'source': 'openstreetmap',
'coordinates': [ 'source_id': 'source_id_3',
-73.985656, 'layer': 'venue',
40.748432 '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', 'type': 'Feature',
'gid': 'openstreetmap:venue:node:34633854', 'geometry': {
'layer': 'venue', 'type': 'Point',
'source': 'openstreetmap', 'coordinates': [
'source_id': 'source_id_3', -73.985656,
'label': 'Empire State Building, Manhattan, New York, NY, USA', 40.748432
'name': 'Empire State Building', ]
'country_a': 'USA', },
'country': 'United States', 'properties': {
'dependency': 'dependency name', 'id': 'node:34633854',
'region': 'New York', 'gid': 'openstreetmap:venue:node:34633854',
'region_a': 'NY', 'layer': 'venue',
'county': 'New York', 'source': 'openstreetmap',
'borough': 'Manhattan', 'source_id': 'source_id_3',
'locality': 'New York', 'label': 'label for node:34633854',
'neighbourhood': 'Koreatown', 'name': 'Empire State Building',
'category': [ 'country_a': 'USA',
'tourism', 'country': 'United States',
'transport' '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.deepEqual(json, expected, 'all docs mapped');
t.end(); t.end();
@ -374,7 +390,7 @@ module.exports.tests.geojsonify = function(test, common) {
'localadmin_gid': '404521211', 'localadmin_gid': '404521211',
'locality': 'New York', 'locality': 'New York',
'locality_gid': '85977539', '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], 'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588],
'geometry': { '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.deepEqual(json, expected, 'all wanted properties exposed');
t.end(); t.end();
@ -434,7 +456,7 @@ module.exports.tests.categories = function (test, common) {
'source_id': '85816607', 'source_id': '85816607',
'name': 'East New York', 'name': 'East New York',
'category': ['government'], 'category': ['government'],
'label': 'East New York' 'label': 'label for 85816607'
}, },
'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588], 'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588],
'geometry': { '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.deepEqual(json, expected, 'all wanted properties exposed');
t.end(); t.end();

Loading…
Cancel
Save