первое решённое задание

This commit is contained in:
2023-01-30 18:40:03 +07:00
parent 070001f4b2
commit 8e144b01e5
2 changed files with 15 additions and 45 deletions
+12 -43
View File
@@ -1,52 +1,21 @@
import sys
from io import BytesIO
from get_a_useless_value import get_a_useless_value
import requests
from PIL import Image
# Пусть наше приложение предполагает запуск:
# python search.py Москва, ул. Ак. Королева, 12
# Тогда запрос к геокодеру формируется следующим образом:
toponym_to_find = " ".join(sys.argv[1:])
api_server = "http://static-maps.yandex.ru/1.x/"
geocoder_api_server = "http://geocode-maps.yandex.ru/1.x/"
lon = "37.530887"
lat = "55.703118"
delta = "0.002"
geocoder_params = {
"apikey": "40d1649f-0493-4b70-98ba-98533de7710b",
"geocode": toponym_to_find,
"format": "json"}
response = requests.get(geocoder_api_server, params=geocoder_params)
if not response:
# обработка ошибочной ситуации
pass
# Преобразуем ответ в json-объект
json_response = response.json()
# Получаем первый топоним из ответа геокодера.
toponym = json_response["response"]["GeoObjectCollection"][
"featureMember"][0]["GeoObject"]
# Координаты центра топонима:
toponym_coodrinates = toponym["Point"]["pos"]
# Долгота и широта:
vrem = []
for i in toponym_coodrinates.split(" "):
vrem.append(float(i))
point = vrem
org_point = "{0},{1}".format(point[0], point[1])
toponym_longitude, toponym_lattitude = toponym_coodrinates.split(" ")
# Собираем параметры для запроса к StaticMapsAPI:
map_params = {
"ll": ",".join([toponym_longitude, toponym_lattitude]),
"spn": ",".join(get_a_useless_value(toponym["boundedBy"]["Envelope"])),
"l": "map",
"pt": "{0},pm2dgl".format(org_point)
params = {
"ll": ",".join([lon, lat]),
"spn": ",".join([delta, delta]),
"l": "map"
}
map_api_server = "http://static-maps.yandex.ru/1.x/"
# ... и выполняем запрос
response = requests.get(map_api_server, params=map_params)
Image.open(BytesIO(
response.content)).show()
def get_img_map():
response = requests.get(api_server, params=params)
Image.open(BytesIO(
response.content)).save("image.png")