The Find nearest analysis enables you to find the closest points between two datasets in CARTO Builder. This is useful if your map contains a large amount of point data within a close proximity.
"Nearest" is measured by linear distance, which calculates the spatial relationship from one point of an input layer to another point of a target layer.
The result is a modified dataset that includes the target geometries as a new column.
Some examples of how you can use the Find nearest analysis might be to find the five closest ATMs near a cash-only bar, identify the top two competitors near your store, or locate the closest open spaces available for real-estate development in urban areas. For this guide, let's find the closest fast food restaurants near downtown metro stops in Madrid, Spain.
Import the template .carto file packaged from "Download resources" of this guide and create the map. Builder opens with Madrid Metro Lines as the first map layer, Station Names as the second layer, and Fast Food Locations as the third map layer.
From the LAYERS pane, create a new layer from the original source of data for the Madrid Metro Lines.
B (Station Names), click the original source of data,
Drag it above layer
B (Station Names), underneath layer
C (Fast Food Locations).
A new layer,
D, is created.
Rename the map layer to Nearest Food to Metro.
Click the Nearest Food to Metro layer.
Click the ANALYSIS tab to add an analysis to the layer.
Apply the Find nearest analysis using the following parameters:
The result displays the closest fast food location near every metro station. Optionally, you can change the MAX RESULTS to change the closest number of results. For example, show me the top three results, or the top five results, and so on.
Switch to the Data View of the Nearest Food to Metro layer to see the new columns that were added to your data,
Let's apply some styling to better visualize the results.
Switch back to the Map View and modify the size of the marker:
Enable hover pop-up information windows to display more details about the data:
Optionally, you can enhance the Find nearest analysis using the PER GROUP option, which enables you to categorize search result by a specified column. This results in a larger number of points, since you are visualizing the closest results for each group of data.
From the Nearest Food to Metro layer, click the ANALYLSIS tab.
From the Find nearest parameter options, enable the PER GROUP checkbox.
Select name as the column to categorize results by.
Click APPLY to rerun the analysis.
The results display the closest Fast Food location, by name (McDonalds, Burger King, KFC, and so on), near each metro station. Try using the widgets to filter the results by cuisine and/or Fast Food Name.
If you are interested in using the underlying functions in the SQL view of Builder, view the
ST_Distance PostGIS documentation about spatial relationships, and the
ORDER BY description about PostGIS operators.
We would love to hear from you! Was it easy to understand? Do you need more information? Let us know.