cleaned rate limiting in osm

main
Inga 🏳‍🌈 7 months ago
parent 50d84576fc
commit b8f9ee0e76
  1. 55
      src/clients/geocoding/osm.ts

@ -29,35 +29,36 @@ export const createOsmClient = (): Geocoder => {
});
return {
geocode: async (query) => {
const result = await queue.add(() => geocoder.geocode(query));
geocode: async (query) =>
queue.add(async () => {
const result = await geocoder.geocode(query);
if (!result.length) {
throw new Error('No results found');
}
if (!result.length) {
throw new Error('No results found');
}
const meanLatitude = mean(
compact(result.map(({ latitude }) => latitude)),
);
const meanLongitude = mean(
compact(result.map(({ longitude }) => longitude)),
);
if (
!result.every(
({ latitude, longitude }) =>
latitude &&
longitude &&
Math.abs(latitude - meanLatitude) < 0.01 &&
Math.abs(longitude - meanLongitude) < 0.01,
)
) {
throw new Error('Ambiguous address');
}
const meanLatitude = mean(
compact(result.map(({ latitude }) => latitude)),
);
const meanLongitude = mean(
compact(result.map(({ longitude }) => longitude)),
);
if (
!result.every(
({ latitude, longitude }) =>
latitude &&
longitude &&
Math.abs(latitude - meanLatitude) < 0.01 &&
Math.abs(longitude - meanLongitude) < 0.01,
)
) {
throw new Error('Ambiguous address');
}
return {
latitude: meanLatitude,
longitude: meanLongitude,
};
},
return {
latitude: meanLatitude,
longitude: meanLongitude,
};
}),
};
};

Loading…
Cancel
Save