| import math |
| import unittest |
|
|
| from src.geometry import ( |
| calculate_site_metrics, |
| normalize_map_state, |
| polygon_area_local, |
| polygon_perimeter_local, |
| ) |
|
|
|
|
| class GeometryTests(unittest.TestCase): |
| def test_local_polygon_area_and_perimeter(self): |
| points = [(0, 0), (10, 0), (10, 5), (0, 5), (0, 0)] |
|
|
| self.assertEqual(polygon_area_local(points), 50) |
| self.assertEqual(polygon_perimeter_local(points), 30) |
|
|
| def test_drawn_polygon_normalizes_to_site_selection(self): |
| map_state = { |
| "mode": "polygon", |
| "geometry": { |
| "type": "Polygon", |
| "coordinates": [ |
| [ |
| [70.0, 21.0], |
| [70.002, 21.0], |
| [70.002, 21.002], |
| [70.0, 21.002], |
| [70.0, 21.0], |
| ] |
| ], |
| }, |
| } |
|
|
| selection = normalize_map_state(map_state) |
|
|
| self.assertEqual(selection.selection_type, "drawn_polygon") |
| self.assertEqual(selection.coordinate_mode, "wgs84") |
| self.assertIn("approximate", selection.accuracy_label) |
| self.assertGreater(selection.area_sqm, 0) |
| self.assertGreater(selection.perimeter_m, 0) |
|
|
| def test_pin_radius_normalizes_to_site_selection(self): |
| selection = normalize_map_state( |
| {"mode": "pin_radius", "lat": 21.0, "lon": 70.0, "radius_m": 250} |
| ) |
|
|
| self.assertEqual(selection.selection_type, "pin_radius") |
| self.assertEqual(selection.radius_m, 250) |
| self.assertAlmostEqual(selection.area_sqm, math.pi * 250 * 250, delta=1) |
| self.assertIn("not exact", " ".join(selection.limitations)) |
|
|
| def test_calculate_site_metrics_rejects_empty_geometry(self): |
| with self.assertRaises(ValueError): |
| calculate_site_metrics({"type": "Polygon", "coordinates": []}) |
|
|
|
|
| if __name__ == "__main__": |
| unittest.main() |
|
|