示例#1
0
    def test_vector_multiply(self):
        vector = Vector2D(1, 1)
        vector1 = vector.multiply(10)
        self.assertEqual(Vector2D(10, 10), vector1)

        vector2 = vector * 3
        self.assertEqual(Vector2D(3, 3), vector2)
示例#2
0
    def test_eq(self):
        vector1 = Vector2D(1, 3.14)
        vector2 = Vector2D(-1, -2)
        vector3 = Vector2D(1, 3.14)

        self.assertEqual(vector1, vector3)
        self.assertNotEqual(vector1, vector2)
示例#3
0
    def test_get_unit_vector(self):
        vector1 = Vector2D(2, 2)
        vector2 = Vector2D(1, 1)
        self.assertEqual(vector1.unit(), vector2.unit())
        self.assertFalse(vector1 is vector1.unit())

        vector3 = Vector2D(2, 0)
        self.assertEqual(Vector2D(1, 0), vector3.unit())
示例#4
0
    def test_len(self):
        vector1 = Vector2D(1, 0)
        self.assertAlmostEqual(1.0, vector1.length)

        vector2 = Vector2D(2, 0)
        self.assertAlmostEqual(2.0, vector2.length)

        vector3 = Vector2D(1, 1)
        self.assertAlmostEqual(2 ** 0.5, vector3.length)
示例#5
0
    def test_apply(self):
        polygon = box(0, 0, 1, 1)
        vector = Vector2D(1, 1)
        expected = box(1, 1, 2, 2)
        self.assertTrue(is_geom_equal(expected, vector.apply(polygon)))

        vector = Vector2D(1, 0)
        expected = box(1, 0, 2, 1)
        self.assertTrue(is_geom_equal(expected, vector.apply(polygon)))
示例#6
0
    def test_dot(self):
        vector1 = Vector2D(1, 1)
        vector2 = Vector2D(-1, -1)
        self.assertAlmostEqual(-2, vector1.dot(vector2), delta=0)
        self.assertAlmostEqual(-2, vector2.dot(vector1), delta=0)

        vector3 = Vector2D(1, 1)
        vector4 = Vector2D(-1, 1)
        self.assertEqual(0, vector3.dot(vector4))
        self.assertEqual(0, vector4.dot(vector1))
示例#7
0
    def test_from_coordinates(self):
        vector1 = Vector2D.from_coordinates((0, 0), (1, 1))
        self.assertTrue(isinstance(vector1, Vector2D))
        self.assertEqual(1, vector1.x)
        self.assertEqual(1, vector1.y)

        with self.assertRaises(ValueError):
            Vector2D.from_coordinates(('a', 1), [1, 1])
        with self.assertRaises(ValueError):
            Vector2D.from_coordinates((0, 1), [1])
示例#8
0
 def test_plus(self):
     vector1 = Vector2D(1, 3.14)
     vector2 = Vector2D(-1, -2)
     expected = Vector2D(0, 1.14)
     self.assertTrue(expected, vector1.plus(vector2))
     self.assertTrue(expected, vector2.plus(vector1))
     self.assertEqual(1, vector1.x)
     self.assertEqual(3.14, vector1.y)
     self.assertEqual(-1, vector2.x)
     self.assertEqual(-2, vector2.y)
示例#9
0
 def test_insert_other_geom_projections_into_geom(self):
     polygon1 = Polygon([(3.5, 1), (6, 1), (6, 3), (3.5, 3)])
     polygon2 = Polygon([(1, 1), (3, 1), (3, 4), (4, 4), (4, 5), (0, 5),
                         (0, 4), (1, 4), (1, 1)])
     projector = Projector(polygon2, Vector2D(1, 0))
     new_polygon2 = projector._insert_other_geom_projections_into_geom(
         other_geom=polygon1,
         projecting_vector=Vector2D(-1, 0),
         geom=polygon2)
     self.assertTrue(
         len(polygon2.exterior.coords) < len(new_polygon2.exterior.coords))
示例#10
0
    def test_get_angle_with(self):
        vector1 = Vector2D(1, 0)
        other1 = Vector2D(1, 1)
        self.assertAlmostEqual(45, vector1.angle_ccw_rotating_to(other1), delta=MATH_EPS)

        other2 = Vector2D(1, -1)
        self.assertAlmostEqual(315, vector1.angle_ccw_rotating_to(other2), delta=MATH_EPS)

        vector2 = Vector2D(1, 0)
        other3 = Vector2D(1, math.sqrt(3))
        self.assertAlmostEqual(60, vector2.angle_ccw_rotating_to(other3), delta=MATH_EPS)
        self.assertAlmostEqual(300, other3.angle_ccw_rotating_to(vector2), delta=MATH_EPS)
