Match address and output to .CSV
View RawThis sample demonstrates the process of reading a .csv (comma delimited) file that contains address information, making a geocode request using the LightBox Geocoding endpoint, and writing the result back to out output .csv file.
In this example we will demonstrate:
- Reading a .csv file using the csv python library
- Making a LightBox geocoding request in a multi threaded approach using the python thread library
- Reading the geocode response as json
- Writing the response to an output .csv file
addresses.csv
view raw_X_Coord | _y_coord | STREET | SUITE | CITY | STATE | POSTAL_CODE | COUNTRY_CODE | IN_BUSINESS | |
---|---|---|---|---|---|---|---|---|---|
1 | -122.956348 | 46.718869 | 303 W Magnolia St | Centralia | WA | 98531-4349 | US | yes | |
2 | -76.57831 | 43.285986 | 440 State Route 34 | Hannibal | NY | 13074-3127 | US | yes |
1import csv
2import requests
3import threading
4from threading import Thread
5import os
6from datetime import datetime
7
8api_url = 'https://api.lightboxre.com/v1/addresses/search?text='
9
10# Add your LightBox API Key below
11lightBoxKey = '<your LightBox API Key>'
12
13cnt = 0
14g_lock = threading.Lock()
15g_fileName = "addresses_out.csv"
16
17# Make the geocode request
18def GetByGeocode(row):
19 try:
20
21 # Read fields from CSV to create text string
22 addrStr = ""
23 if(len(row[2]) > 0):
24 addrStr += row[2]
25 if(len(row[3]) > 0):
26 addrStr += " Unit " + row[2]
27 else:
28 addrStr += " "
29 if(len(row[4]) > 0):
30 addrStr += row[4] + ", "
31 if(len(row[5]) > 0):
32 addrStr += row[5] + ", "
33 if(len(row[6]) > 0):
34 addrStr += row[6]
35
36
37 if len(row[7]) > 0:
38 url = api_url + addrStr
39 response = requests.get(url, headers={"x-api-key" : lightBoxKey })
40 if response.status_code != 200:
41 raise Exception(response.status_code)
42 response = response.json()
43 else:
44 raise Exception("No Lat/Lng")
45
46 except:
47 # Write out row and continue
48 row.append('')
49 row.append('')
50 row.append('')
51 row.append('')
52 row.append('')
53 row.append('')
54 row.append('')
55 row.append('')
56 writeRow(row , g_fileName)
57 return
58
59 if not 'addresses' in response:
60 return
61
62 for address in response['addresses']:
63
64 # append the data to columns in the output
65 row.append(address['location']['representativePoint']['longitude'])
66 row.append(address['location']['representativePoint']['latitude'])
67 row.append(address['$metadata']['geocode']['precisionCode'])
68 row.append(address['$metadata']['geocode']['confidence']['score'])
69 row.append(address['location']['streetAddress'])
70 row.append(address['location']['locality'])
71 row.append(address['location']['regionCode'])
72 row.append(address['location']['postalCode'])
73
74 # Write row to the output file
75 writeRow(row,g_fileName)
76
77def writeRow(row, file):
78 with g_lock:
79 with open(file, 'a', newline='') as outputFile:
80 writer = csv.writer(outputFile)
81 writer.writerow(row)
82
83
84# Delete output if exists
85if os.path.exists(g_fileName):
86 os.remove(g_fileName)
87
88# Open the csv with read
89with open('addresses.csv', newline='') as inputFile:
90 cnt = 1
91 reader = csv.reader(inputFile)
92
93 row = next(reader)
94 cnt+=1
95
96 # print out count every 100 records
97 if cnt % 100 == 0:
98 print(cnt)
99
100 # add new columns to the row! We will append the result to these columns
101 row.append('geoLat')
102 row.append('geoLon')
103 row.append('geoPc')
104 row.append('geoScore')
105 row.append('geoAddress')
106 row.append('geoCity')
107 row.append('geoState')
108 row.append('geoPostal')
109
110 while True:
111
112 threads = []
113 for n in range(1, 25):
114 try:
115 row = next(reader)
116 except:
117 break;
118 t = Thread(target=GetByGeocode, args=(row,))
119 threads.append(t)
120 t.start()
121
122 # wait for the threads to complete
123 for t in threads:
124 t.join()
125
126