You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

32 lines
1.0 KiB

  1. import psycopg2
  2. import time
  3. from geopy.geocoders import Nominatim
  4. con = psycopg2.connect(database="dvdrental", user="postgres",
  5. password="postgres", host="127.0.0.1", port="5432")
  6. geolocator = Nominatim(user_agent="curl")
  7. cur = con.cursor()
  8. cur.execute('ALTER TABLE address ADD COLUMN IF NOT EXISTS latitude VARCHAR(255) DEFAULT (\'0\');')
  9. cur.execute('ALTER TABLE address ADD COLUMN IF NOT EXISTS longitude VARCHAR(255) DEFAULT (\'0\');')
  10. con.commit()
  11. cur.callproc('get_address_and_city_for_ex1')
  12. rows = cur.fetchall()
  13. for row in rows:
  14. print(row[0])
  15. location = geolocator.geocode(row[0], timeout=None)
  16. lat = 0
  17. lon = 0
  18. if location is not None:
  19. print(location.address)
  20. print((location.latitude, location.longitude))
  21. lat = location.latitude
  22. lon = location.longitude
  23. addr = row[0].split(', ')
  24. cur.execute(f'UPDATE address SET latitude=\'{lat}\', longitude=\'{lon}\' WHERE address=\'{addr[0]}\' and city_id=\'{addr[1]}\'')
  25. time.sleep(1)
  26. con.commit()