Skip to content

Commit b157c1a

Browse files
authored
batch example: print exception, write doesnt work (#45)
1 parent 24e17c8 commit b157c1a

3 files changed

Lines changed: 38 additions & 29 deletions

File tree

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,24 @@ key = 'your-api-key-here'
3636
geocoder = OpenCageGeocode(key)
3737
```
3838

39-
Pass a string containing the query or address to be geocoded to the modules's `geocode` method:
39+
Pass a string containing the query or address to be geocoded to the modules' `geocode` method:
4040

4141
```python
42-
query = "82 Clerkenwell Road, London"
43-
result = geocoder.geocode(query)
42+
query = '82 Clerkenwell Road, London'
43+
results = geocoder.geocode(query)
4444
```
4545

4646
You can add [additional parameters](https://opencagedata.com/api#forward):
4747

4848
```python
49-
result = geocoder.geocode('London', no_annotations=1, language='es')
49+
results = geocoder.geocode('London', no_annotations=1, language='es')
5050
```
5151

52-
You can use the proximity parameter to provide the geocoder with a hint:
52+
For example you can use the proximity parameter to provide the geocoder with a hint:
5353

5454
```python
55-
result = geocoder.geocode('London', proximity='42.828576, -81.406643')
56-
print(result[0]['formatted'])
55+
results = geocoder.geocode('London', proximity='42.828576, -81.406643')
56+
print(results[0]['formatted'])
5757
# u'London, ON N6A 3M8, Canada'
5858
```
5959

@@ -63,7 +63,7 @@ print(result[0]['formatted'])
6363
Turn a lat/long into an address with the ``reverse_geocode`` method:
6464

6565
```python
66-
results = geocoder.reverse_geocode(51.51024, -0.10303)
66+
result = geocoder.reverse_geocode(51.51024, -0.10303)
6767
```
6868

6969
### Sessions

examples/batch.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,37 @@
1212
import csv
1313
import backoff
1414
import asyncio
15+
import traceback
1516
from opencage.geocoder import OpenCageGeocode, AioHttpError
1617

1718
api_key = ''
1819
infile = 'file_to_geocode.csv'
1920
outfile = 'file_geocoded.csv'
2021

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
2223
num_workers = 3 # For 10 requests per second try 2-5
2324
timeout = 5 # For individual HTTP requests. In seconds, default is 1
2425
retry_max_tries = 10 # How often to retry if a HTTP request times out
2526
retry_max_time = 60 # Limit in seconds for retries
2627

2728
csv_writer = csv.writer(open(outfile, 'w', newline=''))
2829

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]
3233
row = [
3334
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']
4546
]
4647

4748
else:
@@ -77,11 +78,19 @@ def backoff_hdlr(details):
7778
on_backoff=backoff_hdlr)
7879
async def geocode_one_address(address, address_id):
7980
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+
8190
try:
82-
await write_one_geocoding_result(geocoding_result, address, address_id)
91+
await write_one_geocoding_result(geocoding_results, address, address_id)
8392
except Exception as e:
84-
sys.stderr.write(e)
93+
traceback.print_exception(e, file=sys.stderr)
8594

8695

8796

opencage/geocoder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class OpenCageGeocode:
115115
116116
>>> geocoder.geocode("London")
117117
118-
Reverse geocode a latitude & longitude into a point:
118+
Reverse geocode a latitude & longitude into a place:
119119
120120
>>> geocoder.reverse_geocode(51.5104, -0.1021)
121121
@@ -152,7 +152,7 @@ async def __aexit__(self, *args):
152152

153153
def geocode(self, query, **kwargs):
154154
"""
155-
Given a string to search for, return the results from OpenCage's Geocoder.
155+
Given a string to search for, return the list (array) of results from OpenCage's Geocoder.
156156
157157
:param string query: String to search for
158158
@@ -176,7 +176,7 @@ async def geocode_async(self, query, **kwargs):
176176
"""
177177
Aync version of `geocode`.
178178
179-
Given a string to search for, return the results from OpenCage's Geocoder.
179+
Given a string to search for, return the list (array) of results from OpenCage's Geocoder.
180180
181181
:param string query: String to search for
182182

0 commit comments

Comments
 (0)