Skip to content

Commit

Permalink
Merge pull request #1597 from globaldothealth/task-1306-enable-geocod…
Browse files Browse the repository at this point in the history
…e-limittocountry

Add optional limiToCountry param to geocoder
  • Loading branch information
calremmel authored Feb 23, 2021
2 parents 1064662 + 6bc9b58 commit b28edd6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
28 changes: 28 additions & 0 deletions data-serving/data-service/src/controllers/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,9 @@ export class CasesController {
'location.query must be specified to be able to geocode',
);
}

const opts: GeocodeOptions = {};

if (location['limitToResolution']) {
opts.limitToResolution = [];
location['limitToResolution']
Expand All @@ -699,6 +701,13 @@ export class CasesController {
opts.limitToResolution?.push(resolution);
});
}

if (location['limitToCountry']) {
opts.limitToCountry = location['limitToCountry']
.split(',')
.filter((countryCode: string) => countryCode.length === 2);
}

for (const geocoder of this.geocoders) {
const features = await geocoder.geocode(location?.query, opts);
if (features.length === 0) {
Expand Down Expand Up @@ -736,6 +745,25 @@ export class CasesController {
}
}

/*
"location": {
"country": "Costa Rica",
"administrativeAreaLevel1": "Any province in Costa Rica",
"administrativeAreaLevel2": "Any canton in Costa Rica",
"administrativeAreaLevel3": "Any district in Costa Rica",
"place": "Boston Children's Hospital",
"name": "Lyon, Auvergne-Rhône-Alpes, France",
"geoResolution": "Point",
"geometry": {
"latitude": 0,
"longitude": 0
},
"query": "string",
"limitToResolution": "string",
"limitToCountry": string[]
}
*/

// Returns a mongoose query for all cases matching the given search query.
// If count is true, it returns a query for the number of cases matching
// the search query.
Expand Down
1 change: 1 addition & 0 deletions data-serving/data-service/src/geocoding/geocoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface GeocodeOptions {
// If set, will only return features with the given resolutions.
// Otherwise all available resolutions can be returned.
limitToResolution?: Resolution[];
limitToCountry?: string[];
}

// A geocoder can geocode queries into places.
Expand Down
5 changes: 5 additions & 0 deletions data-serving/data-service/src/geocoding/mapbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ export default class MapboxGeocoder {
});
}
}

if (opts?.limitToCountry) {
req.countries = opts?.limitToCountry;
}

return await new Promise<GeocodeResult[]>((resolve, reject) => {
this.limiter.removeTokens(1, async (err, _) => {
if (err) {
Expand Down
6 changes: 6 additions & 0 deletions verification/curator-service/api/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,12 @@ components:
description: >
Comma-separated list of resolutions to restrict geocoding to.
Allowed values are: Point, Admin3, Admin2, Admin1, Country.
limitToCountry:
type: string
description: >
Limit results to one or more countries.
Permitted values are ISO 3166 alpha 2 country codes separated by commas.
required:
- caseReference
- location
Expand Down

0 comments on commit b28edd6

Please sign in to comment.