test.temporal.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. import os
  2. from grass.script.temporal import *
  3. import grass.script as grass
  4. ###############################################################################
  5. def test_dict_sql_serializer():
  6. t = dict_sql_serializer()
  7. t.test()
  8. def test_dataset_identifer():
  9. for i in range(2):
  10. base = raster_base(ident="soil" + str(i) + "@PERMANENT", name="soil" + str(i), mapset="PERMANENT", creator="soeren", temporal_type="absolute", revision=1)
  11. base.insert()
  12. base.select()
  13. base.print_self()
  14. base.clear()
  15. base.set_creator("rene")
  16. base.update()
  17. base.select()
  18. base.print_self()
  19. for i in range(2):
  20. base = raster3d_base(ident="soil" + str(i) + "@PERMANENT", name="soil" + str(i), mapset="PERMANENT", temporal_type="absolute", creator="soeren")
  21. base.insert()
  22. base.select()
  23. base.print_self()
  24. base.clear()
  25. base.set_creator("rene")
  26. base.update()
  27. base.select()
  28. base.print_self()
  29. for i in range(2):
  30. base = vector_base(ident="soil" + str(i) + "@PERMANENT", name="soil" + str(i), mapset="PERMANENT", temporal_type="absolute", creator="soeren")
  31. base.insert()
  32. base.select()
  33. base.print_self()
  34. base.clear()
  35. base.set_creator("rene")
  36. base.update()
  37. base.select()
  38. base.print_self()
  39. for i in range(2):
  40. base = strds_base(ident="soil" + str(i) + "@PERMANENT", name="soil" + str(i), mapset="PERMANENT", creator="soeren", semantic_type="event", temporal_type="absolute", revision=1)
  41. base.insert()
  42. base.select()
  43. base.print_self()
  44. base.clear()
  45. base.set_creator("rene")
  46. base.update()
  47. base.select()
  48. base.print_self()
  49. for i in range(2):
  50. base = str3ds_base(ident="soil" + str(i) + "@PERMANENT", name="soil" + str(i), mapset="PERMANENT", temporal_type="absolute", semantic_type="event", creator="soeren")
  51. base.insert()
  52. base.select()
  53. base.print_self()
  54. base.clear()
  55. base.set_creator("rene")
  56. base.update()
  57. base.select()
  58. base.print_self()
  59. for i in range(2):
  60. base = stvds_base(ident="soil" + str(i) + "@PERMANENT", name="soil" + str(i), mapset="PERMANENT", temporal_type="absolute", semantic_type="event", creator="soeren")
  61. base.insert()
  62. base.select()
  63. base.print_self()
  64. base.clear()
  65. base.set_creator("rene")
  66. base.update()
  67. base.select()
  68. base.print_self()
  69. def test_absolute_timestamp():
  70. for i in range(2):
  71. base = raster_absolute_time(ident="soil" + str(i) + "@PERMANENT", start_time=datetime(2011,01,01), end_time=datetime(2011,07,01), timezone=1)
  72. base.insert()
  73. base.select()
  74. base.print_self()
  75. base.clear()
  76. base.set_start_time(datetime(2010,01,01))
  77. base.update()
  78. base.select()
  79. base.print_self()
  80. for i in range(2):
  81. base = raster3d_absolute_time(ident="soil" + str(i) + "@PERMANENT", start_time=datetime(2011,01,01), end_time=datetime(2011,07,01), timezone=1)
  82. base.insert()
  83. base.select()
  84. base.print_self()
  85. base.clear()
  86. base.set_start_time(datetime(2010,01,01))
  87. base.update()
  88. base.select()
  89. base.print_self()
  90. for i in range(2):
  91. base = vector_absolute_time(ident="soil" + str(i) + "@PERMANENT", start_time=datetime(2011,01,01), end_time=datetime(2011,07,01), timezone=1)
  92. base.insert()
  93. base.select()
  94. base.print_self()
  95. base.clear()
  96. base.set_start_time(datetime(2010,01,01))
  97. base.update()
  98. base.select()
  99. base.print_self()
  100. for i in range(2):
  101. base = strds_absolute_time(ident="soil" + str(i) + "@PERMANENT", start_time=datetime(2011,01,01), end_time=datetime(2011,07,01), granularity="1 day", timezone=1)
  102. base.insert()
  103. base.select()
  104. base.print_self()
  105. base.clear()
  106. base.set_start_time(datetime(2010,01,01))
  107. base.update()
  108. base.select()
  109. base.print_self()
  110. for i in range(2):
  111. base = str3ds_absolute_time(ident="soil" + str(i) + "@PERMANENT", start_time=datetime(2011,01,01), end_time=datetime(2011,07,01), granularity="1 day", timezone=1)
  112. base.insert()
  113. base.select()
  114. base.print_self()
  115. base.clear()
  116. base.set_start_time(datetime(2010,01,01))
  117. base.update()
  118. base.select()
  119. base.print_self()
  120. for i in range(2):
  121. base = stvds_absolute_time(ident="soil" + str(i) + "@PERMANENT", start_time=datetime(2011,01,01), end_time=datetime(2011,07,01), granularity="1 day", timezone=1)
  122. base.insert()
  123. base.select()
  124. base.print_self()
  125. base.clear()
  126. base.set_start_time(datetime(2010,01,01))
  127. base.update()
  128. base.select()
  129. base.print_self()
  130. def test_spatial_extent():
  131. for i in range(2):
  132. base = raster_spatial_extent(ident="soil" + str(i) + "@PERMANENT", north=100 + i, south=10+i, east=50+i, west=20+i, top=i, bottom=0)
  133. base.insert()
  134. base.select()
  135. base.print_self()
  136. base.clear()
  137. base.set_north(120+i)
  138. base.update()
  139. base.select()
  140. base.print_self()
  141. for i in range(2):
  142. base = raster3d_spatial_extent(ident="soil" + str(i) + "@PERMANENT", north=100 + i, south=10+i, east=50+i, west=20+i, top=i, bottom=0)
  143. base.insert()
  144. base.select()
  145. base.print_self()
  146. base.clear()
  147. base.set_north(120+i)
  148. base.update()
  149. base.select()
  150. base.print_self()
  151. for i in range(2):
  152. base = vector_spatial_extent(ident="soil" + str(i) + "@PERMANENT", north=100 + i, south=10+i, east=50+i, west=20+i, top=i, bottom=0)
  153. base.insert()
  154. base.select()
  155. base.print_self()
  156. base.clear()
  157. base.set_north(120+i)
  158. base.update()
  159. base.select()
  160. base.print_self()
  161. for i in range(2):
  162. base = strds_spatial_extent(ident="soil" + str(i) + "@PERMANENT", north=100 + i, south=10+i, east=50+i, west=20+i, top=i, bottom=0)
  163. base.insert()
  164. base.select()
  165. base.print_self()
  166. base.clear()
  167. base.set_north(120+i)
  168. base.update()
  169. base.select()
  170. base.print_self()
  171. for i in range(2):
  172. base = str3ds_spatial_extent(ident="soil" + str(i) + "@PERMANENT", north=100 + i, south=10+i, east=50+i, west=20+i, top=i, bottom=0)
  173. base.insert()
  174. base.select()
  175. base.print_self()
  176. base.clear()
  177. base.set_north(120+i)
  178. base.update()
  179. base.select()
  180. base.print_self()
  181. for i in range(2):
  182. base = stvds_spatial_extent(ident="soil" + str(i) + "@PERMANENT", north=100 + i, south=10+i, east=50+i, west=20+i, top=i, bottom=0)
  183. base.insert()
  184. base.select()
  185. base.print_self()
  186. base.clear()
  187. base.set_north(120+i)
  188. base.update()
  189. base.select()
  190. base.print_self()
  191. def test_relative_timestamp():
  192. for i in range(2):
  193. base = raster_relative_time(ident="soil" + str(i) + "@PERMANENT", interval=i)
  194. base.insert()
  195. base.select()
  196. base.print_self()
  197. base.clear()
  198. base.set_interval(i+1)
  199. base.update()
  200. base.select()
  201. base.print_self()
  202. for i in range(2):
  203. base = raster3d_relative_time(ident="soil" + str(i) + "@PERMANENT", interval=i)
  204. base.insert()
  205. base.select()
  206. base.print_self()
  207. base.clear()
  208. base.set_interval(i+1)
  209. base.update()
  210. base.select()
  211. base.print_self()
  212. for i in range(2):
  213. base = vector_relative_time(ident="soil" + str(i) + "@PERMANENT", interval=i)
  214. base.insert()
  215. base.select()
  216. base.print_self()
  217. base.clear()
  218. base.set_interval(i+1)
  219. base.update()
  220. base.select()
  221. base.print_self()
  222. for i in range(2):
  223. base = strds_relative_time(ident="soil" + str(i) + "@PERMANENT", interval=i, granularity=5.5)
  224. base.insert()
  225. base.select()
  226. base.print_self()
  227. base.clear()
  228. base.set_interval(i+1)
  229. base.update()
  230. base.select()
  231. base.print_self()
  232. for i in range(2):
  233. base = str3ds_relative_time(ident="soil" + str(i) + "@PERMANENT", interval=i, granularity=5.5)
  234. base.insert()
  235. base.select()
  236. base.print_self()
  237. base.clear()
  238. base.set_interval(i+1)
  239. base.update()
  240. base.select()
  241. base.print_self()
  242. for i in range(2):
  243. base = stvds_relative_time(ident="soil" + str(i) + "@PERMANENT", interval=i, granularity=5.5)
  244. base.insert()
  245. base.select()
  246. base.print_self()
  247. base.clear()
  248. base.set_interval(i+1)
  249. base.update()
  250. base.select()
  251. base.print_self()
  252. def test_map_metadata():
  253. for i in range(2):
  254. base = raster_metadata(ident="soil" + str(i) + "@PERMANENT", strds_register="PERMANENT_soil_strds-register", datatype="CELL", \
  255. cols=500, rows=400, number_of_cells=200000,nsres=1, ewres=1, min=0, max=33)
  256. base.insert()
  257. base.select()
  258. base.print_self()
  259. base.clear()
  260. base.set_datatype("FCELL")
  261. base.update()
  262. base.select()
  263. base.print_self()
  264. for i in range(2):
  265. base = raster3d_metadata(ident="soil" + str(i) + "@PERMANENT", str3ds_register="PERMANENT_soil_str3ds-register", datatype="FCELL", \
  266. cols=500, rows=400, depths=20, number_of_cells=200000,nsres=1, ewres=1, tbres=10, min=0, max=33)
  267. base.insert()
  268. base.select()
  269. base.print_self()
  270. base.clear()
  271. base.set_datatype("DCELL")
  272. base.update()
  273. base.select()
  274. base.print_self()
  275. for i in range(2):
  276. base = vector_metadata(ident="soil" + str(i) + "@PERMANENT", stvds_register="PERMANENT_soil_stvds_register")
  277. base.insert()
  278. base.select()
  279. base.print_self()
  280. base.clear()
  281. base.set_stvds_register("PERMANENT_soil_stvds_register")
  282. base.update()
  283. base.select()
  284. base.print_self()
  285. def test_base_absolute_time_extent_metadata():
  286. for i in range(10):
  287. base = vector_base(ident="water" + str(i) + "@PERMANENT", name="water" + str(i), mapset="PERMANENT", creator="soeren")
  288. base.insert()
  289. base = raster_base(ident="water" + str(i) + "@PERMANENT", name="water" + str(i), mapset="PERMANENT", creator="soeren")
  290. base.insert()
  291. base = raster3d_base(ident="water" + str(i) + "@PERMANENT", name="water" + str(i), mapset="PERMANENT", creator="soeren")
  292. base.insert()
  293. for i in range(10):
  294. base = vector_base(ident="water" + str(i) + "@PERMANENT")
  295. base.set_creator("rene")
  296. base.update()
  297. base = raster_base(ident="water" + str(i) + "@PERMANENT")
  298. base.set_creator("rene")
  299. base.update()
  300. base = raster3d_base(ident="water" + str(i) + "@PERMANENT")
  301. base.set_creator("rene")
  302. base.update()
  303. for i in range(10):
  304. base = vector_absolute_time(ident="water" + str(i) + "@PERMANENT", start_time=datetime.now(), end_time=datetime.now(), timezone=1)
  305. base.insert()
  306. base = raster_absolute_time(ident="water" + str(i) + "@PERMANENT", start_time=datetime.now(), end_time=datetime.now(), timezone=1)
  307. base.insert()
  308. base = raster3d_absolute_time(ident="water" + str(i) + "@PERMANENT", start_time=datetime.now(), end_time=datetime.now(), timezone=1)
  309. base.insert()
  310. for i in range(10):
  311. base = vector_absolute_time(ident="water" + str(i) + "@PERMANENT")
  312. base.set_start_time(datetime(2010, 6, 1))
  313. base.update()
  314. base = raster_absolute_time(ident="water" + str(i) + "@PERMANENT")
  315. base.set_start_time(datetime(2010, 6, 1))
  316. base.update()
  317. base = raster3d_absolute_time(ident="water" + str(i) + "@PERMANENT")
  318. base.set_start_time(datetime(2010, 6, 1))
  319. base.update()
  320. for i in range(10):
  321. base = vector_spatial_extent(ident="water" + str(i) + "@PERMANENT", north=100 + i, south=10+i, east=50+i, west=20+i, top=i, bottom=0)
  322. base.insert()
  323. base = raster_spatial_extent(ident="water" + str(i) + "@PERMANENT", north=100 + i, south=10+i, east=50+i, west=20+i, top=0, bottom=0)
  324. base.insert()
  325. base = raster3d_spatial_extent(ident="water" + str(i) + "@PERMANENT", north=100 + i, south=10+i, east=50+i, west=20+i, top=i, bottom=0)
  326. base.insert()
  327. for i in range(10):
  328. base = vector_spatial_extent(ident="water" + str(i) + "@PERMANENT")
  329. base.set_north(120 + i)
  330. base.set_south(20 + i)
  331. base.update()
  332. base = raster_spatial_extent(ident="water" + str(i) + "@PERMANENT")
  333. base.set_north(120 + i)
  334. base.set_south(20 + i)
  335. base.update()
  336. base = raster3d_spatial_extent(ident="water" + str(i) + "@PERMANENT")
  337. base.set_north(120 + i)
  338. base.set_south(20 + i)
  339. base.update()
  340. for i in range(10):
  341. base = vector_metadata(ident="water" + str(i) + "@PERMANENT", stvds_register="PERMANENT_water_stvds_register")
  342. base.insert()
  343. base = raster_metadata(ident="water" + str(i) + "@PERMANENT", strds_register="PERMANENT_water_strds-register", datatype="CELL", \
  344. cols=500, rows=400, number_of_cells=200000,nsres=1, ewres=1, min=0, max=33)
  345. base.insert()
  346. base = raster3d_metadata(ident="water" + str(i) + "@PERMANENT", str3ds_register="PERMANENT_water_str3ds-register", datatype="FCELL", \
  347. cols=500, rows=400, depths=20, number_of_cells=200000,nsres=1, ewres=1, tbres=10, min=0, max=33)
  348. base.insert()
  349. for i in range(10):
  350. base = vector_metadata(ident="water" + str(i) + "@PERMANENT")
  351. base.set_stvds_register("PERMANENT_water_stvds_register")
  352. base.update()
  353. base = raster_metadata(ident="water" + str(i) + "@PERMANENT")
  354. base.set_datatype("DCELL")
  355. base.update()
  356. base = raster3d_metadata(ident="water" + str(i) + "@PERMANENT")
  357. base.set_datatype("DCELL")
  358. base.update()
  359. for i in range(10):
  360. base = vector_base(ident="water" + str(i) + "@PERMANENT")
  361. base.select()
  362. base.print_self()
  363. base = raster_base(ident="water" + str(i) + "@PERMANENT")
  364. base.select()
  365. base.print_self()
  366. base = raster3d_base(ident="water" + str(i) + "@PERMANENT")
  367. base.select()
  368. base.print_self()
  369. print "Create a raster object"
  370. rds = raster_dataset("water0@PERMANENT")
  371. rds.select()
  372. rds.print_self()
  373. print rds.temporal_relation(rds)
  374. def test_absolut_time_temporal_relations():
  375. A = raster_absolute_time(ident="test1@PERMANENT", start_time=datetime(year=2000, month=1, day=1), \
  376. end_time=datetime(year=2001, month=1, day=1), timezone=1)
  377. B = raster_absolute_time(ident="test2@PERMANENT", start_time=datetime(year=2001, month=1, day=1), \
  378. end_time=datetime(year=2002, month=1, day=1), timezone=1)
  379. print "Precedes: ", A.temporal_relation(B)
  380. print "Follows: ", B.temporal_relation(A)
  381. A = raster_absolute_time(ident="test1@PERMANENT", start_time=datetime(year=2000, month=1, day=1), \
  382. end_time=datetime(year=2001, month=1, day=1), timezone=1)
  383. B = raster_absolute_time(ident="test2@PERMANENT", start_time=datetime(year=2001, month=1, day=2), \
  384. end_time=datetime(year=2002, month=1, day=1), timezone=1)
  385. print "Before: ", A.temporal_relation(B)
  386. print "After: ", B.temporal_relation(A)
  387. A = raster_absolute_time(ident="test1@PERMANENT", start_time=datetime(year=2000, month=1, day=1), \
  388. end_time=datetime(year=2001, month=1, day=1), timezone=1)
  389. B = raster_absolute_time(ident="test2@PERMANENT", start_time=datetime(year=2000, month=1, day=1), \
  390. end_time=datetime(year=2002, month=1, day=1), timezone=1)
  391. print "Starts: ", A.temporal_relation(B)
  392. print "Started: ", B.temporal_relation(A)
  393. A = raster_absolute_time(ident="test1@PERMANENT", start_time=datetime(year=2000, month=1, day=1), \
  394. end_time=datetime(year=2001, month=1, day=1), timezone=1)
  395. B = raster_absolute_time(ident="test2@PERMANENT", start_time=datetime(year=2000, month=1, day=2), \
  396. end_time=datetime(year=2001, month=1, day=1), timezone=1)
  397. print "Finished: ", A.temporal_relation(B)
  398. print "Finishes: ", B.temporal_relation(A)
  399. A = raster_absolute_time(ident="test1@PERMANENT", start_time=datetime(year=2000, month=1, day=1), \
  400. end_time=datetime(year=2001, month=1, day=1), timezone=1)
  401. B = raster_absolute_time(ident="test2@PERMANENT", start_time=datetime(year=2000, month=3, day=1), \
  402. end_time=datetime(year=2000, month=9, day=1), timezone=1)
  403. print "Contains: ", A.temporal_relation(B)
  404. print "During: ", B.temporal_relation(A)
  405. A = raster_absolute_time(ident="test1@PERMANENT", start_time=datetime(year=2000, month=1, day=1), \
  406. end_time=datetime(year=2000, month=6, day=1), timezone=1)
  407. B = raster_absolute_time(ident="test2@PERMANENT", start_time=datetime(year=2000, month=3, day=1), \
  408. end_time=datetime(year=2000, month=9, day=1), timezone=1)
  409. print "Overlap: ", A.temporal_relation(B)
  410. print "Overlapped:", B.temporal_relation(A)
  411. A = raster_absolute_time(ident="test1@PERMANENT", start_time=datetime(year=2000, month=1, day=1), \
  412. end_time=datetime(year=2000, month=1, day=1), timezone=1)
  413. B = raster_absolute_time(ident="test2@PERMANENT", start_time=datetime(year=2000, month=1, day=1), \
  414. end_time=datetime(year=2000, month=1, day=1), timezone=1)
  415. print "Equivalent:", A.temporal_relation(B)
  416. print "Equivalent:", B.temporal_relation(A)
  417. def test_raster_dataset():
  418. grass.raster.mapcalc("test = sin(x()) + cos(y())", overwrite = True)
  419. name = "test"
  420. mapset = grass.gisenv()["MAPSET"]
  421. print "Create a raster object"
  422. rds = raster_dataset(name + "@" + mapset)
  423. rds.load()
  424. print "Is in db: ", rds.is_in_db()
  425. rds.base.set_ttype("absolue")
  426. rds.absolute_time.set_start_time(datetime(year=2000, month=1, day=1))
  427. rds.absolute_time.set_end_time(datetime(year=2010, month=1, day=1))
  428. # Remove the entry if it is in the db
  429. rds.delete()
  430. rds.insert()
  431. rds.print_self()
  432. print rds.temporal_relation(rds)
  433. def test_raster3d_dataset():
  434. grass.raster3d.mapcalc3d("test = sin(x()) + cos(y()) + sin(z())", overwrite = True)
  435. name = "test"
  436. mapset = grass.gisenv()["MAPSET"]
  437. print "Create a raster3d object"
  438. r3ds = raster3d_dataset(name + "@" + mapset)
  439. r3ds.load()
  440. print "Is in db: ", r3ds.is_in_db()
  441. r3ds.print_self()
  442. r3ds.base.set_ttype("absolue")
  443. r3ds.absolute_time.set_start_time(datetime(year=2000, month=1, day=1))
  444. r3ds.absolute_time.set_end_time(datetime(year=2010, month=1, day=1))
  445. # Remove the entry if it is in the db
  446. r3ds.delete()
  447. r3ds.insert()
  448. r3ds.print_self()
  449. print r3ds.temporal_relation(r3ds)
  450. def test_vector_dataset():
  451. grass.run_command("v.random", output="test", n=20, column="height", zmin=0, \
  452. zmax=100, flags="z", overwrite = True)
  453. name = "test"
  454. mapset = grass.gisenv()["MAPSET"]
  455. print "Create a vector object"
  456. vds = vector_dataset(name + "@" + mapset)
  457. vds.load()
  458. print "Is in db: ", vds.is_in_db()
  459. vds.print_self()
  460. vds.base.set_ttype("absolue")
  461. vds.absolute_time.set_start_time(datetime(year=2000, month=1, day=1))
  462. vds.absolute_time.set_end_time(datetime(year=2010, month=1, day=1))
  463. # Remove the entry if it is in the db
  464. vds.delete()
  465. vds.insert()
  466. vds.print_self()
  467. print vds.temporal_relation(vds)
  468. #test_dict_sql_serializer()
  469. create_temporal_database()
  470. test_dataset_identifer()
  471. test_absolute_timestamp()
  472. test_relative_timestamp()
  473. test_spatial_extent()
  474. #test_map_metadata()
  475. #test_base_absolute_time_extent_metadata()
  476. #test_absolut_time_temporal_relations()
  477. #test_raster_dataset()
  478. #test_raster3d_dataset()
  479. #test_vector_dataset()