### Sarah Battersby's Blog

May 12, 2017 Previous day Next day

# Working with projected data in Tableau – Part I: Map projection basics

Posted by Sarah Battersby May 12, 2017

Sometimes people send me questions about how to work with custom map projections in Tableau.  For instance, Alan Eldridge asked me about working with polar ice data. Or, sometimes I get questions about making a US map with Alaska and Hawaii in a little inset in the corner of your map – without needing to use three worksheets, or how to make a map of Canada that is in the Lambert Conformal Conic projection (the most common projection used at Statistics Canada) instead of Web Mercator... It turns out that all of these things are possible in Tableau:

Since working with map projections is a pretty big topic, and something that I like quite a bit, I’ve written up a few blog posts on it.  I’ve broken them into two parts: map projection basics and data manipulation for Tableau.  The two parts should stand alone, so if you want more background about what is going on under the hood, you can start here with Part I (map projection basics), and if you don’t care about the basic mappy science that goes behind the fun tricks, you can just jump to Part II (data manipulation)

Since we have a lot to cover, let’s get rolling and go through enough projection basics (in general) for you to be dangerous be creative with map projections in your visualizations.

While I’ll give a brief overview of what happens in the projection process, I’m going to focus on a small set of topics important to working with alternate map projections in Tableau.  For more in-depth discussion of projections, I recommend:

## What is a map projection?

Map projection is the process of transforming angular (e.g., latitude and longitude) coordinates into planar coordinates.   The projection is just a mathematical definition of how we go from the angles (spherical or geographic coordinate system) into a planar, projected coordinate system (the good ol’ Cartesian coordinates that you might remember from elementary school).

The fun maddening part of map projections is that whenever we do this transformation from 3-dimensional angular coordinates to 2-dimensional planar coordinates we introduce distortion in the form of one or more of the following: shapes, areas, angles, and distances…and just to make things more fun with distortion, know that it’s impossible to preserve both areas and angles on the same flat map!

Why do we have these distortion problems to deal with?  I find that an easy way to think about this is to peel the skin off an orange.  Is there any way to remove the skin and end up with a completely flat, un-torn, un-mangled peel? Even if you can manage a one-piece skin that looks pretty flat when you squish it down do you think it would look good as a map?  Probably not. See my attempts:

To bring that back to the map, think of a map projection as the mathematical warping, stretching, tearing, and stitching back together of the orange peel / Earth surface.  There are infinite ways of doing this, so the possibilities are endless with respect to what your final map looks like. For instance:

When making maps, a good cartographer will try to do two things: 1) minimize distortions so that their map is as accurate as possible for the location being mapped and the purpose of the map (e.g., do you need to take angular measurements?  Are distances really important?  Are relative areas really important?), and 2) make the map look nice.

Equal area projections with two different looks…I like one more than the other…

Most geopolitical entities (countries, states, provinces, counties, etc.) will have specific projections suggested for use in order to minimize the distortion in that area. It’s common to see the same general projection used in different locations, but with slight modifications to the parameters of the projection to tailor it to the location.  Here is a Lambert Conformal Conic a central meridian of 0° and standard parallels at 20° and 50°:

For regional data, you might find Canadian data in this very same Lambert conformal conic map projection, but with standard parallels at 49° and 77° and with a central meridian of 91° 52’ W (Statistics Canada details here; image on the left below), while for the conterminous United States (the ‘lower 48’), you might see the same projection used, but with standard parallels of 33° and 45° and a central meridian of 96° W (image on the right below).  They are similar, but not the same – look at the stretching in the far north for a good visual indicator.

Or, consider them arranged on top of one another.  The projection with the parameters set to optimize for Canada are in orange, and for the US are in blue:

While cartographers typically like to pick different projections based on the location they are mapping, in many general purpose, non-geographic information system (GIS) mapping tools, there is only one projection to choose from and that one projection has to work for mapping local-scale (e.g., city), global-scale, and everything in-between.  This projection is Web Mercator.  I will use this particular projection as the example moving forward to explain the basic principles behind bringing your projected spatial data into Tableau.

## Well, hello Web Mercator.  It’s nice to meet you!

Tableau uses the Web Mercator map projection for mapping; this is the same map projection that you’ll see in pretty much every web mapping service (Google Maps, Yahoo Maps, etc.).  While some people have strong negative opinions of this projection due to the distortion in the high latitude regions, for better or worse, it has become the standard for online mapping.

Why is this particular map projection so popular??  Most map services are designed for mapping from local to global scale – and we have to pick a single projection to work with, nothing will be optimal and this projection has some benefits, like:

1. it’s rectangular so it is easy to chop into the individual little map tiles that nest inside each other for viewing at different scales,
2. the math behind it is simple and quick,
3. because it is a rectangular projection, North is always the same direction (typically the top of the map), and
4. because it is (nearly) conformal (preserves angles on the map), for local-scale mapping we don’t see things like distortions of the angles at which roads intersect.  There are some other reasons, and if you are really a glutton for map projection punishment, I can recommend a bit of reading that you might enjoy (Implications of Web Mercator and its use in online mapping)

Now let’s get to the fun stuff – what do you need to master if you want to make maps in Tableau that are not displayed in Web Mercator coordinates? It’s really just remembering a simple property of map projections – that the projected coordinates are just one big Cartesian coordinate system, and that in order to draw your geographic data, the system (Tableau, a GIS, whatever) displaying your data doesn’t care what projection you are using, it just wants to know what the x&y coordinates for every vertex in your data and then each vertex is dropped onto the right spot on the Cartesian coordinate system.

What does that mean to you? It means that you get a big blank ‘canvas’ on which you can draw your data, and so long as your data set’s coordinates are within the right range (and your coordinates think they are in the same coordinate system as our base map, Web Mercator), you should be able to drop any polygons you want onto the map.  Here is what that map ‘canvas’ looks like in Tableau with our Web Mercator base map underneath:

Take, for example, the coordinates for the conterminous United States (‘lower 48 states’)….here is the same dataset in a few different map projections – note the differences in shapes and bounding box coordinates listed for each:

Pretty pictureDetails

Plate Carrée - Treats spherical coordinates as planar coordinates

Bounding box for the US:

xMin, yMin (-124.7, 24.9)

xMax, yMax (-66.9, 49.4)

The range will be between  -180 and 180 for x and -90 and 90 for y (degrees of longitude and latitude)

Web Mercator

Bounding box for the US:

xMin, yMin (-13885338.72, 2870437.22)

xMax, yMax (-7454921.06, 6338171.97)

Albers Equal Area

Bounding box for the US:

xMin, yMin (-2235903.34,-1643504.03)

xMax, yMax (2125235.69,1322165.76)

You should be able to see in the bounding box coordinates that they would each draw in different locations on our ‘canvas,’ but let’s go ahead and throw them all into a Tableau base map to see where they fall.  Before we do this, think back to our graphic of the coordinate system that we’re working with, take a look at the bounding boxes listed above for each of the datasets (in projected coordinates), and imagine where they will fall on the map. Then keep reading to see if they line up with what you expected.

Plate Carrée – when zoomed to the extent of the data it looks just as expected.

But when we zoom out, something strange happens…our states disappear!  Why?  Think about where data with this bounding box falls in a coordinate system that runs from -20mil to +20mil:

xMin, yMin (-124.7, 24.9)

xMax, yMax (-66.9, 49.4)

Yes, it becomes a super-tiny set of polygons right around 0,0.

Web Mercator – it shows up in the right place…because the coordinates are just right for dropping that US shape on top of the US in our Web Mercator base map

Albers Equal Area - looks good, but totally in the wrong place! Why?  Because this particular variant of the Albers projection was tailored to be centered on the US, which puts 0,0 right in the middle of the US.

## The big so-what…

Map projections just define the translation from angular to planar coordinates.  Once your data is in planar coordinates, so long as the range on those coordinates fits inside the extent of our Web Mercator map tiles, you can put any data you want onto the map.  But…as you can see above, the Tableau base map tiles may not provide quite the right context underneath your map data.

In my next post, I’ll go through an example of how to adjust projections on your spatial datasets and bring them into Tableau.

# Working with projected data in Tableau – Part II: Data manipulation

Posted by Sarah Battersby May 12, 2017

In Part I: map projection basics, we took a look at what map projections are in general, learned about the map projection that is used in the Tableau base map (Web Mercator), and saw a few examples of what a single dataset represented in different coordinate systems would look like if it were drawn in the Web Mercator coordinate system.

In Part II, we’re going to walk through an example of how you can move away from the Web Mercator projection with some creative data adjustment.

Ingredients needed:

• A spatial data file (Shapefile, JSON, etc.) [the example data that I used came from Statistics Canada]
• QGIS – a FREE and open-source Geographic Information System (or you can use whatever GIS, database, programming language, or other tool you prefer for manipulating spatial data…there are plenty of options)

Caveats:

• If you use this trick you will need to make the Tableau basemap fully transparent (it has to disappear) or make your own custom tiles in Mapbox.  It’s best if the dataset you are working with is polygons that are familiar enough in shape that you don’t need the extra spatial context provided by the Tableau base map.

• You will not be able to use the Tableau Map Search.  Remember from Part I, we are going to move things around on the base map, so if you search for a location Tableau will take you to where that spot is in Web Mercator coordinates, which may be very different than where your data is on the map. But, you can use filters or a highlighter to allow people to find locations based on a dimension in dataset (so, in a sense, create your own custom map search based on the locations in your dataset).

So, now let’s dig in and see how this all works!

Open your spatial file in QGIS.  Click on Layer -> Add Layer -> Add Vector Layer and select the .shp file, or other spatial file format of choice, that you want to work with (either your own file or the example data file linked above)

Check the projection of your dataset.  The short story here is that so long as you have a projection defined (and that it is correct), you are good to go.  If you want to know more about the projection of the dataset you are working with you can right click on the listing for your dataset in the legend on the left side of the QGIS window and select Properties.  In the properties window, look under General and Coordinate reference system.

The Coordinate reference system information for our sample file tells us that this is “EPSG:4269 – NAD83.”  Whaaaa? This information is actually pretty simple to decode once you know the trick:

EPSG = European Petroleum Survey Group.  They catalogue definitions for map coordinate systems and assign each a unique reference code.

4269 = The reference code for this particular coordinate system.  You can get more information at the for this particular code at the spatialreference.org website

NAD83 = North American Datum of 1983.

What I read here is that the dataset has a bounding box that is within the range I would expect for latitude and longitude (longitude: -180 to 180, latitude: -90 to 90), so even if I didn’t know that the dataset was in EPSG:4269 I could make a good guess that it was just plain ol’ latitude and longitude coordinates.

Project your dataset into your preferred coordinate system.  We could bring this Shapefile into Tableau directly using the spatial file support in 10.2…and it would work just fine:

But…I assume that since you’re reading this, you really want to see the data in a different coordinate system – like, perhaps, the coordinate system recommended by Statistics Canada (like the Statistics Canada Lambert, which happens to be defined by EPSG:3347).

To update the projection for our dataset, right click on the dataset name in the legend on the left side of the QGIS window and select ‘Save As…’

Give the file a new name, and change the CRS to ‘Selected CRS’ and then click on the ‘Change…’ button.  This will open the Coordinate Reference System Selector (shown on the right below).  Find the coordinate system that you want to use – I just typed in the EPSG code (3347) because I happened to know it.  Otherwise you can scoll through the large number of coordinate systems supported in QGIS and select the one you like.

Check out the properties of your new file.  If you look at the properties for the new file that you saved, you should see updated information for the coordinate system and for the bounding box listed in the metadata.  Use the same steps we used in #2 above.

Note how much larger these coordinates are and that the definition states that the coordinates are now provided in ‘m’ (meters)

If you open a new file in QGIS and add this file, you’ll also notice that it looks different than the original:

Note that we could bring this NEW file, in the new coordinate system, into Tableau 10.2 and it would also work just fine.  Tableau is smart enough to look for the projection definition in your file and do the conversion to put it into the same coordinate system as our basemap.

Now we get to the fun part...Tell a little white lie with your data.  We’ve seen above that Tableau will take a projected Shapefile and convert it into Web Mercator coordinates to match our basemap.  But, sometimes we don’t want Web Mercator.  So, here is where the fun comes in.  We make our data think that it’s in Web Mercator so that it will use that coordinate system when it draws.

DISCLAIMER
You want to be really careful here – you’re about to edit your Shapefile so that it has the wrong coordinate system listed for it.  Label this file carefully; you do not want to accidentally try to use this in analyses outside of Tableau.  We really are telling a white lie with data here and this is just a trick so that you can have a different ‘shape’ map in Tableau for visual analysis, it is not a pathway to success in spatial data analysis.

Now that I’ve gotten that out of the way, we will continue with the process of little white data lies.

Open the properties for your newly projected file (right click on the name -> properties) and where it says that the Coordinate reference system is EPSG:3347 – NAD83 / Statistics Canada Lambert…go ahead and click Specify and just change that to WGS84 / Pseudo Mercator (EPSG: 3857).

What that did was update the text that defines the coordinate system for your dataset. It did not actually change the coordinate system, it just changed what the file thinks its coordinate system is.

Now you need to save a new copy of the Shapefile (right click -> Save As…) to make sure that the new definition is saved.  When I do this, I like to append some information in the file name to remind myself that this is a faux Web Mercator file.

Now what will happen if we add this new file to Tableau??

We have our newly projected data available in maps!

Granted, you need to make the basemap transparent so that you don’t see that this is just a little data trick…

Why would we want to do this?  Check out a side-by-side comparison of the data in Web Mercator coordinates vs. Statistics Canada Lambert… The distortion on Web Mercator makes a big difference in the visual for this dataset!

Using this trick you should now be able to see and understand (and communicate!) using projected spatial data in Tableau.

By date: By tag: