Another day, another random request for converting alternative coordinate systems to latitude and longitude to map in Tableau. Today's request was for using What3Words coordinates...so here are some basic instructions (and a script)!
What3Words is an addressing system that assigns a unique set of three words for each 3 meter square in the world. Fun!
Conveniently What3Words has an API to convert back and forth between latitude and longitude and What3Words coordinates. You can easily use this in your Tableau Prep workflow.
To use the API, you'll need a FREE developer key - get one here
I have opted to use the What3Words Python library (similar to what I demonstrated with converting geohashes in Tableau Prep), though there is also a service call that you can call instead (similar to what I demonstrated for geocoding in Tableau Prep)
1. Go to the Tableau Github repo and check out the code for . Follow the instructions to install and get it running.
3. Add a table with What3Words coordinates to your Prep workflow. I made a table with random What3Words coordinates (and lat/lon values in case you want to confirm the results). The table is on Github
4. Make sure your TabPy server is up and running, then add the what3words to lat/lon script to your Prep workflow. Make sure to connect to the TabPy server, select the file name, and enter the right name for the function that needs to be called. So long as you have the columns specified correctly, you should end up with new latitude and longitude fields with values to decode the what3words coordinates. (Note: check out how the script works, read my earlier blog posts on this topic if needed... and just make sure that you either ADD the fields in advance in your Prep workflow or set up the get_output_schema() function in the Python script. Either should work just fine).
How does the script work? What does it do?
The script simply walks through all of the rows in your table, grabs the what3words coordinate and decodes to get the latitude and longitude values. Here is the script...we'll walk through the individual parts next.
Breaking down the script:
First - import the library. You need to have this installed in your Python environment first.
Then we define the function that we'll call from prep. It takes a dataframe as an input and returns a dataframe to Prep. Tableau Prep will automatically convert your input table into a dataframe, so no work you need to do here other than call the script.
<do a bunch of stuff...we'll discuss the 'stuff' next>
What is the 'stuff' that goes in the function?
1. Put in your API key
W3W_API_KEY = 'YOUR_API_KEY_GOES_HERE'
2. Set up your what3words geocoder
g = what3words.Geocoder(W3W_API_KEY)
3. Walk through your data, row by row, and convert the what3words coordinate to lat/lon, then set the values in the appropriate columns
for i in range(0, len(df)):
res = g.convert_to_coordinates(df.iloc[i]['w3w'])
df.set_value(i, 'Latitude', res['coordinates']['lat'])
df.set_value(i, 'Longitude', res['coordinates']['lng'])
I have assumed that the what3words coordinate is all in one column (all three words in one place, separated by a '.'). If you have your words in three separate columns, you'll just need to combine those in the Python script before you convert to lat/lon. It's simple and you should be able to do it all at once before you start walking though the data row by row, for instance:
df['w3w'] = df['word1'] + '.' + df['word2'] + '.' + df['word3']
And that should be about it for making it work! Just make sure your input data matches the field names in the script and that you have the right output fields names defined in the get_output_schema() function OR have them already created as a step in Prep before you run the script.
Oh, and don't forget to start the TabPy Server. I forget just about every time and wonder why the script doesn't work. One of these days I'll learn...