In Part II: Data manipulation, we walked through an example to convert a spatial boundary file for Canada so that it was in the Statistics Canada Lambert coordinate reference system. In Part III, we are going to use the same process to project a US dataset, but we’re going to add the special bonus of moving polygons to new locations!  For use in Tableau, this comes in handy when you have spatial data that is distributed across a large space and you want to have all of your geography in one worksheet and the true spatial relationships do not matter

map_tableauPublic.png

 

Before I go into instructions on how to do this, I want to reiterate my disclaimer / warning from Part II, with a few minor updates:

 

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 (and/or move geographic data to the wrong place in the world).  Label the files that you are working with 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 general spatial data analysis.    

 

Ingredients needed:

  • A spatial data file (Shapefile, JSON, etc.) [the example data that I used came from the US Census – I used 2016 Counties (and equivalent)]
  • 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 is 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).

 

If you’re feeling lazy… I’ve put my completed dataset up on Tableau Public for reference.

 

 

If you’re feeling adventurous, let’s dig in and make a new map!

 

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)

 

If needed, delete unnecessary bits.  Since the US county dataset includes territories as well as states, I am going to clean up a bit before we do our heavy editing.  This editing can be done graphically (Click on the pencil icon to start editing; use the selection tool to select the polygons to delete; then delete), but to save time, I’m going to do my selection in the attribute table:

Start editing – click on the pencil icon in the menu:

start_editing.png

Open the attribute table – right click on the layer name and select ‘Open Attribute Table’:

open_attribute_table.png

 

Click on the header in the ‘STATEFP’ column to sort ascending. Select all of the rows with a STATEFP value greater than 60 and then delete. 

 

Click on the pencil again to stop editing; save your edits.

 

We should now have a shapefile with counties from just the 50 US states.

 

 

Check the projection of your dataset.  As we saw in Part II, so long as you have a projection defined (or know the projection and can set it), you are good to go.  The Census data that we are working with in this example is NAD83 (North American Datum of 1983), and has an EPSG code of 4269.   You can read up on those acronyms in Part II, or through the links that I attached to the key terms.

crs_original.png

Project your dataset into your preferred coordinate system.   This part may take a bit of trial and error, based on the dataset that you are working with.  You want to pick a projection that looks good for all of your regions.  For the US dataset, I find that a Lambert Conformal Conic is quite nice.  Note that this was just a ‘select based on look’ process, and not on the technical characteristics of the projection. 

To check out the look of different projections, click on the CRS button in the lower right corner of QGIS:

crs_button.png

Check the box at the top of the window to ‘Enable ‘on the fly’ CRS transformation.’  This will allow you to update the projection / coordinate reference system for the entire view in QGIS.  Search for Lambert and select the USA Contiguous Lambert Conformal Conic.

crs_setLambert.png

 

You should now have a US that looks something like this:

us_lambert.png

To save the geographic coordinates in this projection (not just draw them in this coordinate system), we need to save a new copy of the shapefile and make sure QGIS saves the coordinates in the new coordinate system. First, right click on the dataset name and Save As…  Browse to the folder where you would like to save the file, and set the CRS to Lambert Conformal Conic.  Uncheck the ‘Add saved file to map’ checkbox.

save_as_lambert.png

 

Delete the projection information.  QGIS is smart and when we start moving around polygons to new places on the map, it will update the coordinates for the polygon based on the distortion patterns in the projection – so the nice shape of the polygons in our projection may not be quite so nice when we move them.  We’re going to avoid that problem and make QGIS forget that it knows what the projection for the data is (if you want to see what happens if you don’t do this, just ignore this step, move to the next step in the instructions, and have fun watching the shapes warp as you move them around). 

 

Find the new file that you saved and delete the .prj and .qpj files.  This is just removing any of the files that QGIS would use to identify the projection for the dataset. 

prj_qpj.png

 

Back in QGIS, open a new project (Project -> New).  You do not need to save the changes to your current project.

 

Add your newly projected file (the one where you just deleted the prj and qpj files). When you add this to QGIS you should see a warning that there is no coordinate reference system defined.  If you don’t see this, remove your file and go back and make sure that you deleted both the prj and qpj file.

crs_warning.png

 

Move some geography!  To move Alaska and Hawaii to your preferred spot we just need to edit, select the geography, move to a new location, and (maybe) re-scale so that it’s a good size. 

Start editing by clicking on the pencil icon.

 

Select the geography that you want to move – let’s start with Alaska.  The easiest way to get all of the little bits (darned Aleutian Islands), the easy way is to select by an expression. 

select_by_expression.png

We want to select all of the polygons where the STATEFP is ‘02’ (that is the official state code for Alaska):

select_by_expression_statefpis02.png

 

All of Alaska should now be selected.  It’ll be highlighted in yellow.  It may be hard to tell when zoomed out to the full extent of the dataset, since in edit mode all of the vertices are shown as big red x.

 

With the Alaska counties selected, use the Move Features tool

move_features.png

Grab Alaska, and drag it to approximately where you want it.

AK_move1.png

 

I think this is too large, so I’m going to re-scale the geography.  I use an Affine Transformation tool to scale the geography. If you don’t see an Affine Transformation tool in your Vector toolset, you will need to add it (Plugins-> Manage and Install Plugins; then search for affine transformation)

 

affine_transform_tool.png

Note that we’re really getting into lying with the data now – besides the distortion inherent in the projection, and the moving of geographies, we’re also now changing the relative size… So, I’m going to take a second to re-iterate and rephrase my original disclaimer: You are taking liberties with geographic data here. This is entirely for graphical benefit. Spatial relationships (size, distance, shape) are being altered!

 

Transform both the X and Y axis by 0.5 and click Transform. 

affine_transform_AK.png

 

This may take a minute to finish…there are a lot of vertices to update.  When it’s done, you should have a half-size Alaska.    You will probably need to use the Move tool to update the location after it’s been scaled. My data before and after moving Alaska:

AK_move2.png

 

Now select Hawaii (“STATEFP” = ‘15’) and do the same process.  Hawaii probably doesn’t need to be scaled quite so much.

 

When you’ve finished, save the edits by clicking on the pencil icon again and saving.  

 

One last step – Make Tableau think this dataset is in Web Mercator coordinates.  Open the properties for your edited file (right click on the name -> properties) and where it says that the Coordinate reference system is EPSG:4326, WGS84 (this is the default QGIS uses when it doesn’t know the real coordinate system)…go ahead and click on the Select CRS button and change the projection listed to WGS84 / Pseudo Mercator (EPSG: 3857).

change_crs_final.png

 

Now right click on the name of the dataset and Save As… give the file a new name, make sure that it is using the EPSG:3857 Web Mercator (or Pseudo Mercator) projection. 

 

Add your new dataset to Tableau, set the washout on the base map to 100%, and enjoy!  To add in attributes, just join them in Tableau using the FIPS code (COUNTYFP is the county-level codes in this dataset...note that these are STRINGS and have a leading 0 so that all state codes are two digits)

map_inTableau.png