Skip to content

Instantly share code, notes, and snippets.

@Jxck-S
Last active March 3, 2023 09:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jxck-S/f2a89a1b8c1dc9a9a3f32bdf8400b439 to your computer and use it in GitHub Desktop.
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.
#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
@kenny1025
Copy link

I want to write this similar code in R. Can I get help please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment