Tableau Geospatial: Nearest Neighbor Join in QGIS

Tableau Geospatial: Nearest Neighbor Join in QGIS

This is the third post in our geospatial series.  Check out Polygons, Lines and Points and Merging SHP files in QGIS.  This post will describe how to add a Nearest Neighbor Join to your shapefiles in QGIS.  Allan Walker showed me how to do this last year, but it was a good time to break this out again for the #IronViz geospatial competition.

What the heck is a nearest neighbor join?

For this example, I am going to use the NOAA hurricane data I used in my Sharknado Recreation viz. These shp files contain the lat/long point of every recorded hurricane.  However, it doesn’t have any information relating to the nearest point on land.  We might want to know the location of the nearest populated place on earth.  After all, these storms can have a devastating effect on people/places so this is the most important part of hurricane analysis…errr aerial shark attacks.

We are going to use an additional data source to add the nearest populated place to our hurricane data (i.e. nearest neighbor).


You can go to NOAA and download the entire dataset, but I am going to use a subset of cat3-5 hurricanes data for this example.  Download the file, unzip it.

1. These shp files do not have any attributes that help identify the files so I am going to add a new attribute called ‘Layer’ with the values ‘cat 3’, ‘cat 4’, and ‘cat 5’.  This will be very useful later in Tableau.  We could use this new dimension for color, shape, sorting, etc.  After adding the new field, merge the shp together in QGIS.  Need help here?  Check out the last post.

It should look something like this:

2. Now we need to add our second data source.  Go to Natural Earth Data.  Download the Populated Places file.  Unzip it and add the new vector layer in QGIS.

3. Install the NNJOIN Plugin in QGIS.  Click on Plugins->Manage and Install Plugins.  In the dialog box that opens, search for NNJOIN.  Select it and install it.

4. Click on the new NNJOIN button.  Select the Input and Join layers.

5. Right click on the new joined layer and select Save As.  Browse to save the new file.

Back to Tableau

1. Connect to your new NNJOIN spatial file in Tableau and drop Geometry on the marks card.

2. Style that map!  Need some ideas?  Check out Jeffrey Shaffer’s posts here and here.  This is all Tableau map styling, no Mapbox.


  • I added additional fields to the marks card to provide the hurricane name, ISO time, hurricane season.
  • I created some calculations specifically for using color in the Tooltip.
  • I used the Layer field we created in QGIS to bring in some custom shapes.  I also sorted the layers in descending order since there are less of the higher category storms.  This would not have been possible if we didn’t create this field in QGIS.

3. How about using the Nearest Neighbor in the tooltip?  The join gave us various levels of geo data on the nearest neighbor.  I created a custom tooltip calculation to create a multi-line description because sometimes city, state were null.

Here is the tooltip setup:

Here is the calculation:

IF ISNULL(IF [join ADM1N] = [join NAME]
    THEN [join NAME]
    ELSE [join NAME] + ', ' + [join ADM1N]

THEN [join ADM0N]


IF [join ADM1N] = [join NAME]
    THEN [join NAME] + char(10) +[join ADM0N]
    ELSE [join NAME] + ', ' + [join ADM1N] + char(10) +[join ADM0N]

Here is a GIF showing various use of the nearest neighbor in the tooltip.

Thanks for reading!  Hope this was interesting/helpful.

Leave a Reply