7
7
parser .add_argument ('output' , help = 'file to output accuracy data' )
8
8
parser .add_argument ('key' , help = 'a file containing a valid google api key' )
9
9
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' )
11
12
args = parser .parse_args ()
12
13
13
14
#check address against google api
@@ -32,6 +33,17 @@ def check_address_zip(parts, session, key):
32
33
result = (session .get (url_pre + (parts [2 ] + ' ' + parts [3 ] + ', ' + parts [4 ] + ', ' + parts [6 ] + ' ' + parts [7 ]).replace (' ' , '+' ) + url_post )).json ()
33
34
return parse_response (result )
34
35
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
+
35
47
def parse_response (result ):
36
48
#if google finds point
37
49
if result ['status' ] == 'OK' :
@@ -52,11 +64,12 @@ def parse_response(result):
52
64
53
65
key = open (args .key , 'r' ).read ()
54
66
with requests .Session () as google :
55
- output = open (args .output , 'w ' )
67
+ output = open (args .output , 'a ' )
56
68
with open (args .input , 'r' ) as source :
57
69
for row in source :
58
70
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 ' )
60
73
elif args .reverse : output .write (',' .join (parts + [check_address_reverse (parts , google , key )]) + '\n ' )
61
74
elif args .zip : output .write (',' .join (parts + [check_address_zip (parts , google , key )]) + '\n ' )
62
75
else : output .write (',' .join (parts + [check_address (parts , google , key )]) + '\n ' )
0 commit comments