Skip to content

Commit

Permalink
23 use mapbox location pattern (#24)
Browse files Browse the repository at this point in the history
* WIP update data structure to allow storing all location values

* WIP region working

* wip missing openapi docs

* Working location form

* Update dataservice tests

* Cypress tests update

* Update test_geocoder.py

* Fix eslint and remove console.log

* Fix cypress tests
  • Loading branch information
stanislaw-zakrzewski authored Sep 18, 2023
1 parent 2d0e9b8 commit 7f34891
Show file tree
Hide file tree
Showing 32 changed files with 466 additions and 314 deletions.
14 changes: 13 additions & 1 deletion data-serving/data-service/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,11 @@ components:
country:
type: string
description: name of a country
city:
region:
type: string
district:
type: string
place:
type: string
location:
type: string
Expand Down Expand Up @@ -946,6 +950,14 @@ components:
type: string
description: Third administrative subdivision of a country
example: Any district in Costa Rica
region:
type: string
description: Name of the region this location refers to
example: Lodz Voivodeship
district:
type: string
description: Name of the district this location refers to
example: Lodz County
place:
type: string
description: Name of the place this location refers to
Expand Down
18 changes: 17 additions & 1 deletion data-serving/data-service/src/controllers/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ export class CasesController {
const numCases = Number(req.query.num_cases) || 1;

try {
await this.geocode(req);
this.addGeoResolution(req);
const receivedCase = req.body as CaseDTO;

const c = fillEmpty(new Day0Case(await caseFromDTO(receivedCase)));
Expand Down Expand Up @@ -1066,6 +1066,22 @@ export class CasesController {

req.body['location'] = { ...req.body['location'], ...locationObject };
}

private addGeoResolution(req: Request | any): void {
const location = req.body.location;
if (location.geoResolution) return;
if (location.geometry?.longitude && location.geometry?.latitude) {
req.body.location.geoResolution = 'Point';
} else if (location.place) {
req.body.location.geoResolution = 'Admin3';
} else if (location.district) {
req.body.location.geoResolution = 'Admin2';
} else if (location.region) {
req.body.location.geoResolution = 'Admin1';
} else if (location.country) {
req.body.location.geoResolution = 'Country';
}
}
}

// Returns a mongoose query for all cases matching the given search query.
Expand Down
6 changes: 5 additions & 1 deletion data-serving/data-service/src/geocoding/geocoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export interface GeocodeResult {
administrativeAreaLevel2?: string;
// Third administrative division (cities usually).
administrativeAreaLevel3?: string;
// A precise location, such as an establishment or POI.
// Region, equivalent to mapbox admin1.
region: string | undefined;
// District, equivalent to mapbox admin2.
district: string | undefined;
// A precise location, such as an establishment or POI, equivalent to mapbox admin3.
place: string | undefined;
// Human readable place name.
name: string;
Expand Down
4 changes: 3 additions & 1 deletion data-serving/data-service/src/model/fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"events.dateRecovered",
"location.country",
"location.countryISO3",
"location.city",
"location.region",
"location.district",
"location.place",
"location.location",
"location.query",
"preexistingConditions.previousInfection",
Expand Down
8 changes: 6 additions & 2 deletions data-serving/data-service/src/model/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const locationSchema = new mongoose.Schema(
geoResolution: String,
// Location represents a precise location, such as an establishment or POI.
location: String,
city: String,
region: String,
district: String,
place: String,
query: String,
name: String,
geometry: {
Expand All @@ -31,7 +33,9 @@ export type LocationDocument = mongoose.Document & {
countryISO3: string;
geoResolution: string;
location?: string;
city?: string;
region?: string;
district?: string;
place?: string;
query?: string;
name?: string;
geometry?: {
Expand Down
8 changes: 4 additions & 4 deletions data-serving/data-service/src/util/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export enum SortBy {
Default = 'default',
ConfirmationDate = 'confirmationDate',
Country = 'country',
City = 'city',
Place = 'place',
Location = 'location',
Age = 'age',
Occupation = 'occupation',
Expand Down Expand Up @@ -64,8 +64,8 @@ export const getSortByKeyword = (sortBy: SortBy): string => {
case SortBy.Country:
keyword = 'location.countryISO3';
break;
case SortBy.City:
keyword = 'location.city';
case SortBy.Place:
keyword = 'location.place';
break;
case SortBy.Location:
keyword = 'location.location';
Expand Down Expand Up @@ -251,7 +251,7 @@ function denormalizeLocationFields(
denormalizedData['location.country'] = doc.country || '';
denormalizedData['location.countryISO3'] = doc.countryISO3 || '';
denormalizedData['location.location'] = doc.location || '';
denormalizedData['location.city'] = doc.city || '';
denormalizedData['location.place'] = doc.place || '';
denormalizedData['location.geoResolution'] = doc.geoResolution || '';
denormalizedData['location.geometry.latitude'] =
doc.geometry?.latitude || '';
Expand Down
2 changes: 1 addition & 1 deletion data-serving/data-service/src/util/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const keywords = new Map<string, string>([
['gender', 'demographics.gender'],
['occupation', 'demographics.occupation'],
['country', 'location.countryISO3'],
['city', 'location.city'],
['place', 'location.place'],
['location', 'location.location'],
['outcome', 'events.outcome'],
['caseId', '_id'],
Expand Down
Loading

0 comments on commit 7f34891

Please sign in to comment.