unit_tests.py 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  1. """
  2. Depricazed unittests
  3. (C) 2008-2011 by the GRASS Development Team
  4. This program is free software under the GNU General Public
  5. License (>=v2). Read the file COPYING that comes with GRASS
  6. for details.
  7. :authors: Soeren Gebbert
  8. """
  9. import copy
  10. from datetime import datetime
  11. import grass.script.core as core
  12. from temporal_granularity import *
  13. from datetime_math import *
  14. from space_time_datasets import *
  15. import grass.lib.vector as vector
  16. import grass.lib.rtree as rtree
  17. import grass.lib.gis as gis
  18. from ctypes import *
  19. # Uncomment this to detect the error
  20. core.set_raise_on_error(True)
  21. ###############################################################################
  22. def test_increment_datetime_by_string():
  23. # First test
  24. print "# Test 1"
  25. dt = datetime(2001, 9, 1, 0, 0, 0)
  26. string = "60 seconds, 4 minutes, 12 hours, 10 days, 1 weeks, 5 months, 1 years"
  27. dt1 = datetime(2003, 2, 18, 12, 5, 0)
  28. dt2 = increment_datetime_by_string(dt, string)
  29. print dt
  30. print dt2
  31. delta = dt1 - dt2
  32. if delta.days != 0 or delta.seconds != 0:
  33. core.fatal("increment computation is wrong %s" % (delta))
  34. # Second test
  35. print "# Test 2"
  36. dt = datetime(2001, 11, 1, 0, 0, 0)
  37. string = "1 months"
  38. dt1 = datetime(2001, 12, 1)
  39. dt2 = increment_datetime_by_string(dt, string)
  40. print dt
  41. print dt2
  42. delta = dt1 - dt2
  43. if delta.days != 0 or delta.seconds != 0:
  44. core.fatal("increment computation is wrong %s" % (delta))
  45. # Third test
  46. print "# Test 3"
  47. dt = datetime(2001, 11, 1, 0, 0, 0)
  48. string = "13 months"
  49. dt1 = datetime(2002, 12, 1)
  50. dt2 = increment_datetime_by_string(dt, string)
  51. print dt
  52. print dt2
  53. delta = dt1 - dt2
  54. if delta.days != 0 or delta.seconds != 0:
  55. core.fatal("increment computation is wrong %s" % (delta))
  56. # 4. test
  57. print "# Test 4"
  58. dt = datetime(2001, 1, 1, 0, 0, 0)
  59. string = "72 months"
  60. dt1 = datetime(2007, 1, 1)
  61. dt2 = increment_datetime_by_string(dt, string)
  62. print dt
  63. print dt2
  64. delta = dt1 - dt2
  65. if delta.days != 0 or delta.seconds != 0:
  66. core.fatal("increment computation is wrong %s" % (delta))
  67. ###############################################################################
  68. def test_adjust_datetime_to_granularity():
  69. # First test
  70. print "Test 1"
  71. dt = datetime(2001, 8, 8, 12, 30, 30)
  72. result = adjust_datetime_to_granularity(dt, "5 seconds")
  73. correct = datetime(2001, 8, 8, 12, 30, 30)
  74. delta = correct - result
  75. if delta.days != 0 or delta.seconds != 0:
  76. core.fatal("Granularity adjustment computation is wrong %s" % (delta))
  77. # Second test
  78. print "Test 2"
  79. result = adjust_datetime_to_granularity(dt, "20 minutes")
  80. correct = datetime(2001, 8, 8, 12, 30, 00)
  81. delta = correct - result
  82. if delta.days != 0 or delta.seconds != 0:
  83. core.fatal("Granularity adjustment computation is wrong %s" % (delta))
  84. # Third test
  85. print "Test 2"
  86. result = adjust_datetime_to_granularity(dt, "20 minutes")
  87. correct = datetime(2001, 8, 8, 12, 30, 00)
  88. delta = correct - result
  89. if delta.days != 0 or delta.seconds != 0:
  90. core.fatal("Granularity adjustment computation is wrong %s" % (delta))
  91. # 4. test
  92. print "Test 4"
  93. result = adjust_datetime_to_granularity(dt, "3 hours")
  94. correct = datetime(2001, 8, 8, 12, 00, 00)
  95. delta = correct - result
  96. if delta.days != 0 or delta.seconds != 0:
  97. core.fatal("Granularity adjustment computation is wrong %s" % (delta))
  98. # 5. test
  99. print "Test 5"
  100. result = adjust_datetime_to_granularity(dt, "5 days")
  101. correct = datetime(2001, 8, 8, 00, 00, 00)
  102. delta = correct - result
  103. if delta.days != 0 or delta.seconds != 0:
  104. core.fatal("Granularity adjustment computation is wrong %s" % (delta))
  105. # 6. test
  106. print "Test 6"
  107. result = adjust_datetime_to_granularity(dt, "2 weeks")
  108. correct = datetime(2001, 8, 6, 00, 00, 00)
  109. delta = correct - result
  110. if delta.days != 0 or delta.seconds != 0:
  111. core.fatal("Granularity adjustment computation is wrong %s" % (delta))
  112. # 7. test
  113. print "Test 7"
  114. result = adjust_datetime_to_granularity(dt, "6 months")
  115. correct = datetime(2001, 8, 1, 00, 00, 00)
  116. delta = correct - result
  117. if delta.days != 0 or delta.seconds != 0:
  118. core.fatal("Granularity adjustment computation is wrong %s" % (delta))
  119. # 8. test
  120. print "Test 8"
  121. result = adjust_datetime_to_granularity(dt, "2 years")
  122. correct = datetime(2001, 1, 1, 00, 00, 00)
  123. delta = correct - result
  124. if delta.days != 0 or delta.seconds != 0:
  125. core.fatal("Granularity adjustment computation is wrong %s" % (delta))
  126. # 9. test
  127. print "Test 9"
  128. result = adjust_datetime_to_granularity(
  129. dt, "2 years, 3 months, 5 days, 3 hours, 3 minutes, 2 seconds")
  130. correct = datetime(2001, 8, 8, 12, 30, 30)
  131. delta = correct - result
  132. if delta.days != 0 or delta.seconds != 0:
  133. core.fatal("Granularity adjustment computation is wrong %s" % (delta))
  134. # 10. test
  135. print "Test 10"
  136. result = adjust_datetime_to_granularity(dt, "3 months, 5 days, 3 minutes")
  137. correct = datetime(2001, 8, 8, 12, 30, 00)
  138. delta = correct - result
  139. if delta.days != 0 or delta.seconds != 0:
  140. core.fatal("Granularity adjustment computation is wrong %s" % (delta))
  141. # 11. test
  142. print "Test 11"
  143. result = adjust_datetime_to_granularity(dt, "3 weeks, 5 days")
  144. correct = datetime(2001, 8, 8, 00, 00, 00)
  145. delta = correct - result
  146. if delta.days != 0 or delta.seconds != 0:
  147. core.fatal("Granularity adjustment computation is wrong %s" % (delta))
  148. ###############################################################################
  149. def test_compute_datetime_delta():
  150. print "Test 1"
  151. start = datetime(2001, 1, 1, 00, 00, 00)
  152. end = datetime(2001, 1, 1, 00, 00, 00)
  153. comp = compute_datetime_delta(start, end)
  154. result = comp["second"]
  155. correct = 0
  156. delta = correct - result
  157. if delta != 0:
  158. core.fatal("Compute datetime delta is wrong %s" % (delta))
  159. print "Test 2"
  160. start = datetime(2001, 1, 1, 00, 00, 14)
  161. end = datetime(2001, 1, 1, 00, 00, 44)
  162. comp = compute_datetime_delta(start, end)
  163. result = comp["second"]
  164. correct = 30
  165. delta = correct - result
  166. if delta != 0:
  167. core.fatal("Compute datetime delta is wrong %s" % (delta))
  168. print "Test 3"
  169. start = datetime(2001, 1, 1, 00, 00, 44)
  170. end = datetime(2001, 1, 1, 00, 01, 14)
  171. comp = compute_datetime_delta(start, end)
  172. result = comp["second"]
  173. correct = 30
  174. delta = correct - result
  175. if delta != 0:
  176. core.fatal("Compute datetime delta is wrong %s" % (delta))
  177. print "Test 4"
  178. start = datetime(2001, 1, 1, 00, 00, 30)
  179. end = datetime(2001, 1, 1, 00, 05, 30)
  180. comp = compute_datetime_delta(start, end)
  181. result = comp["second"]
  182. correct = 300
  183. delta = correct - result
  184. if delta != 0:
  185. core.fatal("Compute datetime delta is wrong %s" % (delta))
  186. print "Test 5"
  187. start = datetime(2001, 1, 1, 00, 00, 00)
  188. end = datetime(2001, 1, 1, 00, 01, 00)
  189. comp = compute_datetime_delta(start, end)
  190. result = comp["minute"]
  191. correct = 1
  192. delta = correct - result
  193. if delta != 0:
  194. core.fatal("Compute datetime delta is wrong %s" % (delta))
  195. print "Test 6"
  196. start = datetime(2011, 10, 31, 00, 45, 00)
  197. end = datetime(2011, 10, 31, 01, 45, 00)
  198. comp = compute_datetime_delta(start, end)
  199. result = comp["minute"]
  200. correct = 60
  201. delta = correct - result
  202. if delta != 0:
  203. core.fatal("Compute datetime delta is wrong %s" % (delta))
  204. print "Test 7"
  205. start = datetime(2011, 10, 31, 00, 45, 00)
  206. end = datetime(2011, 10, 31, 01, 15, 00)
  207. comp = compute_datetime_delta(start, end)
  208. result = comp["minute"]
  209. correct = 30
  210. delta = correct - result
  211. if delta != 0:
  212. core.fatal("Compute datetime delta is wrong %s" % (delta))
  213. print "Test 8"
  214. start = datetime(2011, 10, 31, 00, 45, 00)
  215. end = datetime(2011, 10, 31, 12, 15, 00)
  216. comp = compute_datetime_delta(start, end)
  217. result = comp["minute"]
  218. correct = 690
  219. delta = correct - result
  220. if delta != 0:
  221. core.fatal("Compute datetime delta is wrong %s" % (delta))
  222. print "Test 9"
  223. start = datetime(2011, 10, 31, 00, 00, 00)
  224. end = datetime(2011, 10, 31, 01, 00, 00)
  225. comp = compute_datetime_delta(start, end)
  226. result = comp["hour"]
  227. correct = 1
  228. delta = correct - result
  229. if delta != 0:
  230. core.fatal("Compute datetime delta is wrong %s" % (delta))
  231. print "Test 10"
  232. start = datetime(2011, 10, 31, 00, 00, 00)
  233. end = datetime(2011, 11, 01, 01, 00, 00)
  234. comp = compute_datetime_delta(start, end)
  235. result = comp["hour"]
  236. correct = 25
  237. delta = correct - result
  238. if delta != 0:
  239. core.fatal("Compute datetime delta is wrong %s" % (delta))
  240. print "Test 11"
  241. start = datetime(2011, 10, 31, 12, 00, 00)
  242. end = datetime(2011, 11, 01, 06, 00, 00)
  243. comp = compute_datetime_delta(start, end)
  244. result = comp["hour"]
  245. correct = 18
  246. delta = correct - result
  247. if delta != 0:
  248. core.fatal("Compute datetime delta is wrong %s" % (delta))
  249. print "Test 12"
  250. start = datetime(2011, 11, 01, 00, 00, 00)
  251. end = datetime(2011, 12, 01, 01, 00, 00)
  252. comp = compute_datetime_delta(start, end)
  253. result = comp["hour"]
  254. correct = 30 * 24 + 1
  255. delta = correct - result
  256. if delta != 0:
  257. core.fatal("Compute datetime delta is wrong %s" % (delta))
  258. print "Test 13"
  259. start = datetime(2011, 11, 01, 00, 00, 00)
  260. end = datetime(2011, 11, 05, 00, 00, 00)
  261. comp = compute_datetime_delta(start, end)
  262. result = comp["day"]
  263. correct = 4
  264. delta = correct - result
  265. if delta != 0:
  266. core.fatal("Compute datetime delta is wrong %s" % (delta))
  267. print "Test 14"
  268. start = datetime(2011, 10, 06, 00, 00, 00)
  269. end = datetime(2011, 11, 05, 00, 00, 00)
  270. comp = compute_datetime_delta(start, end)
  271. result = comp["day"]
  272. correct = 30
  273. delta = correct - result
  274. if delta != 0:
  275. core.fatal("Compute datetime delta is wrong %s" % (delta))
  276. print "Test 15"
  277. start = datetime(2011, 12, 02, 00, 00, 00)
  278. end = datetime(2012, 01, 01, 00, 00, 00)
  279. comp = compute_datetime_delta(start, end)
  280. result = comp["day"]
  281. correct = 30
  282. delta = correct - result
  283. if delta != 0:
  284. core.fatal("Compute datetime delta is wrong %s" % (delta))
  285. print "Test 16"
  286. start = datetime(2011, 01, 01, 00, 00, 00)
  287. end = datetime(2011, 02, 01, 00, 00, 00)
  288. comp = compute_datetime_delta(start, end)
  289. result = comp["month"]
  290. correct = 1
  291. delta = correct - result
  292. if delta != 0:
  293. core.fatal("Compute datetime delta is wrong %s" % (delta))
  294. print "Test 17"
  295. start = datetime(2011, 12, 01, 00, 00, 00)
  296. end = datetime(2012, 01, 01, 00, 00, 00)
  297. comp = compute_datetime_delta(start, end)
  298. result = comp["month"]
  299. correct = 1
  300. delta = correct - result
  301. if delta != 0:
  302. core.fatal("Compute datetime delta is wrong %s" % (delta))
  303. print "Test 18"
  304. start = datetime(2011, 12, 01, 00, 00, 00)
  305. end = datetime(2012, 06, 01, 00, 00, 00)
  306. comp = compute_datetime_delta(start, end)
  307. result = comp["month"]
  308. correct = 6
  309. delta = correct - result
  310. if delta != 0:
  311. core.fatal("Compute datetime delta is wrong %s" % (delta))
  312. print "Test 19"
  313. start = datetime(2011, 06, 01, 00, 00, 00)
  314. end = datetime(2021, 06, 01, 00, 00, 00)
  315. comp = compute_datetime_delta(start, end)
  316. result = comp["year"]
  317. correct = 10
  318. delta = correct - result
  319. if delta != 0:
  320. core.fatal("Compute datetime delta is wrong %s" % (delta))
  321. print "Test 20"
  322. start = datetime(2011, 06, 01, 00, 00, 00)
  323. end = datetime(2012, 06, 01, 12, 00, 00)
  324. comp = compute_datetime_delta(start, end)
  325. result = comp["hour"]
  326. d = end - start
  327. correct = 12 + d.days * 24
  328. delta = correct - result
  329. if delta != 0:
  330. core.fatal("Compute datetime delta is wrong %s" % (delta))
  331. print "Test 21"
  332. start = datetime(2011, 06, 01, 00, 00, 00)
  333. end = datetime(2012, 06, 01, 12, 30, 00)
  334. comp = compute_datetime_delta(start, end)
  335. result = comp["minute"]
  336. d = end - start
  337. correct = d.days * 24 * 60 + 12 * 60 + 30
  338. delta = correct - result
  339. if delta != 0:
  340. core.fatal("Compute datetime delta is wrong %s" % (delta))
  341. print "Test 22"
  342. start = datetime(2011, 06, 01, 00, 00, 00)
  343. end = datetime(2012, 06, 01, 12, 00, 05)
  344. comp = compute_datetime_delta(start, end)
  345. result = comp["second"]
  346. d = end - start
  347. correct = 5 + 60 * 60 * 12 + d.days * 24 * 60 * 60
  348. delta = correct - result
  349. if delta != 0:
  350. core.fatal("Compute datetime delta is wrong %s" % (delta))
  351. print "Test 23"
  352. start = datetime(2011, 06, 01, 00, 00, 00)
  353. end = datetime(2012, 06, 01, 00, 30, 00)
  354. comp = compute_datetime_delta(start, end)
  355. result = comp["minute"]
  356. d = end - start
  357. correct = 30 + d.days * 24 * 60
  358. delta = correct - result
  359. if delta != 0:
  360. core.fatal("Compute datetime delta is wrong %s" % (delta))
  361. print "Test 24"
  362. start = datetime(2011, 06, 01, 00, 00, 00)
  363. end = datetime(2012, 06, 01, 00, 00, 05)
  364. comp = compute_datetime_delta(start, end)
  365. result = comp["second"]
  366. d = end - start
  367. correct = 5 + d.days * 24 * 60 * 60
  368. delta = correct - result
  369. if delta != 0:
  370. core.fatal("Compute datetime delta is wrong %s" % (delta))
  371. def test_compute_absolute_time_granularity():
  372. # First we test intervals
  373. print "Test 1"
  374. maps = []
  375. a = datetime(2001, 1, 1)
  376. increment = "1 year"
  377. for i in range(10):
  378. start = increment_datetime_by_string(a, increment, i)
  379. end = increment_datetime_by_string(a, increment, i + 1)
  380. map = RasterDataset(None)
  381. map.set_absolute_time(start, end)
  382. maps.append(map)
  383. gran = compute_absolute_time_granularity(maps)
  384. if increment != gran:
  385. core.fatal("Wrong granularity reference %s != gran %s" % (
  386. increment, gran))
  387. print "Test 2"
  388. maps = []
  389. a = datetime(2001, 1, 1)
  390. increment = "3 years"
  391. for i in range(10):
  392. start = increment_datetime_by_string(a, increment, i)
  393. end = increment_datetime_by_string(a, increment, i + 1)
  394. map = RasterDataset(None)
  395. map.set_absolute_time(start, end)
  396. maps.append(map)
  397. gran = compute_absolute_time_granularity(maps)
  398. if increment != gran:
  399. core.fatal("Wrong granularity reference %s != gran %s" % (
  400. increment, gran))
  401. print "Test 3"
  402. maps = []
  403. a = datetime(2001, 5, 1)
  404. increment = "1 month"
  405. for i in range(20):
  406. start = increment_datetime_by_string(a, increment, i)
  407. end = increment_datetime_by_string(a, increment, i + 1)
  408. map = RasterDataset(None)
  409. map.set_absolute_time(start, end)
  410. maps.append(map)
  411. gran = compute_absolute_time_granularity(maps)
  412. if increment != gran:
  413. core.fatal("Wrong granularity reference %s != gran %s" % (
  414. increment, gran))
  415. print "Test 4"
  416. maps = []
  417. a = datetime(2001, 1, 1)
  418. increment = "3 months"
  419. for i in range(20):
  420. start = increment_datetime_by_string(a, increment, i)
  421. end = increment_datetime_by_string(a, increment, i + 1)
  422. map = RasterDataset(None)
  423. map.set_absolute_time(start, end)
  424. maps.append(map)
  425. gran = compute_absolute_time_granularity(maps)
  426. if increment != gran:
  427. core.fatal("Wrong granularity reference %s != gran %s" % (
  428. increment, gran))
  429. print "Test 3"
  430. maps = []
  431. a = datetime(2001, 1, 1)
  432. increment = "1 day"
  433. for i in range(6):
  434. start = increment_datetime_by_string(a, increment, i)
  435. end = increment_datetime_by_string(a, increment, i + 1)
  436. map = RasterDataset(None)
  437. map.set_absolute_time(start, end)
  438. maps.append(map)
  439. gran = compute_absolute_time_granularity(maps)
  440. if increment != gran:
  441. core.fatal("Wrong granularity reference %s != gran %s" % (
  442. increment, gran))
  443. print "Test 4"
  444. maps = []
  445. a = datetime(2001, 1, 14)
  446. increment = "14 days"
  447. for i in range(6):
  448. start = increment_datetime_by_string(a, increment, i)
  449. end = increment_datetime_by_string(a, increment, i + 1)
  450. map = RasterDataset(None)
  451. map.set_absolute_time(start, end)
  452. maps.append(map)
  453. gran = compute_absolute_time_granularity(maps)
  454. if increment != gran:
  455. core.fatal("Wrong granularity reference %s != gran %s" % (
  456. increment, gran))
  457. print "Test 5"
  458. maps = []
  459. a = datetime(2001, 3, 1)
  460. increment = "1 month, 4 days"
  461. for i in range(20):
  462. start = increment_datetime_by_string(a, increment, i)
  463. end = increment_datetime_by_string(a, increment, i + 1)
  464. map = RasterDataset(None)
  465. map.set_absolute_time(start, end)
  466. maps.append(map)
  467. increment = "1 day"
  468. gran = compute_absolute_time_granularity(maps)
  469. if increment != gran:
  470. core.fatal("Wrong granularity reference %s != gran %s" % (
  471. increment, gran))
  472. print "Test 6"
  473. maps = []
  474. a = datetime(2001, 2, 11)
  475. increment = "1 days, 1 hours"
  476. for i in range(20):
  477. start = increment_datetime_by_string(a, increment, i)
  478. end = increment_datetime_by_string(a, increment, i + 1)
  479. map = RasterDataset(None)
  480. map.set_absolute_time(start, end)
  481. maps.append(map)
  482. increment = "25 hours"
  483. gran = compute_absolute_time_granularity(maps)
  484. if increment != gran:
  485. core.fatal("Wrong granularity reference %s != gran %s" % (
  486. increment, gran))
  487. print "Test 7"
  488. maps = []
  489. a = datetime(2001, 6, 12)
  490. increment = "6 hours"
  491. for i in range(20):
  492. start = increment_datetime_by_string(a, increment, i)
  493. end = increment_datetime_by_string(a, increment, i + 1)
  494. map = RasterDataset(None)
  495. map.set_absolute_time(start, end)
  496. maps.append(map)
  497. gran = compute_absolute_time_granularity(maps)
  498. if increment != gran:
  499. core.fatal("Wrong granularity reference %s != gran %s" % (
  500. increment, gran))
  501. print "Test 8"
  502. maps = []
  503. a = datetime(2001, 1, 1)
  504. increment = "20 minutes"
  505. for i in range(20):
  506. start = increment_datetime_by_string(a, increment, i)
  507. end = increment_datetime_by_string(a, increment, i + 1)
  508. map = RasterDataset(None)
  509. map.set_absolute_time(start, end)
  510. maps.append(map)
  511. gran = compute_absolute_time_granularity(maps)
  512. if increment != gran:
  513. core.fatal("Wrong granularity reference %s != gran %s" % (
  514. increment, gran))
  515. print "Test 9"
  516. maps = []
  517. a = datetime(2001, 1, 1)
  518. increment = "5 hours, 25 minutes"
  519. for i in range(20):
  520. start = increment_datetime_by_string(a, increment, i)
  521. end = increment_datetime_by_string(a, increment, i + 1)
  522. map = RasterDataset(None)
  523. map.set_absolute_time(start, end)
  524. maps.append(map)
  525. increment = "325 minutes"
  526. gran = compute_absolute_time_granularity(maps)
  527. if increment != gran:
  528. core.fatal("Wrong granularity reference %s != gran %s" % (
  529. increment, gran))
  530. print "Test 10"
  531. maps = []
  532. a = datetime(2001, 1, 1)
  533. increment = "5 minutes, 30 seconds"
  534. for i in range(20):
  535. start = increment_datetime_by_string(a, increment, i)
  536. end = increment_datetime_by_string(a, increment, i + 1)
  537. map = RasterDataset(None)
  538. map.set_absolute_time(start, end)
  539. maps.append(map)
  540. increment = "330 seconds"
  541. gran = compute_absolute_time_granularity(maps)
  542. if increment != gran:
  543. core.fatal("Wrong granularity reference %s != gran %s" % (
  544. increment, gran))
  545. print "Test 11"
  546. maps = []
  547. a = datetime(2001, 12, 31)
  548. increment = "60 minutes, 30 seconds"
  549. for i in range(24):
  550. start = increment_datetime_by_string(a, increment, i)
  551. end = increment_datetime_by_string(a, increment, i + 1)
  552. map = RasterDataset(None)
  553. map.set_absolute_time(start, end)
  554. maps.append(map)
  555. increment = "3630 seconds"
  556. gran = compute_absolute_time_granularity(maps)
  557. if increment != gran:
  558. core.fatal("Wrong granularity reference %s != gran %s" % (
  559. increment, gran))
  560. print "Test 12"
  561. maps = []
  562. a = datetime(2001, 12, 31, 12, 30, 30)
  563. increment = "3600 seconds"
  564. for i in range(24):
  565. start = increment_datetime_by_string(a, increment, i)
  566. end = increment_datetime_by_string(a, increment, i + 1)
  567. print start
  568. print end
  569. map = RasterDataset(None)
  570. map.set_absolute_time(start, end)
  571. maps.append(map)
  572. gran = compute_absolute_time_granularity(maps)
  573. if increment != gran:
  574. core.fatal("Wrong granularity reference %s != gran %s" % (
  575. increment, gran))
  576. # Test absolute time points
  577. print "Test 13"
  578. maps = []
  579. a = datetime(2001, 12, 31, 12, 30, 30)
  580. increment = "3600 seconds"
  581. for i in range(24):
  582. start = increment_datetime_by_string(a, increment, i)
  583. end = None
  584. map = RasterDataset(None)
  585. map.set_absolute_time(start, end)
  586. maps.append(map)
  587. gran = compute_absolute_time_granularity(maps)
  588. if increment != gran:
  589. core.fatal("Wrong granularity reference %s != gran %s" % (
  590. increment, gran))
  591. print "Test 14"
  592. maps = []
  593. a = datetime(2001, 12, 31, 00, 00, 00)
  594. increment = "20 days"
  595. for i in range(24):
  596. start = increment_datetime_by_string(a, increment, i)
  597. end = None
  598. map = RasterDataset(None)
  599. map.set_absolute_time(start, end)
  600. maps.append(map)
  601. gran = compute_absolute_time_granularity(maps)
  602. if increment != gran:
  603. core.fatal("Wrong granularity reference %s != gran %s" % (
  604. increment, gran))
  605. print "Test 15"
  606. maps = []
  607. a = datetime(2001, 12, 01, 00, 00, 00)
  608. increment = "5 months"
  609. for i in range(24):
  610. start = increment_datetime_by_string(a, increment, i)
  611. end = None
  612. map = RasterDataset(None)
  613. map.set_absolute_time(start, end)
  614. maps.append(map)
  615. gran = compute_absolute_time_granularity(maps)
  616. if increment != gran:
  617. core.fatal("Wrong granularity reference %s != gran %s" % (
  618. increment, gran))
  619. # Test absolute time interval and points
  620. print "Test 16"
  621. maps = []
  622. a = datetime(2001, 12, 31, 12, 30, 30)
  623. increment = "3600 seconds"
  624. for i in range(24):
  625. start = increment_datetime_by_string(a, increment, i)
  626. end = increment_datetime_by_string(a, increment, i + 1)
  627. map = RasterDataset(None)
  628. map.set_absolute_time(start, end)
  629. maps.append(map)
  630. a = datetime(2002, 02, 01, 12, 30, 30)
  631. for i in range(24):
  632. start = increment_datetime_by_string(a, increment, i)
  633. end = None
  634. map = RasterDataset(None)
  635. map.set_absolute_time(start, end)
  636. maps.append(map)
  637. gran = compute_absolute_time_granularity(maps)
  638. if increment != gran:
  639. core.fatal("Wrong granularity reference %s != gran %s" % (
  640. increment, gran))
  641. print "Test 17"
  642. maps = []
  643. a = datetime(2001, 1, 1)
  644. increment = "2 days"
  645. for i in range(8):
  646. start = increment_datetime_by_string(a, increment, i)
  647. end = increment_datetime_by_string(a, increment, i + 1)
  648. map = RasterDataset(None)
  649. map.set_absolute_time(start, end)
  650. maps.append(map)
  651. a = datetime(2001, 02, 02)
  652. for i in range(8):
  653. start = increment_datetime_by_string(a, increment, i)
  654. end = None
  655. map = RasterDataset(None)
  656. map.set_absolute_time(start, end)
  657. maps.append(map)
  658. gran = compute_absolute_time_granularity(maps)
  659. if increment != gran:
  660. core.fatal("Wrong granularity reference %s != gran %s" % (
  661. increment, gran))
  662. ###############################################################################
  663. def test_spatial_extent_intersection():
  664. # Generate the extents
  665. A = SpatialExtent(
  666. north=80, south=20, east=60, west=10, bottom=-50, top=50)
  667. A.print_info()
  668. B = SpatialExtent(
  669. north=80, south=20, east=60, west=10, bottom=-50, top=50)
  670. B.print_info()
  671. C = A.intersect(B)
  672. C.print_info()
  673. if C.get_north() != B.get_north() or C.get_south() != B.get_south() or \
  674. C.get_west() != B.get_west() or C.get_east() != B.get_east() or \
  675. C.get_bottom() != B.get_bottom() or C.get_top() != B.get_top():
  676. core.fatal("Wrong intersection computation")
  677. B = SpatialExtent(
  678. north=40, south=30, east=60, west=10, bottom=-50, top=50)
  679. B.print_info()
  680. C = A.intersect(B)
  681. C.print_info()
  682. if C.get_north() != B.get_north() or C.get_south() != B.get_south() or \
  683. C.get_west() != B.get_west() or C.get_east() != B.get_east() or \
  684. C.get_bottom() != B.get_bottom() or C.get_top() != B.get_top():
  685. core.fatal("Wrong intersection computation")
  686. B = SpatialExtent(
  687. north=40, south=30, east=60, west=30, bottom=-50, top=50)
  688. B.print_info()
  689. C = A.intersect(B)
  690. C.print_info()
  691. if C.get_north() != B.get_north() or C.get_south() != B.get_south() or \
  692. C.get_west() != B.get_west() or C.get_east() != B.get_east() or \
  693. C.get_bottom() != B.get_bottom() or C.get_top() != B.get_top():
  694. core.fatal("Wrong intersection computation")
  695. B = SpatialExtent(
  696. north=40, south=30, east=60, west=30, bottom=-30, top=50)
  697. B.print_info()
  698. C = A.intersect(B)
  699. C.print_info()
  700. if C.get_north() != B.get_north() or C.get_south() != B.get_south() or \
  701. C.get_west() != B.get_west() or C.get_east() != B.get_east() or \
  702. C.get_bottom() != B.get_bottom() or C.get_top() != B.get_top():
  703. core.fatal("Wrong intersection computation")
  704. B = SpatialExtent(
  705. north=40, south=30, east=60, west=30, bottom=-30, top=30)
  706. B.print_info()
  707. C = A.intersect(B)
  708. C.print_info()
  709. if C.get_north() != B.get_north() or C.get_south() != B.get_south() or \
  710. C.get_west() != B.get_west() or C.get_east() != B.get_east() or \
  711. C.get_bottom() != B.get_bottom() or C.get_top() != B.get_top():
  712. core.fatal("Wrong intersection computation")
  713. ###############################################################################
  714. def test_spatial_relations():
  715. # Generate the extents
  716. A = SpatialExtent(
  717. north=80, south=20, east=60, west=10, bottom=-50, top=50)
  718. A.print_info()
  719. B = SpatialExtent(
  720. north=80, south=20, east=60, west=10, bottom=-50, top=50)
  721. B.print_info()
  722. relation = A.spatial_relation(B)
  723. print relation
  724. if relation != "equivalent":
  725. core.fatal("Wrong spatial relation: %s" % (relation))
  726. B = SpatialExtent(
  727. north=70, south=20, east=60, west=10, bottom=-50, top=50)
  728. B.print_info()
  729. relation = A.spatial_relation_2d(B)
  730. print relation
  731. if relation != "cover":
  732. core.fatal("Wrong spatial relation: %s" % (relation))
  733. relation = A.spatial_relation(B)
  734. print relation
  735. if relation != "cover":
  736. core.fatal("Wrong spatial relation: %s" % (relation))
  737. B = SpatialExtent(
  738. north=70, south=30, east=60, west=10, bottom=-50, top=50)
  739. B.print_info()
  740. relation = A.spatial_relation_2d(B)
  741. print relation
  742. if relation != "cover":
  743. core.fatal("Wrong spatial relation: %s" % (relation))
  744. relation = A.spatial_relation(B)
  745. print relation
  746. if relation != "cover":
  747. core.fatal("Wrong spatial relation: %s" % (relation))
  748. relation = B.spatial_relation_2d(A)
  749. print relation
  750. if relation != "covered":
  751. core.fatal("Wrong spatial relation: %s" % (relation))
  752. relation = B.spatial_relation(A)
  753. print relation
  754. if relation != "covered":
  755. core.fatal("Wrong spatial relation: %s" % (relation))
  756. B = SpatialExtent(
  757. north=70, south=30, east=50, west=10, bottom=-50, top=50)
  758. B.print_info()
  759. relation = A.spatial_relation_2d(B)
  760. print relation
  761. if relation != "cover":
  762. core.fatal("Wrong spatial relation: %s" % (relation))
  763. relation = B.spatial_relation_2d(A)
  764. print relation
  765. if relation != "covered":
  766. core.fatal("Wrong spatial relation: %s" % (relation))
  767. relation = A.spatial_relation(B)
  768. print relation
  769. if relation != "cover":
  770. core.fatal("Wrong spatial relation: %s" % (relation))
  771. B = SpatialExtent(
  772. north=70, south=30, east=50, west=20, bottom=-50, top=50)
  773. relation = B.spatial_relation(A)
  774. print relation
  775. if relation != "covered":
  776. core.fatal("Wrong spatial relation: %s" % (relation))
  777. B = SpatialExtent(
  778. north=70, south=30, east=50, west=20, bottom=-50, top=50)
  779. B.print_info()
  780. relation = A.spatial_relation_2d(B)
  781. print relation
  782. if relation != "contain":
  783. core.fatal("Wrong spatial relation: %s" % (relation))
  784. relation = A.spatial_relation(B)
  785. print relation
  786. if relation != "cover":
  787. core.fatal("Wrong spatial relation: %s" % (relation))
  788. B = SpatialExtent(
  789. north=70, south=30, east=50, west=20, bottom=-40, top=50)
  790. B.print_info()
  791. relation = A.spatial_relation(B)
  792. print relation
  793. if relation != "cover":
  794. core.fatal("Wrong spatial relation: %s" % (relation))
  795. B = SpatialExtent(
  796. north=70, south=30, east=50, west=20, bottom=-40, top=40)
  797. B.print_info()
  798. relation = A.spatial_relation(B)
  799. print relation
  800. if relation != "contain":
  801. core.fatal("Wrong spatial relation: %s" % (relation))
  802. relation = B.spatial_relation(A)
  803. print relation
  804. if relation != "in":
  805. core.fatal("Wrong spatial relation: %s" % (relation))
  806. B = SpatialExtent(
  807. north=90, south=30, east=50, west=20, bottom=-40, top=40)
  808. B.print_info()
  809. relation = A.spatial_relation_2d(B)
  810. print relation
  811. if relation != "overlap":
  812. core.fatal("Wrong spatial relation: %s" % (relation))
  813. relation = A.spatial_relation(B)
  814. print relation
  815. if relation != "overlap":
  816. core.fatal("Wrong spatial relation: %s" % (relation))
  817. B = SpatialExtent(north=90, south=5, east=70, west=5, bottom=-40, top=40)
  818. A.print_info()
  819. B.print_info()
  820. relation = A.spatial_relation_2d(B)
  821. print relation
  822. if relation != "in":
  823. core.fatal("Wrong spatial relation: %s" % (relation))
  824. relation = A.spatial_relation(B)
  825. print relation
  826. if relation != "overlap":
  827. core.fatal("Wrong spatial relation: %s" % (relation))
  828. B = SpatialExtent(north=90, south=5, east=70, west=5, bottom=-40, top=60)
  829. A.print_info()
  830. B.print_info()
  831. relation = A.spatial_relation(B)
  832. print relation
  833. if relation != "overlap":
  834. core.fatal("Wrong spatial relation: %s" % (relation))
  835. B = SpatialExtent(north=90, south=5, east=70, west=5, bottom=-60, top=60)
  836. A.print_info()
  837. B.print_info()
  838. relation = A.spatial_relation(B)
  839. print relation
  840. if relation != "in":
  841. core.fatal("Wrong spatial relation: %s" % (relation))
  842. A = SpatialExtent(
  843. north=80, south=60, east=60, west=10, bottom=-50, top=50)
  844. A.print_info()
  845. B = SpatialExtent(
  846. north=60, south=20, east=60, west=10, bottom=-50, top=50)
  847. B.print_info()
  848. relation = A.spatial_relation_2d(B)
  849. print relation
  850. if relation != "meet":
  851. core.fatal("Wrong spatial relation: %s" % (relation))
  852. relation = A.spatial_relation(B)
  853. print relation
  854. if relation != "meet":
  855. core.fatal("Wrong spatial relation: %s" % (relation))
  856. A = SpatialExtent(
  857. north=60, south=40, east=60, west=10, bottom=-50, top=50)
  858. A.print_info()
  859. B = SpatialExtent(
  860. north=80, south=60, east=60, west=10, bottom=-50, top=50)
  861. B.print_info()
  862. relation = A.spatial_relation_2d(B)
  863. print relation
  864. if relation != "meet":
  865. core.fatal("Wrong spatial relation: %s" % (relation))
  866. relation = A.spatial_relation(B)
  867. print relation
  868. if relation != "meet":
  869. core.fatal("Wrong spatial relation: %s" % (relation))
  870. A = SpatialExtent(
  871. north=80, south=40, east=60, west=40, bottom=-50, top=50)
  872. A.print_info()
  873. B = SpatialExtent(
  874. north=80, south=40, east=40, west=20, bottom=-50, top=50)
  875. B.print_info()
  876. relation = A.spatial_relation_2d(B)
  877. print relation
  878. if relation != "meet":
  879. core.fatal("Wrong spatial relation: %s" % (relation))
  880. relation = A.spatial_relation(B)
  881. print relation
  882. if relation != "meet":
  883. core.fatal("Wrong spatial relation: %s" % (relation))
  884. A = SpatialExtent(
  885. north=80, south=40, east=40, west=20, bottom=-50, top=50)
  886. A.print_info()
  887. B = SpatialExtent(
  888. north=90, south=30, east=60, west=40, bottom=-50, top=50)
  889. B.print_info()
  890. relation = A.spatial_relation_2d(B)
  891. print relation
  892. if relation != "meet":
  893. core.fatal("Wrong spatial relation: %s" % (relation))
  894. relation = A.spatial_relation(B)
  895. print relation
  896. if relation != "meet":
  897. core.fatal("Wrong spatial relation: %s" % (relation))
  898. A = SpatialExtent(
  899. north=80, south=40, east=40, west=20, bottom=-50, top=50)
  900. A.print_info()
  901. B = SpatialExtent(
  902. north=70, south=50, east=60, west=40, bottom=-50, top=50)
  903. B.print_info()
  904. relation = A.spatial_relation_2d(B)
  905. print relation
  906. if relation != "meet":
  907. core.fatal("Wrong spatial relation: %s" % (relation))
  908. relation = A.spatial_relation(B)
  909. print relation
  910. if relation != "meet":
  911. core.fatal("Wrong spatial relation: %s" % (relation))
  912. A = SpatialExtent(
  913. north=80, south=40, east=40, west=20, bottom=-50, top=50)
  914. A.print_info()
  915. B = SpatialExtent(
  916. north=60, south=20, east=60, west=40, bottom=-50, top=50)
  917. B.print_info()
  918. relation = A.spatial_relation_2d(B)
  919. print relation
  920. if relation != "meet":
  921. core.fatal("Wrong spatial relation: %s" % (relation))
  922. relation = A.spatial_relation(B)
  923. print relation
  924. if relation != "meet":
  925. core.fatal("Wrong spatial relation: %s" % (relation))
  926. A = SpatialExtent(
  927. north=80, south=40, east=40, west=20, bottom=-50, top=50)
  928. A.print_info()
  929. B = SpatialExtent(
  930. north=40, south=20, east=60, west=40, bottom=-50, top=50)
  931. B.print_info()
  932. relation = A.spatial_relation_2d(B)
  933. print relation
  934. if relation != "disjoint":
  935. core.fatal("Wrong spatial relation: %s" % (relation))
  936. relation = A.spatial_relation(B)
  937. print relation
  938. if relation != "disjoint":
  939. core.fatal("Wrong spatial relation: %s" % (relation))
  940. A = SpatialExtent(
  941. north=80, south=40, east=40, west=20, bottom=-50, top=50)
  942. A.print_info()
  943. B = SpatialExtent(
  944. north=60, south=20, east=60, west=40, bottom=-60, top=60)
  945. B.print_info()
  946. relation = A.spatial_relation(B)
  947. print relation
  948. if relation != "meet":
  949. core.fatal("Wrong spatial relation: %s" % (relation))
  950. A = SpatialExtent(
  951. north=80, south=40, east=40, west=20, bottom=-50, top=50)
  952. A.print_info()
  953. B = SpatialExtent(
  954. north=90, south=30, east=60, west=40, bottom=-40, top=40)
  955. B.print_info()
  956. relation = A.spatial_relation(B)
  957. print relation
  958. if relation != "meet":
  959. core.fatal("Wrong spatial relation: %s" % (relation))
  960. A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
  961. A.print_info()
  962. B = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
  963. B.print_info()
  964. relation = A.spatial_relation(B)
  965. print relation
  966. if relation != "meet":
  967. core.fatal("Wrong spatial relation: %s" % (relation))
  968. A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
  969. A.print_info()
  970. B = SpatialExtent(north=80, south=50, east=60, west=30, bottom=-50, top=0)
  971. B.print_info()
  972. relation = A.spatial_relation(B)
  973. print relation
  974. if relation != "meet":
  975. core.fatal("Wrong spatial relation: %s" % (relation))
  976. A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
  977. A.print_info()
  978. B = SpatialExtent(north=70, south=50, east=50, west=30, bottom=-50, top=0)
  979. B.print_info()
  980. relation = A.spatial_relation(B)
  981. print relation
  982. if relation != "meet":
  983. core.fatal("Wrong spatial relation: %s" % (relation))
  984. A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
  985. A.print_info()
  986. B = SpatialExtent(north=90, south=30, east=70, west=10, bottom=-50, top=0)
  987. B.print_info()
  988. relation = A.spatial_relation(B)
  989. print relation
  990. if relation != "meet":
  991. core.fatal("Wrong spatial relation: %s" % (relation))
  992. A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
  993. A.print_info()
  994. B = SpatialExtent(north=70, south=30, east=50, west=10, bottom=-50, top=0)
  995. B.print_info()
  996. relation = A.spatial_relation(B)
  997. print relation
  998. if relation != "meet":
  999. core.fatal("Wrong spatial relation: %s" % (relation))
  1000. A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
  1001. A.print_info()
  1002. B = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
  1003. B.print_info()
  1004. relation = A.spatial_relation(B)
  1005. print relation
  1006. if relation != "meet":
  1007. core.fatal("Wrong spatial relation: %s" % (relation))
  1008. A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
  1009. A.print_info()
  1010. B = SpatialExtent(north=80, south=50, east=60, west=30, bottom=0, top=50)
  1011. B.print_info()
  1012. relation = A.spatial_relation(B)
  1013. print relation
  1014. if relation != "meet":
  1015. core.fatal("Wrong spatial relation: %s" % (relation))
  1016. A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
  1017. A.print_info()
  1018. B = SpatialExtent(north=70, south=50, east=50, west=30, bottom=0, top=50)
  1019. B.print_info()
  1020. relation = A.spatial_relation(B)
  1021. print relation
  1022. if relation != "meet":
  1023. core.fatal("Wrong spatial relation: %s" % (relation))
  1024. A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
  1025. A.print_info()
  1026. B = SpatialExtent(north=90, south=30, east=70, west=10, bottom=0, top=50)
  1027. B.print_info()
  1028. relation = A.spatial_relation(B)
  1029. print relation
  1030. if relation != "meet":
  1031. core.fatal("Wrong spatial relation: %s" % (relation))
  1032. A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
  1033. A.print_info()
  1034. B = SpatialExtent(north=70, south=30, east=50, west=10, bottom=0, top=50)
  1035. B.print_info()
  1036. relation = A.spatial_relation(B)
  1037. print relation
  1038. if relation != "meet":
  1039. core.fatal("Wrong spatial relation: %s" % (relation))
  1040. ###############################################################################
  1041. def test_temporal_topology_builder():
  1042. map_listA = []
  1043. _map = RasterDataset(ident="1@a")
  1044. _map.set_absolute_time(datetime(2001, 01, 01), datetime(2001, 02, 01))
  1045. map_listA.append(copy.copy(_map))
  1046. _map = RasterDataset(ident="2@a")
  1047. _map.set_absolute_time(datetime(2001, 02, 01), datetime(2001, 03, 01))
  1048. map_listA.append(copy.copy(_map))
  1049. _map = RasterDataset(ident="3@a")
  1050. _map.set_absolute_time(datetime(2001, 03, 01), datetime(2001, 04, 01))
  1051. map_listA.append(copy.copy(_map))
  1052. _map = RasterDataset(ident="4@a")
  1053. _map.set_absolute_time(datetime(2001, 04, 01), datetime(2001, 05, 01))
  1054. map_listA.append(copy.copy(_map))
  1055. _map = RasterDataset(ident="5@a")
  1056. _map.set_absolute_time(datetime(2001, 05, 01), datetime(2001, 06, 01))
  1057. map_listA.append(copy.copy(_map))
  1058. tb = SpatioTemporalTopologyBuilder()
  1059. tb.build(map_listA)
  1060. count = 0
  1061. for _map in tb:
  1062. print "[%s]" % (_map.get_name())
  1063. _map.print_topology_info()
  1064. if _map.get_id() != map_listA[count].get_id():
  1065. core.fatal("Error building temporal topology <%s> != <%s>" %
  1066. (_map.get_id(), map_listA[count].get_id()))
  1067. count += 1
  1068. map_listB = []
  1069. _map = RasterDataset(ident="1@b")
  1070. _map.set_absolute_time(datetime(2001, 01, 14), datetime(2001, 03, 14))
  1071. map_listB.append(copy.copy(_map))
  1072. _map = RasterDataset(ident="2@b")
  1073. _map.set_absolute_time(datetime(2001, 02, 01), datetime(2001, 04, 01))
  1074. map_listB.append(copy.copy(_map))
  1075. _map = RasterDataset(ident="3@b")
  1076. _map.set_absolute_time(datetime(2001, 02, 14), datetime(2001, 04, 30))
  1077. map_listB.append(copy.copy(_map))
  1078. _map = RasterDataset(ident="4@b")
  1079. _map.set_absolute_time(datetime(2001, 04, 02), datetime(2001, 04, 30))
  1080. map_listB.append(copy.copy(_map))
  1081. _map = RasterDataset(ident="5@b")
  1082. _map.set_absolute_time(datetime(2001, 05, 01), datetime(2001, 05, 14))
  1083. map_listB.append(copy.copy(_map))
  1084. tb = SpatioTemporalTopologyBuilder()
  1085. tb.build(map_listB)
  1086. # Probing some relations
  1087. if map_listB[0].get_overlapped()[0] != map_listB[1]:
  1088. core.fatal("Error building temporal topology")
  1089. if map_listB[0].get_overlapped()[1] != map_listB[2]:
  1090. core.fatal("Error building temporal topology")
  1091. if map_listB[2].get_contains()[0] != map_listB[3]:
  1092. core.fatal("Error building temporal topology")
  1093. if map_listB[3].get_during()[0] != map_listB[2]:
  1094. core.fatal("Error building temporal topology")
  1095. count = 0
  1096. for _map in tb:
  1097. print "[%s]" % (_map.get_map_id())
  1098. _map.print_topology_shell_info()
  1099. if _map.get_id() != map_listB[count].get_id():
  1100. core.fatal("Error building temporal topology <%s> != <%s>" %
  1101. (_map.get_id(), map_listB[count].get_id()))
  1102. count += 1
  1103. tb = SpatioTemporalTopologyBuilder()
  1104. tb.build(map_listA, map_listB)
  1105. count = 0
  1106. for _map in tb:
  1107. print "[%s]" % (_map.get_map_id())
  1108. _map.print_topology_shell_info()
  1109. if _map.get_id() != map_listA[count].get_id():
  1110. core.fatal("Error building temporal topology <%s> != <%s>" %
  1111. (_map.get_id(), map_listA[count].get_id()))
  1112. count += 1
  1113. count = 0
  1114. for _map in map_listB:
  1115. print "[%s]" % (_map.get_map_id())
  1116. _map.print_topology_shell_info()
  1117. # Probing some relations
  1118. if map_listA[3].get_follows()[0] != map_listB[1]:
  1119. core.fatal("Error building temporal topology")
  1120. if map_listA[3].get_precedes()[0] != map_listB[4]:
  1121. core.fatal("Error building temporal topology")
  1122. if map_listA[3].get_overlaps()[0] != map_listB[2]:
  1123. core.fatal("Error building temporal topology")
  1124. if map_listA[3].get_contains()[0] != map_listB[3]:
  1125. core.fatal("Error building temporal topology")
  1126. if map_listA[2].get_during()[0] != map_listB[1]:
  1127. core.fatal("Error building temporal topology")
  1128. if map_listA[2].get_during()[1] != map_listB[2]:
  1129. core.fatal("Error building temporal topology")
  1130. ###############################################################################
  1131. def test_map_list_sorting():
  1132. map_list = []
  1133. _map = RasterDataset(ident="1@a")
  1134. _map.set_absolute_time(datetime(2001, 02, 01), datetime(2001, 03, 01))
  1135. map_list.append(copy.copy(_map))
  1136. _map = RasterDataset(ident="2@a")
  1137. _map.set_absolute_time(datetime(2001, 01, 01), datetime(2001, 02, 01))
  1138. map_list.append(copy.copy(_map))
  1139. _map = RasterDataset(ident="3@a")
  1140. _map.set_absolute_time(datetime(2001, 03, 01), datetime(2001, 04, 01))
  1141. map_list.append(copy.copy(_map))
  1142. print "Original"
  1143. for _map in map_list:
  1144. print _map.get_temporal_extent_as_tuple()[0], _map.get_temporal_extent_as_tuple()[1]
  1145. print "Sorted by start time"
  1146. new_list = sorted(map_list, key=AbstractDatasetComparisonKeyStartTime)
  1147. for _map in new_list:
  1148. print _map.get_temporal_extent_as_tuple()[0], _map.get_temporal_extent_as_tuple()[1]
  1149. if new_list[0] != map_list[1]:
  1150. core.fatal("Sorting by start time failed")
  1151. if new_list[1] != map_list[0]:
  1152. core.fatal("Sorting by start time failed")
  1153. if new_list[2] != map_list[2]:
  1154. core.fatal("Sorting by start time failed")
  1155. print "Sorted by end time"
  1156. new_list = sorted(map_list, key=AbstractDatasetComparisonKeyEndTime)
  1157. for _map in new_list:
  1158. print _map.get_temporal_extent_as_tuple()[0], _map.get_temporal_extent_as_tuple()[1]
  1159. if new_list[0] != map_list[1]:
  1160. core.fatal("Sorting by end time failed")
  1161. if new_list[1] != map_list[0]:
  1162. core.fatal("Sorting by end time failed")
  1163. if new_list[2] != map_list[2]:
  1164. core.fatal("Sorting by end time failed")
  1165. ###############################################################################
  1166. def test_1d_rtree():
  1167. """Testing the rtree ctypes wrapper"""
  1168. tree = rtree.RTreeCreateTree(-1, 0, 1)
  1169. for i in xrange(10):
  1170. rect = rtree.RTreeAllocRect(tree)
  1171. rtree.RTreeSetRect1D(rect, tree, float(i - 2), float(i + 2))
  1172. rtree.RTreeInsertRect(rect, i + 1, tree)
  1173. rect = rtree.RTreeAllocRect(tree)
  1174. rtree.RTreeSetRect1D(rect, tree, 2.0, 7.0)
  1175. list_ = gis.ilist()
  1176. num = vector.RTreeSearch2(tree, rect, byref(list_))
  1177. rtree.RTreeFreeRect(rect)
  1178. # print rectangle ids
  1179. print "Number of overlapping rectangles", num
  1180. for i in xrange(list_.n_values):
  1181. print "id", list_.value[i]
  1182. rtree.RTreeDestroyTree(tree)
  1183. ###############################################################################
  1184. def test_2d_rtree():
  1185. """Testing the rtree ctypes wrapper"""
  1186. tree = rtree.RTreeCreateTree(-1, 0, 2)
  1187. for i in xrange(10):
  1188. rect = rtree.RTreeAllocRect(tree)
  1189. rtree.RTreeSetRect2D(rect, tree,
  1190. float(i - 2), float(i + 2),
  1191. float(i - 2), float(i + 2))
  1192. rtree.RTreeInsertRect(rect, i + 1, tree)
  1193. rect = rtree.RTreeAllocRect(tree)
  1194. rtree.RTreeSetRect2D(rect, tree, 2.0, 7.0, 2.0, 7.0)
  1195. list_ = gis.ilist()
  1196. num = vector.RTreeSearch2(tree, rect, byref(list_))
  1197. rtree.RTreeFreeRect(rect)
  1198. # print rectangle ids
  1199. print "Number of overlapping rectangles", num
  1200. for i in xrange(list_.n_values):
  1201. print "id", list_.value[i]
  1202. rtree.RTreeDestroyTree(tree)
  1203. ###############################################################################
  1204. def test_3d_rtree():
  1205. """Testing the rtree ctypes wrapper"""
  1206. tree = rtree.RTreeCreateTree(-1, 0, 3)
  1207. for i in xrange(10):
  1208. rect = rtree.RTreeAllocRect(tree)
  1209. rtree.RTreeSetRect3D(rect, tree,
  1210. float(i - 2), float(i + 2),
  1211. float(i - 2), float(i + 2),
  1212. float(i - 2), float(i + 2))
  1213. rtree.RTreeInsertRect(rect, i + 1, tree)
  1214. print i + 1
  1215. rtree.RTreePrintRect(rect, 1, tree)
  1216. rect = rtree.RTreeAllocRect(tree)
  1217. rtree.RTreeSetRect3D(rect, tree, 2.0, 7.0, 2.0, 7.0, 2.0, 7.0)
  1218. print "Select"
  1219. rtree.RTreePrintRect(rect, 1, tree)
  1220. list_ = gis.ilist()
  1221. num = vector.RTreeSearch2(tree, rect, byref(list_))
  1222. rtree.RTreeFreeRect(rect)
  1223. # print rectangle ids
  1224. print "Number of overlapping rectangles", num
  1225. for i in xrange(list_.n_values):
  1226. print "id", list_.value[i]
  1227. rtree.RTreeDestroyTree(tree)
  1228. ###############################################################################
  1229. def test_4d_rtree():
  1230. """Testing the rtree ctypes wrapper"""
  1231. tree = rtree.RTreeCreateTree(-1, 0, 4)
  1232. for i in xrange(10):
  1233. # Allocate the boundary
  1234. rect = rtree.RTreeAllocRect(tree)
  1235. rtree.RTreeSetRect4D(rect, tree,
  1236. float(i - 2), float(i + 2),
  1237. float(i - 2), float(i + 2),
  1238. float(i - 2), float(i + 2),
  1239. float(i - 2), float(i + 2))
  1240. rtree.RTreeInsertRect(rect, i + 1, tree)
  1241. rect = rtree.RTreeAllocRect(tree)
  1242. rtree.RTreeSetRect4D(rect, tree, 2.0, 7.0, 2.0,
  1243. 7.0, 2.0, 7.0, 2.0, 7.0)
  1244. list_ = gis.ilist()
  1245. num = vector.RTreeSearch2(tree, rect, byref(list_))
  1246. rtree.RTreeFreeRect(rect)
  1247. # print rectangle ids
  1248. print "Number of overlapping rectangles", num
  1249. for i in xrange(list_.n_values):
  1250. print "id", list_.value[i]
  1251. rtree.RTreeDestroyTree(tree)
  1252. ###############################################################################
  1253. if __name__ == "__main__":
  1254. init()
  1255. test_increment_datetime_by_string()
  1256. test_adjust_datetime_to_granularity()
  1257. test_spatial_extent_intersection()
  1258. test_compute_absolute_time_granularity()
  1259. test_compute_datetime_delta()
  1260. test_spatial_extent_intersection()
  1261. test_spatial_relations()
  1262. test_temporal_topology_builder()
  1263. test_map_list_sorting()
  1264. test_1d_rtree()
  1265. test_2d_rtree()
  1266. test_3d_rtree()
  1267. test_4d_rtree()