This week there was a question on the Tableau Community forums about working with Google Plus codes in Tableau. That made me wonder how difficult it would be to convert between lat/lon pairs and Google Plus codes using Tableau Prep. It turns out to be pretty easy using the same general steps as I've used in other Prep geocoding / data conversion examples - you just need a data source with Google Plus codes or lat/lon pairs in Tableau Prep, to start up TabPy Server, and then run a simple Python script. In this post I'll give a rundown on how to make it work.
Quick links to the important parts to making this all work:
And...if you are interested in other data conversion options, here are some more bits to read:
First - what are Google Plus codes? Google Plus codes are just another method for encoding specific locations on the earth's surface. Instead of using a latitude and longitude pair, or a place name (e.g., 'Seattle'), a Google Plus code uses a set of letters and numbers to identify the location. For instance, the location of the Tableau headquarters can be identified in a number of ways - address, latitude and longitude, or with the Google Plus code of 84VVJMX6+2Q
Now, how do we work with them in Tableau Prep? It's easy to convert between latitude and longitude to Google Plus codes using the Google Plus Codes API
The API provides an easy URL-based method for requesting conversion to or from Google Plus Codes. You do not need a Google API key to do basic conversions! You can just send in a basic request using your email address as an ID. For instance:
returns the following with all sorts of interesting location data.
To use this in Tableau Prep, all you need is a data table, TabPy Server, and a small Python script. Let's get rolling and build a Prep workflow and try it out!
We'll start with lat/lon to Google Plus code. Add a dataset with some lat/lon values in Tableau Prep. I'm going to use a test dataset that I created with both lat/lon and Google Plus codes so that I could use the same dataset for both examples. I've put the file up on Github if you want to use it for your own testing.
To make our lives easy with wrestling the Python script, I'm going to add a new plus_code field to the table with a 'Clean' step (that way I don't just overwrite the existing plus_code field
I'm going to fill in a dummy value so that I know if new results were calculated as a result of running the python script:
Now it's time to check out the Python script and see how it's going to do its magic...
Grab the script from Github
Save it somewhere where you can find it again.
Take a look through to see if you need to edit any of the field names to match up with your dataset or Tableau Prep workflow... it's a simple script, I'll walk through it and highlight the parts that you might edit:
These are libraries that you need to make the script work. If you don't already have them as part of your Python installation, get them.
Defines the function that you'll call from Tableau Prep. You just tell Prep which function name to go to and magic happens
The Google Plus Codes API requires an email address. Edit this line and put your email address in
This part walks through your input file (what Tableau Prep has sent over to the script) and grabs the values from your latitude and longitude fields. In my sample data they are named 'lat' and 'lng' - if you are using other field names, change these in the script!
Once the values have been grabbed from the data, they are sent off to the Google Plus URL in line 37
Next the script sends the URL off and gets some data back
If the result is good (status = 'OK') then the data is dropped into a field named 'plus_code2' (that was the name I used in the cleaning step in Prep when I made a dummy field for the results to go into...if you used a different name, edit this to reflect the name you chose).
And then it's done and you can run your script in Tableau Prep and check out the result.
How do you run the script in Prep???
First, Start TabPy Server! I forget this part all the time and wonder why things aren't working... Remember to start TabPy Server. I use a windows machine, so I just open a command prompt, navigate to the location where I dropped TabPy-server and start it up. I'm on a windows machine and see something like this:
In Tableau Prep, add a Script step
Point it to the script and the right function name:
Let the script run.
Look at the results. My result table has duplicates for the Plus Codes because my original table already had the values in it...but you can see the new values made it successfully into the plus_codes2 field! Yay!
If you want to go back to lat and lon from the plus code, you can just use the other function in the Python file that I linked on Github. That will go from a field of Google Plus codes and return TWO fields with the latitude and longitude.
Again, just make sure that the input column names and output column names match ones in your table in Tableau Prep, and then point your script to the right function
And that should be all there is to it! Hopefully this works for you as well.