-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
91 lines (70 loc) · 2.25 KB
/
main.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
import json
import os
import requests
from dotenv import load_dotenv
from geopy import distance
import folium
def fetch_coordinates(apikey, address):
base_url = "https://geocode-maps.yandex.ru/1.x"
response = requests.get(base_url, params={
"geocode": address,
"apikey": apikey,
"format": "json",
})
response.raise_for_status()
found_places = response.json()['response']['GeoObjectCollection']['featureMember']
if not found_places:
return None
most_relevant = found_places[0]
lon, lat = most_relevant['GeoObject']['Point']['pos'].split(" ")
return lon, lat
def read_json(path):
with open(path, "r", encoding="CP1251") as file:
return json.loads(file.read())
def get_coffee(coffee_shops, coords):
struct = list()
for shop in coffee_shops:
dist = distance.distance(
coords,
[shop["Latitude_WGS84"], shop["Longitude_WGS84"]]
).km
struct.append(
{
"distance": dist,
"latitude": shop["Latitude_WGS84"],
"longitude": shop["Longitude_WGS84"],
"name": shop["Name"]
}
)
return struct
def get_dist_coffee(shop):
return shop["distance"]
def create_map(map, coords, coffee_shops):
folium.Marker(
location=coords,
tooltip="Вы тут)",
popup="Да да, вы тут))",
icon=folium.Icon(color="red"),
).add_to(map)
for shop in coffee_shops:
folium.Marker(
location=[shop["latitude"], shop["longitude"]],
tooltip=shop["name"],
popup=f"По прямой {shop['distance']}км",
icon=folium.Icon(color="blue"),
).add_to(map)
def main():
load_dotenv()
apikey = os.environ["API_MAP"]
my_place = input("Где вы примерно?")
coords = fetch_coordinates(apikey, my_place)
coords = list(coords)
coords[0], coords[1] = coords[1], coords[0]
coffee_shops = read_json("coffee.json")
coffee_shops = get_coffee(coffee_shops, coords)
coffee_shops.sort(key=get_dist_coffee)
map = folium.Map(coords)
create_map(map, coords, coffee_shops[:5])
map.save("index.html")
if __name__ == '__main__':
main()