test_geometry.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.script.core import run_command
  13. from grass.pygrass.vector import VectorTopo
  14. from grass.pygrass.vector.geometry import Point, Line, Node
  15. class PointTestCase(TestCase):
  16. def test_empty_init(self):
  17. """Test Point()"""
  18. point = Point()
  19. self.assertEqual(point.gtype, libvect.GV_POINT)
  20. self.assertEqual(point.x, 0)
  21. self.assertEqual(point.y, 0)
  22. self.assertIsNone(point.z)
  23. self.assertTrue(point.is2D)
  24. def test_init_3d(self):
  25. """Test 3D Point(1, 2, 3)"""
  26. point = Point(1, 2, 3)
  27. self.assertEqual(point.x, 1)
  28. self.assertEqual(point.y, 2)
  29. self.assertEqual(point.z, 3)
  30. self.assertFalse(point.is2D)
  31. def test_switch_2D_3D_2D(self):
  32. """Test switch between: 2D => 3D => 2D"""
  33. point = Point()
  34. self.assertIsNone(point.z)
  35. self.assertTrue(point.is2D)
  36. point.z = 1
  37. self.assertFalse(point.is2D)
  38. point.z = None
  39. self.assertTrue(point.is2D, True)
  40. def test_coords(self):
  41. """Test coords method"""
  42. self.assertEqual(Point(1, 2).coords(), (1, 2))
  43. self.assertEqual(Point(1, 2, 3).coords(), (1, 2, 3))
  44. def test_to_wkt(self):
  45. """Test coords method"""
  46. self.assertEqual(Point(1, 2).to_wkt(), 'POINT(1.000000 2.000000)')
  47. self.assertEqual(Point(1, 2, 3).to_wkt(),
  48. 'POINT(1.000000 2.000000 3.000000)')
  49. def test_distance(self):
  50. """Test distance method"""
  51. point0 = Point(0, 0, 0)
  52. point1 = Point(1, 0)
  53. self.assertEqual(point0.distance(point1), 1.0)
  54. point1.z = 1
  55. self.assertAlmostEqual(point0.distance(point1), np.sqrt(2.))
  56. def test_eq(self):
  57. """Test __eq__"""
  58. point0 = Point(0, 0)
  59. point1 = Point(1, 0)
  60. self.assertFalse(point0 == point1)
  61. self.assertFalse(point0 == (1, 0))
  62. self.assertTrue(point0 == point0)
  63. self.assertTrue(point0 == (0, 0))
  64. def test_repr(self):
  65. """Test __eq__"""
  66. self.assertEqual(repr(Point(1, 2)), 'Point(1.000000, 2.000000)')
  67. self.assertEqual(repr(Point(1, 2, 3)),
  68. 'Point(1.000000, 2.000000, 3.000000)')
  69. @unittest.skip("Not implemented yet.")
  70. def test_buffer(self):
  71. """Test buffer method"""
  72. # TODO: verify if the buffer depends from the mapset's projection
  73. pass
  74. class LineTestCase(TestCase):
  75. tmpname = "LineTestCase_map"
  76. @classmethod
  77. def setUpClass(cls):
  78. from grass.pygrass import utils
  79. utils.create_test_vector_map(cls.tmpname)
  80. cls.vect = None
  81. cls.vect = VectorTopo(cls.tmpname)
  82. cls.vect.open('r')
  83. cls.c_mapinfo = cls.vect.c_mapinfo
  84. @classmethod
  85. def tearDownClass(cls):
  86. if cls.vect is not None:
  87. cls.vect.close()
  88. cls.c_mapinfo = None
  89. """Remove the generated vector map, if exist"""
  90. from grass.pygrass.utils import get_mapset_vector
  91. mset = get_mapset_vector(cls.tmpname, mapset='')
  92. if mset:
  93. run_command("g.remove", flags='f', type='vector', name=cls.tmpname)
  94. def test_len(self):
  95. """Test __len__ magic method"""
  96. self.assertEqual(len(Line()), 0)
  97. self.assertEqual(len(Line([(0, 0), (1, 1)])), 2)
  98. @unittest.skipIf(sys.version_info[:2] < (2, 7), "Require Python >= 2.7")
  99. def test_getitem(self):
  100. """Test __getitem__ magic method"""
  101. line = Line([(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)])
  102. self.assertTupleEqual(line[0].coords(), (0, 0))
  103. self.assertTupleEqual(line[1].coords(), (1, 1))
  104. self.assertTupleEqual(line[-2].coords(), (3, 3))
  105. self.assertTupleEqual(line[-1].coords(), (4, 4))
  106. self.assertListEqual([p.coords() for p in line[:2]], [(0, 0), (1, 1)])
  107. self.assertListEqual([p.coords() for p in line[::2]],
  108. [(0, 0), (2, 2), (4, 4)])
  109. with self.assertRaises(IndexError):
  110. line[5]
  111. @unittest.skipIf(sys.version_info[:2] < (2, 7), "Require Python >= 2.7")
  112. def test_setitem(self):
  113. """Test __setitem__ magic method"""
  114. line = Line([(0, 0), (1, 1)])
  115. self.assertTupleEqual(line[0].coords(), (0., 0.))
  116. line[0] = (10, 10)
  117. self.assertTupleEqual(line[0].coords(), (10., 10.))
  118. @unittest.skipIf(sys.version_info[:2] < (2, 7), "Require Python >= 2.7")
  119. def test_get_pnt(self):
  120. """Test get_pnt method"""
  121. line = Line([(0, 0), (1, 1)])
  122. with self.assertRaises(ValueError):
  123. line.point_on_line(5)
  124. vals = (0.7071067811865475, 0.7071067811865475)
  125. self.assertTupleEqual(line.point_on_line(1).coords(), vals)
  126. def test_bbox(self):
  127. """Test bbox method"""
  128. line = Line([(0, 10), (0, 11), (1, 11), (1, 10)])
  129. bbox = line.bbox()
  130. self.assertEqual(11, bbox.north)
  131. self.assertEqual(10, bbox.south)
  132. self.assertEqual(1, bbox.east)
  133. self.assertEqual(0, bbox.west)
  134. def test_nodes(self):
  135. """Test nodes method"""
  136. def nodes2tuple(nodes):
  137. """Convert an iterable of nodes to a tuple of nodes id"""
  138. return tuple(n.id for n in nodes)
  139. with VectorTopo("LineTestCase_map", mode='r') as vect:
  140. self.assertTupleEqual((1, 2), nodes2tuple(vect[4].nodes()))
  141. self.assertTupleEqual((3, 4), nodes2tuple(vect[5].nodes()))
  142. self.assertTupleEqual((5, 6), nodes2tuple(vect[6].nodes()))
  143. class NodeTestCase(TestCase):
  144. tmpname = "NodeTestCase_map"
  145. @classmethod
  146. def setUpClass(cls):
  147. from grass.pygrass import utils
  148. utils.create_test_vector_map(cls.tmpname)
  149. cls.vect = None
  150. cls.vect = VectorTopo(cls.tmpname)
  151. cls.vect.open('r')
  152. cls.c_mapinfo = cls.vect.c_mapinfo
  153. @classmethod
  154. def tearDownClass(cls):
  155. if cls.vect is not None:
  156. cls.vect.close()
  157. cls.c_mapinfo = None
  158. """Remove the generated vector map, if exist"""
  159. from grass.pygrass.utils import get_mapset_vector
  160. mset = get_mapset_vector(cls.tmpname, mapset='')
  161. if mset:
  162. run_command("g.remove", flags='f', type='vector', name=cls.tmpname)
  163. def test_init(self):
  164. """Test Node __init__"""
  165. node = Node(v_id=4, c_mapinfo=self.c_mapinfo)
  166. self.assertEqual(4, node.id)
  167. self.assertTrue(node.is2D)
  168. self.assertEqual(1, node.nlines)
  169. def test_coords(self):
  170. """Test Node coordinates"""
  171. node = Node(v_id=4, c_mapinfo=self.c_mapinfo)
  172. self.assertTupleEqual((12.0, 0.0),
  173. node.coords())
  174. def test_ilines(self):
  175. """Test Node coordinates"""
  176. node = Node(v_id=4, c_mapinfo=self.c_mapinfo) # Line 5 ends in this node
  177. self.assertTupleEqual((-5,), tuple(node.ilines()))
  178. self.assertTupleEqual((-5,), tuple(node.ilines(only_in=True)))
  179. node = Node(v_id=3, c_mapinfo=self.c_mapinfo) # Line 5 starts at this node
  180. self.assertTupleEqual((5,), tuple(node.ilines(only_out=True)))
  181. def test_angles(self):
  182. """Test Node angles"""
  183. node = Node(v_id=4, c_mapinfo=self.c_mapinfo)
  184. angles = (1.5707963705062866,) # 90°
  185. self.assertTupleEqual(angles, tuple(node.angles()))
  186. if __name__ == '__main__':
  187. test()