All tutorials
Python
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.
A full example of how to integrate the what3words Python wrapper is available in our
Python Github repository
The library is available through PyPi. To install what3words, simply:
pip install what3words
You need to import the library
import what3words
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.
# Using the Public API: geocoder = what3words.Geocoder("what3words-api-key")
# Using the API Server: geocoder = what3words.Geocoder("what3words-api-key", end_point='http://localhost:8080/v3')
Convert to what3words address
This function converts coordinates (expressed as latitude and longitude) to a what3words address.
More information about ConvertTo3wa
, 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)
Convert to coordinates
This function converts a what3words address to a position, expressed as coordinates of latitude and longitude.
It takes the words parameter as a string of a what3words 'table.book.chair'
More information about ConvertToCoordinates
, 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)
AutoSuggest
When presented with a what3words 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)
Code example AutoSuggest, clipping the results returned to the United Kingdom and Belgium:
res = geocoder.autosuggest('filled.count.soap', clip_to_country="GB,BE") print(res)
Code example AutoSuggest, Focus on (51.4243877,-0.34745
).
res = geocoder.autosuggest('filled.count.soap', \ focus=what3words.Coordinates(51.4243877,-0.34745)) print(res)
Code example AutoSuggest, with Generic Voice input type.
res = geocoder.autosuggest('fun with code', input_type='generic-voice', language='en') 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 information about GridSection
, 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)
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 AvailableLanguages
, including returned results is available in the what3words REST API documentation.
res = geocoder.available_languages() print(res)
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)
This section introduces RegEx functions that can assist with checking and finding possible what3words addresses in strings. The three main functions covered are:
isPossible3wa
– Match what3words address format;findPossible3wa
– Find what3words address in Text;isValid3wa
– Verify a what3words address with the API;
isPossible3wa
Our API wrapper RegEx function “isPossible3wa
” can be used to detect if a text string (like “filled.count.soap
“) in the format of a what3words address without having to ask the API. This functionality checks if a given string could be a what3words address. It returns true if it could be, otherwise false.
Note: This function checks the text format but not the validity of a what3words address. Use isValid3wa
to verify validity.
import what3words def main(): # Initialize the What3Words API with your API key api_key = 'YOUR_API_KEY' w3w = what3words.Geocoder(api_key) # Example what3words addresses addresses = ["filled.count.soap", "not a 3wa", "not.3wa address"] # Check if the addresses are possible what3words addresses for address in addresses: is_possible = w3w.isPossible3wa(address) print(f"Is '{address}' a possible what3words address? {is_possible}") if __name__ == "__main__": main()
Expected Output
- isPossible3wa(“filled.count.soap”) returns
true
- isPossible3wa(“not a 3wa”) returns
false
- isPossible3wa(“not.3wa address”)returns
false
findPossible3wa
Our API wrapper RegEx function “findPossible3wa
” can be used to detect a what3words address within a block of text, useful for finding a what3words address in fields like Delivery Notes
. For example, it can locate a what3words address in a note like “Leave at my front door ///filled.count.soap
”. The function will match if there is a what3words address within the text. If no possible addresses are found, it returns an empty list.
Note:
- This function checks the text format but not the validity of a what3words address. Use
isValid3wa
to verify validity. - This function is designed to work across languages but does not work for
Vietnamese (VI)
due to spaces within words.
import what3words def main(): # Initialize the what3words API with your API key api_key = 'YOUR_API_KEY' w3w = what3words.Geocoder(api_key) # Example texts texts = [ "Please leave by my porch at filled.count.soap", "Please leave by my porch at filled.count.soap or deed.tulip.judge", "Please leave by my porch at" ] # Check if the texts contain possible what3words addresses for text in texts: possible_addresses = w3w.findPossible3wa(text) print(f"Possible what3words addresses in '{text}': {possible_addresses}") if __name__ == "__main__": main()
Expected Output
- findPossible3wa(“Please leave by my porch at filled.count.soap”) returns
['filled.count.soap']
- findPossible3wa(“Please leave by my porch at filled.count.soap or deed.tulip.judge”) returns
['filled.count.soap', 'deed.tulip.judge']
- findPossible3wa(“Please leave by my porch at”) returns
[]
isValid3wa
Our API wrapper RegEx function “isValid3wa
” can be used to determine if a string is a valid what3words address by checking it against the what3words RegEx filter and verifying it with the what3words API.
import what3words def main(): # Initialize the what3words API with your API key api_key = 'YOUR_API_KEY' w3w = what3words.Geocoder(api_key) # Example addresses addresses = [ "filled.count.soap", "filled.count.", "coding.is.cool" ] # Check if the addresses are valid what3words addresses for address in addresses: is_valid = w3w.isValid3wa(address) print(f"Is '{address}' a valid what3words address? {is_valid}") if __name__ == "__main__": main()
Expected Outputs
- isValid3wa(“filled.count.soap”) returns
True
- isValid3wa(“filled.count.”) returns
False
- isValid3wa(“coding.is.cool”) returns
False
Also make sure to replace <YOUR_API_KEY>
with your actual API key. These functionalities provide different levels of validation for what3words addresses, from simply identifying potential addresses to verifying their existence on Earth.
If you encounter errors or issues related to convert-to-coordinate requests while using the Free plan, please check the network panel for the following error message Error 402 payment required
and its response, indicating the need to upgrade to a higher plan:
{ "error": { "code": "QuotaExceeded", "message": "Quota Exceeded. Please upgrade your usage plan, or contact support@what3words.com" } }
For more information, visit our API plans page. If you need further assistance, contact support@what3words.com.
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']))