import unittest import numpy as np from surveyladder.geo import chainage_to_xy, chainage_offset_to_xy, poly_area, poly_centroid, trilaterate_xy class TestPolyArea(unittest.TestCase): def test_poly_area(self): polygon = np.array([[0, 0], [4, 0], [4, 3]]) self.assertEqual(poly_area(polygon), 6) class TestTrilaterateXY(unittest.TestCase): def test_trilaterate_xy_on_line(self): A = np.array([0, 0]) B = np.array([10, 0]) a = 5 b = 5 C = trilaterate_xy(A, a, B, b) np.testing.assert_almost_equal(C, [5, 0]) def test_trilaterate_xy_right_triangle(self): A = np.array([0, 0]) B = np.array([4, 0]) a = 5 b = 3 C = trilaterate_xy(A, a, B, b) np.testing.assert_almost_equal(C, [4, 3]) def test_trilaterate_xy_right_triangle_swapped(self): A = np.array([4, 0]) B = np.array([0, 0]) a = 3 b = 5 C = trilaterate_xy(A, a, B, b) np.testing.assert_almost_equal(C, [4, -3]) class TestChainageToXY(unittest.TestCase): def test_positive_distance(self): np.testing.assert_almost_equal( chainage_to_xy(np.array([3, 5]), np.array([6, 9]), 10), [9, 13]) def test_negative_distance(self): np.testing.assert_almost_equal( chainage_to_xy(np.array([3, 5]), np.array([6, 9]), -5), [0, 1]) class TestChainageOffsetToXY(unittest.TestCase): def test_positive_offset(self): A = np.array([0, 0]) B = np.array([0, 1]) a_distance = 5 perp_distance = 2 P = chainage_offset_to_xy(A, B, a_distance, perp_distance) np.testing.assert_almost_equal(P, [-2, 5]) class TestPolyCentroid(unittest.TestCase): def test_pos_square(self): # mathematically positive point ordering poly = np.array([[0,0], [1,0], [1,2], [0,2]]) np.testing.assert_almost_equal(poly_centroid(poly), [0.5, 1]) def test_neg_square(self): # mathematically negative point ordering poly = np.array([[0,0], [1,0], [1,-2], [0,-2]]) np.testing.assert_almost_equal(poly_centroid(poly), [0.5, -1]) unittest.main()