示例#11
0
 def test_is_valid_2d_coordinate(self):
     self.assertTrue(Vector2D.is_valid_2d_coordinate((0, 1)))
     self.assertTrue(Vector2D.is_valid_2d_coordinate((0., 1)))
     self.assertTrue(Vector2D.is_valid_2d_coordinate((0., 1, 2)))
     self.assertTrue(Vector2D.is_valid_2d_coordinate((0., 1, '2')))
     self.assertFalse(Vector2D.is_valid_2d_coordinate(0))
     self.assertFalse(Vector2D.is_valid_2d_coordinate([9]))
     self.assertFalse(Vector2D.is_valid_2d_coordinate(['a']))
     self.assertFalse(Vector2D.is_valid_2d_coordinate(['a', 1]))
示例#12
0
 def test_large_polygon_project_onto_small_polygon(self):
     polygon1 = Polygon([(3.5, 1), (6, 1), (6, 3), (3.5, 3)])
     polygon2 = Polygon([(1, 1), (3, 1), (3, 4), (4, 4), (4, 5), (0, 5),
                         (0, 4), (1, 4), (1, 1)])
     line = Projector(polygon2,
                      projecting_vector=Vector2D(1,
                                                 0)).project_onto(polygon1)
     self.assertTrue(isinstance(line, LineString))
     self.assertEqual(line.length,
                      polygon1.buffer(MATH_EPS).intersection(line).length)
示例#13
0
 def get_projection_point(self, start_point: Point,
                          other_geom: BaseGeometry,
                          projecting_vector: Vector2D):
     ray_reaching_point = projecting_vector.multiply(
         self._max_projecting_length).apply(start_point)
     ray = LineString([start_point, ray_reaching_point])
     ray_intersection = ray.intersection(other_geom)
     if ray_intersection.is_empty:
         return None
     projection_point = nearest_points(ray_intersection, start_point)[0]
     return projection_point
