Browse Source

inlined and condensed doc setup

pull/1002/head
Stephen Hess 7 years ago
parent
commit
cd99f87c04
  1. 41
      helper/geojsonify.js
  2. 24
      test/unit/helper/geojsonify.js

41
helper/geojsonify.js

@ -39,41 +39,28 @@ function geojsonifyPlaces( params, docs ){
}
function geojsonifyPlace(params, place) {
const output = {
// setup the base doc
const doc = {
id: place._id,
gid: new Document(place.source, place.layer, place._id).getGid(),
layer: place.layer,
source: place.source,
source_id: place.source_id
source_id: place.source_id,
bounding_box: place.bounding_box,
lat: parseFloat(place.center_point.lat),
lng: parseFloat(place.center_point.lon)
};
if (place.hasOwnProperty('bounding_box')) {
output.bounding_box = place.bounding_box;
// assign name, logging a warning if it doesn't exist
if (_.has(place, 'name.default')) {
doc.name = place.name.default;
} else {
logger.warn(`doc ${doc.gid} does not contain name.default`);
}
addName(place, output);
addDetails(params, place, output);
// map center_point for GeoJSON to work properly
// these should not show up in the final feature properties
output.lat = parseFloat(place.center_point.lat);
output.lng = parseFloat(place.center_point.lon);
addDetails(params, place, doc);
return output;
}
/**
* Validate and add name property
*
* @param {object} src
* @param {object} dst
*/
function addName(src, dst) {
if (_.has(src, 'name.default')) {
dst.name = src.name.default;
} else {
logger.warn(`doc ${dst.gid} does not contain name.default`);
}
return doc;
}
/**
@ -106,6 +93,7 @@ function addBBoxPerFeature(geojson) {
*/
function extractExtentPoints(geodata) {
return geodata.reduce((extentPoints, place) => {
// if there's a bounding_box, use the LL/UR for the extent
if (place.bounding_box) {
extentPoints.push({
lng: place.bounding_box.min_lon,
@ -118,6 +106,7 @@ function extractExtentPoints(geodata) {
}
else {
// otherwise, use the point for the extent
extentPoints.push({
lng: place.lng,
lat: place.lat

24
test/unit/helper/geojsonify.js

@ -591,6 +591,30 @@ module.exports.tests.non_optimal_conditions = (test, common) => {
});
test('no points', t => {
const logger = require('pelias-mock-logger')();
const input = [];
const geojsonify = proxyquire('../../../helper/geojsonify', {
'./geojsonify_place_details': (params, source, dst) => {
t.fail('should not have bee called');
},
'pelias-logger': logger
});
const actual = geojsonify({}, input);
const expected = {
type: 'FeatureCollection',
features: []
};
t.deepEquals(actual, expected);
t.end();
});
};
module.exports.all = (tape, common) => {

Loading…
Cancel
Save