geocode.py (742B)
1 #!/usr/bin/env python3 2 3 # read human-readable locations on stdin, produce same list with added GPS 4 # coordinates on STDOUT 5 6 from geopy.geocoders import GeoNames 7 import os 8 import sys 9 from time import sleep 10 11 USER="a3nm" # user on geonames.org, serves as API key 12 geolocator = GeoNames(username=USER) 13 14 def searchGeonames(place): 15 # chatgpt 16 global geolocator 17 location = geolocator.geocode(place, exactly_one=True) 18 if location is None: 19 print("Error: no result for %s" % place, file=sys.stderr) 20 sys.exit(42) 21 return (location.latitude, location.longitude) 22 23 for l in sys.stdin.readlines(): 24 l = l.strip() 25 origin_lat, origin_lng = searchGeonames(l) 26 27 print(origin_lat, origin_lng, l) 28 29 sleep(1) 30