|
12 | 12 | import csv |
13 | 13 | import backoff |
14 | 14 | import asyncio |
| 15 | +import traceback |
15 | 16 | from opencage.geocoder import OpenCageGeocode, AioHttpError |
16 | 17 |
|
17 | 18 | api_key = '' |
18 | 19 | infile = 'file_to_geocode.csv' |
19 | 20 | outfile = 'file_geocoded.csv' |
20 | 21 |
|
21 | | -max_items = 100 # Set to 0 for unlimited |
| 22 | +max_items = 100 # How man lines to read from the input file. Set to 0 for unlimited |
22 | 23 | num_workers = 3 # For 10 requests per second try 2-5 |
23 | 24 | timeout = 5 # For individual HTTP requests. In seconds, default is 1 |
24 | 25 | retry_max_tries = 10 # How often to retry if a HTTP request times out |
25 | 26 | retry_max_time = 60 # Limit in seconds for retries |
26 | 27 |
|
27 | 28 | csv_writer = csv.writer(open(outfile, 'w', newline='')) |
28 | 29 |
|
29 | | -async def write_one_geocoding_result(geocoding_result, address, address_id): |
30 | | - if geocoding_result != None: |
31 | | - geocoding_result = geocoding_result[0] |
| 30 | +async def write_one_geocoding_result(geocoding_results, address, address_id): |
| 31 | + if geocoding_results != None and len(geocoding_results): |
| 32 | + first_result = geocoding_results[0] |
32 | 33 | row = [ |
33 | 34 | address_id, |
34 | | - geocoding_result['geometry']['lat'], |
35 | | - geocoding_result['geometry']['lng'], |
36 | | - # Any of these components might be empty : |
37 | | - geocoding_result['components'].get('country', ''), |
38 | | - geocoding_result['components'].get('county', ''), |
39 | | - geocoding_result['components'].get('city', ''), |
40 | | - geocoding_result['components'].get('postcode', ''), |
41 | | - geocoding_result['components'].get('road', ''), |
42 | | - geocoding_result['components'].get('house_number', ''), |
43 | | - geocoding_result['confidence'], |
44 | | - geocoding_result['formatted'] |
| 35 | + first_result['geometry']['lat'], |
| 36 | + first_result['geometry']['lng'], |
| 37 | + # Any of the components might be empty: |
| 38 | + first_result['components'].get('country', ''), |
| 39 | + first_result['components'].get('county', ''), |
| 40 | + first_result['components'].get('city', ''), |
| 41 | + first_result['components'].get('postcode', ''), |
| 42 | + first_result['components'].get('road', ''), |
| 43 | + first_result['components'].get('house_number', ''), |
| 44 | + first_result['confidence'], |
| 45 | + first_result['formatted'] |
45 | 46 | ] |
46 | 47 |
|
47 | 48 | else: |
@@ -77,11 +78,19 @@ def backoff_hdlr(details): |
77 | 78 | on_backoff=backoff_hdlr) |
78 | 79 | async def geocode_one_address(address, address_id): |
79 | 80 | async with OpenCageGeocode(api_key) as geocoder: |
80 | | - geocoding_result = await geocoder.geocode_async(address) |
| 81 | + # address -> coordinates |
| 82 | + geocoding_results = await geocoder.geocode_async(address) |
| 83 | + |
| 84 | + # coordinates -> address, e.g. '40.78,-73.97' => 101, West 91st Street, New York |
| 85 | + # lon_lat = address.split(',') |
| 86 | + # geocoding_result = await geocoder.reverse_geocode_async(lon_lat[0], lon_lat[1]) |
| 87 | + # returns a single result so we convert it to a list |
| 88 | + # geocoding_results = [geocoding_result] |
| 89 | + |
81 | 90 | try: |
82 | | - await write_one_geocoding_result(geocoding_result, address, address_id) |
| 91 | + await write_one_geocoding_result(geocoding_results, address, address_id) |
83 | 92 | except Exception as e: |
84 | | - sys.stderr.write(e) |
| 93 | + traceback.print_exception(e, file=sys.stderr) |
85 | 94 |
|
86 | 95 |
|
87 | 96 |
|
|
0 commit comments