unit_tests.py 48 KB

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