test_geometry.py 14 KB

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