Skip to main content
The following content is a read-only preview of an executable Jupyter notebook.To run this notebook interactively:
  1. Go to Wherobots Cloud.
  2. Start a runtime.
  3. Open the notebook.
  4. In the Jupyter Launcher:
    1. Click File > Open Path.
    2. Paste the following path to access this notebook: examples/Analyzing_Data/K_Nearest_Neighbor_Join.ipynb
    3. Click Enter.
In this notebook we will demostrate how to perform k-Nearest Neighbors (kNN) joins in WherobotsDB. A geospatial k-Nearest Neighbors (kNN) join is a specialized form of the kNN join that specifically deals with geospatial data. This method involves identifying the k-nearest neighbors for a given spatial point or region based on geographic proximity, typically using spatial coordinates and a suitable distance metric like Euclidean or great-circle distance. There are two types of kNN joins supported in WherobotsDB:
  • Approximate kNN Join: The approximate k-Nearest Neighbor (kNN) algorithm utilizes an approximation method to map and distribute multidimensional data into a single dimension while maintaining some degree of locality. This approach allows for the efficient generation of approximate k-Nearest Neighbors for each geometry in the query dataset.
  • Exact kNN Join: The exact k-Nearest Neighbor (kNN) algorithm begins by partitioning the dataset to maintain spatial locality. It then constructs an efficient structure over another dataset to quickly find accurate kNN matches. By combining local results, the algorithm delivers the complete kNN join for both datasets.
For a more in-depth explanation, including additional examples, API details, and usage guidelines, please refer to the documentation here or check out our detailed tech blog here.

Initial Configuration

Use Case Example

In this use case, the k-Nearest Neighbor (kNN) join is applied to match places with nearby flights. The Queries table contains the locations of Overture Maps places, such as schools, businesses, hospitals, religious organizations, landmarks, and mountain peak, while the Objects table holds the locations of flights. The goal is to find which flights are closest to each place, which can be crucial for making real-time decisions in air traffic management and ensuring flight safety.

State Boundary

Pick a state or other boundary

Click here for boundaries of other states

Queries Table: Places

This table contains the objects for which you want to find the nearest neighbors.

Objects Table: Flights

This table contains the objects that are potential neighbors to the objects in the Queries table.

Run KNN Join

The spatial SQL below demonstrates the new SQL syntax for performing KNN joins on the Wherobots platform.