當前位置: 首頁>>代碼示例>>Python>>正文


Python Polygon.apply_transform方法代碼示例

本文整理匯總了Python中karta.vector.geometry.Polygon.apply_transform方法的典型用法代碼示例。如果您正苦於以下問題:Python Polygon.apply_transform方法的具體用法?Python Polygon.apply_transform怎麽用?Python Polygon.apply_transform使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在karta.vector.geometry.Polygon的用法示例。


在下文中一共展示了Polygon.apply_transform方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: TestAffineTransforms

# 需要導入模塊: from karta.vector.geometry import Polygon [as 別名]
# 或者: from karta.vector.geometry.Polygon import apply_transform [as 別名]
class TestAffineTransforms(unittest.TestCase):

    def setUp(self):
        self.square = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])
        return

    def test_translate_point(self):
        pt = Point((0, 0), crs=Cartesian)

        with self.assertRaises(ValueError):
            newpt = pt.apply_transform(np.array([[0, 0, 1, 0], [0, 0, 2, 0]]))

        newpt = pt.apply_transform(np.array([[0, 0, 1], [0, 0, 2]]))
        self.assertEqual(newpt.x, 1.0)
        self.assertEqual(newpt.y, 2.0)
        return

    def test_translate_point_3(self):
        pt = Point((0, 0, 3), crs=Cartesian)
        newpt = pt.apply_transform(np.array([[0, 0, 1], [0, 0, 2]]))
        self.assertEqual(newpt.x, 1.0)
        self.assertEqual(newpt.y, 2.0)
        self.assertEqual(newpt.z, 3.0)
        return

    def test_transform_multiline(self):
        g = Multiline([[(0,0), (1,1), (1,2)], [(-3,2),  (-2,-1), (0,1)]])
        pi = math.pi
        gnew = g.apply_transform(np.array([[math.cos(0.5*pi), -math.sin(0.5*pi), 0],
                                           [math.sin(0.5*pi), math.cos(0.5*pi), 0]]))
        l1, l2 = gnew.vertices()
        self.assertTrue(np.allclose(l1, np.array([(0,0), (-1,1), (-2,1)])))
        self.assertTrue(np.allclose(l2, np.array([(-2,-3), (1,-2), (-1,0)])))

        gnew2 = gnew.apply_transform(np.array([[2, 0, 0],
                                               [0, -3, 0]]))
        l1, l2 = gnew2.vertices()
        self.assertTrue(np.allclose(l1, np.array([(0,0), (-2,-3), (-4,-3)])))
        self.assertTrue(np.allclose(l2, np.array([(-4,9), (2,6), (-2,0)])))
        return

    def test_translate_multipoint(self):
        M = affine_matrix(Multipoint([(0,0), (1,0), (0,1)]),
                          Multipoint([(1,0), (2,0), (1,1)]))
        Mans = np.array([[1, 0, 1], [0, 1, 0]])
        self.assertTrue(np.allclose(M, Mans))

        translated_square = self.square.apply_transform(M)
        ans = np.array([[1, 0], [1, 1], [2, 1], [2, 0]])
        self.assertTrue(np.allclose(translated_square.vertices(), ans))
        return

    def test_rotate_multipoint(self):
        s2 = math.sqrt(0.5)
        M = affine_matrix(Multipoint([(0,0), (1,0), (0,1)]),
                          Multipoint([(0,0), (s2,s2), (-s2,s2)]))
        Mans = np.array([[s2, -s2, 0], [s2, s2, 0]])
        self.assertTrue(np.allclose(M, Mans))

        translated_square = self.square.apply_transform(M)
        ans = np.array([[0, 0], [-s2, s2], [0, 2*s2], [s2, s2]])
        self.assertTrue(np.allclose(translated_square.vertices(), ans))
        return

    def test_stretch_multipoint(self):
        M = affine_matrix(Multipoint([(0,0), (1,0), (0,1)]),
                          Multipoint([(0,0), (2,0), (0,2)]))
        Mans = np.array([[2, 0, 0], [0, 2, 0]])
        self.assertTrue(np.allclose(M, Mans))

        translated_square = self.square.apply_transform(M)
        ans = np.array([[0, 0], [0, 2], [2, 2], [2, 0]])
        self.assertTrue(np.allclose(translated_square.vertices(), ans))
        return
開發者ID:fortyninemaps,項目名稱:karta,代碼行數:76,代碼來源:geometry_tests.py


注:本文中的karta.vector.geometry.Polygon.apply_transform方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。