unit_tests.py 48 KB

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