-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaps.py
107 lines (95 loc) · 4.23 KB
/
maps.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# file for threaded processes from flask
# initializes location and other data for use for
# other features
import io, os
import time
import traceback
from config import apiConfig
import requests
from geopy.geocoders import Nominatim
import logging
from io import BytesIO
from PIL import Image
logger = logging.getLogger()
INIT = (0, 0)
# overseer of all things in this one
class GeoHandler:
location = (0, 0)
def test(self):
return "test passed"
def acq(self, datag):
t = time.time()
loc = self.coordToPlace(datag.to_dict())
logger.info("Time taken to get location data : " + str(time.time() - t))
logger.info("Location data is: " + str(loc))
# find the smallest possible unit that would have some news
# Would likely have to scrap this due to api ratelimits
# Replace with recommendations for bikimg locations, and setting paths for those?
def coordToPlace(self, usedict):
payload = ""
blacklist = ['building', 'house', 'house_number', 'road', 'quarter']
geolocator = Nominatim(user_agent="BikeTes")
if ('data[lat]' in usedict.keys() and 'data[lng]' in usedict.keys()):
payload = usedict['data[lat]'] + "," + usedict['data[lng]']
self.location = (float(usedict['data[lat]']), float(usedict['data[lng]']))
loc = geolocator.reverse(payload)
addr = loc.raw['address']
arr = list(addr.keys())
# naive filtration
if (all(x != arr[1] for x in blacklist)):
return addr[arr[1]]
elif (all(x != arr[2] for x in blacklist)):
return addr[arr[2]]
elif (all(x != arr[3] for x in blacklist)):
return addr[arr[3]]
else:
return addr[arr[4]]
def placeDetails(self, place_id):
url = apiConfig.urls['place_details'].replace('{_key}', apiConfig.maps_key).replace('{_pid}', place_id)
logger.info("placeDetails() suggestions url: " + str(url))
try:
response = requests.get(url)
data = response.json()
# logger.info("placeDetails() suggestions response: " + str(data))
return data
except Exception as e:
logger.error("Error in placeDetails(): " + str(e) + traceback.format_exc())
def nearbyPlaces(self, lat, lng):
url = apiConfig.urls['nearby_places'].replace('${lt}', str(lat)).replace('${ln}', str(lng)).replace('{_key}',
apiConfig.maps_key)
logger.info("nearbySearch() suggestions url: " + str(url))
try:
response = requests.get(url)
data = response.json()
# logger.info("nearbySearch() suggestions response: " + str(data))
return data
except Exception as e:
logger.error("Error in nearbySearch(): " + str(e) + traceback.format_exc())
# implement nearby biking suggestions
def nameSearch(self, name):
url = apiConfig.urls['text_search'].replace('${_name}', str(name)).replace('{_key}', apiConfig.maps_key)
logger.info("nameSearch() suggestions url: " + str(url))
try:
response = requests.get(url)
data = response.json()
# logger.info("nearbySearch() suggestions response: " + str(data))
return data
except Exception as e:
logger.error("Error in nearbySearch(): " + str(e) + traceback.format_exc())
def placeImages(self, pref):
url = apiConfig.urls['getImages'].replace('{_param}', str(pref)).replace('{_key}', apiConfig.maps_key)
logger.info("placeImages() url: " + str(url))
try:
response = requests.get(url)
data = response.content
logger.info("placeImages() suggestions response: " + str(data))
path = "static/images/" + str(pref) + ".jpg"
try:
if not os.path.exists(path):
with open(path, "wb") as f:
f.write(data)
except Exception as e:
logger.error("Does not become an image" + str(e))
return data
except Exception as e:
logger.error("Error in placeImages(): " + str(e) + traceback.format_exc())