Browse Source

filter out results and hierarchies that don't match boundary.country

pull/850/head
Stephen Hess 8 years ago
parent
commit
a6f6d3d6cd
  1. 42
      controller/placeholder.js
  2. 6
      test/unit/controller/placeholder.js

42
controller/placeholder.js

@ -23,7 +23,19 @@ function hasName(result) {
return !_.isEmpty(_.trim(result.name)); return !_.isEmpty(_.trim(result.name));
} }
function synthesizeDocs(result) { // return true if the hierarchy does not have a country.abbr
// OR hierarchy country.abbr matches boundary.country
function matchesBoundaryCountry(boundaryCountry, hierarchy) {
return !boundaryCountry || _.get(hierarchy, 'country.abbr') === boundaryCountry;
}
// return true if the result does not have a lineage
// OR at least one lineage matches the requested boundary.country
function atLeastOneLineageMatchesBoundaryCountry(boundaryCountry, result) {
return !result.lineage || result.lineage.some(_.curry(matchesBoundaryCountry)(boundaryCountry));
}
function synthesizeDocs(boundaryCountry, result) {
const doc = new Document('whosonfirst', result.placetype, result.id.toString()); const doc = new Document('whosonfirst', result.placetype, result.id.toString());
doc.setName('default', result.name); doc.setName('default', result.name);
@ -51,12 +63,11 @@ function synthesizeDocs(result) {
logger.error(`could not parse bbox for id ${result.id}: ${result.geom.bbox}`); logger.error(`could not parse bbox for id ${result.id}: ${result.geom.bbox}`);
} }
if (_.isEmpty(result.lineage)) { _.defaultTo(result.lineage, [])
// there are no hierarchies so just return what's been assembled so far // remove all lineages that don't match an explicit boundary.country
return buildESDoc(doc); .filter(_.curry(matchesBoundaryCountry)(boundaryCountry))
} // add all the lineages to the doc
.map((hierarchy) => {
result.lineage.map((hierarchy) => {
Object.keys(hierarchy) Object.keys(hierarchy)
.filter(doc.isSupportedParent) .filter(doc.isSupportedParent)
.filter((placetype) => { return !_.isEmpty(_.trim(hierarchy[placetype].name)); } ) .filter((placetype) => { return !_.isEmpty(_.trim(hierarchy[placetype].name)); } )
@ -105,19 +116,20 @@ function setup(placeholderService, should_execute) {
// filter that passes only documents that match on boundary.country // filter that passes only documents that match on boundary.country
// passed everything if req.clean['boundary.country'] is not found // passed everything if req.clean['boundary.country'] is not found
const matchesBoundaryCountry = (doc) => { const boundaryCountry = _.get(req, ['clean', 'boundary.country']);
return _.includes(doc.parent.country_a, req.clean['boundary.country']); const boundaryCountryFilter = !!boundaryCountry ?
}; _.curry(atLeastOneLineageMatchesBoundaryCountry)(boundaryCountry) : _.constant(true);
const countryFilter = _.has(req, ['clean', 'boundary.country']) ?
matchesBoundaryCountry : _.constant(true);
// convert results to ES docs // convert results to ES docs
// boundary.country filter must happen after synthesis since multiple // boundary.country filter must happen after synthesis since multiple
// lineages may produce different country docs // lineages may produce different country docs
res.meta = {}; res.meta = {};
res.data = _.flatten( res.data = results
results.filter(hasName).filter(layersFilter).map(synthesizeDocs)) .filter(hasName)
.filter(countryFilter); .filter(layersFilter)
// filter out results that don't match on any lineage country
.filter(boundaryCountryFilter)
.map(_.curry(synthesizeDocs)(boundaryCountry));
const messageParts = [ const messageParts = [
'[controller:placeholder]', '[controller:placeholder]',

6
test/unit/controller/placeholder.js

@ -747,9 +747,9 @@ module.exports.tests.result_filtering = (test, common) => {
'default': 'name 1' 'default': 'name 1'
}, },
parent: { parent: {
country: ['country name 1', 'country name 2'], country: ['country name 1'],
country_id: ['1', '2'], country_id: ['1'],
country_a: ['ABC', 'DEF'] country_a: ['ABC']
} }
}, },
{ {

Loading…
Cancel
Save