This is a tutorial for using the OpenCage Geocoding API in a Python Jupyter Notebook.
Before you can query the API you will need to sign up for an OpenCage API key.
Once you've done that we recommend you spend five minutes on:
Install with pip, compatible with Python 3.7+.
% pip install opencage
from opencage.geocoder import OpenCageGeocode
from pprint import pprint
Replace YOUR-API-KEY
below with the OpenCage API key for your account.
key = 'YOUR-API-KEY'
geocoder = OpenCageGeocode(key)
results = geocoder.reverse_geocode(44.8303087, -0.5761911)
pprint(results)
[{'annotations': {'DMS': {'lat': "44° 49' 49.11132'' N", 'lng': "0° 34' 34.28796'' E"}, 'MGRS': '30TXQ9159666958', 'Maidenhead': 'IN94rt09ug', 'Mercator': {'x': -64141.3, 'y': 5564710.789}, 'NUTS': {'NUTS0': {'code': 'FR'}, 'NUTS1': {'code': 'FRI'}, 'NUTS2': {'code': 'FRI1'}, 'NUTS3': {'code': 'FRI12'}}, 'OSM': {'edit_url': 'https://www.openstreetmap.org/edit?node=2754853042#map=16/44.83031/-0.57619', 'note_url': 'https://www.openstreetmap.org/note/new#map=16/44.83031/-0.57619&layers=N', 'url': 'https://www.openstreetmap.org/?mlat=44.83031&mlon=-0.57619#map=16/44.83031/-0.57619'}, 'UN_M49': {'regions': {'EUROPE': '150', 'FR': '250', 'WESTERN_EUROPE': '155', 'WORLD': '001'}, 'statistical_groupings': ['MEDC']}, 'callingcode': 33, 'currency': {'alternate_symbols': [], 'decimal_mark': ',', 'html_entity': '€', 'iso_code': 'EUR', 'iso_numeric': '978', 'name': 'Euro', 'smallest_denomination': 1, 'subunit': 'Cent', 'subunit_to_unit': 100, 'symbol': '€', 'symbol_first': 0, 'thousands_separator': '.'}, 'flag': '🇫🇷', 'geohash': 'ezzx4ch7mz0zbyftcpdf', 'qibla': 111.75, 'roadinfo': {'drive_on': 'right', 'road': 'Rue Sauteyron', 'speed_in': 'km/h'}, 'sun': {'rise': {'apparent': 1691384160, 'astronomical': 1691376960, 'civil': 1691382180, 'nautical': 1691379720}, 'set': {'apparent': 1691436000, 'astronomical': 1691443140, 'civil': 1691437920, 'nautical': 1691440380}}, 'timezone': {'name': 'Europe/Paris', 'now_in_dst': 1, 'offset_sec': 7200, 'offset_string': '+0200', 'short_name': 'CEST'}, 'what3words': {'words': 'advice.quiet.surveyed'}}, 'bounds': {'northeast': {'lat': 44.8303587, 'lng': -0.5761411}, 'southwest': {'lat': 44.8302587, 'lng': -0.5762411}}, 'components': {'ISO_3166-1_alpha-2': 'FR', 'ISO_3166-1_alpha-3': 'FRA', 'ISO_3166-2': ['FR-NAQ', 'FR-33'], '_category': 'building', '_type': 'building', 'city': 'Bordeaux', 'continent': 'Europe', 'country': 'France', 'country_code': 'fr', 'county': 'Gironde', 'house_number': '11', 'municipality': 'Bordeaux', 'political_union': 'European Union', 'postcode': '33000', 'region': 'Metropolitan France', 'road': 'Rue Sauteyron', 'state': 'Nouvelle-Aquitaine', 'state_code': 'NAQ', 'suburb': 'Victoire'}, 'confidence': 10, 'formatted': '11 Rue Sauteyron, 33000 Bordeaux, France', 'geometry': {'lat': 44.8303087, 'lng': -0.5761911}}]
from opencage.geocoder import RateLimitExceededError
try:
results = geocoder.reverse_geocode(44.8303087, -0.5761911, language='de', no_annotation='1')
if results and len(results):
print(results[0]['formatted'])
except RateLimitExceededError as ex:
print(ex)
# Your rate limit has expired ...
# Upgrade on https://opencagedata.com/pricing
11 Rue Sauteyron, 33000 Bordeaux, Frankreich
query = u'Bosutska ulica 10, Trnje, Zagreb, Croatia'
results = geocoder.geocode(query)
print(u'%f;%f;%s;%s' % (results[0]['geometry']['lat'],
results[0]['geometry']['lng'],
results[0]['components']['country_code'],
results[0]['annotations']['timezone']['name']))
45.796933;15.981894;hr;Europe/Zagreb
Install ipyleaflet
% pip install ipyleaflet
from ipyleaflet import *
results = geocoder.geocode(u'Brussels, Belgium')
center = (results[0]['geometry']['lat'], results[0]['geometry']['lng'])
map = Map(center=center, zoom=6)
marker = Marker(location=center, draggable=False)
map.add_layer(marker)
map
Map(center=[50.8465573, 4.351697], controls=(ZoomControl(options=['position', 'zoom_in_text', 'zoom_in_title',…
You can also access the OpenCage geocoder via Denis Carriere's geocoder library. Here is a code sample showing how to query our API using python geocoder.