How to Convert Shapefiles to #Tableau Polygons

featured_text

Introduction

This article is a collaborative segment of work in many ways. I show two methods to convert shapefiles to Tableau polygons and one method to generate polygon centroids. In the first method, which is very quick, you do not need to have Alteryx. There is an Alteryx Analytics Gallery application that will do the conversion for you.

In the second method, you do need an Alteryx license to do the work. The reason I chose to demonstrate each of these methods is that I have used both methods and I wanted to document how I use them. The second method also produces additional output compared to the first method.

The second method is more of an Alteryx master-class approach in which a macro is used to process the shapefile, which gives Alteryx users a nice building block from which to extend their work, if needed.

If this is true, you might wonder why I didn’t use Alteryx in the title? Well, I wanted all Tableau users to know that the first conversion method does not need an Alteryx license to use it and I want them to begin exploring how to use Alteryx effectively!

Background

The reason for this work is that I am trying to add more detail to some recent work I did for Path.org In this work, I visualized overland surface water flow in Tableau. In that article, I did not include any man-made structures (i.e., buildings) in the analysis.

Now I want to include the effects of the buildings so that I can visualize how the overland flow changes due to the man-made structures. The results of that work will be shown in an upcoming article, assuming it is possible for me to do it! I’m out on the cutting edge here and I do not yet know what is going to happen, although my brain thinks it is possible.

To do this requires that I have all the man-made structures in the model. The incredibly prolific and brilliant Allan Walker has provided to me a shapefile that contains 28,524 buildings in the Livingstone district in Zambia, Africa.

This information was assembled by a team of volunteers that worked tirelessly over a long time period to digitize the buildings. If you want to see how some of that was done, click this link to read about the work of Max – a highly talented 11 year-old. From that article, you can follow another link that gives you more detailed insights into the data I show in this article.

Taking a Peek At the Shapefile Content

Before I launched into processing the shapefiles to convert them to polygons for Tableau, I used Alteryx to give me a quick peek at the content of the files.

As shown in Figure 1, a quick workflow was all that was needed to create a map (Figure 2) to show the Livingstone District buildings.

quick_shapefile_viz

Figure 1 – A really quick visualization of the Livingstone building shapefile coverage. Alteryx reads the *.shp file directly and allows you to pick a basemap in the map report tool to visualize your site data.


alteryx_buildings
Figure 2 – The Alteryx report map showing the Livingstone District buildings.

Now that I knew what to expect from the data, I launched into the conversion methods for turning the shapefile into Tableau polygons.

Method 1 – The Alteryx Gallery Method

You can learn about this method by reviewing a few references:

  1. Tableau Mapping Website – A collaboration of Allan Walker and Craig Bloodworth.
  2. The Alteryx Analytic Gallery Link – This does the job for you and you do not have to an Alteryx license.
  3. Bryant Howell’s Website – Tableau and Behold! – I included this link because I have been looking for a reason to point readers to this site. Bryant works for Tableau and is in my estimation one of the best Technical Tableau bloggers I have discovered in the past year or so. If you haven’t seen this site, go there and begin following him.

The video shown below demonstrates the usage of this tool for the Livingstone buildings data. I go from showing the original files, to uploading a zip file to the analytic app, to drawing the polygons in Tableau.


Method 2 – The Alan Eldridge Alteryx Macro Method

If you have access to Alteryx and you want to practice your geospatial skills or you want to build out a more comprehensive workflow, you can use this information as a starting point. I used this method last night to process this data and thought it would be a good instructional example because I had to modify Alan’s macro to make it work for me.

You can learn about this method by reviewing this reference:

  1. Using Shape Files for Boundaries in Tableau – You will need to be able to use dropbox to download an Alteryx macro file (*.yxmc). For me, the dropbox sent an ascii file that I had to drop into a text editor and save as a *.yxmc file. The link for downloading this is shown in Alan’s article in Figure 3 (the word “here”). I include my modified version of this macro below.

shapefiles

Figure 3 – Go to Alan’s article and click the word “here” to get his original *.xymc file and/or commands from dropbox if you want. Otherwise, you can download my copy below.


I used Alan’s macro as a starting point because I wanted to do some other geospatial operations on the Livingstone Data. I wanted to write out separate files for the building polygons and the polygon centroids. I wanted to calculate the area of the buildings in square feet, and I needed to change the threshold for the polygon generalization because the buildings were small relative to the spatial extents of my data set.

To do these things required that I change Alan’s macro settings and then add some additional controls in my macro driver program. For these reasons, I’m going to provide the macro code and my driver program in this article. Click here if you want to download this information. You will need to open the Excel file and save the *.yxmc and *.yxmd files to your system (use the guidelines Alan gives in his article).

To gain access to the building (polygon) areas, I updated the Spatial Info tool to include additional fields (areas, number of points = vertices) as shown in Figure 4.

spatial_info

Figure 4 – The Spatial Info tools (the ?) allows us to write out a variety of properties for spatial objects. In this application, I also had to change the generalize polygon setting to a much lower value than Alan had since the buildings are small.


After I changed the macro settings, I built a driver program as shown in Figure 5.

macro_driver_program

Figure 5 – A macro driver program to write out separate polygon and building centroid files.


This program connects to the shapefile and performs a few operations to be able to split the building polygons and the building centroids into separate files.  The video shown below shows how you can use the building centroid file in Tableau. The usage of the polygon file is the same as what was shown in the previous video.


After running the macro, the building centroid data file can be loaded into Tableau and the dot sizes and colors can be used to represent the area of the buildings, as shown in Figure 6. Alternatively, as shown in Figure 7, colors can be used to represent the area of the buildings to be able to visually determine where the larger buildings are located. When combined with a filter, this action makes for a fun dynamic game of “watching the buildings disappear!”.

livingstone_buiding_centroids

Figure 6 – The building centroids and areas in square feet.


Livingstone_buildings2.jpg

Figure 7 – Buildings colored by area (ft^2), which was calculated by Alteryx. Two or three years ago, who would have ever thought that Tableau and Alteryx could be combined to create a map like this?

Additional Resources

Ken Flerlage wrote a program to convert SVG (scalable vector graphics) to Tableau polygons. Click here to read his work.

One thought on “How to Convert Shapefiles to #Tableau Polygons

  1. Pingback: 3danim8's Blog - Test 6 of Tableau Vs Power BI: The Shapefile Showdown

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.