Skip to main content

FindNearest

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.

tip

Geographic points must be in Well-known Text (WKT) format. Use the CreatePoint gem to convert longitude and latitude coordinates to WKT format.

Input and Output

PortDescription
in0Input dataset containing the source points for which you want to find the nearest points.
in1Input dataset containing the target points to compare against.
outOutput dataset with the nearest point(s) from in1 for each point in in0.

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

Parameters

Configure the FindNearest gem using the following parameters.

Spatial Object Fields

ParameterDescription
Source Centroid TypeType of geospatial object in in0. Currently, only Point is supported.
Source Centroid ColumnColumn in in0 that contains the source spatial points.
Target Centroid TypeType of geospatial object in in1. Currently, only Point is supported.
Target Centroid ColumnColumn in in1 that contains the target spatial points.

Select Output Options

ParameterDescription
How many nearest points to find?Number of closest points to return from in1 for each point in in0.
Maximum distanceOption to limit the search to target points within this distance (specify units). When the maximum distance is 0, no maximum is enforced.
Ignore 0 distance matchesWhether to exclude points that have exactly the same coordinates as the source point.

Example: Customer service centers

Assume you have two tables:

  • customer_locations that contains the location of each customer.

    customer_idcustomer_point
    C001POINT(-122.4194 37.7749)
    C002POINT(-74.0060 40.7128)
  • service_centers that contains the location of each service center.

    center_idcenter_point
    S100POINT(-122.4192 37.7793)
    S200POINT(-73.9352 40.7306)
    S300POINT(-118.2437 34.0522)

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.

customer_idcustomer_pointcenter_idcenter_pointrank_numberdistanceKilometerscardinal_direction
C001POINT(-122.4194 37.7749)S100POINT(-122.4192 37.7793)10.48957333464416436N
C001POINT(-122.4194 37.7749)S300POINT(-118.2437 34.0522)2559.1205770615533SE
C002POINT(-74.0060 40.7128)S200POINT(-73.9352 40.7306)16.286267237667312E