lat_long.py 661 B

1234567891011121314151617181920
  1. from opencage.geocoder import OpenCageGeocode
  2. import json
  3. key = '59713351589a4d55a9fa1b286191d47b'
  4. geocoder = OpenCageGeocode(key)
  5. towns = []
  6. with open('towns.txt', encoding='utf8') as f:
  7. town = f.readline().strip('\n')
  8. while town:
  9. result = geocoder.geocode(town)
  10. if result and len(result):
  11. print("writing " + town)
  12. longitude = result[0]['geometry']['lng']
  13. latitude = result[0]['geometry']['lat']
  14. towns.append((town, {'lat': latitude, 'lon': longitude}))
  15. town = f.readline().strip('\n')
  16. with open('lats_lons.txt', 'w+', encoding='utf8') as f:
  17. f.write(json.dumps(towns))