Created
May 3, 2020 22:29
-
-
Save LevBravE/3cb8f7435aa181301a589fd507e266a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from math import sin, cos, sqrt, atan2, radians | |
def get_coordinates(city): | |
url = "https://geocode-maps.yandex.ru/1.x/" | |
params = { | |
'geocode': city, | |
'format': 'json' | |
} | |
response = requests.get(url, params) | |
json = response.json() | |
point_str = json['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['Point']['pos'] | |
point_array = [float(x) for x in point_str.split(' ')] | |
return point_array | |
def get_country(city): | |
url = "https://geocode-maps.yandex.ru/1.x/" | |
params = { | |
'geocode': city, | |
'format': 'json' | |
} | |
response = requests.get(url, params) | |
json = response.json() | |
return json['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['metaDataProperty']['GeocoderMetaData']['AddressDetails']['Country']['CountryName'] | |
def get_distance(p1, p2): | |
R = 6373.0 | |
lon1 = radians(p1[0]) | |
lat1 = radians(p1[1]) | |
lon2 = radians(p2[0]) | |
lat2 = radians(p2[1]) | |
dlon = lon2 - lon1 | |
dlat = lat2 - lat1 | |
a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2 | |
c = 2 * atan2(sqrt(a), sqrt(1 - a)) | |
distance = R * c | |
return distance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment