test_geometry.py 6.4 KB

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