A mobile robot's map can contain several records that sit close together on the floor plan. The records may come from separate survey passes, different detectors, or distinct physical markers mounted near one another. A useful review starts by listing every pair of observations that meets a distance cutoff. Starting from a single nearest match can miss other close pairs, including pairs involving observations with identical coordinates.
SciPy's cKDTree query_pairs method starts from one collection of points and returns the row-index pairs within a specified distance. Each pair contains two different indices, listed with the smaller index first. That convention makes the result easy to use as an undirected graph. The positions describe where records were measured, while the two indices identify which records were compared. Preserve that distinction if a later stage decides which landmarks to merge.
Compare the tree against an independent loop
The following synthetic example uses five observations on a planar map and a Euclidean cutoff of 0.75 meters. Rows zero and three were given identical coordinates to represent separate observations. Rows zero, one, and two form a chain with step lengths of 0.75 meters. The fifth row sits away from the chain. These values fit exactly in binary for the horizontal distances of interest, so the example tests the inclusive cutoff without depending on a rounding tolerance.
from itertools import combinations
from math import dist
from scipy.spatial import cKDTree
points = [(0, 0), (0.75, 0), (1.5, 0), (0, 0), (3, 3)]
radius = 0.75
tree = cKDTree(points)
pairs = tree.query_pairs(r=radius, p=2, eps=0)
reference = {
(i, j)
for i, j in combinations(range(len(points)), 2)
if dist(points[i], points[j]) <= radius
}
assert pairs == reference
assert sorted(pairs) == [(0, 1), (0, 3), (1, 2), (1, 3)]
The two methods agreed in a local run with SciPy 1.18.1. The pairs involving consecutive positions are admitted at precisely the cutoff. The pair between zero and three is also present because their distance is zero and both rows exist. The fifth row contributes no edge. Using SciPy's ndarray output gave the same membership when converted into a set of tuples. For a report that must be stable across runs, explicitly sort the pairs before serialization and retain the exact input mapping between row indices and observation IDs.
A chain describes reachability
Rows zero and two are 1.5 meters apart. They have no direct edge at this radius, even though each is linked to row one. A connected-component algorithm will put both in the same component because it can follow a path through row one. As a result, a component can have a much larger span than the distance used to create individual edges. The same behavior can occur in longer chains assembled from a series of overlapping survey observations.
Decide whether a map consumer wants candidate pairs, components of linked observations, or groups with a maximum internal distance. Each of those can be an appropriate output for a different review. A visual map may display the edges for a human to inspect. A batch review may choose to visit each connected component. A landmark merge constrained by the distance between every member needs an additional group-level check. An edge between two rows can also be evaluated against observation times, detector labels, or survey provenance before applying any identity decision.
Make the metric and approximation explicit
The fixture sets p to two for Euclidean distance and eps to zero for exact search. In a separate two-point check, positions (0, 0) and (0.5, 0.5) passed a cutoff of 0.75 under the Euclidean metric. They failed the same cutoff under the Manhattan metric with p equal to one, where the distance is one. Save the metric with the radius because the same numeric cutoff represents a different neighborhood when p changes.
SciPy describes how positive eps changes branch pruning and bulk acceptance. If the output is used for a strict near-boundary comparison, start with zero and validate against a small exhaustive reference. If a separate application uses approximation, measure differences in retrieved pairs against the exact result on its own input. A higher throughput target does not establish how many near-boundary pairs a report may omit or include.
Preserve the model context
Convert coordinates to a common frame and common unit before building the tree. Two nearby coordinates from separate, unaligned maps can be close numerically even when their physical positions differ. If different floors can share the same horizontal coordinates, preserve floor identity or apply a spatial model that includes a relevant vertical coordinate. A two-dimensional comparison applies only to the axes included in the data.
Keep observation identifiers and store representative edge distances in the review export. Keep the coordinate precision alongside the metric, since rounding near the cutoff can change membership when the report is reconstructed. The original index is especially important when loading a filtered table. A row numbered two in the filtered table may come from a much later row in the underlying observation log. Store the mapping before querying and translate the returned pairs back to stable identifiers immediately. Then a report can trace its decisions to the original records independently of the sort order used to create the input array.
The AI-generated concept image shows local links between three floor markers beside a mobile robot. It illustrates the chain geometry. The coordinates, output pairs, and metric comparison above come from the synthetic fixture.
Sources checked November 28, 2026. SciPy cKDTree query_pairs documentation at https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.cKDTree.query_pairs.html and Python math.dist documentation at https://docs.python.org/3/library/math.html#math.dist.