Last active
March 3, 2023 09:45
-
-
Save Jxck-S/f2a89a1b8c1dc9a9a3f32bdf8400b439 to your computer and use it in GitHub Desktop.
Lookup nearest airport from coordinates using Google Maps. Not a very good way much better options and better data.
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
#Scraped this for airports.dat from https://openflights.org/data.html use haversine formula to calculate distance from each airport, lowest is closest. | |
#Or use https://rapidapi.com/sujayvsarma/api/ourairport-data-search?endpoint=apiendpoint_157e1886-2798-46cf-83e6-b71c2cb76273 from https://ourairports.com/data/ | |
import googlemaps | |
import json | |
import configparser | |
config = configparser.ConfigParser() | |
config.read('config.ini') | |
def getAirport(rank_by, latitude, longitude): | |
nrby_key = config.get('GOOGLE', 'NEARBYSEARCHKEY') | |
gmaps = googlemaps.Client(key=nrby_key) | |
if rank_by == "distance": | |
airports_near = gmaps.places_nearby(location=(latitude, longitude), rank_by=("distance"), type="airport") | |
elif rank_by == "prominence": | |
#Rankby defaults to prominence puting prominence will cause the request to fail | |
airports_near = gmaps.places_nearby(location=(latitude, longitude), radius=16093, type="airport") | |
airport_names= [] | |
for result in airports_near['results']: | |
airport_names.append(result['name']) | |
print("First Airport", airport_names[0]) | |
return airport_names[0], rank_by |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want to write this similar code in R. Can I get help please?