Every once in a while a question comes up about using geohash values for mapping in Tableau, so I figured it was time to write up a quick blog post on how to deal with these in Tableau.
Geohash is a hierarchical method of encoding spatial location using a string of letters and numbers instead of using a latitude and longitude value.
For instance, the geohash s27efy is the equivalent of (16.3° E, 2.1°N).
While it is easy to convert between geohash and latitude/longitude, there isn't currently a method to do this built into Tableau. But...it is easy to do with a short Python script as part of a Tableau Prep workflow.
This morning I decided to knock out the script to convert geohash to latitude/longitude (and latitude/longitude to geohash, just to be comprehensive...who knows, maybe someone really wants to incorporate that into a Prep workflow as well!) (Script on Github)
It's a simple process to incorporate the script into your Tableau Prep workflow:
I discussed the basic setup in my last Prep-related blog post (Geocoding in Tableau Prep 2019.3 with TabPy) so I won't go into deep detail on setup in this post, and will just give a cursory review and then get straight to running through how the script works.
1. Go to the Tableau Github repo and check out the code for . Follow the instructions to install and get it running.
2. Install the pygeohash library in your Python environment
3. Add a table with geohashes to your Prep workflow. I made a table with random geohash values if you want something to use for testing
4. Make sure your TabPy server is up and running, then add the geohash 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.
How does the script work? What does it do?
The script is super simple... all it does is walk through the input table from Prep, for each row it looks at the value in the 'geohash' field (if your field has a different name, just update the script so that the field name matches...), then decodes the geohash to lat/lon using the pygeohash library. Once we have the latitude and longitude values that are returned from pygeohash, we just drop those into new fields in the dataframe and return the new values to Tableau Prep. Easy peasy.
The script that I used also defines an output schema - you can use this or comment it out. All this does is define what the table being returned by the Python script will look like. Since we're returning more columns than the original data source, we have to tell Tableau Prep what to add into the output table (otherwise the nicely calculated latitude and longitude values disappear).
This output schema is based on the format of the sample input table that I used and the two new fields that I calculated in the script. Edit based on YOUR data so that the table returned has all of the columns that you want. OR, you can just comment this part of the script out and add in the new Latitude and Longitude columns as calculated fields as part of your Tableau Prep workflow. Your choice...but you only have to do one!
The bonus bit in the script:
Just in case you have latitude and longitude values and prefer to have them encoded as geohashes (which Tableau won't geocode by default, but maybe you want them for some reason...I'm not judging...) - I went ahead and wrote the little bit of Python to convert back to geohash. By default I'm using 6 characters of precision. Edit as you see fit to get a more or less precise geohash value:
And that is all there is to it! Hopefully this helps make it just a bit easier for you to use geohash values in Tableau!