Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ __pycache__

# virtual environment
venv/
.venv/

# Editor settings
.vscode/

# Installer logs
pip-log.txt
Expand Down
39 changes: 39 additions & 0 deletions libpysal/cg/tests/test_kdtree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Tests derived from Arc_KDTree docstring examples to get the expected behavior


import math

import pytest

from libpysal.cg import sphere
from libpysal.cg.kdtree import KDTree


def test_arc_kdtree_basic_query():
pts = [(0, 90), (0, 0), (180, 0), (0, -90)]
kd = KDTree(pts, distance_metric="Arc", radius=sphere.RADIUS_EARTH_KM)

d, i = kd.query((90, 0), k=4)

assert len(d) == 4
assert len(i) == 4


def test_arc_distance_quarter_circumference():
pts = [(0, 90), (0, 0), (180, 0), (0, -90)]
kd = KDTree(pts, distance_metric="Arc", radius=sphere.RADIUS_EARTH_KM)

d, _ = kd.query((90, 0), k=4)

expected = 2 * math.pi * sphere.RADIUS_EARTH_KM / 4
assert abs(d[0] - expected) < 1e-5


def test_arc_query_ball_point_raises_for_large_radius():
pts = [(0, 0), (90, 0)]
kd = KDTree(pts, distance_metric="Arc", radius=sphere.RADIUS_EARTH_KM)

too_large_radius = kd.circumference

with pytest.raises(ValueError):
kd.query_ball_point((0, 0), r=too_large_radius)