# Nearest-neighbor returns different results based on coordinates chosen

I'm working on a large-scale grouping code that uses pairing of nearest-neighbors to combine points. The points are defined by a spherical coordinates in this way:

    azimuth      polar      radius
(1) 169.322 deg, 24.63 deg, 6741
(2) 169.291 deg, 25.84 deg, 6739
(3) 169.339 deg, 25.43 deg, 6688


For the purposes of my problem, there are two ways of defining "Cartesian" axes for the coordinates. One way is to let the radius retain its role as the radius of a sphere, and let x and y become the lengths of arcs defined by the azimuthal and polar angles on that sphere (for example, y = polar angle * radius, 'z'). The other way is the "normal" way of thinking of a spherical to Cartesian conversion, in which, for example, z = radius * cos(90 - polar angle). I'm running my nearest-neighbor tests in Python using scipy.cKDTree:

import numpy as np
from scipy.spatial import cKDTree

# input coordinates
azimuthdeg = np.array([169.322, 169.291, 169.339])
polardeg = np.array([24.63, 25.84, 25.43])

# find nearest neighbors (using arc definition of x,y, and z).
Z = radius # using cz includes peculiar motions
X = 2.*np.pi * Z * azimuthdeg * np.cos(np.pi*polardeg/180.) / 360.
Y = 2.*np.pi * Z * polardeg / 360.

# find 3D neighbors with Z_Mpc
coords = np.array([X, Y, Z]).T
kdt = cKDTree(coords)
neighbordist, neighbori = kdt.query(coords, k=2)
neighbordist = neighbordist[:,1] # ignore self-match
neighbori = neighbori[:,1] # ignore self-match

print(neighbori, neighbordist)

# first nearest neighbors (using cartesian definiton of x,y,z).
phi = azimuthdeg * np.pi/180.
theta = np.pi/2. - polardeg*np.pi/180.

# find 3D neighbors with Z_Mpc
coords = np.array([X, Y, Z]).T
kdt = cKDTree(coords)
neighbordist, neighbori = kdt.query(coords, k=2)
neighbordist = neighbordist[:,1] # ignore self-match
neighbori = neighbori[:,1] # ignore self-match

print(neighbori, neighbordist)


which returns the output:

[1 2 1] [235.24968796 111.51791663 111.51791663]
[2 2 1] [107.70996224  70.24660993  70.24660993]


Why are the identified nearest-neighbors different for the two ways of defining coordinates? (and the NN distances?) While I realize that the first definition is quasi-spherical, the angles subtended these three points on the sphere is about ~1 degree, well within the small-angle approximation, where I believe Euclidean geometry should be approximately correct on this small subsurface.

• It does not seem to me that these two different coordinate systems are identical when distances are measured as Euclidean distances (as cKDTree presumably does). Have you checked aht the proper spatial distance metric is for your first coordinate system? Jul 22, 2020 at 12:48
• @cdalitz I've confirmed that the distances that cKDTree returns are taken through a Euclidean distance equation sqrt(dx^2+dy^2+dz^2) where x,y, and z are some input coordinates. Is that what you mean? I thought that it might be able to handle both, since the actual targets subtend such a small of the sphere defined in the first coordinate system.
– zh1
Jul 22, 2020 at 13:50
• "'I've confirmed that the distances that cKDTree returns are taken through a Euclidean distance sqrt(dx^2+dy^2+dz^2)." In the case of your first coordinate system, this distance does not represent the spatial distance and a different metric needs to be used. Most likely, this is the reason for the difference. Jul 22, 2020 at 13:53
• Do you think that would apply even though the points are close together on the sphere? I wasn't sure-some others seem to say it should still work (stackoverflow.com/questions/10549402/…).
– zh1
Jul 22, 2020 at 13:59
• You can test it yourself for this simple example of three points: print out the respective coordinates and check for the minimum distance a^2 + b^2 + c^2, where a,b, and are the coordinates in each coordinate space. Jul 22, 2020 at 14:04