test_distr_tgis_db_raster3d.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. """test distributed temporal databases with str3ds
  2. (C) 2014 by the GRASS Development Team
  3. This program is free software under the GNU General Public
  4. License (>=v2). Read the file COPYING that comes with GRASS
  5. for details.
  6. :authors: Soeren Gebbert
  7. """
  8. from grass.gunittest.case import TestCase
  9. from grass.gunittest.gmodules import SimpleModule
  10. from grass.gunittest.utils import silent_rmtree
  11. import os
  12. class testRaster3dExtraction(TestCase):
  13. mapsets_to_remove = []
  14. outfile = 'rast3dlist.txt'
  15. @classmethod
  16. def setUpClass(cls):
  17. os.putenv("GRASS_OVERWRITE", "1")
  18. for i in range(1, 5):
  19. mapset_name = "test3d%i" % i
  20. cls.runModule("g.mapset", flags="c", mapset=mapset_name)
  21. cls.mapsets_to_remove.append(mapset_name)
  22. cls.runModule("g.region", s=0, n=80,
  23. w=0, e=120, b=0, t=50, res=10, res3=10)
  24. # Use always the current mapset as temporal database
  25. cls.runModule("r3.mapcalc", expression="a1 = 100")
  26. cls.runModule("r3.mapcalc", expression="a2 = 200")
  27. cls.runModule("r3.mapcalc", expression="a3 = 300")
  28. # Create the temporal database
  29. cls.runModule("t.connect", flags="d")
  30. cls.runModule("t.info", flags="s")
  31. cls.runModule("t.create", type="str3ds", temporaltype="absolute",
  32. output="A", title="A test3d", description="A test3d")
  33. cls.runModule(
  34. "t.register", flags="i", type="raster_3d", input="A",
  35. maps="a1,a2,a3",
  36. start="2001-01-01", increment="%i months" % i)
  37. # Add the new mapsets to the search path
  38. for mapset in cls.mapsets_to_remove:
  39. cls.runModule("g.mapset", mapset=mapset)
  40. cls.runModule("g.mapsets", operation="add", mapset=','.join(cls.mapsets_to_remove))
  41. @classmethod
  42. def tearDownClass(cls):
  43. gisenv = SimpleModule('g.gisenv', get='GISDBASE')
  44. cls.runModule(gisenv, expecting_stdout=True)
  45. gisdbase = gisenv.outputs.stdout.strip()
  46. gisenv = SimpleModule('g.gisenv', get='LOCATION_NAME')
  47. cls.runModule(gisenv, expecting_stdout=True)
  48. location = gisenv.outputs.stdout.strip()
  49. for mapset_name in cls.mapsets_to_remove:
  50. mapset_path = os.path.join(gisdbase, location, mapset_name)
  51. silent_rmtree(mapset_path)
  52. def test_tlist(self):
  53. self.runModule("g.mapset", mapset="test3d1")
  54. list_string = """A|test3d1|2001-01-01 00:00:00|2001-04-01 00:00:00|3
  55. A|test3d2|2001-01-01 00:00:00|2001-07-01 00:00:00|3
  56. A|test3d3|2001-01-01 00:00:00|2001-10-01 00:00:00|3
  57. A|test3d4|2001-01-01 00:00:00|2002-01-01 00:00:00|3"""
  58. t_list = SimpleModule(
  59. "t.list", quiet=True,
  60. columns=["name", "mapset,start_time", "end_time", "number_of_maps"],
  61. type="str3ds", where='name = "A"')
  62. self.assertModule(t_list)
  63. out = t_list.outputs["stdout"].value
  64. for a, b in zip(list_string.split("\n"), out.split("\n")):
  65. self.assertEqual(a.strip(), b.strip())
  66. t_list = SimpleModule(
  67. "t.list", quiet=True,
  68. columns=["name", "mapset,start_time", "end_time", "number_of_maps"],
  69. type="str3ds", where='name = "A"', output=self.outfile)
  70. self.assertModule(t_list)
  71. self.assertFileExists(self.outfile)
  72. with open(self.outfile, 'r') as f:
  73. read_data = f.read()
  74. for a, b in zip(list_string.split("\n"), read_data.split("\n")):
  75. self.assertEqual(a.strip(), b.strip())
  76. #self.assertLooksLike(reference=read_data, actual=list_string)
  77. if os.path.isfile(self.outfile):
  78. os.remove(self.outfile)
  79. def test_trast_list(self):
  80. self.runModule("g.mapset", mapset="test3d1")
  81. list_string = """a1|test3d1|2001-01-01 00:00:00|2001-02-01 00:00:00
  82. a2|test3d1|2001-02-01 00:00:00|2001-03-01 00:00:00
  83. a3|test3d1|2001-03-01 00:00:00|2001-04-01 00:00:00"""
  84. trast_list = SimpleModule(
  85. "t.rast3d.list", quiet=True, flags="s", input="A@test3d1")
  86. self.assertModule(trast_list)
  87. out = trast_list.outputs["stdout"].value
  88. for a, b in zip(list_string.split("\n"), out.split("\n")):
  89. self.assertEqual(a.strip(), b.strip())
  90. list_string = """a1|test3d2|2001-01-01 00:00:00|2001-03-01 00:00:00
  91. a2|test3d2|2001-03-01 00:00:00|2001-05-01 00:00:00
  92. a3|test3d2|2001-05-01 00:00:00|2001-07-01 00:00:00"""
  93. trast_list = SimpleModule(
  94. "t.rast3d.list", quiet=True, flags="s", input="A@test3d2")
  95. self.assertModule(trast_list)
  96. out = trast_list.outputs["stdout"].value
  97. for a, b in zip(list_string.split("\n"), out.split("\n")):
  98. self.assertEqual(a.strip(), b.strip())
  99. list_string = """a1|test3d3|2001-01-01 00:00:00|2001-04-01 00:00:00
  100. a2|test3d3|2001-04-01 00:00:00|2001-07-01 00:00:00
  101. a3|test3d3|2001-07-01 00:00:00|2001-10-01 00:00:00"""
  102. trast_list = SimpleModule(
  103. "t.rast3d.list", quiet=True, flags="s", input="A@test3d3")
  104. self.assertModule(trast_list)
  105. out = trast_list.outputs["stdout"].value
  106. for a, b in zip(list_string.split("\n"), out.split("\n")):
  107. self.assertEqual(a.strip(), b.strip())
  108. list_string = """a1|test3d4|2001-01-01 00:00:00|2001-05-01 00:00:00
  109. a2|test3d4|2001-05-01 00:00:00|2001-09-01 00:00:00
  110. a3|test3d4|2001-09-01 00:00:00|2002-01-01 00:00:00"""
  111. trast_list = SimpleModule(
  112. "t.rast3d.list", quiet=True, flags="s", input="A@test3d4")
  113. self.assertModule(trast_list)
  114. out = trast_list.outputs["stdout"].value
  115. for a, b in zip(list_string.split("\n"), out.split("\n")):
  116. self.assertEqual(a.strip(), b.strip())
  117. trast_list = SimpleModule("t.rast3d.list", quiet=True, flags="s",
  118. input="A@test3d4", output=self.outfile)
  119. self.assertModule(trast_list)
  120. self.assertFileExists(self.outfile)
  121. with open(self.outfile, 'r') as f:
  122. read_data = f.read()
  123. for a, b in zip(list_string.split("\n"), read_data.split("\n")):
  124. self.assertEqual(a.strip(), b.strip())
  125. if os.path.isfile(self.outfile):
  126. os.remove(self.outfile)
  127. def test_strds_info(self):
  128. self.runModule("g.mapset", mapset="test3d4")
  129. tinfo_string = """id=A@test3d1
  130. name=A
  131. mapset=test3d1
  132. start_time=2001-01-01 00:00:00
  133. end_time=2001-04-01 00:00:00
  134. granularity=1 month"""
  135. info = SimpleModule(
  136. "t.info", flags="g", type="str3ds", input="A@test3d1")
  137. self.assertModuleKeyValue(
  138. module=info, reference=tinfo_string, precision=2, sep="=")
  139. self.runModule("g.mapset", mapset="test3d3")
  140. tinfo_string = """id=A@test3d2
  141. name=A
  142. mapset=test3d2
  143. start_time=2001-01-01 00:00:00
  144. end_time=2001-07-01 00:00:00
  145. granularity=2 months"""
  146. info = SimpleModule(
  147. "t.info", flags="g", type="str3ds", input="A@test3d2")
  148. self.assertModuleKeyValue(
  149. module=info, reference=tinfo_string, precision=2, sep="=")
  150. self.runModule("g.mapset", mapset="test3d2")
  151. tinfo_string = """id=A@test3d3
  152. name=A
  153. mapset=test3d3
  154. start_time=2001-01-01 00:00:00
  155. end_time=2001-10-01 00:00:00
  156. granularity=3 months"""
  157. info = SimpleModule(
  158. "t.info", flags="g", type="str3ds", input="A@test3d3")
  159. self.assertModuleKeyValue(
  160. module=info, reference=tinfo_string, precision=2, sep="=")
  161. self.runModule("g.mapset", mapset="test3d1")
  162. tinfo_string = """id=A@test3d4
  163. name=A
  164. mapset=test3d4
  165. start_time=2001-01-01 00:00:00
  166. end_time=2002-01-01 00:00:00
  167. granularity=4 months"""
  168. info = SimpleModule(
  169. "t.info", flags="g", type="str3ds", input="A@test3d4")
  170. self.assertModuleKeyValue(
  171. module=info, reference=tinfo_string, precision=2, sep="=")
  172. def test_raster_info(self):
  173. self.runModule("g.mapset", mapset="test3d3")
  174. tinfo_string = """id=a1@test3d1
  175. name=a1
  176. mapset=test3d1
  177. temporal_type=absolute
  178. start_time=2001-01-01 00:00:00
  179. end_time=2001-02-01 00:00:00 """
  180. info = SimpleModule(
  181. "t.info", flags="g", type="raster_3d", input="a1@test3d1")
  182. self.assertModuleKeyValue(
  183. module=info, reference=tinfo_string, precision=2, sep="=")
  184. tinfo_string = """id=a1@test3d2
  185. name=a1
  186. mapset=test3d2
  187. temporal_type=absolute
  188. start_time=2001-01-01 00:00:00
  189. end_time=2001-03-01 00:00:00 """
  190. info = SimpleModule(
  191. "t.info", flags="g", type="raster_3d", input="a1@test3d2")
  192. self.assertModuleKeyValue(
  193. module=info, reference=tinfo_string, precision=2, sep="=")
  194. tinfo_string = """id=a1@test3d3
  195. name=a1
  196. mapset=test3d3
  197. temporal_type=absolute
  198. start_time=2001-01-01 00:00:00
  199. end_time=2001-04-01 00:00:00 """
  200. info = SimpleModule(
  201. "t.info", flags="g", type="raster_3d", input="a1@test3d3")
  202. self.assertModuleKeyValue(
  203. module=info, reference=tinfo_string, precision=2, sep="=")
  204. tinfo_string = """id=a1@test3d4
  205. name=a1
  206. mapset=test3d4
  207. temporal_type=absolute
  208. start_time=2001-01-01 00:00:00
  209. end_time=2001-05-01 00:00:00 """
  210. info = SimpleModule(
  211. "t.info", flags="g", type="raster_3d", input="a1@test3d4")
  212. self.assertModuleKeyValue(
  213. module=info, reference=tinfo_string, precision=2, sep="=")
  214. if __name__ == '__main__':
  215. from grass.gunittest.main import test
  216. test()