-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeocoder.py
More file actions
33 lines (29 loc) · 1.78 KB
/
geocoder.py
File metadata and controls
33 lines (29 loc) · 1.78 KB
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
import requests
from config import geocoder_api
from staticmaps import static_maps
def geocode(address):
server_address = 'http://geocode-maps.yandex.ru/1.x/?'
geocoder_request = f'{server_address}apikey={geocoder_api}&geocode={address}&format=json'
response = requests.get(geocoder_request)
try:
if response:
json_response = response.json()
toponym = json_response["response"]["GeoObjectCollection"]["featureMember"][0]["GeoObject"]
toponym_address = toponym["metaDataProperty"]["GeocoderMetaData"]["text"]
toponym_coordinates = toponym["Point"]["pos"]
address_components = toponym["metaDataProperty"]["GeocoderMetaData"]["Address"]["Components"]
country = next((comp["name"] for comp in address_components if comp["kind"] == "country"), None)
province = next((comp["name"] for comp in address_components if comp["kind"] == "province"), None)
area = next((comp["name"] for comp in address_components if comp["kind"] == "area"), None)
locality = next((comp["name"] for comp in address_components if comp["kind"] == "locality"), None)
street = next((comp["name"] for comp in address_components if comp["kind"] == "street"), None)
house = next((comp["name"] for comp in address_components if comp["kind"] == "house"), None)
coords = toponym_coordinates.split()
coords1 = coords[0]
coords2 = coords[1]
# Получаем изображение карты
map_image = static_maps(coords1, coords2)
return toponym_address, toponym_coordinates, map_image, country, province, area, locality, street, house
except Exception as e:
print(f"Ошибка: {e}")
return None, None, None