2/20/2018
Discovering the Nearest Ocean Coast or any Nearest Level on a Map in Tableau
Brooke requested on Twitter if there was a option to discover the closest ocean coast for a given level on a map. I mocked up an answer for this and posted it on Twitter here. On this tutorial, I’ll stroll via the steps of my mock up.
How one can discover the closest coastal level
Step 1: Construct the coastal factors (or use any set of factors with latitude and longitude)
Any knowledge set of factors with latitude and longitude might be used, however to get the coasts for the mock up, I began with an information set of the continental United States (USA2163) in polygon format (latitude, longitude, polygon ID, Level Order). I plotted the factors and filtered out the factors that aren’t on the coast. This was fairly simple, simply highlighting the Mexican and Canadian borders and the Nice Lakes after which excluded them.
As soon as this was filtered out, I exported the information set with solely these factors into a brand new file. Should you favor to make use of your individual file of factors then all you want is latitude, longitude and a few kind of Level ID.
Step 2: Add an empty row to the information and import into Tableau
I added an empty row to the information. This might be achieved by becoming a member of a single row of information up with the polygon factors, however I simply manually added one row earlier than importing into Tableau. This empty row would be the level of origin on the map. I additionally created a brand new discipline referred to as « Hyperlink ». The vacation spot factors ought to be marked as « Hyperlink » (or Vacation spot) and the empty row is marked « origin ». If you wish to set the origin level within the knowledge, then add a discipline for « Origin Latitude » and « Origin Longitude » and enter that knowledge. In any other case, you may create these fields in Tableau or use a Parameter, which is what I did within the mock up.
The information is now able to be import into Tableau.
Step 3: Create parameters and calculated fields
Parameter Title: Origin Latitude as a Float (optionally available)
Parameter Title: Origin Longitude as a Float (optionally available)
Calculated Discipline Title: Origin Latitude
Components: [Parameters].[Origin Latitude]
(this may be hardcoded or taken from an information supply)
Calculated Discipline Title: Origin Longitude
Components: [Parameters].[Origin Longitude]
(this may be hardcoded or taken from an information supply)
To be able to calculate the closest level to the origin, we have to calculate the gap. I used the next distance components in Tableau to calculate the gap between the origin level and all the different factors.
Calculated Discipline Title: Distance
Components:
3959 * ACOS(SIN(RADIANS([Origin Latitude])) * SIN(RADIANS([Latitude])) + COS(RADIANS([Origin Latitude]))
* COS(RADIANS([Latitude])) * COS(RADIANS([Longitude]) – RADIANS([Origin Longitude])))
Subsequent, create a calculated discipline to get the minimal distance within the knowledge set.
Calculated Discipline Title: MIN Distance
Components: {mounted : MIN([Distance])}
In case you are utilizing a Parameter to vary the lat/lengthy then calculate a brand new set of lat/lengthy that may alter with the parameter for the origin level. I included extra elseif traces to populate the lat/lengthy for the Metropolis dropdown menu.
Calculated Discipline Title: New Latitude
Components:
if [Link] = ‘hyperlink’ then [Latitude]
else [Parameters].[Origin Latitude]
END
Calculated Discipline Title: New Longitude
Components:
if [Link] = ‘hyperlink’ then [Longitude]
else [Parameters].[Origin Longitude]
END
Lastly, create two fields for the trail, Path ID to group the factors collectively and Path Order to specify the opposite of the trail for the road.
Calculated Discipline Title: Path ID
Components:
If [Distance] = [MIN Distance] then 1
elseif [Link] = ‘origin’ then 1
else NULL
END
Calculated Discipline Title: Path Order
Components:
If [Distance] = [MIN Distance] then 1
elseif [Link] = ‘origin’ then 2
else NULL
END
As an optionally available step, create a label for the gap for the origin level.
Calculated Discipline Title: Label
Components:
if [Link] = ‘origin’ then [MIN Distance]
else NULL
END
Step 4: Constructing the Viz
Transfer New Longitude to Columns
Transfer New Latitude to Rows
Transfer Level ID to Element (Within the coastal file I used Level Order and Sub Polygon ID)
Transfer Path ID to Dimension (Elective – This may measurement the origin level bigger than the opposite factors)
You need to now have a map with the factors plotted. Within the mock up that is all the coastal factors.
Duplicate New Latitude on Columns.
On the second Marks Card:
Take away all the things from Element
Transfer Path ID to Colour
Change Mark sort to Line
Transfer Path Order to Path
Transfer MIN Distance to Tooltip
Transfer Label to Label
Proper-click on the duplicated New Latitude and choose Twin Axis
Give it a strive within the viz beneath. Choose a Metropolis from the dropdown menu or enter your individual latitude and longitude throughout the continental U.S. The viz will replace, calculating and plotting the minimal distance to the closest ocean coast. Be at liberty to obtain the Tableau Public workbook for the information and viz.
I hope you discover this data helpful. In case you have any questions be happy to e mail me at Jeff@DataPlusScience.com
Jeffrey A. Shaffer
Observe on Twitter @HighVizAbility