Skip to content

Commit e122fdf

Browse files
committed
add mapbox api check
1 parent bfab337 commit e122fdf

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

Google_Check.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
parser.add_argument('output', help = 'file to output accuracy data')
88
parser.add_argument('key', help = 'a file containing a valid google api key')
99
parser.add_argument('-r', '--reverse', help = 'use a reverse geocode search', action = 'store_true')
10-
parser.add_argument('-zip', '--zip', help = 'search with zip code', action = 'store_true')
10+
parser.add_argument('-z', '--zip', help = 'search with zip code', action = 'store_true')
11+
parser.add_argument('-m', '--mapbox', help = 'geocode from the mapbox API', action = 'store_true')
1112
args = parser.parse_args()
1213

1314
#check address against google api
@@ -32,6 +33,17 @@ def check_address_zip(parts, session, key):
3233
result = (session.get(url_pre + (parts[2] + ' ' + parts[3] + ', ' + parts[4] + ', ' + parts[6] + ' ' + parts[7]).replace(' ', '+') + url_post)).json()
3334
return parse_response(result)
3435

36+
def check_address_mapbox(parts, session, key):
37+
url_pre = 'https://api.mapbox.com/geocoding/v5/mapbox.places/'
38+
url_post = '.json?types=postcode&access_token=' + key
39+
result = (session.get(url_pre + parts[0] + ',' + parts[1] + url_post)).json()
40+
if result['features']:
41+
if result['features'][0]['text'] == parts[7]:
42+
return 'Same'
43+
else:
44+
return result['features'][0]['text']
45+
else: return 'Not Found'
46+
3547
def parse_response(result):
3648
#if google finds point
3749
if result['status'] == 'OK':
@@ -52,11 +64,12 @@ def parse_response(result):
5264

5365
key = open(args.key, 'r').read()
5466
with requests.Session() as google:
55-
output = open(args.output, 'w')
67+
output = open(args.output, 'a')
5668
with open(args.input, 'r') as source:
5769
for row in source:
5870
parts = (row.strip('\r\n')).split(',')
59-
if parts[7] = 'None': output.write(','.join(parts + ['None']) + '\n')
71+
if parts[7] == 'None': output.write(','.join(parts + ['None']) + '\n')
72+
elif args.mapbox: output.write(','.join(parts + [check_address_mapbox(parts, google, key)]) + '\n')
6073
elif args.reverse: output.write(','.join(parts + [check_address_reverse(parts, google, key)]) + '\n')
6174
elif args.zip: output.write(','.join(parts + [check_address_zip(parts, google, key)]) + '\n')
6275
else: output.write(','.join(parts + [check_address(parts, google, key)]) + '\n')

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ Running the current test file of 36 rows, three rows per state for three states
2424
- 30/36 are found and backfilled with zip codes
2525
- 27/30 of the backfilled zip codes agree with what the Google api returns as the zip code
2626

27-
2,000 rows randomly picked from all files:
27+
3,000 rows randomly picked from all files:
2828

29-
- 1,717/2,000 = 86%: backfilled
30-
- 1,129/1,259 = 90%: agree with Google api
29+
- 11,148/13,000 = 86%: backfilled
30+
31+
- 1,608/1,750 = 92%: agree with Google api
32+
33+
- 8587/8593 = 99% agree with Mapbox api
3134

3235

3336

0 commit comments

Comments
 (0)