Skip to content

Commit

Permalink
chore: modify tests based on the latest API changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Taher Chegini committed Jan 9, 2025
1 parent d3ef6be commit b6a0019
Showing 1 changed file with 48 additions and 39 deletions.
87 changes: 48 additions & 39 deletions tests/test_3dep.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
import pytest
from __future__ import annotations

import shutil
from pathlib import Path
import seamless_3dep as sdem

import pytest
import rasterio

import seamless_3dep as sdem


@pytest.fixture
def valid_bbox() -> tuple[float, float, float, float]:
return (-122.0, 37.0, -121.0, 38.0)


@pytest.fixture
def small_resolution() -> float:
return 30.0
def small_res() -> int:
return 30


@pytest.fixture
def pixel_max() -> int:
return 1_000_000

def test_decompose_bbox_no_division(valid_bbox: tuple[float, float, float, float], small_resolution: int, pixel_max: int):

def test_decompose_bbox_no_division(
valid_bbox: tuple[float, float, float, float], small_res: int, pixel_max: int
):
"""Test when bbox is small enough to not require division."""
boxes, *_ = sdem.decompose_bbox(valid_bbox, small_resolution, pixel_max * 100)
boxes, *_ = sdem.decompose_bbox(valid_bbox, small_res, pixel_max * 100)
assert len(boxes) == 1
assert boxes[0] == valid_bbox


def test_decompose_bbox_with_division(valid_bbox: tuple[float, float, float, float]):
"""Test when bbox needs to be divided."""
# Use small pixel_max to force division
small_pixel_max = 1000
boxes, *_ = sdem.decompose_bbox(valid_bbox, 30.0, small_pixel_max)
boxes, *_ = sdem.decompose_bbox(valid_bbox, 30, small_pixel_max)
assert len(boxes) > 1

# Check that all boxes are within original bbox
for box in boxes:
west, south, east, north = box
Expand All @@ -39,79 +49,70 @@ def test_decompose_bbox_with_division(valid_bbox: tuple[float, float, float, flo
assert east <= orig_east + 1e-10
assert north <= orig_north + 1e-10


def test_decompose_bbox_with_buffer():
"""Test decompose_bbox with buffer."""
bbox = (-122.0, 37.0, -121.0, 38.0)
boxes, *_ = sdem.decompose_bbox(
bbox,
resolution=30.0,
pixel_max=1000,
buff_npixels=2.0
)
boxes, *_ = sdem.decompose_bbox(bbox, res=30, pixel_max=1000, buff_npixels=2)
# Check that boxes overlap due to buffer
for i in range(len(boxes) - 1):
current_box = boxes[i]
next_box = boxes[i + 1]
# Either boxes should overlap in x or y direction
has_overlap = (
(current_box[2] > next_box[0]) or # x overlap
(current_box[3] > next_box[1]) # y overlap
(current_box[2] > next_box[0]) # x overlap
or (current_box[3] > next_box[1]) # y overlap
)
assert has_overlap


def test_decompose_bbox_invalid_resolution():
"""Test decompose_bbox with resolution larger than bbox dimension."""
bbox = (-122.001, 37.001, -122.0, 37.002) # Very small bbox
with pytest.raises(ValueError):
sdem.decompose_bbox(bbox, resolution=10000.0, pixel_max=1000)
with pytest.raises(ValueError, match="Resolution must be less"):
sdem.decompose_bbox(bbox, res=10000, pixel_max=1000)


def test_decompose_bbox_aspect_ratio():
"""Test that decomposed boxes maintain approximate aspect ratio."""
bbox = (-122.0, 37.0, -121.0, 38.0)
boxes, *_ = sdem.decompose_bbox(
bbox,
resolution=30.0,
pixel_max=1000
)

boxes, *_ = sdem.decompose_bbox(bbox, res=30, pixel_max=1000)

# Calculate original aspect ratio
orig_width = abs(bbox[2] - bbox[0])
orig_height = abs(bbox[3] - bbox[1])
orig_aspect = orig_width / orig_height

# Calculate aspect ratio of subdivisions
box = boxes[0]
sub_width = abs(box[2] - box[0])
sub_height = abs(box[3] - box[1])
sub_aspect = sub_width / sub_height

# Aspect ratios should be similar
assert abs(orig_aspect - sub_aspect) < 0.5 # Allow some deviation due to rounding


def test_decompose_bbox_coverage():
"""Test that decomposed boxes cover the entire original bbox."""
bbox = (-122.0, 37.0, -121.0, 38.0)
boxes, *_ = sdem.decompose_bbox(
bbox,
resolution=30.0,
pixel_max=1000
)

boxes, *_ = sdem.decompose_bbox(bbox, res=30, pixel_max=1000)

# Convert boxes to set of points for easier comparison
points_covered = set()
for box in boxes:
west, south, east, north = box
for x in [west, east]:
for y in [south, north]:
points_covered.add((round(x, 6), round(y, 6)))

# Original bbox corners should be in points covered
orig_west, orig_south, orig_east, orig_north = bbox
orig_points = {
(round(orig_west, 6), round(orig_south, 6)),
(round(orig_west, 6), round(orig_north, 6)),
(round(orig_east, 6), round(orig_south, 6)),
(round(orig_east, 6), round(orig_north, 6))
(round(orig_east, 6), round(orig_north, 6)),
}
assert orig_points.issubset(points_covered)

Expand All @@ -120,10 +121,10 @@ def test_dem():
bbox = (-121.1, 37.9, -121.0, 38.0)
tiff_files = sdem.get_dem(bbox, "dem_data", 30)
tiff_files[0].unlink()
tiff_files = sdem.get_dem(bbox, "dem_data", 30, None)
tiff_files = sdem.get_dem(bbox, "dem_data", 30, pixel_max=None)
with rasterio.open(tiff_files[0]) as src:
assert src.shape == (359, 359)
tiff_files = sdem.get_dem(bbox, "dem_data", 30, 80000)
tiff_files = sdem.get_dem(bbox, "dem_data", 30, pixel_max=80000)
with rasterio.open(tiff_files[0]) as src:
assert src.shape == (359, 179)
vrt_file = Path("dem_data", "dem.vrt")
Expand All @@ -134,11 +135,19 @@ def test_dem():

def test_3dep():
bbox = (-121.1, 37.9, -121.0, 38.0)
tiff_files = sdem.get_map("Slope Degrees", bbox, "slope_data", 30, None)
tiff_files = sdem.get_map("Slope Degrees", bbox, "slope_data", 30, pixel_max=None)
tiff_files = sdem.get_map("Slope Degrees", bbox, "slope_data", 30)
with rasterio.open(tiff_files[0]) as src:
assert src.shape == (371, 293)
tiff_files = sdem.get_map("Slope Degrees", bbox, "slope_data", 30, 80000)
tiff_files = sdem.get_map("Slope Degrees", bbox, "slope_data", 30, pixel_max=80000)
with rasterio.open(tiff_files[0]) as src:
assert src.shape == (371, 147)
shutil.rmtree("slope_data", ignore_errors=True)
shutil.rmtree("slope_data", ignore_errors=True)


def test_3dep_3857():
bbox = (-121.1, 37.9, -121.0, 38.0)
tiff_files = sdem.get_map("DEM", bbox, "data_3857", 30, 3857)
with rasterio.open(tiff_files[0]) as src:
assert src.shape == (371, 293)
shutil.rmtree("data_3857", ignore_errors=True)

0 comments on commit b6a0019

Please sign in to comment.