forked from ALPHARUSOPENCOLLECTIVE/cambridgeAnalytica
-
Notifications
You must be signed in to change notification settings - Fork 1
/
geoLocation.py
440 lines (366 loc) · 17.1 KB
/
geoLocation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# -*- coding: utf-8 -*-
"""
Created on Thur Mar 17 9:21:35 2016
@author: Michael Phillips
@email: [email protected]
Script's main purpose is to complete an array of addresses with accurate latitudes and longitudes using the completeAddress function
Includes another function compareAPItoSource for testing APIs with source latitude longitudes.
More detailed comments are above and within each primary function. Utility functions are fairly self explanatory.
"""
import sys
import time
## Importing data analysis libraries
import numpy as np
import pandas as pd
## Importing googlemap libraries
from googlemaps import Client
import googlemaps as gmaps
## Importing geo-enconding libraries
from geopy.geocoders import Nominatim
from geopy.exc import GeocoderTimedOut
import geocoder
## Importing time, distance measuring libraries
from random import random
from time import sleep
from geopy.distance import vincenty
##
import scipy.stats
from collections import OrderedDict
##################
#UTILITY FUNCTIONS
##################
def readFromFileA(filename,splitter=',', lineStart = 0, lineEnd = 1000):
f = open(filename,'r')
lines_list = f.readlines()
f.close()
my_data = [[str(val) for val in line.split(splitter)[0:]] for line in lines_list[lineStart:lineEnd]]
my_data = filter(lambda a: a != ['\n'], my_data)
data = np.asarray(my_data)
return data
## Google -- Converting Address to Coordinate
def getLatLngGoog(client, addr):
location = gmaps.geocoding.geocode(client, addr)[0].get('geometry').get('location')
lat = location.get('lat')
lng = location.get('lng')
return lat,lng
## Geopy -- Converting Address to Coordinate
def getLatLngGeopy(key, address): ## key is not used
geolocator = Nominatim()
#address = (house_num + " " + street_name + " " + city + ", " + state_abrv + " " + zip_code)
sleep(0.1 + 0.2 *random())
location = geolocator.geocode(address)
if location:
return (location.latitude, location.longitude)
else:
return 0
## ArcGIS -- Converting Address to Cooordinate
def getLatLngArcGIS(key, address): ##key is not used. Only present to support googleMaps.
g = geocoder.arcgis(address)
if g.latlng == []:
return 0
h = [str(g.latlng[0]), str(g.latlng[1])]
return h
## Measuring distance between 2 coordinates
def getDistance(latlng1, latlng2):
return vincenty(latlng1, latlng2).miles
"""
This Function is meant as a test function used to determine accuracy of source lng/lat data.
_client is for googlemaps when the APIkey is needed. Pass it something like client for:
api_keyServer = 'AIzaSyA-Qh3hS_6uZjCXsayf53m-1-Q3h-5KEPo'
client = Client(api_keyServer)
APIfunction is a function that takes a client and an addr as parameters (see getLatLngArcGIS(key, address) as the recommended option)
_fullAddresses is a list of addresses where each line is:
, address_id, voter_id, AddressLine, ExtraAddressLine, HouseNumber, PrefixDirection, StreetName, Designator, SuffixDirection, ApartmentNum, Zip, ZipPlus4, City, County, CongressionalDistrict, State, latitude, longitude, ar_latitude, ar_longitude
only columns 5, 7, 8, 13, 16, 17, 18, 19, 20 are required, the rest can be blank.
probabilities are printed out as "prob: [(x miles, probability of being within x miles),...]" where we're comparing Aristotle and IG data to the values fetched by the APIfunction.
The function returns how long it took in seconds to complete. This may be useful for comparing APIs to each other.
"""
def compareAPItoSource(_client, APIfunction, _fullAddresses):
start_time = time.time()
lng_lat_pairs_API = []
lng_lat_pairs_ar = []
lng_lat_pairs_ig = []
exceptionCount = 0
getllfails = 0
lineCount = 0
for line in _fullAddresses:
lineCount = lineCount + 1
sys.stdout.flush()
if lineCount % 500 == 0:
print "line count: " + str(lineCount)
try:
address = str(line[5] + " " + line[7] + " " + line[8] + " " + line[13] + ", " + line[16])
ll = APIfunction(_client, address)
if ll == 0:
getllfails = getllfails + 1
continue
else:
lng_lat_pairs_API.append(ll)
lng_lat_pairs_ig.append((line[17], line[18]))
lng_lat_pairs_ar.append((line[19], line[20]))
except:
exceptionCount = exceptionCount + 1
continue
try:
lng_lat_pairs_API = np.array(lng_lat_pairs_API)
lng_lat_pairs_ar = np.array(lng_lat_pairs_ar)
lng_lat_pairs_ig = np.array(lng_lat_pairs_ig)
APIzip = zip(lng_lat_pairs_API[:, 0], lng_lat_pairs_API[:, 1])
arzip = zip(lng_lat_pairs_ar[:, 0], lng_lat_pairs_ar[:, 1])
igzip = zip(lng_lat_pairs_ig[:, 0], lng_lat_pairs_ig[:, 1])
except:
print "Failure to complete "
return(time.time() - start_time)
ardistanceList = []
igdistanceList = []
i = 0
for pair in APIzip:
ardistanceList.append(getDistance(pair, arzip[i]))
i = i + 1
ardistanceNp = np.array(ardistanceList)
i = 0
for pair in APIzip:
igdistanceList.append(getDistance(pair, igzip[i]))
i = i + 1
igdistanceNp = np.array(igdistanceList)
arbuckets = {.01: float(0), .1: float(0), 1: float(0), 10: float(0), 100: float(0)}
for dis in ardistanceNp:
if dis < .01:
arbuckets[.01] = arbuckets[.01] + 1
if dis < .1:
arbuckets[.1] = arbuckets[.1] + 1
if dis < 1:
arbuckets[1] = arbuckets[1] + 1
if dis < 10:
arbuckets[10] = arbuckets[10] + 1
if dis < 100:
arbuckets[100] = arbuckets[100] + 1
arorderedBuckets = OrderedDict(sorted(arbuckets.items(), key=lambda t: t[0]))
arprintBuckets = []
for k,v in arorderedBuckets.iteritems():
value = v/float(len(ardistanceNp))
value = int((value * 100) + 0.5) / 100.0 #rounding to 2 decimals
arprintBuckets.append((k, value))
igbuckets = {.01: float(0), .1: float(0), 1: float(0), 10: float(0), 100: float(0)}
for dis in igdistanceNp:
if dis < .01:
igbuckets[.01] = igbuckets[.01] + 1
if dis < .1:
igbuckets[.1] = igbuckets[.1] + 1
if dis < 1:
igbuckets[1] = igbuckets[1] + 1
if dis < 10:
igbuckets[10] = igbuckets[10] + 1
if dis < 100:
igbuckets[100] = igbuckets[100] + 1
igorderedBuckets = OrderedDict(sorted(igbuckets.items(), key=lambda t: t[0]))
igprintBuckets = []
for k,v in igorderedBuckets.iteritems():
value = v/float(len(igdistanceNp))
value = int((value * 100) + 0.5) / 100.0 #rounding to 2 decimals
igprintBuckets.append((k, value))
print "******Overall run stats*******"
print "get lat/lng fails: " + str(getllfails)
print "number of other exceptions: " + str(exceptionCount)
print "total number of successes " + str(len(ardistanceNp))
print "*******Aristotle Data******"
print "mean distance: " + str(np.mean(ardistanceNp))
print "probs: " + str(arprintBuckets)
print "*******IG Data******"
print "mean distance: " + str(np.mean(igdistanceNp))
print "probs: " + str(igprintBuckets)
return(time.time() - start_time)
"""
This function is really the purpose of this script. Essentially what it does is:
For each address in the addresses file, try to get an accurate lng/lat quickly (comparing available data
from Aristotle/IG to the zip code file data to determine accuracy), but if we can't, we fetch it from ArcGIS.
addresses is an array of addresses each in the form:
1. address_id,
2. voter_id, ***
3. AddressLine,
4. ExtraAddressLine,
5. HouseNumber, ***
6. PrefixDirection,
7. StreetName, ***
8. Designator, ***
9. SuffixDirection,
10. ApartmentNum,
11. Zip,
12. ZipPlus4,
13. City, ***
14. County,
15. CongressionalDistrict,
16. State ***
17. latitude
18. longitude
19. ar_latitude
20. ar_longitude
only lines 5, 7, 8, 13, 16 are used though, the rest can be blank.
lines 17,18,19,20 are optional, they are the data from Aristotle and IG lat/lng data.
_zips is an array of zip codes in the form:
zip, city, state, latitude, longitude, timezone, dst
where latitude and longitude correspond to the center of the zip code.
note that zip codes should be in the same format as provided by the addresses file. sometimes this means trimming leading zeroes.
latlngFunc is the function you want to use to fetch lat/lngs that are not supplied in the addresses array.
getLatLngArcGIS is recommended due to accuracy and the fact that there is no usage restriction.
The function returns an array which adds 2 extra columns to the original addresses array. The extra columns are the accurate lat/lngs.
Output is a little confusing, but the important bits are the fetch rate (basically dictates how quickly the function goes),
and the errors (which is going to be the number of address lines which we couldn't get accurate data for from any source.)
********THINGS THAT CAN BE ADDED*************
Right now the exception clause basically just says that the latlngFunc failed to find the address and give a valid lat/lng.
Instead, it could use googleMaps to fill in the field. It would have to be limited to 2500 uses in a day, but at the average hit rate for
ArcGIS, it would be difficult for it to fail 2500 times in a single day. This would increase coverage to possibly 100%, but if we did
hit our googleMaps fetch limit, it could cause the program to crash or take virtually forever.
"""
def completeAddresses(addresses, _zips, latlngFunc):
completeAddrs = np.hstack((addresses, np.zeros([len(addresses), 2])))
completeAddrs = pd.DataFrame(completeAddrs)
#create dictionary for zip lookups
zipsList = _zips[:,0].tolist()
latitudeList = _zips[:,3]
longitudeList = _zips[:,4]
latlngList = zip(latitudeList, longitudeList)
zipsDict = dict(zip(zipsList, latlngList))
radius = 15
errorCount = 0
igHitNumber = 0
igMissNumber = 0
arHitNumber = 0
arMissNumber = 0
numberOfFetches = 0
for index, line in completeAddrs.iterrows():
if int(line[0]) % 100 == 0:
print line[0]
try:
#create address for future use
address = str(line[5] + " " + line[7] + " " + line[8] + " " + line[13] + ", " + line[16])
#if no lat/lng present:
if len(line[17]) + len(line[19]) == 0:
#fetch lat/lng and plug it into completeAddrs
(line[21],line[22]) = latlngFunc(19, address)
numberOfFetches = numberOfFetches + 1
#elif best source is available:
elif len(line[17]) > 0:
#compare to zipcode
zipcodeFromFile = line[11]
ziplatlng = zipsDict.get(zipcodeFromFile)
dist = getDistance(ziplatlng, (line[17], line[18]))
#if reasonable:
if dist < radius:
#put it into completeAddrs
igHitNumber = igHitNumber + 1
(line[21], line[22]) = (line[17], line[18])
#elif second best source is available:
elif len(line[19]) > 0:
igMissNumber = igMissNumber + 1
#compare to zipcode
zipcodeFromFile = line[11]
ziplatlng = zipsDict.get(zipcodeFromFile)
dist = getDistance(ziplatlng, (line[17], line[18]))
#if reasonable:
if dist < radius:
arHitNumber = arHitNumber + 1
#put it into copmleteAddrs
(line[21], line[22]) = (line[19], line[20])
#else:
else:
arMissNumber = arMissNumber + 1
#fetch lat/lng and plug it into completeAddrs
(line[21],line[22]) = latlngFunc(19, address)
numberOfFetches = numberOfFetches + 1
#else if second best source is available
elif len(line[19]) > 0:
#compare to zipcode
zipcodeFromFile = line[11]
ziplatlng = zipsDict.get(zipcodeFromFile)
dist = getDistance(ziplatlng, (line[19], line[20]))
#if reasonable:
if dist < radius:
arHitNumber = arHitNumber + 1
#put it into copmleteAddrs
(line[21], line[22]) = (line[19], line[20])
#else:
else:
arMissNumber = arMissNumber + 1
#fetch lat/lng and plug it into completeAddrs
(line[21],line[22]) = latlngFunc(19, address)
numberOfFetches = numberOfFetches + 1
except: ### TypeError probably means that the latlngFunc returned 0.
# print "exception: ", sys.exc_info()[0]
# print "at line of addresses file: " , line[0]
errorCount = errorCount + 1
continue
print "errors: " , errorCount
print "Number of hits for IG: " , igHitNumber
print "Number of misses for IG: " , igMissNumber
print "Number of hits for Aristotle: " , arHitNumber
print "Number of misses for Aristotle: " , arMissNumber
print "Number of fetches from API: " , numberOfFetches
print "Fetch %: ", float(numberOfFetches) / len(completeAddrs)
return completeAddrs
"""
This right now assigns the completedAddresses array to the variable x, which can then be written to a different csv file or something if you want.
Commented out at the bottem are some API tests. Line by line comments are provided in the main script.
"""
if __name__ == "__main__":
#keys for googleMaps client. can be set to whatever and commented out if not using that API.
api_keyBrowser = 'AIzaSyAnrOVz4qbOZveNHty6Yx8s-E_6BDzHRTk'
api_keyServer = 'AIzaSyA-Qh3hS_6uZjCXsayf53m-1-Q3h-5KEPo'
#creates the googlemaps client. should be commented out if the googlemaps API isnt being used.
client = Client(api_keyServer)
#file path for the address file
dataFileName = "./data/address_nh.csv"
#file path for the zip code file
zipsFileName = "./data/zipcode.csv"
#simple timer to check run speed
start = time.time()
print "reading file..."
#readFromFile lineStart should be 1 if the first line is a key, 0 if the first line is data to be used. lineEnd should be length of the file + 1 if you want it all
addresses = readFromFileA(dataFileName, splitter=',', lineStart = 40000, lineEnd = 41000)
#readFromFile lineStart should be 1 if the first line is a key, 0 if the first line is data to be used. lineEnd should be length of the file + 1 if you want it all
zips = readFromFileA(zipsFileName, splitter=',', lineStart = 1, lineEnd = 1392)
#get rid of apostrophes without corrupting file.
# had to include because zip file I was given corrupted when I did a find and replace within the actual text file.
i = 0
for line in zips:
j = 0
for col in line:
zips[i][j] = col.replace("\"", "")
j = j + 1
i = i + 1
#remove lead zeros so the dictionary within completeAddresses() works. Can be altered depening on formatting of zip and addresses files. just make sure they match.
i = 0
for line in zips:
zips[i][0] = str(line[0]).lstrip("0")
i = i + 1
#############
#lat/lng filler script
#############
x = completeAddresses(addresses, zips, getLatLngArcGIS)
#################
########################
#API comparisons Script
######################
#Set fullAddresses to addresses for which we have both aristotle and ig data
# fullAddresses = np.empty((0,21))
#
# print "finding suitable addresses..."
# for line in addresses:
# if len(line[17]) > 0 and len(line[19]) > 0:
# fullAddresses = np.vstack((fullAddresses, line))
# if len(fullAddresses) == 5000:
# break
# print "-------GEOPY-------"
# print "time taken: " + str(compareAPItoSource(client, getLatLngGeopy, fullAddresses)) ###client is not used
# print "-------ArcGIS------"
# print "time taken: " + str(compareAPItoSource(client, getLatLngArcGIS, fullAddresses))
# sys.stdout.flush()
# print "-----GoogleMaps----"
# print "time taken: " + str(compareAPItoSource(client, getLatLngGoog, fullAddresses))
# sys.stdout.flush()
#
###########################################
print time.time() - start
################################
#compare the API results to googlemaps results
################################