Python Wrapper for the what3words Web API

A Python library to use the what3words REST API.

The Python wrapper is useful for Python developers who wish to seamlessly integrate the what3words API into their Python applications, without the hassle of having to manage the low level API calls themselves.

The what3words API is a fast, simple interface which allows you to convert 3 word addresses such as ///index.home.raft to latitude and longitude coordinates such as (-0.203586, 51.521251) and vice versa. It features a powerful autosuggest function, which can validate and autocorrect user input and limit it to certain geographic areas (this powers the search box on our map site). It allows you to request a section of the what3words grid (which can be requested as GeoJSON for easy display on online maps), and to request the list of all languages supported by what3words. For advanced users, autosuggest can be used to post-process voice output. See links on the left to navigate.

All coordinates are latitude,longitude pairs in standard WGS-84 (as commonly used worldwide in GPS systems). All latitudes must be in the range of -90 to 90 (inclusive).

Step 1: Obtain an API key

Sign up to obtain your free API key.

Step 2: Installation

The library is available through PyPi. To install what3words, simply:

pip install what3words

Step 3: Setup

Import

import what3words

Initialise

By default, the wrapper will point to the public version of the what3words API. However, if required, the wrapper can be configured to point to a custom endpoint, for example a locally running version of the API. In this case an optional end_point parameter can be passed to the what3words.Geocoder constructor.

geocoder = what3words.Geocoder("what3words-api-key")
geocoder = what3words.Geocoder("what3words-api-key", end_point='http://localhost:8080/v3')

Step 4: Usage

Convert to 3 word address

Convert coordinates, expressed as latitude and longitude to a 3 word address.

More info about convert_to_3wa, including returned results is available in the what3words REST API documentation.

Code Example
res = geocoder.convert_to_3wa(what3words.Coordinates(51.484463, -0.195405))
print(res)

Convert to coordinates

Convert a 3 word address to a position, expressed as coordinates of latitude and longitude.

This function takes the words parameter as a string of 3 words 'table.book.chair'

More info about convert_to_coordinates, including returned results is available in the what3words REST API documentation.

Code Example
res = geocoder.convert_to_coordinates('prom.cape.pump')
print(res)

Autosuggest

When presented with a 3 words address which may be incorrectly entered, autosuggest returns a list of potential correct 3 word addresses. It needs the first two words plus at least the first character of the third word to produce suggestions.

This method provides corrections for mis-typed words (including plural VS singular), and words being in the wrong order.

Optionally, clipping can narrow down the possibilities, and limit results to:

  • one or more countries
  • a geographic area (a circle, box or polygon)

This dramatically improves results, so we recommend that you use clipping if possible.

To improve results even further, set the focus to user’s current location. This will make autosuggest return results which are closer to the user.

More info about autosuggest, including returned results is available in the what3words REST API documentation.

Code Example Simple basic call.
res = geocoder.autosuggest('filled.count.soap')
print(res)
Code Example Autosuggest, clipping the results returned to France and Germany.
res = geocoder.autosuggest('filled.count.soap', clip_to_country="FR,DE")
print(res)
Code Example Focus on (51.4243877,-0.34745).
res = geocoder.autosuggest('filled.count.soap', \
    focus=what3words.Coordinates(51.4243877,-0.34745))
print(res)

Grid Section

Grid Section returns a section of the what3words 3m x 3m grid as a set of horizontal and vertical lines covering the requested area, which can then be drawn onto a map.

The requested box must not exceed 4km from corner to corner, or a BadBoundingBoxTooBig error will be returned.

More info about grid_section, including returned results is available in the what3words REST API documentation.

Code Example
sw = what3words.Coordinates(52.207988,0.116126)
ne = what3words.Coordinates(52.208867,0.117540)
bb = what3words.BoundingBox(sw, ne)

res = geocoder.grid_section(bb)
print(res)

Available Languages

This function returns the currently supported languages. It will return the two letter code, and the name of the language both in that language and in English.

More info about available_languages, including returned results is available in the what3words REST API documentation.

Code Example
res = geocoder.available_languages()
print(res)

Handling Errors

The API returns a dictionary object representing the JSON response returned by the API. In the case of an error, we would expect to see an error key in the API response object. Within the error key, there will be code and message values, which represent the error.

Code Example
autosuggest = geocoder.autosuggest('freshen.overlook.clo', clip_to_country="FR")

if 'error' in autosuggest: # An error has been returned from the API
    code = autosuggest['error']['code']
    message = autosuggest['error']['message']

    print (code, message)

Step 5: Putting It All Together

The example below takes the concepts described above, and turns some of them into a complete example. Here we take a partial 3 word address and pass it into Autosuggest - clipping the results to consider only addresses in France, setting a focus of Paris, and returning a single result

We then take the result and convert the 3 word address to coordinates, and find the nearest place.

import what3words
from os import environ

geocoder = what3words.Geocoder("what3words-api-key")

autosuggest = geocoder.autosuggest('freshen.overlook.clo', \
    clip_to_country="FR", \
    focus=what3words.Coordinates(48.856618, 2.3522411), \
    n_results=1, \
)

if 'error' in autosuggest: # An error has been returned from the API
    code = autosuggest['error']['code']
    message = autosuggest['error']['message']

    print (code, message)
else:
    # Obtains the one, and only result from the returned list of suggestions
    words = autosuggest['suggestions'][0]['words']
    print("Top 3 word address match: {}".format(words))

    # Use the `convert_to_coordinates` API to convert the returned 3 word address into coordinates
    convert_to_coordinates = geocoder.convert_to_coordinates(words)

    print("WGS84 Coordinates: {}, {}".format( \
        convert_to_coordinates['coordinates']['lat'], \
        convert_to_coordinates['coordinates']['lng']))
    print("Nearest Place: {}".format(convert_to_coordinates['nearestPlace']))

Step 6: Further Reading

See the what3words GitHub repository for access to full source code of the Python wrapper and further documentation.

Complete examples of each endpoint are also available in the sample application