All tutorials

Python

intermediate

The Python wrapper is useful for Python developers who wish to seamlessly integrate the what3words Public 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).

1
2

Installation

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

pip install what3words
Copied
3

Setup

Import

import what3words
Copied

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.

Public API:

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

API Server:

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

Usage

Convert to 3 word address

This function converts coordinates (expressed as latitude and longitude) to a 3 word address.

More information about convert_To_3wa, including returned results is available in the what3words REST API documentation.

Find the words for (51.484463, -0.195405):

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

Convert to coordinates

This function converts a 3 word address to a position, expressed as coordinates of latitude and longitude.

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

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

Find the words for ///prom.cape.pump:

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

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 information 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)
Copied

Code example AutoSuggest, clipping the results returned to France and Germany:

res = geocoder.autosuggest('filled.count.soap', clip_to_country="FR,DE")
print(res)
Copied

Code example AutoSuggest, Focus on (51.4243877,-0.34745).

res = geocoder.autosuggest('filled.count.soap', \
    focus=what3words.Coordinates(51.4243877,-0.34745))
print(res)
Copied

Code example AutoSuggest, with Generic Voice input type.

res = geocoder.autosuggest('fun with code', input_type='generic-voice', language='en')
print(res)
Copied

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 information about grid_section, including returned results is available in the what3words REST API documentation.

Get a grid for 52.207988,0.116126) in the south west, and 52.208867,0.117540 in the north east:

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)
Copied

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 information about available_languages, including returned results is available in the what3words REST API documentation.

res = geocoder.available_languages()
print(res)
Copied

Handling errors

Errors returned from the API can be caught with the wrapper through the use of a catch function.

Within the catch function, code and message values which represent the error, are accessible from the error object parameter

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)
Copied

isPossible3wa

This method takes a string as a parameter and returns whether the string is in the format of a 3WA (e.g. “filled.count.soap”).

Returns a boolean parameter [True or False].

Note: Do not check if it is an actual existing 3WA.

isPossible3wa(“filled.count.soap”) returns True
isPossible3wa(“not a 3wa”) returns False
isPossible3wa(“not.3wa address”) returns False
Copied

findPossible3wa

This method takes a string as a parameter and searches the string for any possible instances of a 3WA – e.g. “leave in my porch at word.word.word.”

Returns an array of matched items or returns an empty array if no matches are found.

We recommend using this method within your called delivery notes.

Note: Do not check if it is an actual existing 3WA.

findPossible3wa(“Please leave by my porch at filled.count.soap”) will return [‘filled.count.soap’]
findPossible3wa(“Please leave by my porch at filled.count.soap or deed.tulip.judge”) will return [‘filled.count.soap’, ‘deed.tulip.judge’]
findPossible3wa(“Please leave by my porch at”) will return []
Copied

isValid3wa

This method takes a string as a parameter and first passes it through the what3words regex filter (akin to calling isPossible3wa() on the string) and then calls the what3words API to verify it is a real what3words address.

isValid3wa(“filled.count.soap”) returns True
isValid3wa(“filled.count.) returns False
isValid3wa(“python.is.cool”) returns False
Copied

Related tutorials