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.
- You can define the maximum number of points to be visualized near your target.
- Additionally, you can enhance the analysis by finding the closest results per group; which calculate and categorize results by a specified column from your dataset.
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.
Find the Nearest Results
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.
Click on “Download resources” from this guide to download the zip file to your local machine. Extract the zip file to view the .carto file(s) used for this guide.
From the LAYERS pane, create a new layer from the original source of data for the Madrid Metro Lines.
CHEATSHEET: Create Layer from original source data
Each map layer identifies the connected dataset as the original source of data. From the LAYERS pane of Builder, drag and drop the original source above or below the selected layer to create a new layer. Note that the original source data is separate from analysis nodes of data (A1, A2, B1, B2), which are temporary cached results for the map layer.
- From Layer
B(Station Names), click the original source of data,
Click the original source of data connected to the layer. For more details about how to use map layers, see the Understanding Map Layers in Builder Guide.
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 INPUT layer is the selected layer.
- For the TARGET, select layer C0, Fast Food Locations.
- For MAX RESULTS, keep the default of 1 selected. This indicates that only a single, closest result will appear.
- Leave PER GROUP unspecified for now.
- Click APPLY.
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,
The Data View and Map View appear as icons on your map visualization when a map layer is selected. Click to switch between viewing your connected dataset as a table, or show the map view of 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:
Click the STYLE tab.
Click the SIZE value to open the size options for the marker. The FIXED tab appears by default.
Type 14 as the value.
Enable hover pop-up information windows to display more details about the data:
- Click the POP-UP tab.
- Select the HOVER subtab.
- Select the LIGHT style for the pop-up.
- Click the checkbox next to the
- Rename them Type of Cuisine and Fast Food Name, respectively.
View Nearest Results per Group
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.
If calculating Find nearest PER GROUP, duplicated points may be generated in order to represent every instance near a group. This is intentional by design. Output geometries may be duplicated, but will have different ids.
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.
Download the final .carto file, packaged from the “Download resources” of this guide, to view how all of these enhancements were made. The final map also contains a custom legend, which include symbols and text to describe your map data. Additional enhancements could be to join the results to the source dataset as another analysis, or style the layer by a column value (using the newly created columns).
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.
Did you like this guide?
We would love to hear from you! Was it easy to understand? Do you need more information? Let us know.