soundbirder/controllers/region.js

42 lines
1.0 KiB
JavaScript
Raw Normal View History

require("dotenv").config();
const debug = require("debug")("soundbirder:api:region");
const cache = require("./cache");
const axios = require("axios");
const { lookUp } = require("geojson-places");
async function getRegion(lat, lon) {
// 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;
}
} catch (error) {
throw error;
}
const result = lookUp(lat, lon);
const region = result.country_a2;
cache.cacheResponse(key, {region});
return region;
}
function getRegionCode({ country_code, country, state, county }) {
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;
});
}
module.exports = {
getRegion,
};