Skip to content

Commit

Permalink
Adds tests for new polygons module
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesVarndell committed Nov 21, 2024
1 parent fa61f78 commit 194c0b1
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions tests/test_polygons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# (C) Copyright 2024 ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
#


import pytest
from shapely.geometry import MultiPolygon, Polygon

from earthkit.geo.polygons import (
_closest_resolution,
country_polygons,
multipolygon_to_coordinates,
)


@pytest.mark.parametrize(
"distance_m, expected_resolution",
[
(0, "10m"), # Closest to 10m
(20e6, "10m"), # Closest to 10m
(80e6, "110m"), # Closest to 110m
(79e6, "50m"), # Closest to 50m
(45e6, "50m"), # Closest to 50m
(110e6, "110m"), # Exactly 110m
],
)
def test_closest_resolution(distance_m, expected_resolution):
assert _closest_resolution(distance_m) == expected_resolution


def test_multipolygon_to_coordinates():
poly1 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
poly2 = Polygon([(2, 2), (3, 2), (3, 3), (2, 3), (2, 2)])
multipolygon = MultiPolygon([poly1, poly2])

coordinates = multipolygon_to_coordinates(multipolygon)

expected_coordinates = [
[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]],
[[2, 2], [2, 3], [3, 3], [3, 2], [2, 2]],
]

assert coordinates == expected_coordinates


def test_country_polygons_invalid_country():
with pytest.raises(ValueError):
country_polygons("InvalidCountryName")


def test_country_polygons_single_country():
coordinates = country_polygons("France")

# Verify the output format
assert isinstance(coordinates, list)
assert all(isinstance(polygon, list) for polygon in coordinates)
assert all(
isinstance(coord, list) and len(coord) == 2
for polygon in coordinates
for coord in polygon
)


def test_country_polygons_multiple_countries():
coordinates = country_polygons(["Poland", "Germany"])

# Verify the output format
assert isinstance(coordinates, list)
assert all(isinstance(polygon, list) for polygon in coordinates)
assert all(
isinstance(coord, list) and len(coord) == 2
for polygon in coordinates
for coord in polygon
)


def test_country_polygons_output_format():
poly = Polygon([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
multipolygon = MultiPolygon([poly])

coordinates = multipolygon_to_coordinates(multipolygon)

# Verify the output format
assert isinstance(coordinates, list)
assert all(isinstance(polygon, list) for polygon in coordinates)
assert all(
isinstance(coord, list) and len(coord) == 2
for polygon in coordinates
for coord in polygon
)

0 comments on commit 194c0b1

Please sign in to comment.