test_geometry.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Jun 19 14:13:53 2014
  4. @author: pietro
  5. """
  6. import sys
  7. import unittest
  8. import numpy as np
  9. from grass.gunittest import TestCase, test
  10. import grass.lib.vector as libvect
  11. from grass.pygrass.vector import VectorTopo
  12. from grass.pygrass.vector.geometry import Point, Line, Node
  13. class PointTestCase(TestCase):
  14. def test_empty_init(self):
  15. """Test Point()"""
  16. point = Point()
  17. self.assertEqual(point.gtype, libvect.GV_POINT)
  18. self.assertEqual(point.x, 0)
  19. self.assertEqual(point.y, 0)
  20. self.assertIsNone(point.z)
  21. self.assertTrue(point.is2D)
  22. def test_init_3d(self):
  23. """Test 3D Point(1, 2, 3)"""
  24. point = Point(1, 2, 3)
  25. self.assertEqual(point.x, 1)
  26. self.assertEqual(point.y, 2)
  27. self.assertEqual(point.z, 3)
  28. self.assertFalse(point.is2D)
  29. def test_switch_2D_3D_2D(self):
  30. """Test switch between: 2D => 3D => 2D"""
  31. point = Point()
  32. self.assertIsNone(point.z)
  33. self.assertTrue(point.is2D)
  34. point.z = 1
  35. self.assertFalse(point.is2D)
  36. point.z = None
  37. self.assertTrue(point.is2D, True)
  38. def test_coords(self):
  39. """Test coords method"""
  40. self.assertEqual(Point(1, 2).coords(), (1, 2))
  41. self.assertEqual(Point(1, 2, 3).coords(), (1, 2, 3))
  42. def test_get_wkt(self):
  43. """Test coords method"""
  44. self.assertEqual(Point(1, 2).get_wkt(), 'POINT(1.000000 2.000000)')
  45. self.assertEqual(Point(1, 2, 3).get_wkt(),
  46. 'POINT(1.000000 2.000000 3.000000)')
  47. def test_distance(self):
  48. """Test distance method"""
  49. point0 = Point(0, 0, 0)
  50. point1 = Point(1, 0)
  51. self.assertEqual(point0.distance(point1), 1.0)
  52. point1.z = 1
  53. self.assertAlmostEqual(point0.distance(point1), np.sqrt(2.))
  54. def test_eq(self):
  55. """Test __eq__"""
  56. point0 = Point(0, 0)
  57. point1 = Point(1, 0)
  58. self.assertFalse(point0 == point1)
  59. self.assertFalse(point0 == (1, 0))
  60. self.assertTrue(point0 == point0)
  61. self.assertTrue(point0 == (0, 0))
  62. def test_repr(self):
  63. """Test __eq__"""
  64. self.assertEqual(repr(Point(1, 2)), 'Point(1.000000, 2.000000)')
  65. self.assertEqual(repr(Point(1, 2, 3)),
  66. 'Point(1.000000, 2.000000, 3.000000)')
  67. @unittest.skip("Not implemented yet.")
  68. def test_buffer(self):
  69. """Test buffer method"""
  70. # TODO: verify if the buffer depends from the mapset's projection
  71. pass
  72. class LineTestCase(TestCase):
  73. def test_len(self):
  74. """Test __len__ magic method"""
  75. self.assertEqual(len(Line()), 0)
  76. self.assertEqual(len(Line([(0, 0), (1, 1)])), 2)
  77. @unittest.skipIf(sys.version_info[:2] < (2, 7), "Require Python >= 2.7")
  78. def test_getitem(self):
  79. """Test __getitem__ magic method"""
  80. line = Line([(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)])
  81. self.assertTupleEqual(line[0].coords(), (0, 0))
  82. self.assertTupleEqual(line[1].coords(), (1, 1))
  83. self.assertTupleEqual(line[-2].coords(), (3, 3))
  84. self.assertTupleEqual(line[-1].coords(), (4, 4))
  85. self.assertListEqual([p.coords() for p in line[:2]], [(0, 0), (1, 1)])
  86. self.assertListEqual([p.coords() for p in line[::2]],
  87. [(0, 0), (2, 2), (4, 4)])
  88. with self.assertRaises(IndexError):
  89. line[5]
  90. @unittest.skipIf(sys.version_info[:2] < (2, 7), "Require Python >= 2.7")
  91. def test_setitem(self):
  92. """Test __setitem__ magic method"""
  93. line = Line([(0, 0), (1, 1)])
  94. self.assertTupleEqual(line[0].coords(), (0., 0.))
  95. line[0] = (10, 10)
  96. self.assertTupleEqual(line[0].coords(), (10., 10.))
  97. @unittest.skipIf(sys.version_info[:2] < (2, 7), "Require Python >= 2.7")
  98. def test_get_pnt(self):
  99. """Test get_pnt method"""
  100. line = Line([(0, 0), (1, 1)])
  101. with self.assertRaises(ValueError):
  102. line.get_pnt(5)
  103. vals = (0.7071067811865475, 0.7071067811865475)
  104. self.assertTupleEqual(line.get_pnt(1).coords(), vals)
  105. def test_bbox(self):
  106. """Test bbox method"""
  107. line = Line([(0, 10), (0, 11), (1, 11), (1, 10)])
  108. bbox = line.bbox()
  109. self.assertEqual(11, bbox.north)
  110. self.assertEqual(10, bbox.south)
  111. self.assertEqual(1, bbox.east)
  112. self.assertEqual(0, bbox.west)
  113. def test_nodes(self):
  114. """Test inodes method"""
  115. def nodes2tuple(nodes):
  116. """Convert an iterable of nodes to a tuple of nodes id"""
  117. return tuple(n.id for n in nodes)
  118. with VectorTopo("roadsmajor", mode='r') as vect:
  119. self.assertTupleEqual((206, 172), nodes2tuple(vect[284].nodes()))
  120. self.assertTupleEqual((208, 206), nodes2tuple(vect[287].nodes()))
  121. self.assertTupleEqual((206, 209), nodes2tuple(vect[288].nodes()))
  122. self.assertTupleEqual((218, 206), nodes2tuple(vect[301].nodes()))
  123. class NodeTestCase(TestCase):
  124. @classmethod
  125. def setUpClass(cls):
  126. cls.vect = None
  127. cls.vect = VectorTopo("roadsmajor")
  128. cls.vect.open('r')
  129. cls.c_mapinfo = cls.vect.c_mapinfo
  130. @classmethod
  131. def tearDownClass(cls):
  132. if cls.vect is not None:
  133. cls.vect.close()
  134. cls.c_mapinfo = None
  135. def test_init(self):
  136. """Test Node __init__"""
  137. node = Node(v_id=206, c_mapinfo=self.c_mapinfo)
  138. self.assertEqual(206, node.id)
  139. self.assertTrue(node.is2D)
  140. self.assertEqual(4, node.nlines)
  141. def test_coords(self):
  142. """Test Node coordinates"""
  143. node = Node(v_id=206, c_mapinfo=self.c_mapinfo)
  144. self.assertTupleEqual((620906.5786131569, 221685.65913128198),
  145. node.coords())
  146. def test_ilines(self):
  147. """Test Node coordinates"""
  148. node = Node(v_id=206, c_mapinfo=self.c_mapinfo)
  149. self.assertTupleEqual((288, -301, -287, 284), tuple(node.ilines()))
  150. self.assertTupleEqual((-301, -287), tuple(node.ilines(only_in=True)))
  151. self.assertTupleEqual((288, 284), tuple(node.ilines(only_out=True)))
  152. def test_angles(self):
  153. """Test Node angles"""
  154. node = Node(v_id=206, c_mapinfo=self.c_mapinfo)
  155. angles = (-3.044905185699463, -1.026218056678772,
  156. 0.10362745821475983, 2.2236430644989014)
  157. self.assertTupleEqual(angles, tuple(node.angles()))
  158. if __name__ == '__main__':
  159. test()