From 788a8c8069c85187ecea3b7f7ed48b44b877bed5 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Fri, 2 Jun 2017 16:55:17 -0400 Subject: [PATCH] refactored for lodash because its siren song is so sweet --- controller/coarse_reverse.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/controller/coarse_reverse.js b/controller/coarse_reverse.js index 0d64855f..bb974940 100644 --- a/controller/coarse_reverse.js +++ b/controller/coarse_reverse.js @@ -18,6 +18,7 @@ const coarse_granularities = [ // remove non-coarse layers and return what's left (or all if empty) function getEffectiveLayers(requested_layers) { + // remove non-coarse layers const non_coarse_layers_removed = _.without(requested_layers, 'venue', 'address', 'street'); // if resulting array is empty, use all coarse granularities @@ -62,30 +63,32 @@ function synthesizeDoc(results) { const doc = new Document('whosonfirst', most_granular_layer, id.toString()); doc.setName('default', results[most_granular_layer][0].name); - if (results[most_granular_layer][0].hasOwnProperty('centroid')) { + // assign the administrative hierarchy + _.keys(results).forEach((layer) => { + doc.addParent(layer, results[layer][0].name, results[layer][0].id.toString(), results[layer][0].abbr); + }); + + // set centroid if available + if (_.has(results[most_granular_layer][0], 'centroid')) { doc.setCentroid( results[most_granular_layer][0].centroid ); } - if (results[most_granular_layer][0].hasOwnProperty('bounding_box')) { - const parsedBoundingBox = results[most_granular_layer][0].bounding_box.split(',').map(parseFloat); + // set bounding box if available + if (_.has(results[most_granular_layer][0], 'bounding_box')) { + const parsed_bounding_box = results[most_granular_layer][0].bounding_box.split(',').map(parseFloat); doc.setBoundingBox({ upperLeft: { - lat: parsedBoundingBox[3], - lon: parsedBoundingBox[0] + lat: parsed_bounding_box[3], + lon: parsed_bounding_box[0] }, lowerRight: { - lat: parsedBoundingBox[1], - lon: parsedBoundingBox[2] + lat: parsed_bounding_box[1], + lon: parsed_bounding_box[2] } }); } - // assign the administrative hierarchy - Object.keys(results).forEach((layer) => { - doc.addParent(layer, results[layer][0].name, results[layer][0].id.toString(), results[layer][0].abbr); - }); - const esDoc = doc.toESDocument(); esDoc.data._id = esDoc._id; esDoc.data._type = esDoc._type;