Skip to content

Commit

Permalink
Merge pull request #4168 from mozilla/mntor-2913
Browse files Browse the repository at this point in the history
Include locations that have a population of zero on GeoNames (MNTOR-2913)
  • Loading branch information
mansaj committed Feb 7, 2024
2 parents 6e72b75 + e3961fc commit 1f6bc47
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 30 deletions.
2 changes: 1 addition & 1 deletion locationAutocompleteData.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions src/app/api/v1/location-autocomplete/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ function getLocationsByQuery(searchQuery: string) {
}

const locationNames = locationData.data.map((location: RelevantLocation) => {
const { name, stateCode, countryCode, alternateNames } = location;
const alternateNamesJoined = alternateNames ? alternateNames.join(" ") : "";
const { n, s, a } = location;
const alternateNamesJoined = a ? a.join(" ") : "";
const countryCode = "USA";

return `${name} ${stateCode} ${countryCode} ${alternateNamesJoined}`;
return `${n} ${s} ${countryCode} ${alternateNamesJoined}`;
});

// For search options see: https://github.com/leeoniya/uFuzzy#options
Expand Down Expand Up @@ -79,7 +80,7 @@ function getLocationsByQuery(searchQuery: string) {
.slice(0, locationSplitIndex)
.sort(
(a: RelevantLocation, b: RelevantLocation) =>
Number(b.population) - Number(a.population),
Number(b.p ?? 0) - Number(a.p ?? 0),
),
...resultsOrdered.slice(locationSplitIndex + 1),
];
Expand Down
15 changes: 7 additions & 8 deletions src/app/api/v1/location-autocomplete/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -834,15 +834,14 @@ export type LocationData = [
LocationModificationDate,
];

// NOTE: The location JSON uses short keys and
// optional properties to reduce filesize.
export interface RelevantLocation {
id: string;
name: string;
stateCode: string;
countryCode: string;
featureClass: string;
featureCode: string;
population: string;
alternateNames?: Array<string>;
id: string; // GeoName location ID
n: string; // location name
s: string; // state code
p?: string; // population
a?: Array<string>; // alternate location names
}

export interface RelevantLocationAlternate {
Expand Down
4 changes: 2 additions & 2 deletions src/app/components/client/LocationAutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export function getDetailsFromLocationString(locationString: string) {
}

function getLocationString(location: RelevantLocation) {
const { name, stateCode, countryCode } = location;
return `${name}, ${stateCode}, ${countryCode}`;
const { n: name, s: stateCode } = location;
return `${name}, ${stateCode}, USA`;
}

function getLocationStringByKey(
Expand Down
10 changes: 4 additions & 6 deletions src/app/hooks/__mocks__/locationSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ export const useLocationSuggestions: typeof ogUseLocationSuggestions = () => {
items: [
{
id: "4553433",
name: "Tulsa",
stateCode: "OK",
countryCode: "USA",
n: "Tulsa",
s: "OK",
},
{
id: "5318313",
name: "Tucson",
stateCode: "AZ",
countryCode: "USA",
n: "Tucson",
s: "AZ",
},
] as RelevantLocation[],
setFilterText: mockSetFilterText,
Expand Down
18 changes: 9 additions & 9 deletions src/scripts/uploadAutoCompleteLocations.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,8 @@ try {
const isPopulatedPlaceOfInterest =
featureClass === allowedFeatureClass &&
allowedFeatureCodes.includes(featureCode);
const hasPopulation = Number(population) !== 0;

if (isPopulatedPlaceOfInterest && hasPopulation) {
if (isPopulatedPlaceOfInterest) {
const alternateNames = parsedAlternateNames.filter(
({ alternateOf, name: alternateName }) =>
alternateOf === geonameId && alternateName !== name,
Expand All @@ -259,18 +258,19 @@ try {
return alternateName.name;
});

// NOTE: Using short keys and only including entries when available
// keeps the resulting JSON significantly smaller.
relevantLocations.push({
id: geonameId,
// switch names if an alternate name is the preferred location name
name: preferredName ? preferredName.name : name,
stateCode: admin1Code,
countryCode: "USA",
featureClass,
featureCode,
population,
n: preferredName ? preferredName.name : name,
s: admin1Code,
...(Number(population) > 0 && {
p: population,
}),
...(alternateNames &&
alternateNames.length > 0 && {
alternateNames: alternateNamesFinal,
a: alternateNamesFinal,
}),
});
}
Expand Down

0 comments on commit 1f6bc47

Please sign in to comment.