Find Nearest

Analysis Guides

Find Nearest

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

  1. 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.
  2. From the LAYERS pane, create a new layer from the original source of data for the Madrid Metro Lines.

    • From Layer B (Station Names), click the original source of data, madrid_stations.

      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.

      Create new layer from source

  3. Click the Nearest Food to Metro layer.

  4. Click the ANALYSIS tab to add an analysis to the layer.

  5. 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.

Styling Results

  1. Switch to the Data View of the Nearest Food to Metro layer to see the new columns that were added to your data, source_cartodb_id, target_cartodb_id, and closest_dist.

    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.

    Data View of map layer

    Let's apply some styling to better visualize the results.

  2. 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.

    Modify fill size of marker

  3. 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 cuisine and name columns.
    • Rename them Type of Cuisine and Fast Food Name, respectively.

    Hover pop-up information window

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.
  1. From the Nearest Food to Metro layer, click the ANALYLSIS tab.

  2. From the Find nearest parameter options, enable the PER GROUP checkbox.

  3. Select name as the column to categorize results by.

    group nearest results by column

  4. 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).

External Resources

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.