Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2020
1,400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. import sys
  2. from io import BytesIO
  3. # Этот класс поможет нам сделать картинку из потока байт
  4.  
  5. import requests
  6. from PIL import Image
  7.  
  8. # Пусть наше приложение предполагает запуск:
  9. # python search.py Москва, ул. Ак. Королева, 12
  10. # Тогда запрос к геокодеру формируется следующим образом:
  11. toponym_to_find = " ".join(sys.argv[1:])
  12.  
  13. geocoder_api_server = "http://geocode-maps.yandex.ru/1.x/"
  14.  
  15. geocoder_params = {
  16. "apikey": "40d1649f-0493-4b70-98ba-98533de7710b",
  17. "geocode": toponym_to_find,
  18. "format": "json"}
  19.  
  20. response = requests.get(geocoder_api_server, params=geocoder_params)
  21.  
  22. if not response:
  23. # обработка ошибочной ситуации
  24. pass
  25.  
  26. # Преобразуем ответ в json-объект
  27. json_response = response.json()
  28. # Получаем первый топоним из ответа геокодера.
  29. toponym = json_response["response"]["GeoObjectCollection"][
  30. "featureMember"][0]["GeoObject"]
  31. # Координаты центра топонима:
  32. toponym_coodrinates = toponym["Point"]["pos"]
  33. # Долгота и широта:
  34. toponym_longitude, toponym_lattitude = toponym_coodrinates.split(" ")
  35.  
  36. delta = "0.005"
  37.  
  38. # Собираем параметры для запроса к StaticMapsAPI:
  39. map_params = {
  40. "ll": ",".join([toponym_longitude, toponym_lattitude]),
  41. "spn": ",".join([delta, delta]),
  42. "l": "map"
  43. }
  44.  
  45. map_api_server = "http://static-maps.yandex.ru/1.x/"
  46.  
  47.  
  48. # Ближайшая аптека
  49. search_api_server = "https://search-maps.yandex.ru/v1/"
  50. api_key = "dda3ddba-c9ea-4ead-9010-f43fbc15c6e3"
  51.  
  52. address_ll = "37.588392,55.734036"
  53. address_ll = ",".join(toponym["Point"]["pos"].split())
  54.  
  55. search_params = {
  56. "apikey": api_key,
  57. "text": "аптека",
  58. "lang": "ru_RU",
  59. "ll": address_ll,
  60. "type": "biz"
  61. }
  62.  
  63. response = requests.get(search_api_server, params=search_params)
  64. if not response:
  65. #...
  66. pass
  67.  
  68. json_response = response.json()
  69.  
  70. # Получаем первую найденную организацию.
  71. organization = json_response["features"][0]
  72. # Название организации.
  73. org_name = organization["properties"]["CompanyMetaData"]["name"]
  74. # Адрес организации.
  75. org_address = organization["properties"]["CompanyMetaData"]["address"]
  76.  
  77. # Получаем координаты ответа.
  78. point = organization["geometry"]["coordinates"]
  79. org_point = "{0},{1}".format(point[0], point[1])
  80. delta = "0.003"
  81.  
  82. # Собираем параметры для запроса к StaticMapsAPI:
  83. map_params = {
  84. # позиционируем карту центром на наш исходный адрес
  85. "ll": address_ll,
  86. "spn": ",".join([delta, delta]),
  87. "l": "map",
  88. # добавим точку, чтобы указать найденную аптеку
  89. "pt": "{0},pm2dgl".format(org_point)
  90. }
  91.  
  92. map_api_server = "http://static-maps.yandex.ru/1.x/"
  93. # ... и выполняем запрос
  94. response = requests.get(map_api_server, params=map_params)
  95.  
  96. print(address_ll)
  97. print(org_point)
  98. print(org_name)
  99.  
  100. Image.open(BytesIO(
  101. response.content)).show()
  102. # Создадим картинку
  103. # и тут же ее покажем встроенным просмотрщиком операционной системы
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement