Created
December 22, 2018 18:31
-
-
Save richardbasile/f669692f7555c9a5c579a7bd0cac4971 to your computer and use it in GitHub Desktop.
Find nearest meteor fall to Whitehouse
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 | |
import os | |
import math | |
key=os.environ['GOOGLE_API_KEY'] | |
address='1600 Pennsylvania Ave NW, Washington, DC 20500' | |
endpoint='https://maps.googleapis.com/maps/api/geocode/json?' | |
response = requests.get(endpoint + 'address=' + address + '&key=' + key) | |
location=response.json()['results'][0]['geometry']['location'] | |
response = requests.get('https://data.nasa.gov/resource/y77d-th95.json') | |
meteors = response.json() | |
# type(meteors) # list | |
# type(meteors[0]) # dictionary | |
def haversine(lat1, lon1, lat2, lon2): | |
lat1 = math.radians(lat1) | |
lon1 = math.radians(lon1) | |
lat2 = math.radians(lat2) | |
lon2 = math.radians(lon2) | |
h = math.sin( (lat2 - lat1) / 2 ) ** 2 + \ | |
math.cos(lat1) * \ | |
math.cos(lat2) * \ | |
math.sin( (lon2 - lon1) / 2 ) ** 2 | |
return 6372.8 * 2 * math.asin(math.sqrt(h)) | |
# lat1=location['lat'] | |
# lon1=location['lng'] | |
# lat2=float(meteors[0]['reclat']) | |
# lon2=float(meteors[0]['reclong']) | |
# distance=haversine(lat1, lon1, lat2, lon2) | |
for meteor in meteors: | |
if not ('reclat' in meteor and 'reclong' in meteor): continue | |
meteor['distance']=haversine( \ | |
location['lat'], \ | |
location['lng'], \ | |
float(meteor['reclat']), \ | |
float(meteor['reclong'])) | |
def sort_key(e): | |
return e.get('distance', math.inf) | |
meteors.sort(key=sort_key) | |
meteors[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment