From 4ffef9be275307e24b8f42f2300e717e08d7b074 Mon Sep 17 00:00:00 2001 From: Clement Date: Sat, 18 May 2024 14:35:18 +0200 Subject: [PATCH] add box function --- Express/barAndCafe/src/openTripMaps.ts | 78 ++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 12 deletions(-) diff --git a/Express/barAndCafe/src/openTripMaps.ts b/Express/barAndCafe/src/openTripMaps.ts index 2ef91d0..bbedd21 100644 --- a/Express/barAndCafe/src/openTripMaps.ts +++ b/Express/barAndCafe/src/openTripMaps.ts @@ -1,10 +1,47 @@ -import axios from 'axios'; +import axios from 'axios' import express from "express" -import * as dotenv from "dotenv"; +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) { + 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, + 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 @@ -27,10 +64,10 @@ async function callRadius(lon: string, lat: string, radius = '1000') { } try { - const { data } = await axios.request(optionsDrink); + const { data } = await axios.request(optionsDrink) return data } catch (error) { - console.error(error); + console.error(error) } } @@ -48,13 +85,13 @@ async function callCity(name:string) { apikey: key }, headers: {'Content-Type': 'application/json'} - }; + } try { - const { data } = await axios.request(optionsCity); + const { data } = await axios.request(optionsCity) return data } catch (error) { - console.error(error); + console.error(error) } } @@ -71,13 +108,13 @@ async function callId(id:string) { apikey: key }, headers: {'Content-Type': 'application/json'} - }; + } try { - const { data } = await axios.request(optionsId); + const { data } = await axios.request(optionsId) return data } catch (error) { - console.error(error); + console.error(error) } } @@ -111,7 +148,7 @@ export async function getRadius(req:express.Request, res: express.Response) { const lat = req.query["lat"] as string let radius = req.query["radius"] if(!lon || !lat){ - res.status(400).send("Missing Argument name") + res.status(400).send("Missing Argument") return } if(!radius){ @@ -135,5 +172,22 @@ export async function getPoiId(req: express.Request, res: express.Response){ res.send( await callId( id as string)) } -//TODO: fair une route ou l'on donne l'id un établicement pour avoir des détailles +/** + * 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 + if(!lon1 || !lat1 || !lon2 || !lat2){ + res.status(400).send("Missing Argument") + return + } + + res.send( await callBox(lon1, lat1, lon2, lat2)) +} + //TODO: fair une route ou l'on donne 2 coordonée