示例#14
0
 def test_invalid_geometry_created_by_insert_coord(self):
     geom = wkt_loads(
         "POLYGON ((1087.38777718021 587.6238258803972, 1066.990091678121 587.6238258803972, 1066.990091580104 587.6238258852126, 1066.990091483031 587.6238258996119, 1066.990091387836 587.6238259234569, 1066.990091295438 587.6238259565177, 1066.990091206724 587.623825998476, 1066.990091122551 587.6238260489276, 1066.990091043728 587.6238261073868, 1066.990090971014 587.6238261732905, 1066.990090905111 587.623826246004, 1066.990090846652 587.6238263248271, 1066.9900907962 587.6238264090005, 1066.990090754242 587.6238264977138, 1066.990090721181 587.6238265901126, 1066.990090697336 587.6238266853069, 1066.990090682936 587.6238267823801, 1066.990090678121 587.6238268803972, 1066.990090678121 615.1628893803972, 1066.990090682936 615.1628894784144, 1066.990090697336 615.1628895754876, 1066.990090721181 615.1628896706819, 1066.990090754242 615.1628897630807, 1066.9900907962 615.162889851794, 1066.990090846652 615.1628899359674, 1066.990090905111 615.1628900147905, 1066.990090971014 615.162890087504, 1066.990091043728 615.1628901534077, 1066.990091122551 615.1628902118669, 1066.990091206724 615.1628902623185, 1066.990091295438 615.1628903042767, 1066.990091387836 615.1628903373376, 1066.990091483031 615.1628903611826, 1066.990091580104 615.1628903755819, 1066.990091678121 615.1628903803972, 1087.38777718021 615.1628903803972, 1087.387777278227 615.1628903755819, 1087.3877773753 615.1628903611826, 1087.387777470495 615.1628903373376, 1087.387777562894 615.1628903042767, 1087.387777651607 615.1628902623185, 1087.38777773578 615.1628902118669, 1087.387777814603 615.1628901534077, 1087.387777887317 615.162890087504, 1087.387777953221 615.1628900147905, 1087.38777801168 615.1628899359674, 1087.387778062131 615.162889851794, 1087.38777810409 615.1628897630807, 1087.38777813715 615.1628896706819, 1087.387778160995 615.1628895754876, 1087.387778175395 615.1628894784144, 1087.38777818021 615.1628893803972, 1087.38777818021 587.6238268803972, 1087.387778175395 587.6238267823801, 1087.387778160995 587.6238266853069, 1087.38777813715 587.6238265901126, 1087.38777810409 587.6238264977138, 1087.387778062131 587.6238264090005, 1087.38777801168 587.6238263248271, 1087.387777953221 587.623826246004, 1087.387777887317 587.6238261732905, 1087.387777814603 587.6238261073868, 1087.38777773578 587.6238260489276, 1087.387777651607 587.623825998476, 1087.387777562894 587.6238259565177, 1087.387777470495 587.6238259234569, 1087.3877773753 587.6238258996119, 1087.387777278227 587.6238258852126, 1087.38777718021 587.6238258803972))"
     )
     other_geom = wkt_loads(
         "POLYGON ((1107.224685371296 596.8330699036721, 1097.188366375415 596.8330699036721, 1097.188366277398 596.8330699084875, 1097.188366180325 596.8330699228868, 1097.18836608513 596.8330699467318, 1097.188365992732 596.8330699797926, 1097.188365904018 596.8330700217509, 1097.188365819845 596.8330700722025, 1097.188365741022 596.8330701306617, 1097.188365668308 596.8330701965654, 1097.188365602405 596.8330702692789, 1097.188365543946 596.833070348102, 1097.188365493494 596.8330704322753, 1097.188365451536 596.8330705209887, 1097.188365418475 596.8330706133875, 1097.18836539463 596.8330707085818, 1097.18836538023 596.833070805655, 1097.188365375415 596.8330709036721, 1097.188365375415 602.9854146536721, 1097.18836538023 602.9854147516893, 1097.18836539463 602.9854148487625, 1097.188365418475 602.9854149439568, 1097.188365451536 602.9854150363556, 1097.188365493494 602.9854151250689, 1097.188365543946 602.9854152092423, 1097.188365602405 602.9854152880654, 1097.188365668308 602.9854153607789, 1097.188365741022 602.9854154266826, 1097.188365819845 602.9854154851417, 1097.188365904018 602.9854155355933, 1097.188365992732 602.9854155775516, 1097.18836608513 602.9854156106124, 1097.188366180325 602.9854156344575, 1097.188366277398 602.9854156488568, 1097.188366375415 602.9854156536721, 1107.224685371296 602.9854156536721, 1107.224685469313 602.9854156488568, 1107.224685566386 602.9854156344575, 1107.22468566158 602.9854156106124, 1107.224685753979 602.9854155775516, 1107.224685842692 602.9854155355933, 1107.224685926866 602.9854154851417, 1107.224686005689 602.9854154266826, 1107.224686078403 602.9854153607789, 1107.224686144306 602.9854152880654, 1107.224686202765 602.9854152092423, 1107.224686253217 602.9854151250689, 1107.224686295175 602.9854150363556, 1107.224686328236 602.9854149439568, 1107.224686352081 602.9854148487625, 1107.22468636648 602.9854147516893, 1107.224686371296 602.9854146536721, 1107.224686371296 596.8330709036721, 1107.22468636648 596.833070805655, 1107.224686352081 596.8330707085818, 1107.224686328236 596.8330706133875, 1107.224686295175 596.8330705209887, 1107.224686253217 596.8330704322753, 1107.224686202765 596.833070348102, 1107.224686144306 596.8330702692789, 1107.224686078403 596.8330701965654, 1107.224686005689 596.8330701306617, 1107.224685926866 596.8330700722025, 1107.224685842692 596.8330700217509, 1107.224685753979 596.8330699797926, 1107.22468566158 596.8330699467318, 1107.224685566386 596.8330699228868, 1107.224685469313 596.8330699084875, 1107.224685371296 596.8330699036721))"
     )
     projector = Projector(geom, None)
     new_geom = projector._insert_other_geom_projections_into_geom(
         other_geom, Vector2D(-1, 0), geom)
     self.assertTrue(new_geom.is_valid)
示例#15
0
    def test_from_coordinate(self):
        vector1 = Vector2D.from_coordinate([0, 1])
        self.assertTrue(isinstance(vector1, Vector2D))
        self.assertEqual(0, vector1.x)
        self.assertEqual(1, vector1.y)

        vector2 = Vector2D.from_coordinate((1, 2, 1))
        self.assertTrue(isinstance(vector2, Vector2D))
        self.assertEqual(1, vector2.x)
        self.assertEqual(2, vector2.y)

        with self.assertRaises(ValueError):
            Vector2D.from_coordinate([1])
        with self.assertRaises(ValueError):
            Vector2D.from_coordinate(['a', 1, 1])
示例#16
0
 def test_reverse(self):
     vector1 = Vector2D(2, 1)
     reversed_vec = vector1.reverse()
     self.assertEqual(Vector2D(-2, -1), reversed_vec)
示例#17
0
 def _is_corner(cur_coord, prev_coord, next_coord,
                min_corner_angle_degree: float) -> bool:
     prev_vec = Vector2D.from_coordinates(prev_coord, cur_coord)
     next_vec = Vector2D.from_coordinates(cur_coord, next_coord)
     angle_in_degree = prev_vec.angle_to(next_vec)
     return angle_in_degree >= min_corner_angle_degree
示例#18
0
 def test_rotate_to(self):
     vector1 = Vector2D(1, 0)
     other1 = Vector2D(1, -1)
     self.assertAlmostEqual(45, vector1.angle_to(other1), delta=MATH_EPS)
     other2 = Vector2D(1, 1)
     self.assertAlmostEqual(45, vector1.angle_to(other2), delta=MATH_EPS)