Spatial data is any type of data that directly or indirectly references a specific geographical area or location.
Tableau can map three types of spatial data: points, lines, and polygons using Spatial functions. Spatial functions allow you to perform advanced spatial analysis and combine spatial files with data in different formats like text files or spreadsheets. For example, we might have a spatial file that has information regarding city council districts and a text file that has the latitude and longitude coordinates of reported crimes. We can use a spatial calculation while creating your data source to join these files and analyze which district has the highest crime rate.
We can also create a line that connects two data points for origin-destination maps. For example, we can use a spatial calculation to see the flight routes.
In this article, we will learn about various Spatial Functions available in Tableau.
AREA () – Syntax - AREA(polygon, units) is a function used to calculate the total surface area of a spatial polygon.
BUFFER () – Syntax - BUFFER (geometry, number, units) Returns a polygon shape with a radius determined by the distance and unit values defined in the calculation.
DISTANCE () - Syntax - DISTANCE (Start, End, Units) is a function used to measure the distance between two points in a specified unit.
MAKELINE () – Syntax - MAKELINE(start, end) is a function used to generate a line mark between two points. Useful when building origin-destination maps.
MAKEPOINT () - Syntax - MAKEPOINT(number, number) is a function used to convert latitude and longitude columns into spatial objects.
Area() Spatial Function
The area function is used to calculate the area of a spatial polygon. Besides, we can opt for different units (meters, miles, kilometers, etc.). Let us understand the area function with the help of an example.
Dataset - Parks and Gardens (polygons)
The area of the polygon can also be defined in other units such as acres or hectares. We connect to the spatial file containing polygons of parks in central London and used the following calculation to return the area of each polygon in hectares – there are 640 acres in a square mile:
Step 1: Connect to the dataset
Step 2: Drag the geometry field to the detail. When we drag the field to details,a map will be displayed showing all the parks and gardens in the United Kingdom. After zooming in we will have the following visualization.
Step 3: Zoom in further and then drag the Name field to the Label.
Step 4: Select the random parks from the visualization and click Keep only. By doing this, Tableau will automatically filter our data based on our selection and we will be able to work on only selected parks. tableau will automatically create Name filter.
Step 5: Now in this example we want to show areas in Acres that are not supported by the AREA function. So we will create it from other area units that are supported. We will create a calculated field to find the area in miles and then convert miles into acres by multiplying it by 640 (1 mile = 640 acres).
Step 6: Drag the Area calculated field to labels. This will show the area of the parks. Our final visualization will look as below.
It’s important to be aware that the accuracy of the Area calculation is dependent on the accuracy of the polygon being used.
MAKELINE(), MAKEPOINT() AND DISTANCE() Spatial Function
Our next example is how to create a route using spatial functions. In this example we will be covering three spatial functions: MAKELINE, MAKEPOINT, AND DISTANCE to show the route of airlines from origin to destination based on the selection of the origin airport.
Dataset Download -
Step 1: Connect to the Dataset.
Step 2: First we have to create the origin point and destination point. Go to the worksheet and there we will create a calculated field and name it Origin Point. For creating the origin point we will use the function MAKEPOINT which takes two arguments Latitude and Longitude. As we are making Origin point we will use Origin Latitude and Origin Longitude. This will give us the Spatial Object based on the X (Origin Latitude) and Y (Origin Longitude) coordinates.
Step 3: Similarly we will create Destination Point using Destination latitude and Destination Longitude.
Step 4: Now we need to create a line joining Origin Point and Destination Point. For this, we will create another calculated field using the MAKELINE function that takes Starting and End Points as arguments. In our case, starting point is Origin Point and the end point is Destination Point.
Step 5: Drag the Route calculated field to details and drag Origin Airport to the filters shelf and select Atlanta (ATL) or any other airport. Drag Route Details to Details This will show all the flights originating from Atlanta.
Step 6: Now we have to show the distance between the origin point and destination point. For that, we will create a calculated field using the DISTANCE function which takes three arguments: origin point, destination point, and distance (kilometer, miles, feet, etc.). In this example, we will be showing the distance in Miles.
Step 7: Now drag the Distance calculated field to the tooltip and we can see the distance between the origin and destination.
We can add a unit after the distance in the tooltip by double-clicking it and adding miles after the distance.
Step 8: Now we change the background of our map. Here I have chosen the background map as Dark
After all formatting, our final visualization looks as follows. From the filter, we can select the origin airport from which we want to see the flights detail.
BUFFER() Spatial Function
BUFFER function is used to create a boundary around a spatial point - as a way of measuring the distance between two points.
To demonstrate the Buffer function we will see an example that shows the pubs around a selected hotel within a particular radius.
Download Dataset -
Step 1: Before connecting to the data set we have done some changes in the dataset. In our dataset, we have latitude and longitude values for each hotel and pub together. We have to split the values of latitude and longitude for hotels and pubs separately.
Now open a new workbook and connect to the dataset.
Step 2: Drag Latitude and Longitude to rows and columns and change them to the dimension. All the points are plotted on the map.
Step 3: Create a parameter Hotel. Select Data Type as String and Allowable value as List and Add value from Hotel name. This parameter will show the list of all the hotels present in our dataset and will allow the users to select the hotel dynamically.
Step 4: We will create another parameter for the radius of the circle which will show the pubs near the hotel in that radius.
Step 5: We will create a calculated field Selected point using the MAKEPOINT() Spatial function in which we specify that the hotel name selected from the parameter gives the value of the latitude and longitude for the hotel.
Step 6: Create another calculated field Hotel Buffer using the BUFFER() Spatial function which will create a circle around a selected point( in this case Hotel) of the specified radius.
Step 7: Drag Hotel to Label and Hotel buffer to Detail. Show both the Hotel and radius parameters. Change the background map to the street view.
Step 8: Drag the latitude pill by pressing the control button and make another latitude pill. Then create the Dual Axis
Step 9: For the LAT2 field remove the Hotel Buffer and Hotel name and drag Pubs name to the Label.
Now our visualization is ready. The visualization shows the number of pubs that are in a defined radius from the hotel.From the parameters, we can change the hotel name and the radius if we want to see pubs in bigger or smaller area based on our requirements.
In this article we have seen various ways we can apply spatial function in real-life data visualization examples.
Using these spatial functions is actually very straightforward once you have a data source with the location coordinates in it. The addition of Spatial Functions adds loads of new possibilities for using maps in Tableau. I hope you’ve really enjoyed reading this article. Thanks for reading.