-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
61 lines (36 loc) · 1.57 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import requests
from app import request
from json import loads
import os
from geopy.geocoders import Nominatim
from cs50 import SQL
from flask import redirect
# Configure CS50 Library to use SQLite database
db = SQL("sqlite:///project.db")
# Find the lat and long with the path
def path(origin, destination):
# Convert the location to longitude and latitude (GEOPY)
geolocator = Nominatim(user_agent="helpers.py")
startLoc = geolocator.geocode(origin)
endLoc = geolocator.geocode(destination)
if startLoc is None or endLoc is None:
return -1
start_latitude = startLoc.latitude
start_longitude = startLoc.longitude
end_latitude = endLoc.latitude
end_longitude = endLoc.longitude
# OPEN ROUTE SERVICE
headers = { 'Accept': 'application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8', }
API_KEY = os.environ.get("API_KEY")
call = requests.get(f'https://api.openrouteservice.org/v2/directions/driving-car?api_key={API_KEY}&start={start_longitude}, {start_latitude}&end={end_longitude},{end_latitude}', headers=headers)
if call.status_code != 200:
return -1
# Convert the string format of the json to the dict
dict_geojson = loads(call.text)
# Get the values of the key - coordinates
coordinates = dict_geojson['features'][0]['geometry']['coordinates']
# Return the coordinates
return coordinates
def check():
CONN = db.execute('SELECT ask.askuserid, helpers.helperuserid FROM ask,helpers WHERE ask.geo IN (SELECT helpers.geo FROM helpers)')
return CONN