add box function

This commit is contained in:
Clement 2024-05-18 14:35:18 +02:00
parent 70fa338f2f
commit 4ffef9be27

View File

@ -1,10 +1,47 @@
import axios from 'axios'; import axios from 'axios'
import express from "express" import express from "express"
import * as dotenv from "dotenv"; import * as dotenv from "dotenv"
dotenv.config({path: '../../.env'}) dotenv.config({path: '../../.env'})
const key = process.env.OPEN_TRIP_MAPS_KEY 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. * make GET request to the OTM for radius search.
* @param {string} lon Longitude of radius center point * @param {string} lon Longitude of radius center point
@ -27,10 +64,10 @@ async function callRadius(lon: string, lat: string, radius = '1000') {
} }
try { try {
const { data } = await axios.request(optionsDrink); const { data } = await axios.request(optionsDrink)
return data return data
} catch (error) { } catch (error) {
console.error(error); console.error(error)
} }
} }
@ -48,13 +85,13 @@ async function callCity(name:string) {
apikey: key apikey: key
}, },
headers: {'Content-Type': 'application/json'} headers: {'Content-Type': 'application/json'}
}; }
try { try {
const { data } = await axios.request(optionsCity); const { data } = await axios.request(optionsCity)
return data return data
} catch (error) { } catch (error) {
console.error(error); console.error(error)
} }
} }
@ -71,13 +108,13 @@ async function callId(id:string) {
apikey: key apikey: key
}, },
headers: {'Content-Type': 'application/json'} headers: {'Content-Type': 'application/json'}
}; }
try { try {
const { data } = await axios.request(optionsId); const { data } = await axios.request(optionsId)
return data return data
} catch (error) { } 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 const lat = req.query["lat"] as string
let radius = req.query["radius"] let radius = req.query["radius"]
if(!lon || !lat){ if(!lon || !lat){
res.status(400).send("Missing Argument name") res.status(400).send("Missing Argument")
return return
} }
if(!radius){ if(!radius){
@ -135,5 +172,22 @@ export async function getPoiId(req: express.Request, res: express.Response){
res.send( await callId( id as string)) 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 //TODO: fair une route ou l'on donne 2 coordonée