Skip to main content
This gem runs in .

Overview

Find the closest spatial point(s) between two datasets based on geographic distance. This gem compares each point in the first dataset to all points in the second dataset and returns the nearest matches.
The gem has a corresponding interactive gem example. See Interactive gem examples to learn how to run sample pipelines for this and other gems.

Prerequisites

  • Add ProphecyDatabricksSqlSpatial version 0.0.3 or higher to your project.

Input and Output

The output schema of out contains:
  • All columns from in0.
  • All columns from in1.
  • rank_number: The rank of each match, where 1 is the closest point.
  • distance: The calculated distance between the source and target points in the unit of measurement that you specify.
  • cardinal_direction: The compass direction from the source point to the target point (e.g., NW for northwest).
Geographic points must be in Well-known Text (WKT) format. Use the CreatePoint gem to convert longitude and latitude coordinates to WKT format.

Parameters

Configure the FindNearest gem using the following parameters.

Spatial Object Fields

Select Output Options

Example: Customer service centers

Assume you have two tables:
  • customer_locations that contains the location of each customer.
  • service_centers that contains the location of each service center.
You can use the FindNearest gem to find the nearest service centers to each customer.
  1. Add the FindNearest gem to your pipeline canvas.
  2. Connect the customer_locations table to the FindNearest in0 input port.
  3. Connect the service_centers table to the FindNearest in1 input port.
  4. Open the FindNearest gem configuration.
  5. For Source Centroid Type, select Point.
  6. For Source Centroid Column, select the customer_point column.
  7. For Target Centroid Type, select Point.
  8. For Target Centroid Column, select the center_point column.
For this example, let’s find the two nearest service centers in a 1000 km radius.
  1. Type 2 in the How many nearest points to find? field.
  2. Type 1000 and choose Kilometers in the Maximum distance field.
  3. Lastly, save and run the gem.

Result

The output contains the two closest service centers to each customer. Ranks begin at 1, with 1 being the closest point. Note that customer C002 only has one service center within 1000 km from their location.