1
$\begingroup$

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.

$\endgroup$
6
  • $\begingroup$ 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? $\endgroup$
    – cdalitz
    Jul 22, 2020 at 12:48
  • $\begingroup$ @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. $\endgroup$
    – zh1
    Jul 22, 2020 at 13:50
  • $\begingroup$ "'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. $\endgroup$
    – cdalitz
    Jul 22, 2020 at 13:53
  • $\begingroup$ 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/…). $\endgroup$
    – zh1
    Jul 22, 2020 at 13:59
  • $\begingroup$ 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. $\endgroup$
    – cdalitz
    Jul 22, 2020 at 14:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.