Skip to content

Instantly share code, notes, and snippets.

@warm200
Created March 26, 2019 17:07
Show Gist options
  • Save warm200/83b312f10be0dd24837083fe58c495ad to your computer and use it in GitHub Desktop.
Save warm200/83b312f10be0dd24837083fe58c495ad to your computer and use it in GitHub Desktop.
Compare google api vs mapbox api side by side
# python geocode.py -f "filepath.xlsx"
import googlemaps
from datetime import datetime
import json
from mapbox import Geocoder,Directions
import geopy.distance
import xlrd
import csv
import click
import time
def metrics(pickup_address, dropoff_address, Picklatlon, DropLatLon, pickCity, DropCity):
mb_geocoder = Geocoder(access_token="")
gmaps = googlemaps.Client(key='')
print("validating address", pickup_address)
service = Directions()
# Geocoding an address
start = time.clock()
gg_reponse = gmaps.geocode(pickup_address)
gg_request_time = time.clock() - start
gg_request_time = round(gg_request_time * 1000, 2)
print("google response time",gg_request_time)
start = time.clock()
mb_response = mb_geocoder.forward(pickup_address)
bb_request_time = time.clock() - start
bb_request_time = round(bb_request_time * 1000, 2)
print("mapbox response time", bb_request_time)
(gg_lat, gg_lon) = gg_reponse[0]['geometry']["location"].values()
(mb_lon, mb_lat) = mb_response.geojson()['features'][0]['geometry']['coordinates']
# Geo reverse address
start = time.clock()
reverse_gg = gmaps.reverse_geocode(Picklatlon)
gg_reverse_time = time.clock() - start
gg_reverse_time = round(gg_reverse_time * 1000,2)
start = time.clock()
reverse_mb = mb_geocoder.reverse(lon=Picklatlon[1], lat=Picklatlon[0])
mb_reverse_time = time.clock() - start
mb_reverse_time = round(mb_reverse_time * 1000, 2)
GG_REVERSE = reverse_gg[0]['formatted_address']
MB_REVERSE = '{place_name}: {id}'.format(**reverse_mb.geojson()['features'][0])
GG = (gg_lat, gg_lon)
MB = (mb_lat, mb_lon)
distance = round(geopy.distance.distance((gg_lat, gg_lon), (mb_lat, mb_lon)).m, 4)
##### direction service
# map box
origin = {
'type': 'Feature',
'properties': {'name': '%s, NY'%pickCity},
'geometry': {
'type': 'Point',
'coordinates': [Picklatlon[1], Picklatlon[0]]}}
dropoff = {
'type': 'Feature',
'properties': {'name': '%s, NY'%DropCity},
'geometry': {
'type': 'Point',
'coordinates': [DropLatLon[1], DropLatLon[0]]}}
start = time.clock()
response = service.directions([origin, dropoff],'mapbox.driving')
mb_driving_response_time = time.clock() - start
mb_driving_response_time = round(mb_driving_response_time * 1000, 2)
print("mb driving response time", mb_driving_response_time)
# google
start = time.clock()
now = datetime.now()
directions_result = gmaps.directions(address,
dropoff_address,
mode="driving",
departure_time=now)
gg_driving_response_time = time.clock() - start
gg_driving_response_time = round(gg_driving_response_time * 1000, 2)
print("gg driving response time", gg_driving_response_time)
print("google coordinate", GG)
print("mb coordinate ",MB)
print("distance", distance)
return GG, MB, distance, GG_REVERSE, MB_REVERSE,gg_request_time, \
bb_request_time, gg_reverse_time, mb_reverse_time, \
gg_driving_response_time, mb_driving_response_time
def handling_file(path):
workbook = xlrd.open_workbook(path)
sheet = workbook.sheet_by_index(0)
header = sheet.row_values(0)
rs = []
for rowx in range(1, sheet.nrows):
cols = sheet.row_values(rowx)
rs.append(dict(zip(header, cols)))
return rs
def write_to_csv(dic):
keys = dic[0].keys()
with open('result1.csv', 'w') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(dic)
def normalize(coordinate):
return coordinate / 100000.0
@click.command()
@click.option('--file', '-f', help='file path')
def main(file):
rs = handling_file(file)
for hs in rs:
# replace hardcode key with your own file
pickup_address = ",".join([hs['PickHouseNumber'] + " "+ hs['PickAddress1'], hs['PickCity'], "NY", hs['PickZip']])
dropoff_address = ",".join([hs['DropHouseNumber'] + " "+ hs['DropAddress1'], hs['DropCity'], "NY", hs['DropZip']])
Picklatlon = (normalize(hs['PickGridY']), normalize(hs['PickGridX']))
DropLatLon = (normalize(hs['DropGridY']), normalize(hs['DropGridX']))
gg, mb, distance, gg_reversed_address, mb_reversed_address, gg_request_time, \
bb_request_time ,gg_reverse_time, mb_reverse_time ,gg_driving_response_time, \
mb_driving_response_time= metrics(pickup_address,dropoff_address, Picklatlon, DropLatLon, hs['PickCity'], hs['DropCity'])
hs["google_lat"], hs["google_lon"] = gg[0], gg[1]
hs["mb_lat"], hs["mb_lon"] = mb[0], mb[1]
hs["gg_request_time"], hs["bb_request_time"] = gg_request_time, bb_request_time
hs["distance_between_gg_mb"] = distance
hs["google_reversed_address"] = gg_reversed_address
hs["mapbox_reversed_address"] = mb_reversed_address
hs["gg_reverse_time"], hs["mb_reverse_time"] = gg_reverse_time, mb_reverse_time
hs["gg_driving_response_time"] = gg_driving_response_time
hs["mb_driving_response_time"] = mb_driving_response_time
write_to_csv(rs)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment