unit_tests.py 48 KB

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