Browse Source

added helper for removed non-coarse layers

pull/899/head
Stephen Hess 8 years ago
parent
commit
1d4318868b
  1. 17
      controller/coarse_reverse.js

17
controller/coarse_reverse.js

@ -21,6 +21,8 @@ function getMostGranularLayer(results) {
}); });
} }
// removing non-coarse layers could leave effective_layers empty, so it's
// important to check for empty layers here
function hasResultsAtRequestedLayers(results, layers) { function hasResultsAtRequestedLayers(results, layers) {
return _.isEmpty(layers) || !_.isEmpty(_.intersection(layers, Object.keys(results))); return _.isEmpty(layers) || !_.isEmpty(_.intersection(layers, Object.keys(results)));
} }
@ -83,6 +85,15 @@ function setup(service, should_execute) {
return next(); return next();
} }
// because coarse reverse is called when non-coarse reverse didn't return
// anything, don't consider non-coarse layers when filtering
const effective_layers = _.without(req.clean.layers, 'venue', 'address', 'street');
const centroid = {
lat: req.clean['point.lat'],
lon: req.clean['point.lon']
};
service(req, (err, results) => { service(req, (err, results) => {
// if there's an error, log it and bail // if there's an error, log it and bail
if (err) { if (err) {
@ -93,13 +104,13 @@ function setup(service, should_execute) {
// find the finest granularity requested // find the finest granularity requested
const finest_granularity_requested = granularities.findIndex((granularity) => { const finest_granularity_requested = granularities.findIndex((granularity) => {
return req.clean.layers.indexOf(granularity) !== -1; return effective_layers.indexOf(granularity) !== -1;
}); });
logger.info(`[controller:coarse_reverse][queryType:pip][result_count:${Object.keys(results).length}]`); logger.info(`[controller:coarse_reverse][queryType:pip][result_count:${Object.keys(results).length}]`);
// now remove everything from the response that is more granular than the // now remove everything from the response that is more granular than the
// most granular layer requested. that is, if req.clean.layers=['county'], // most granular layer requested. that is, if effective_layers=['county'],
// remove neighbourhoods, localities, and localadmins // remove neighbourhoods, localities, and localadmins
Object.keys(results).forEach((layer) => { Object.keys(results).forEach((layer) => {
if (granularities.indexOf(layer) < finest_granularity_requested) { if (granularities.indexOf(layer) < finest_granularity_requested) {
@ -110,7 +121,7 @@ function setup(service, should_execute) {
res.meta = {}; res.meta = {};
res.data = []; res.data = [];
// synthesize a doc from results if there's a result at the request layer(s) // synthesize a doc from results if there's a result at the request layer(s)
if (hasResultsAtRequestedLayers(results, _.without(req.clean.layers, 'venue', 'address', 'street'))) { if (hasResultsAtRequestedLayers(results, effective_layers)) {
res.data.push(synthesizeDoc(results)); res.data.push(synthesizeDoc(results));
} }

Loading…
Cancel
Save