SHARE
TWEET

Untitled

a guest Jul 17th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Wed Jun  5 16:28:05 2019
  4.  
  5. @author: sgorton
  6. """
  7.  
  8. # import the geocoding services you'd like to try
  9. from geopy.geocoders import ArcGIS, Bing, Nominatim, OpenCage, GoogleV3, OpenMapQuest
  10. import csv, sys
  11. import pandas as pd
  12.  
  13.  
  14.  
  15. path = '//aus-fs.aus.apai/share/Projects/0326/120-01/Richardson/Python/InputCodes/'
  16. in_file = path + sys.argv[1]
  17. out_file = path + 'gc_' + sys.argv[1]
  18. timeout = int(sys.argv[2])
  19.  
  20. print('creating geocoding objects.')
  21.  
  22. arcgis = ArcGIS(timeout=timeout)
  23. bing = Bing('AqlXoamuCx0QOc8nD_08-JkjxPGGjG8oKKWRfM1G4LorjSbSF5avZJpwGU1XTaKe',timeout=100)
  24. nominatim = Nominatim('lijpwyJkq0jPsoGTjvMtniPPBNmd1hU8', timeout=timeout)
  25. opencage = OpenCage('fb899e7f9909403cafc1a284fcd2742b',timeout=timeout)
  26. googlev3 = GoogleV3('AIzaSyCapGQhMFIhx_DBrSgVJfIaCcNirl52IQo', domain='maps.googleapis.com', timeout=timeout)
  27. openmapquest = OpenMapQuest('lijpwyJkq0jPsoGTjvMtniPPBNmd1hU8', timeout=timeout)
  28.  
  29. # choose and order your preference for geocoders here
  30. geocoders = [arcgis,bing,openmapquest]
  31.  
  32.  
  33. def gc(address):
  34.     street = str(address['street'])
  35.     city = str(address['city'])
  36.     state = str(address['state'])
  37.     country = str(address['country'])
  38.     add_concat = street + ", " + city + ", " + state + " " + country
  39.     for gcoder in geocoders:
  40.         location = gcoder.geocode(add_concat)
  41.         if location != None:
  42.             print(f'geocoded record {address.name}: {street}')
  43.             located = pd.Series({
  44.                 'lat': location.latitude,
  45.                 'lng': location.longitude,
  46.                 'time': pd.to_datetime('now')
  47.             })
  48.         else:
  49.             print(f'failed to geolocate record {address.name}: {street}')
  50.             located = pd.Series({
  51.                 'lat': 'null',
  52.                 'lng': 'null',
  53.                 'time': pd.to_datetime('now')
  54.             })
  55.         return located
  56.  
  57. print('opening input.')
  58. reader = pd.read_csv(in_file, header=0)
  59. print('geocoding addresses.')
  60. reader = reader.merge(reader.apply(lambda add: gc(add), axis=1), left_index=True, right_index=True)
  61. print(f'writing to {out_file}.')
  62. reader.to_csv(out_file, encoding='utf-8', index=False)
  63. print('done.')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top