opencage


Nameopencage JSON
Version 3.0.3 PyPI version JSON
download
home_pagehttps://github.com/OpenCageData/python-opencage-geocoder/
SummaryWrapper module for the OpenCage Geocoder API
upload_time2024-11-09 21:26:28
maintainerNone
docs_urlNone
authorOpenCage GmbH
requires_pythonNone
licenseBSD
keywords geocoding geocoder
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # OpenCage Geocoding Module for Python

A Python module to access the [OpenCage Geocoding API](https://opencagedata.com/).

## Build Status / Code Quality / etc

[![PyPI version](https://badge.fury.io/py/opencage.svg)](https://badge.fury.io/py/opencage)
[![Downloads](https://pepy.tech/badge/opencage/month)](https://pepy.tech/project/opencage)
[![Versions](https://img.shields.io/pypi/pyversions/opencage)](https://pypi.org/project/opencage/)
![GitHub contributors](https://img.shields.io/github/contributors/opencagedata/python-opencage-geocoder)
[![Build Status](https://github.com/OpenCageData/python-opencage-geocoder/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/OpenCageData/python-opencage-geocoder/actions/workflows/build.yml)
![Mastodon Follow](https://img.shields.io/mastodon/follow/109287663468501769?domain=https%3A%2F%2Fen.osm.town%2F&style=social)

## Tutorials

You can find a [comprehensive tutorial for using this module on the OpenCage site](https://opencagedata.com/tutorials/geocode-in-python).

There are two brief video tutorials on YouTube, one [covering forward geocoding](https://www.youtube.com/watch?v=9bXu8-LPr5c), one [covering reverse geocoding](https://www.youtube.com/watch?v=u-kkE4yA-z0).

The module installs an `opencage` CLI tool for geocoding files. Check `opencage --help` or the [CLI tutorial](https://opencagedata.com/tutorials/geocode-commandline).


## Usage

Supports Python 3.8 or newer. Starting opencage version 3.0 depends on asyncio package.

Install the module:

```bash
pip install opencage
```

Load the module:

```python
from opencage.geocoder import OpenCageGeocode
```

Create an instance of the geocoder module, passing a valid OpenCage Data Geocoder API key
as a parameter to the geocoder modules's constructor:

```python
key = 'your-api-key-here'
geocoder = OpenCageGeocode(key)
```

Pass a string containing the query or address to be geocoded to the modules' `geocode` method:

```python
query = '82 Clerkenwell Road, London'
results = geocoder.geocode(query)
```

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

```python
results = geocoder.geocode('London', no_annotations=1, language='es')
```

For example you can use the proximity parameter to provide the geocoder with a hint:

```python
results = geocoder.geocode('London', proximity='42.828576, -81.406643')
print(results[0]['formatted'])
# u'London, ON N6A 3M8, Canada'
```

### Reverse geocoding

Turn a lat/long into an address with the `reverse_geocode` method:

```python
result = geocoder.reverse_geocode(51.51024, -0.10303)
```

### Sessions

You can reuse your HTTP connection for multiple requests by
using a `with` block. This can help performance when making
a lot of requests:

```python
queries = ['82 Clerkenwell Road, London', ...]
with OpenCageGeocode(key) as geocoder:
    # Queries reuse the same HTTP connection
    results = [geocoder.geocode(query) for query in queries]
```

### Asyncronous requests

You can run requests in parallel with the `geocode_async` and `reverse_geocode_async`
method which have the same parameters and response as their synronous counterparts.
You will need at least Python 3.8 and the `asyncio` and `aiohttp` packages installed.

```python
async with OpenCageGeocode(key) as geocoder:
    results = await geocoder.geocode_async(address)
```

For a more complete example and links to futher tutorials on asyncronous IO see
`batch.py` in the `examples` directory.

### Non-SSL API use

If you have trouble accesing the OpenCage API with https, e.g. issues with OpenSSL
libraries in your enviroment, then you can set the 'http' protocol instead. Please
understand that the connection to the OpenCage API will no longer be encrypted.

```python
geocoder = OpenCageGeocode('your-api-key', 'http')
```

### Exceptions

If anything goes wrong, then an exception will be raised:

- `InvalidInputError` for non-unicode query strings
- `NotAuthorizedError` if API key is missing, invalid syntax or disabled
- `ForbiddenError` API key is blocked or suspended
- `RateLimitExceededError` if you go past your rate limit
- `UnknownError` if there's some problem with the API (bad results, 500 status code, etc)

## Command-line batch geocoding

Use `opencage forward` or `opencage reverse`

```
opencage forward --help

options:
  -h, --help            show this help message and exit
  --api-key API_KEY     Your OpenCage API key
  --input FILENAME      Input file name
  --output FILENAME     Output file name
  --headers             If the first row should be treated as a header row
  --input-columns       Comma-separated list of integers (default '1')
  --add-columns         Comma-separated list of output columns (default 'lat,lng,_type,_category,country_code,country,state,county,_normalized_city,postcode,road,house_number,confidence,formatted')
  --workers             Number of parallel geocoding requests (default 1)
  --timeout             Timeout in seconds (default 10)
  --retries             Number of retries (default 5)
  --api-domain          API domain (default api.opencagedata.com)
  --optional-api-params
                        Extra parameters for each request (e.g. language=fr,no_dedupe=1)
  --unordered           Allow the output lines to be in different order (can be faster)
  --limit               Stop after this number of lines in the input
  --dry-run             Read the input file but no geocoding
  --no-progress         Display no progress bar
  --quiet               No progress bar and no messages
  --overwrite           Delete the output file first if it exists
  --verbose             Display debug information for each request
```

<img src="batch-progress.gif"/>


## Copyright & License

This software is copyright OpenCage GmbH.
Please see `LICENSE.txt`

### Who is OpenCage GmbH?

<a href="https://opencagedata.com"><img src="opencage_logo_300_150.png"/></a>

We run a worldwide [geocoding API](https://opencagedata.com/api) and [geosearch](https://opencagedata.com/geosearch) service based on open data.
Learn more [about us](https://opencagedata.com/about).

We also run [Geomob](https://thegeomob.com), a series of regular meetups for location based service creators, where we do our best to highlight geoinnovation. If you like geo stuff, you will probably enjoy [the Geomob podcast](https://thegeomob.com/podcast/).

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/OpenCageData/python-opencage-geocoder/",
    "name": "opencage",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "geocoding, geocoder",
    "author": "OpenCage GmbH",
    "author_email": "info@opencagedata.com",
    "download_url": "https://files.pythonhosted.org/packages/17/28/543a42164349694fff81001ca96a2485d4ca587539e2de31f42a1c37dbbe/opencage-3.0.3.tar.gz",
    "platform": null,
    "description": "# OpenCage Geocoding Module for Python\n\nA Python module to access the [OpenCage Geocoding API](https://opencagedata.com/).\n\n## Build Status / Code Quality / etc\n\n[![PyPI version](https://badge.fury.io/py/opencage.svg)](https://badge.fury.io/py/opencage)\n[![Downloads](https://pepy.tech/badge/opencage/month)](https://pepy.tech/project/opencage)\n[![Versions](https://img.shields.io/pypi/pyversions/opencage)](https://pypi.org/project/opencage/)\n![GitHub contributors](https://img.shields.io/github/contributors/opencagedata/python-opencage-geocoder)\n[![Build Status](https://github.com/OpenCageData/python-opencage-geocoder/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/OpenCageData/python-opencage-geocoder/actions/workflows/build.yml)\n![Mastodon Follow](https://img.shields.io/mastodon/follow/109287663468501769?domain=https%3A%2F%2Fen.osm.town%2F&style=social)\n\n## Tutorials\n\nYou can find a [comprehensive tutorial for using this module on the OpenCage site](https://opencagedata.com/tutorials/geocode-in-python).\n\nThere are two brief video tutorials on YouTube, one [covering forward geocoding](https://www.youtube.com/watch?v=9bXu8-LPr5c), one [covering reverse geocoding](https://www.youtube.com/watch?v=u-kkE4yA-z0).\n\nThe module installs an `opencage` CLI tool for geocoding files. Check `opencage --help` or the [CLI tutorial](https://opencagedata.com/tutorials/geocode-commandline).\n\n\n## Usage\n\nSupports Python 3.8 or newer. Starting opencage version 3.0 depends on asyncio package.\n\nInstall the module:\n\n```bash\npip install opencage\n```\n\nLoad the module:\n\n```python\nfrom opencage.geocoder import OpenCageGeocode\n```\n\nCreate an instance of the geocoder module, passing a valid OpenCage Data Geocoder API key\nas a parameter to the geocoder modules's constructor:\n\n```python\nkey = 'your-api-key-here'\ngeocoder = OpenCageGeocode(key)\n```\n\nPass a string containing the query or address to be geocoded to the modules' `geocode` method:\n\n```python\nquery = '82 Clerkenwell Road, London'\nresults = geocoder.geocode(query)\n```\n\nYou can add [additional parameters](https://opencagedata.com/api#forward-opt):\n\n```python\nresults = geocoder.geocode('London', no_annotations=1, language='es')\n```\n\nFor example you can use the proximity parameter to provide the geocoder with a hint:\n\n```python\nresults = geocoder.geocode('London', proximity='42.828576, -81.406643')\nprint(results[0]['formatted'])\n# u'London, ON N6A 3M8, Canada'\n```\n\n### Reverse geocoding\n\nTurn a lat/long into an address with the `reverse_geocode` method:\n\n```python\nresult = geocoder.reverse_geocode(51.51024, -0.10303)\n```\n\n### Sessions\n\nYou can reuse your HTTP connection for multiple requests by\nusing a `with` block. This can help performance when making\na lot of requests:\n\n```python\nqueries = ['82 Clerkenwell Road, London', ...]\nwith OpenCageGeocode(key) as geocoder:\n    # Queries reuse the same HTTP connection\n    results = [geocoder.geocode(query) for query in queries]\n```\n\n### Asyncronous requests\n\nYou can run requests in parallel with the `geocode_async` and `reverse_geocode_async`\nmethod which have the same parameters and response as their synronous counterparts.\nYou will need at least Python 3.8 and the `asyncio` and `aiohttp` packages installed.\n\n```python\nasync with OpenCageGeocode(key) as geocoder:\n    results = await geocoder.geocode_async(address)\n```\n\nFor a more complete example and links to futher tutorials on asyncronous IO see\n`batch.py` in the `examples` directory.\n\n### Non-SSL API use\n\nIf you have trouble accesing the OpenCage API with https, e.g. issues with OpenSSL\nlibraries in your enviroment, then you can set the 'http' protocol instead. Please\nunderstand that the connection to the OpenCage API will no longer be encrypted.\n\n```python\ngeocoder = OpenCageGeocode('your-api-key', 'http')\n```\n\n### Exceptions\n\nIf anything goes wrong, then an exception will be raised:\n\n- `InvalidInputError` for non-unicode query strings\n- `NotAuthorizedError` if API key is missing, invalid syntax or disabled\n- `ForbiddenError` API key is blocked or suspended\n- `RateLimitExceededError` if you go past your rate limit\n- `UnknownError` if there's some problem with the API (bad results, 500 status code, etc)\n\n## Command-line batch geocoding\n\nUse `opencage forward` or `opencage reverse`\n\n```\nopencage forward --help\n\noptions:\n  -h, --help            show this help message and exit\n  --api-key API_KEY     Your OpenCage API key\n  --input FILENAME      Input file name\n  --output FILENAME     Output file name\n  --headers             If the first row should be treated as a header row\n  --input-columns       Comma-separated list of integers (default '1')\n  --add-columns         Comma-separated list of output columns (default 'lat,lng,_type,_category,country_code,country,state,county,_normalized_city,postcode,road,house_number,confidence,formatted')\n  --workers             Number of parallel geocoding requests (default 1)\n  --timeout             Timeout in seconds (default 10)\n  --retries             Number of retries (default 5)\n  --api-domain          API domain (default api.opencagedata.com)\n  --optional-api-params\n                        Extra parameters for each request (e.g. language=fr,no_dedupe=1)\n  --unordered           Allow the output lines to be in different order (can be faster)\n  --limit               Stop after this number of lines in the input\n  --dry-run             Read the input file but no geocoding\n  --no-progress         Display no progress bar\n  --quiet               No progress bar and no messages\n  --overwrite           Delete the output file first if it exists\n  --verbose             Display debug information for each request\n```\n\n<img src=\"batch-progress.gif\"/>\n\n\n## Copyright & License\n\nThis software is copyright OpenCage GmbH.\nPlease see `LICENSE.txt`\n\n### Who is OpenCage GmbH?\n\n<a href=\"https://opencagedata.com\"><img src=\"opencage_logo_300_150.png\"/></a>\n\nWe run a worldwide [geocoding API](https://opencagedata.com/api) and [geosearch](https://opencagedata.com/geosearch) service based on open data.\nLearn more [about us](https://opencagedata.com/about).\n\nWe also run [Geomob](https://thegeomob.com), a series of regular meetups for location based service creators, where we do our best to highlight geoinnovation. If you like geo stuff, you will probably enjoy [the Geomob podcast](https://thegeomob.com/podcast/).\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "Wrapper module for the OpenCage Geocoder API",
    "version": "3.0.3",
    "project_urls": {
        "Download": "https://github.com/OpenCageData/python-opencage-geocoder/tarball/3.0.3",
        "Homepage": "https://github.com/OpenCageData/python-opencage-geocoder/"
    },
    "split_keywords": [
        "geocoding",
        " geocoder"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fe80cc0ff15bbc646c1262895845b2a74b35326c218e4796d27108bdd90eb144",
                "md5": "a31586fa71f61c0b086df5ad8cf1c98d",
                "sha256": "c2344b8ada19f86508be2c428b4a0396f7dd4e5fc8d72e6a5b607ddc281ef469"
            },
            "downloads": -1,
            "filename": "opencage-3.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a31586fa71f61c0b086df5ad8cf1c98d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 22445,
            "upload_time": "2024-11-09T21:26:26",
            "upload_time_iso_8601": "2024-11-09T21:26:26.422264Z",
            "url": "https://files.pythonhosted.org/packages/fe/80/cc0ff15bbc646c1262895845b2a74b35326c218e4796d27108bdd90eb144/opencage-3.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1728543a42164349694fff81001ca96a2485d4ca587539e2de31f42a1c37dbbe",
                "md5": "358e4a978e6cdae5782aad025408686a",
                "sha256": "46b52e48c268aa5103304890dfeb4115d8a8606c8f2459fd0ea26297f27e3fea"
            },
            "downloads": -1,
            "filename": "opencage-3.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "358e4a978e6cdae5782aad025408686a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 19219,
            "upload_time": "2024-11-09T21:26:28",
            "upload_time_iso_8601": "2024-11-09T21:26:28.244337Z",
            "url": "https://files.pythonhosted.org/packages/17/28/543a42164349694fff81001ca96a2485d4ca587539e2de31f42a1c37dbbe/opencage-3.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-09 21:26:28",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "OpenCageData",
    "github_project": "python-opencage-geocoder",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "opencage"
}
        
Elapsed time: 3.57502s