""" Tests for :py:mod:`stems.gis.geom`
"""
from affine import Affine
from rasterio.coords import BoundingBox
from rasterio.windows import Window
import pytest
from stems.gis import geom
BOX_1 = [0, 0, 10, 10]
BOX_2 = [-5, -5, 5, 5]
BOX_3 = [5, 5, 15, 15]
AFFINE_1m = Affine(1., 0., 0., 0., -1., 10.)
AFFINE_5m = Affine(5., 0., 0., 0., -5., 10.)
# ----------------------------------------------------------------------------
# bounds_union
[docs]@pytest.mark.parametrize(('data', 'ans', ), [
((BOX_1, ), BoundingBox(0, 0, 10, 10)),
((BOX_1, BOX_2, ), BoundingBox(-5, -5, 10, 10)),
((BOX_1, BOX_2, BOX_3), BoundingBox(-5, -5, 15, 15)),
])
def test_bounds_union(data, ans):
test = geom.bounds_union(*data)
assert isinstance(test, BoundingBox)
assert test == ans
# ----------------------------------------------------------------------------
# bounds_intersection
[docs]@pytest.mark.parametrize(('data', 'ans', ), (
((BOX_1, ), BoundingBox(0, 0, 10, 10)),
((BOX_1, BOX_2, ), BoundingBox(0, 0, 5, 5)),
((BOX_1, BOX_2, BOX_3), BoundingBox(5, 5, 5, 5)),
))
def test_bounds_intersection(data, ans):
test = geom.bounds_intersection(*data)
assert isinstance(test, BoundingBox)
assert test == ans
# ----------------------------------------------------------------------------
# union_bounds_transforms
# ----------------------------------------------------------------------------
# calculate_src_window
[docs]@pytest.mark.parametrize(('bounds', 'transform', 'dst_bounds', 'result'), [
(BOX_1, AFFINE_1m, BOX_2, (Window(0, 5, 5, 5), BoundingBox(0, 0, 5, 5))),
(BOX_1, AFFINE_5m, BOX_2, (Window(0, 1, 1, 1), BoundingBox(0, 0, 5, 5))),
(BOX_1, AFFINE_5m, BOX_3, (Window(1, 0, 1, 1), BoundingBox(5, 5, 10, 10)))
])
def test_calculate_src_window_exact(bounds, transform, dst_bounds, result):
ans = geom.calculate_src_window(bounds, transform, dst_bounds)
assert ans[0] == result[0]
assert ans[1] == result[1]
[docs]def test_calculate_src_window_nonexact():
bounds = BoundingBox(0, 0, 10, 10)
transform = Affine(5., 0., 0., 0., -5., 10.)
dst_bounds = BoundingBox(-1., 0., 12, 13)
ans = geom.calculate_src_window(bounds, transform, dst_bounds)
assert ans[0] == Window(0, 0, 2, 2)
assert ans[1] == bounds
# ----------------------------------------------------------------------------
# calculate_dst_window
[docs]def test_calculate_dst_window():
src_bounds = BoundingBox(0, 0, 120, 120)
dst_transform = Affine(30., 0., 0,
0., -30., 120)
ans = geom.calculate_dst_window(src_bounds, dst_transform)
assert ans == Window(0, 0, 4, 4)
# dst_transform further left & up
src_bounds = BoundingBox(0, 0, 65, 95)
dst_transform = Affine(30., 0., -35,
0., -30., 155)
ans = geom.calculate_dst_window(src_bounds, dst_transform)
assert ans == Window(1, 2, 2, 3)
# ----------------------------------------------------------------------------
# is_null
[docs]def test_is_null():
pass
# ----------------------------------------------------------------------------
# fix_geojson_rfc7946
[docs]def test_fix_geojson_rfc7946():
pass