test_geometry.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 Vector, VectorTopo
  14. from grass.pygrass.vector.geometry import Point, Line, Node
  15. from grass.pygrass.vector.geometry import Area, Boundary, Centroid
  16. from grass.pygrass.vector.basic import Bbox
  17. class PointTestCase(TestCase):
  18. def test_empty_init(self):
  19. """Test Point()"""
  20. point = Point()
  21. self.assertEqual(point.gtype, libvect.GV_POINT)
  22. self.assertEqual(point.x, 0)
  23. self.assertEqual(point.y, 0)
  24. self.assertIsNone(point.z)
  25. self.assertTrue(point.is2D)
  26. def test_init_3d(self):
  27. """Test 3D Point(1, 2, 3)"""
  28. point = Point(1, 2, 3)
  29. self.assertEqual(point.x, 1)
  30. self.assertEqual(point.y, 2)
  31. self.assertEqual(point.z, 3)
  32. self.assertFalse(point.is2D)
  33. def test_switch_2D_3D_2D(self):
  34. """Test switch between: 2D => 3D => 2D"""
  35. point = Point()
  36. self.assertIsNone(point.z)
  37. self.assertTrue(point.is2D)
  38. point.z = 1
  39. self.assertFalse(point.is2D)
  40. point.z = None
  41. self.assertTrue(point.is2D, True)
  42. def test_coords(self):
  43. """Test coords method"""
  44. self.assertEqual(Point(1, 2).coords(), (1, 2))
  45. self.assertEqual(Point(1, 2, 3).coords(), (1, 2, 3))
  46. def test_to_wkt_p(self):
  47. """Test coords method"""
  48. self.assertEqual(Point(1, 2).to_wkt_p(), 'POINT(1.000000 2.000000)')
  49. self.assertEqual(Point(1, 2, 3).to_wkt_p(),
  50. 'POINT(1.000000 2.000000 3.000000)')
  51. def test_to_wkt(self):
  52. """Test coords method"""
  53. self.assertEqual(Point(1, 2).to_wkt(), 'POINT (1.0000000000000000 2.0000000000000000)')
  54. self.assertEqual(Point(1, 2, 3).to_wkt(),
  55. 'POINT Z (1.0000000000000000 2.0000000000000000 3.0000000000000000)')
  56. def test_to_wkb(self):
  57. """Test to_wkb method"""
  58. self.assertEqual(len(Point(1, 2).to_wkb()), 21)
  59. def test_distance(self):
  60. """Test distance method"""
  61. point0 = Point(0, 0, 0)
  62. point1 = Point(1, 0)
  63. self.assertEqual(point0.distance(point1), 1.0)
  64. point1.z = 1
  65. self.assertAlmostEqual(point0.distance(point1), np.sqrt(2.))
  66. def test_eq(self):
  67. """Test __eq__"""
  68. point0 = Point(0, 0)
  69. point1 = Point(1, 0)
  70. self.assertFalse(point0 == point1)
  71. self.assertFalse(point0 == (1, 0))
  72. self.assertTrue(point0 == point0)
  73. self.assertTrue(point0 == (0, 0))
  74. def test_repr(self):
  75. """Test __eq__"""
  76. self.assertEqual(repr(Point(1, 2)), 'Point(1.000000, 2.000000)')
  77. self.assertEqual(repr(Point(1, 2, 3)),
  78. 'Point(1.000000, 2.000000, 3.000000)')
  79. @unittest.skip("Not implemented yet.")
  80. def test_buffer(self):
  81. """Test buffer method"""
  82. # TODO: verify if the buffer depends from the mapset's projection
  83. pass
  84. class LineTestCase(TestCase):
  85. tmpname = "LineTestCase_map"
  86. @classmethod
  87. def setUpClass(cls):
  88. from grass.pygrass import utils
  89. utils.create_test_vector_map(cls.tmpname)
  90. cls.vect = None
  91. cls.vect = VectorTopo(cls.tmpname)
  92. cls.vect.open('r')
  93. cls.c_mapinfo = cls.vect.c_mapinfo
  94. @classmethod
  95. def tearDownClass(cls):
  96. if cls.vect is not None:
  97. cls.vect.close()
  98. cls.c_mapinfo = None
  99. """Remove the generated vector map, if exist"""
  100. cls.runModule("g.remove", flags='f', type='vector',
  101. name=cls.tmpname)
  102. def test_len(self):
  103. """Test __len__ magic method"""
  104. self.assertEqual(len(Line()), 0)
  105. self.assertEqual(len(Line([(0, 0), (1, 1)])), 2)
  106. @unittest.skipIf(sys.version_info[:2] < (2, 7), "Require Python >= 2.7")
  107. def test_getitem(self):
  108. """Test __getitem__ magic method"""
  109. line = Line([(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)])
  110. self.assertTupleEqual(line[0].coords(), (0, 0))
  111. self.assertTupleEqual(line[1].coords(), (1, 1))
  112. self.assertTupleEqual(line[-2].coords(), (3, 3))
  113. self.assertTupleEqual(line[-1].coords(), (4, 4))
  114. self.assertListEqual([p.coords() for p in line[:2]], [(0, 0), (1, 1)])
  115. self.assertListEqual([p.coords() for p in line[::2]],
  116. [(0, 0), (2, 2), (4, 4)])
  117. with self.assertRaises(IndexError):
  118. line[5]
  119. @unittest.skipIf(sys.version_info[:2] < (2, 7), "Require Python >= 2.7")
  120. def test_setitem(self):
  121. """Test __setitem__ magic method"""
  122. line = Line([(0, 0), (1, 1)])
  123. self.assertTupleEqual(line[0].coords(), (0., 0.))
  124. line[0] = (10, 10)
  125. self.assertTupleEqual(line[0].coords(), (10., 10.))
  126. @unittest.skipIf(sys.version_info[:2] < (2, 7), "Require Python >= 2.7")
  127. def test_get_pnt(self):
  128. """Test get_pnt method"""
  129. line = Line([(0, 0), (1, 1)])
  130. with self.assertRaises(ValueError):
  131. line.point_on_line(5)
  132. vals = (0.7071067811865475, 0.7071067811865475)
  133. self.assertTupleEqual(line.point_on_line(1).coords(), vals)
  134. def test_to_wkt(self):
  135. """Test to_wkt method"""
  136. string = 'LINESTRING (0.0000000000000000 0.0000000000000000, 1.0000000000000000 1.0000000000000000)'
  137. self.assertEqual(Line([(0, 0), (1, 1)]).to_wkt(), string)
  138. def test_to_wkb(self):
  139. """Test to_wkb method"""
  140. self.assertEqual(len(Line([(0, 0), (1, 1)]).to_wkb()), 41)
  141. def test_bbox(self):
  142. """Test bbox method"""
  143. line = Line([(0, 10), (0, 11), (1, 11), (1, 10)])
  144. bbox = line.bbox()
  145. self.assertEqual(11, bbox.north)
  146. self.assertEqual(10, bbox.south)
  147. self.assertEqual(1, bbox.east)
  148. self.assertEqual(0, bbox.west)
  149. def test_nodes(self):
  150. """Test nodes method"""
  151. def nodes2tuple(nodes):
  152. """Convert an iterable of nodes to a tuple of nodes id"""
  153. return tuple(n.id for n in nodes)
  154. with VectorTopo("LineTestCase_map", mode='r') as vect:
  155. self.assertTupleEqual((1, 2), nodes2tuple(vect[4].nodes()))
  156. self.assertTupleEqual((3, 4), nodes2tuple(vect[5].nodes()))
  157. self.assertTupleEqual((5, 6), nodes2tuple(vect[6].nodes()))
  158. class NodeTestCase(TestCase):
  159. tmpname = "NodeTestCase_map"
  160. @classmethod
  161. def setUpClass(cls):
  162. # Tests are based on a stream network
  163. from grass.pygrass import utils
  164. utils.create_test_stream_network_map(cls.tmpname)
  165. cls.vect = None
  166. cls.vect = VectorTopo(cls.tmpname)
  167. cls.vect.open('r')
  168. cls.c_mapinfo = cls.vect.c_mapinfo
  169. @classmethod
  170. def tearDownClass(cls):
  171. if cls.vect is not None:
  172. cls.vect.close()
  173. cls.c_mapinfo = None
  174. """Remove the generated vector map, if exist"""
  175. cls.runModule("g.remove", flags='f', type='vector',
  176. name=cls.tmpname)
  177. def test_init(self):
  178. """Test Node __init__"""
  179. node = Node(v_id=4, c_mapinfo=self.c_mapinfo)
  180. self.assertEqual(4, node.id)
  181. self.assertTrue(node.is2D)
  182. self.assertEqual(5, node.nlines)
  183. def test_coords(self):
  184. """Test Node coordinates"""
  185. node = Node(v_id=4, c_mapinfo=self.c_mapinfo)
  186. self.assertTupleEqual((1.0, 0.0), node.coords())
  187. def test_ilines(self):
  188. """Test Node neighbors"""
  189. node = Node(v_id=4, c_mapinfo=self.c_mapinfo)
  190. self.assertTupleEqual((6, -4, 7, -3, -5), tuple(node.ilines()))
  191. self.assertTupleEqual((-4, -3, -5), tuple(node.ilines(only_in=True)))
  192. node = Node(v_id=4, c_mapinfo=self.c_mapinfo)
  193. self.assertTupleEqual((6, 7), tuple(node.ilines(only_out=True)))
  194. def test_angles(self):
  195. """Test Node angles"""
  196. node = Node(v_id=4, c_mapinfo=self.c_mapinfo)
  197. angles = (-1.5707963705062866, 0.7853981852531433,
  198. 1.2793395519256592, 1.8622530698776245,
  199. 2.356194496154785)
  200. self.assertTupleEqual(angles, tuple(node.angles()))
  201. class AreaTestCase(TestCase):
  202. tmpname = "AreaTestCase_map"
  203. @classmethod
  204. def setUpClass(cls):
  205. # Tests are based on a stream network
  206. from grass.pygrass import utils
  207. utils.create_test_vector_map(cls.tmpname)
  208. cls.vect = None
  209. cls.vect = VectorTopo(cls.tmpname)
  210. cls.vect.open('r')
  211. cls.c_mapinfo = cls.vect.c_mapinfo
  212. @classmethod
  213. def tearDownClass(cls):
  214. if cls.vect is not None:
  215. cls.vect.close()
  216. cls.c_mapinfo = None
  217. """Remove the generated vector map, if exist"""
  218. cls.runModule("g.remove", flags='f', type='vector',
  219. name=cls.tmpname)
  220. def test_init(self):
  221. """Test area __init__ and basic functions"""
  222. area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
  223. self.assertEqual(1, area.id)
  224. self.assertTrue(area.is2D)
  225. self.assertTrue(area.alive())
  226. self.assertEqual(area.area(), 12.0)
  227. def test_to_wkt(self):
  228. """Test to_wkt method"""
  229. area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
  230. # Outer and inner ring!!
  231. string = "POLYGON ((0.0000000000000000 0.0000000000000000, "\
  232. "0.0000000000000000 4.0000000000000000, "\
  233. "0.0000000000000000 4.0000000000000000, "\
  234. "4.0000000000000000 4.0000000000000000, "\
  235. "4.0000000000000000 4.0000000000000000, "\
  236. "4.0000000000000000 0.0000000000000000, "\
  237. "4.0000000000000000 0.0000000000000000, "\
  238. "0.0000000000000000 0.0000000000000000), "\
  239. "(1.0000000000000000 1.0000000000000000, "\
  240. "3.0000000000000000 1.0000000000000000, "\
  241. "3.0000000000000000 3.0000000000000000, "\
  242. "1.0000000000000000 3.0000000000000000, "\
  243. "1.0000000000000000 1.0000000000000000))"
  244. self.assertEqual(area.to_wkt(), string)
  245. def test_to_wkb(self):
  246. """Test to_wkt method"""
  247. area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
  248. self.assertEqual(len(area.to_wkb()), 225)
  249. def test_contains_point(self):
  250. """Test contain_point method"""
  251. area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
  252. p = Point(0.5, 0.5)
  253. bbox = Bbox(4.0, 0.0, 4.0, 0.0)
  254. self.assertTrue(area.contains_point(p, bbox))
  255. self.assertTrue(area.contains_point(p))
  256. def test_bbox(self):
  257. """Test contain_point method"""
  258. area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
  259. self.assertEqual(str(area.bbox()), "Bbox(4.0, 0.0, 4.0, 0.0)")
  260. def test_centroid(self):
  261. """Test centroid access"""
  262. area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
  263. centroid = area.centroid()
  264. self.assertEqual(centroid.id, 18)
  265. self.assertEqual(centroid.area_id, 1)
  266. self.assertEqual(centroid.to_wkt(), 'POINT (3.5000000000000000 3.5000000000000000)')
  267. def test_boundaries_1(self):
  268. """Test boundary access"""
  269. area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
  270. boundaries = area.boundaries()
  271. self.assertEqual(len(boundaries), 4)
  272. string_list = []
  273. string_list.append("LINESTRING (0.0000000000000000 0.0000000000000000, 0.0000000000000000 4.0000000000000000)")
  274. string_list.append("LINESTRING (0.0000000000000000 4.0000000000000000, 4.0000000000000000 4.0000000000000000)")
  275. string_list.append("LINESTRING (4.0000000000000000 4.0000000000000000, 4.0000000000000000 0.0000000000000000)")
  276. string_list.append("LINESTRING (4.0000000000000000 0.0000000000000000, 0.0000000000000000 0.0000000000000000)")
  277. for boundary, i in zip(boundaries, range(4)):
  278. self.assertEqual(len(boundary.to_wkb()), 41)
  279. self.assertEqual(boundary.to_wkt(), string_list[i])
  280. def test_boundaries_2(self):
  281. """Test boundary access"""
  282. area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
  283. boundaries = area.boundaries()
  284. boundary = boundaries[2]
  285. boundary.read_area_ids()
  286. self.assertEqual(boundary.left_area_id, 2)
  287. self.assertEqual(boundary.right_area_id, 1)
  288. self.assertEqual(boundary.left_centroid().to_wkt(), 'POINT (5.5000000000000000 3.5000000000000000)')
  289. self.assertEqual(boundary.right_centroid().to_wkt(), 'POINT (3.5000000000000000 3.5000000000000000)')
  290. def test_isles_1(self):
  291. """Test centroid access"""
  292. area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
  293. self.assertEqual(area.num_isles(), 1)
  294. isles = area.isles()
  295. isle = isles[0]
  296. self.assertEqual(isle.area(), 4.0)
  297. self.assertEqual(isle.points().to_wkt(), "LINESTRING (1.0000000000000000 1.0000000000000000, "
  298. "3.0000000000000000 1.0000000000000000, "
  299. "3.0000000000000000 3.0000000000000000, "
  300. "1.0000000000000000 3.0000000000000000, "
  301. "1.0000000000000000 1.0000000000000000)")
  302. def test_isles_2(self):
  303. """Test centroid access"""
  304. area = Area(v_id=1, c_mapinfo=self.c_mapinfo)
  305. self.assertEqual(area.num_isles(), 1)
  306. isles = area.isles()
  307. isle = isles[0]
  308. self.assertEqual(isle.area_id(), 1)
  309. self.assertTrue(isle.alive())
  310. self.assertEqual(str(isle.bbox()), "Bbox(3.0, 1.0, 3.0, 1.0)")
  311. if __name__ == '__main__':
  312. test()