unit_tests.py 52 KB

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