TransWikia.com

Nearest-neighbor returns different results based on coordinates chosen

Cross Validated Asked by zhutchens1 on November 24, 2021

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])
radius=np.array([6741,6739,6688])

# 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.
X = radius * np.sin(theta)*np.cos(phi)
Y = radius * np.sin(theta)*np.sin(phi)
Z = radius * np.cos(theta)

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

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP