Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Wed Jun 5 16:28:05 2019
- @author: sgorton
- """
- # import the geocoding services you'd like to try
- from geopy.geocoders import ArcGIS, Bing, Nominatim, OpenCage, GoogleV3, OpenMapQuest
- import csv, sys
- import pandas as pd
- path = '//aus-fs.aus.apai/share/Projects/0326/120-01/Richardson/Python/InputCodes/'
- in_file = path + sys.argv[1]
- out_file = path + 'gc_' + sys.argv[1]
- timeout = int(sys.argv[2])
- print('creating geocoding objects.')
- arcgis = ArcGIS(timeout=timeout)
- bing = Bing('AqlXoamuCx0QOc8nD_08-JkjxPGGjG8oKKWRfM1G4LorjSbSF5avZJpwGU1XTaKe',timeout=100)
- nominatim = Nominatim('lijpwyJkq0jPsoGTjvMtniPPBNmd1hU8', timeout=timeout)
- opencage = OpenCage('fb899e7f9909403cafc1a284fcd2742b',timeout=timeout)
- googlev3 = GoogleV3('AIzaSyCapGQhMFIhx_DBrSgVJfIaCcNirl52IQo', domain='maps.googleapis.com', timeout=timeout)
- openmapquest = OpenMapQuest('lijpwyJkq0jPsoGTjvMtniPPBNmd1hU8', timeout=timeout)
- # choose and order your preference for geocoders here
- geocoders = [arcgis,bing,openmapquest]
- def gc(address):
- street = str(address['street'])
- city = str(address['city'])
- state = str(address['state'])
- country = str(address['country'])
- add_concat = street + ", " + city + ", " + state + " " + country
- for gcoder in geocoders:
- location = gcoder.geocode(add_concat)
- if location != None:
- print(f'geocoded record {address.name}: {street}')
- located = pd.Series({
- 'lat': location.latitude,
- 'lng': location.longitude,
- 'time': pd.to_datetime('now')
- })
- else:
- print(f'failed to geolocate record {address.name}: {street}')
- located = pd.Series({
- 'lat': 'null',
- 'lng': 'null',
- 'time': pd.to_datetime('now')
- })
- return located
- print('opening input.')
- reader = pd.read_csv(in_file, header=0)
- print('geocoding addresses.')
- reader = reader.merge(reader.apply(lambda add: gc(add), axis=1), left_index=True, right_index=True)
- print(f'writing to {out_file}.')
- reader.to_csv(out_file, encoding='utf-8', index=False)
- print('done.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement