import axios from 'axios' import express from "express" import * as dotenv from "dotenv" dotenv.config({path: '../../.env'}) const key = process.env.OPEN_TRIP_MAPS_KEY /** * make a GET request to the OTM for a rectangle search. * @param {string} lon1 Longitude of the 1st point of the box * @param {string} lat1 Latitude of the 1st point of the box * @param {string} lon2 Longitude of the 2nd point of the box * @param {string} lat2 Latitude of the 2nd point of the box * @returns {FeatureCollection} a list of POIs with their type, id, etc. (cf: [opentripmap](https://dev.opentripmap.org/docs#)) */ async function callBox(lon1:string, lat1:string, lon2: string, lat2: string, rate: string) { const lonMin = Math.min(parseFloat(lon1), parseFloat(lon2)) const lonMax = Math.max(parseFloat(lon1), parseFloat(lon2)) const latMin = Math.min(parseFloat(lat1), parseFloat(lat2)) const latMax = Math.max(parseFloat(lat1), parseFloat(lat2)) const options = { method: 'GET', url: 'https://api.opentripmap.com/0.1/en/places/bbox', params: { lon_min: lonMin, lon_max: lonMax, lat_min: latMin, lat_max: latMax, rate: rate, apikey: key, kinds: 'bars,cafes,pubs,biergartens' }, headers: {'Content-Type': 'application/json'} } try { const { data } = await axios.request(options) return data } catch (error) { console.error(error) } } /** * make GET request to the OTM for radius search. * @param {string} lon Longitude of radius center point * @param {string} lat Latitude du point central du rayon * @param {string} radius search radius size in meters * @returns {FeatureCollection} a list of POIs with their type, id, etc. (cf: [opentripmap](https://dev.opentripmap.org/docs#)) */ async function callRadius(lon: string, lat: string, radius = '1000') { const optionsDrink = { method: 'GET', url: 'https://api.opentripmap.com/0.1/en/places/radius', params: { radius: radius, lon: lon, lat: lat, apikey: key, kinds: 'bars,cafes,pubs,biergartens' }, headers: {'Content-Type': 'application/json'} } try { const { data } = await axios.request(optionsDrink) return data } catch (error) { console.error(error) } } /** * make GET request to the OTM for city info. * @param {string} name Name of a city * @returns {Geoname} info of a search city their name, country, lat, lon, etc. (cf: [opentripmap](https://dev.opentripmap.org/docs#)) */ async function callCity(name:string) { const optionsCity = { method: 'GET', url: 'https://api.opentripmap.com/0.1/en/places/geoname', params: { name: name, apikey: key }, headers: {'Content-Type': 'application/json'} } try { const { data } = await axios.request(optionsCity) return data } catch (error) { console.error(error) } } /** * make GET request to the OTM for city info. * @param {string} name Name of a city * @returns {Geoname} info of a search city their name, country, lat, lon, etc. (cf: [opentripmap](https://dev.opentripmap.org/docs#)) */ async function callId(id:string) { const optionsId = { method: 'GET', url: 'https://api.opentripmap.com/0.1/en/places/xid/' + id, params: { apikey: key }, headers: {'Content-Type': 'application/json'} } try { const { data } = await axios.request(optionsId) return data } catch (error) { console.error(error) } } /** * Handle GET request for city search route ('/otm/city'). * @param {express.Request} req - HTTP Request object. * @param {express.Response} res - HTTP Response object. */ export async function getCity(req: express.Request, res: express.Response) { const cityName = req.query["name"] let radius = req.query["radius"] if(!cityName){ res.status(400).send("Missing Argument name") return } if(!radius){ radius = "1000" } const cityPose = await callCity(cityName as string) res.send( await callRadius(cityPose.lon,cityPose.lat, radius as string)) } /** * Handle GET request for radius search route ('/otm/radius'). * @param {express.Request} req - HTTP Request object. * @param {express.Response} res - HTTP Response object. */ export async function getRadius(req:express.Request, res: express.Response) { const lon = req.query["lon"] as string const lat = req.query["lat"] as string let radius = req.query["radius"] if(!lon || !lat){ res.status(400).send("Missing Argument") return } if(!radius){ radius = "1000" } res.send( await callRadius(lon,lat,radius as string)) } /** * Handle GET request for radius search route ('/otm/radius'). * @param {express.Request} req - HTTP Request object. * @param {express.Response} res - HTTP Response object. */ export async function getPoiId(req: express.Request, res: express.Response){ const id = req.query["id"] as string if(!id){ res.status(400).send("Missing Argument name") return } res.send( await callId( id as string)) } /** * Handle GET request for radius search route ('/otm/box'). * @param {express.Request} req - HTTP Request object. * @param {express.Response} res - HTTP Response object. */ export async function getBox(req:express.Request, res: express.Response) { const lon1 = req.query["lon1"] as string const lat1 = req.query["lat1"] as string const lon2 = req.query["lon2"] as string const lat2 = req.query["lat2"] as string let rate = req.query["rate"] as string if(!lon1 || !lat1 || !lon2 || !lat2){ res.status(400).send("Missing Argument") return } if(!rate){ rate = "1"; } res.send( await callBox(lon1, lat1, lon2, lat2, rate)) }