Are you wondering how you can make your data pop and sing? Take a lesson from our Head of Analytics, Phillip, who shows you how to create a Tableau shape file from an image. 

In a world where there’s more data than you can shake a stick at, we are highlighting the golden nuggets to help you find out which data points really matter, and what you do with them to make data work harder for your business. Discover Data Pointers…

Ah Tableau, the wonderful software that helps every Analyst understand their data.  Instead of singing Tableau’s praises, we wanted to dive into something deeper – in the form of shapes. We wanted to take advantage of the Tableau feature where you can plot polygons, however to do this you need to have a Shape File which effectively has the X and Y coordinates of each of the individual shapes.

The reason for doing this is that we wanted to have irregular shapes plotted on a data visualisation to help bring it to life, i.e. making key areas become highlighted when the cursor hovered over it. We thought that learning how to do this would give us the key to doing some cool data viz stuff, so here I am sharing how we did this as it ended up being a lot tougher than we thought.

For this piece, I am going to run through how I got from the below image… (which I beautifully created in paint – LONG LIVE PAINT!)

Tableau_Shape_File_Input
Tableau_Shape_File_Input

… to this, which is the above shapes plotted in Tableau.

Tableau_Shape_File_Output
Tableau_Shape_File_Output

 

I thought that converting the above image into a shape file would be relatively straightforward, however this turned into being a bit of a rabbit hole with a few dead-ends along the way. Despite the few hiccups, I’ve listed the steps below on how to do this. This is mainly a re-hash of a blog post here with the addition of a script that takes the inputs and outputs into a shape file usable by Tableau.

You Will Need:

  • Some version of R (I use R Studios to manage and run the script)
  • GDAL Software (this is some fancy-pants software used by Geography/Cartography people) but there is a python script buried in there that we need to make this work.
  • A TIFF image with some shapes in it – you can use the one in the folder you are about to download. (You will just need to rename it to example.tiff to get it to work.)
  • Tableau Desktop (A version that lets you plot shape files)
Step 1: Install GDAL

OK, so the actual ‘thing’ that does the work is a python script called gdal_polygonize.py (and this is called in the R script you are about to download in the following steps.) To get that you need to install a package called GDAL. As I am on Windows machine, I downloaded and installed the http://trac.osgeo.org/gdal/wiki/OSGeo4W version. As a heads up, this is relatively big and takes a bit of time to install.

Step 2: Download Example Files 

You can download all the files you need for this here, once you unzip that you should find the below files.

  • Image_processing_script.R – this is the R script that inputs the Example.tif file and outputs the Shape_File.csv which can be read by Tableau.
  • Example.tif –  this is the image file that the R script will covert, it’s important that this is saved in the TIF format.
  • Shape_File.csv – this is what is outputted from the script, but I have left it in so you can see what you are aiming for.
  • Shape_Plot.tbw – this is a tableau file that plots the polygons.
Step 3: Open and Run the R script in R Studio

The only thing you should need to configure is the working directory, which should be the same as where the R script is. I have clearly labelled this in the script. It’s likely to complain if there are any missing packages, if any are missing please go ahead and install the missing ones.

Note: When you run the script the system.time(p <- polygonizer(edge_raster)) line is going to take a long time, but this is where the magic happens.

Step 4: Open the File in Tableau

If you open the Tableau file in the attached excel, this has plotted the outputted file so you are able to see what you are aiming for. Plotting shape files in Tableau is quite straightforward, and there is a good example on how to do it here.

Hopefully this was useful but if there are any problems please write in the comments, and I will try to answer. Also, if you do anything cool with this it would be awesome to let us know and also link back to the site!

 

Leave a Reply