2024-01-08 19:41:57 +01:00
|
|
|
require("dotenv").config();
|
|
|
|
const debug = require("debug")("soundbirder:api:region");
|
|
|
|
const cache = require("./cache");
|
|
|
|
const axios = require("axios");
|
2022-08-29 06:44:58 +02:00
|
|
|
|
2024-01-08 19:41:57 +01:00
|
|
|
const { lookUp } = require("geojson-places");
|
2022-08-29 06:44:58 +02:00
|
|
|
|
|
|
|
async function getRegion(lat, lon) {
|
2024-01-08 19:41:57 +01:00
|
|
|
// Reverse geocoding to get the region info of Valladolid (Spain)
|
|
|
|
const key = `region-${lat}-${lon}`;
|
|
|
|
try {
|
|
|
|
const cached = await cache.getCached(key);
|
|
|
|
if (cached) {
|
|
|
|
const { region } = cached;
|
|
|
|
return region;
|
2022-08-29 06:44:58 +02:00
|
|
|
}
|
2024-01-08 19:41:57 +01:00
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
const result = lookUp(lat, lon);
|
|
|
|
const region = result.country_a2;
|
|
|
|
cache.cacheResponse(key, {region});
|
|
|
|
return region;
|
2022-08-29 06:44:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getRegionCode({ country_code, country, state, county }) {
|
2024-01-08 19:41:57 +01:00
|
|
|
return eBird.ref.region
|
|
|
|
.list("subnational1", country_code)()
|
|
|
|
.then((states) => {
|
|
|
|
console.log(states);
|
|
|
|
const regionCode = states.find((region) => region.name === state).code;
|
|
|
|
return regionCode;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
throw error;
|
|
|
|
});
|
2022-08-29 06:44:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2024-01-08 19:41:57 +01:00
|
|
|
getRegion,
|
|
|
|
};
|