1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
from typing import Any, Iterable, Iterator
from pystac import Collection, Item
from stactools.core.utils.round import recursive_round, round_coordinates
from tests import test_data
def test_item_geometry_bbox() -> None:
item = Item.from_file(
test_data.get_path(
"data-files/basic/country-1/area-1-1/"
"area-1-1-imagery/area-1-1-imagery.json"
)
)
item = round_coordinates(item, precision=5)
polys = item.geometry["coordinates"]
for poly in polys:
for coord in poly:
for value in coord:
assert str(value)[::-1].find(".") <= 5
bbox = item.bbox
for value in bbox:
assert str(value)[::-1].find(".") <= 5
def test_item_multipolygon_geometry() -> None:
item = Item.from_file(
test_data.get_path("data-files/training/dar/42f235/42f235.json")
)
item = round_coordinates(item, precision=5)
polys = item.geometry["coordinates"]
for poly in polys:
for subpoly in poly:
for coord in subpoly:
for value in coord:
assert str(value)[::-1].find(".") <= 5
bbox = item.bbox
for value in bbox:
assert str(value)[::-1].find(".") <= 5
def test_item_no_geometry_or_bbox() -> None:
item = Item.from_file(
test_data.get_path(
"data-files/basic/country-1/area-1-1/"
"area-1-1-imagery/area-1-1-imagery.json"
)
)
item.bbox = None
item.geometry = None
item = round_coordinates(item, precision=5)
assert isinstance(item, Item)
def test_collection_bbox() -> None:
collection = Collection.from_file(
test_data.get_path("data-files/basic/country-1/area-1-1/collection.json")
)
collection = round_coordinates(collection, precision=5)
coords = collection.extent.spatial.bboxes
for coord in coords:
for value in coord:
assert str(value)[::-1].find(".") <= 5
def test_recursive_round() -> None:
def flatten(nested: Iterable) -> Iterator[Any]:
for value in nested:
if isinstance(value, Iterable):
for nest in flatten(value):
yield nest
else:
yield value
vanilla = [0.123456, 1.12345678]
rounded = recursive_round(vanilla, precision=4)
for coord in rounded:
assert str(coord)[::-1].find(".") == 4
nested_lists = [
[[0.123456, 2.1234567], [1.12345678, 2.12345]],
[[0.123456, 1.12345678]],
]
rounded = recursive_round(nested_lists, precision=3)
for coord in flatten(rounded):
assert str(coord)[::-1].find(".") == 3
nested_tuples = [
((0.123456, 2.1234567), (1.12345678, 2.12345)),
((0.123456, 1.12345678)),
]
rounded = recursive_round(nested_tuples, precision=5)
for coord in flatten(rounded):
assert str(coord)[::-1].find(".") == 5
|