unit_tests.py 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  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. def test_compute_absolute_time_granularity():
  378. # First we test intervals
  379. print "Test 1"
  380. maps = []
  381. a = datetime(2001, 1, 1)
  382. increment = "1 year"
  383. for i in range(10):
  384. start = increment_datetime_by_string(a, increment, i)
  385. end = increment_datetime_by_string(a, increment, i + 1)
  386. map = RasterDataset(None)
  387. map.set_absolute_time(start, end)
  388. maps.append(map)
  389. gran = compute_absolute_time_granularity(maps)
  390. if increment != gran:
  391. core.fatal("Wrong granularity reference %s != gran %s" % (
  392. increment, gran))
  393. print "Test 2"
  394. maps = []
  395. a = datetime(2001, 1, 1)
  396. increment = "3 years"
  397. for i in range(10):
  398. start = increment_datetime_by_string(a, increment, i)
  399. end = increment_datetime_by_string(a, increment, i + 1)
  400. map = RasterDataset(None)
  401. map.set_absolute_time(start, end)
  402. maps.append(map)
  403. gran = compute_absolute_time_granularity(maps)
  404. if increment != gran:
  405. core.fatal("Wrong granularity reference %s != gran %s" % (
  406. increment, gran))
  407. print "Test 3"
  408. maps = []
  409. a = datetime(2001, 5, 1)
  410. increment = "1 month"
  411. for i in range(20):
  412. start = increment_datetime_by_string(a, increment, i)
  413. end = increment_datetime_by_string(a, increment, i + 1)
  414. map = RasterDataset(None)
  415. map.set_absolute_time(start, end)
  416. maps.append(map)
  417. gran = compute_absolute_time_granularity(maps)
  418. if increment != gran:
  419. core.fatal("Wrong granularity reference %s != gran %s" % (
  420. increment, gran))
  421. print "Test 4"
  422. maps = []
  423. a = datetime(2001, 1, 1)
  424. increment = "3 months"
  425. for i in range(20):
  426. start = increment_datetime_by_string(a, increment, i)
  427. end = increment_datetime_by_string(a, increment, i + 1)
  428. map = RasterDataset(None)
  429. map.set_absolute_time(start, end)
  430. maps.append(map)
  431. gran = compute_absolute_time_granularity(maps)
  432. if increment != gran:
  433. core.fatal("Wrong granularity reference %s != gran %s" % (
  434. increment, gran))
  435. print "Test 3"
  436. maps = []
  437. a = datetime(2001, 1, 1)
  438. increment = "1 day"
  439. for i in range(6):
  440. start = increment_datetime_by_string(a, increment, i)
  441. end = increment_datetime_by_string(a, increment, i + 1)
  442. map = RasterDataset(None)
  443. map.set_absolute_time(start, end)
  444. maps.append(map)
  445. gran = compute_absolute_time_granularity(maps)
  446. if increment != gran:
  447. core.fatal("Wrong granularity reference %s != gran %s" % (
  448. increment, gran))
  449. print "Test 4"
  450. maps = []
  451. a = datetime(2001, 1, 14)
  452. increment = "14 days"
  453. for i in range(6):
  454. start = increment_datetime_by_string(a, increment, i)
  455. end = increment_datetime_by_string(a, increment, i + 1)
  456. map = RasterDataset(None)
  457. map.set_absolute_time(start, end)
  458. maps.append(map)
  459. gran = compute_absolute_time_granularity(maps)
  460. if increment != gran:
  461. core.fatal("Wrong granularity reference %s != gran %s" % (
  462. increment, gran))
  463. print "Test 5"
  464. maps = []
  465. a = datetime(2001, 3, 1)
  466. increment = "1 month, 4 days"
  467. for i in range(20):
  468. start = increment_datetime_by_string(a, increment, i)
  469. end = increment_datetime_by_string(a, increment, i + 1)
  470. map = RasterDataset(None)
  471. map.set_absolute_time(start, end)
  472. maps.append(map)
  473. increment = "1 day"
  474. gran = compute_absolute_time_granularity(maps)
  475. if increment != gran:
  476. core.fatal("Wrong granularity reference %s != gran %s" % (
  477. increment, gran))
  478. print "Test 6"
  479. maps = []
  480. a = datetime(2001, 2, 11)
  481. increment = "1 days, 1 hours"
  482. for i in range(20):
  483. start = increment_datetime_by_string(a, increment, i)
  484. end = increment_datetime_by_string(a, increment, i + 1)
  485. map = RasterDataset(None)
  486. map.set_absolute_time(start, end)
  487. maps.append(map)
  488. increment = "25 hours"
  489. gran = compute_absolute_time_granularity(maps)
  490. if increment != gran:
  491. core.fatal("Wrong granularity reference %s != gran %s" % (
  492. increment, gran))
  493. print "Test 7"
  494. maps = []
  495. a = datetime(2001, 6, 12)
  496. increment = "6 hours"
  497. for i in range(20):
  498. start = increment_datetime_by_string(a, increment, i)
  499. end = increment_datetime_by_string(a, increment, i + 1)
  500. map = RasterDataset(None)
  501. map.set_absolute_time(start, end)
  502. maps.append(map)
  503. gran = compute_absolute_time_granularity(maps)
  504. if increment != gran:
  505. core.fatal("Wrong granularity reference %s != gran %s" % (
  506. increment, gran))
  507. print "Test 8"
  508. maps = []
  509. a = datetime(2001, 1, 1)
  510. increment = "20 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. gran = compute_absolute_time_granularity(maps)
  518. if increment != gran:
  519. core.fatal("Wrong granularity reference %s != gran %s" % (
  520. increment, gran))
  521. print "Test 9"
  522. maps = []
  523. a = datetime(2001, 1, 1)
  524. increment = "5 hours, 25 minutes"
  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 = "325 minutes"
  532. gran = compute_absolute_time_granularity(maps)
  533. if increment != gran:
  534. core.fatal("Wrong granularity reference %s != gran %s" % (
  535. increment, gran))
  536. print "Test 10"
  537. maps = []
  538. a = datetime(2001, 1, 1)
  539. increment = "5 minutes, 30 seconds"
  540. for i in range(20):
  541. start = increment_datetime_by_string(a, increment, i)
  542. end = increment_datetime_by_string(a, increment, i + 1)
  543. map = RasterDataset(None)
  544. map.set_absolute_time(start, end)
  545. maps.append(map)
  546. increment = "330 seconds"
  547. gran = compute_absolute_time_granularity(maps)
  548. if increment != gran:
  549. core.fatal("Wrong granularity reference %s != gran %s" % (
  550. increment, gran))
  551. print "Test 11"
  552. maps = []
  553. a = datetime(2001, 12, 31)
  554. increment = "60 minutes, 30 seconds"
  555. for i in range(24):
  556. start = increment_datetime_by_string(a, increment, i)
  557. end = increment_datetime_by_string(a, increment, i + 1)
  558. map = RasterDataset(None)
  559. map.set_absolute_time(start, end)
  560. maps.append(map)
  561. increment = "3630 seconds"
  562. gran = compute_absolute_time_granularity(maps)
  563. if increment != gran:
  564. core.fatal("Wrong granularity reference %s != gran %s" % (
  565. increment, gran))
  566. print "Test 12"
  567. maps = []
  568. a = datetime(2001, 12, 31, 12, 30, 30)
  569. increment = "3600 seconds"
  570. for i in range(24):
  571. start = increment_datetime_by_string(a, increment, i)
  572. end = increment_datetime_by_string(a, increment, i + 1)
  573. print start
  574. print end
  575. map = RasterDataset(None)
  576. map.set_absolute_time(start, end)
  577. maps.append(map)
  578. gran = compute_absolute_time_granularity(maps)
  579. if increment != gran:
  580. core.fatal("Wrong granularity reference %s != gran %s" % (
  581. increment, gran))
  582. # Test absolute time points
  583. print "Test 13"
  584. maps = []
  585. a = datetime(2001, 12, 31, 12, 30, 30)
  586. increment = "3600 seconds"
  587. for i in range(24):
  588. start = increment_datetime_by_string(a, increment, i)
  589. end = None
  590. map = RasterDataset(None)
  591. map.set_absolute_time(start, end)
  592. maps.append(map)
  593. gran = compute_absolute_time_granularity(maps)
  594. if increment != gran:
  595. core.fatal("Wrong granularity reference %s != gran %s" % (
  596. increment, gran))
  597. print "Test 14"
  598. maps = []
  599. a = datetime(2001, 12, 31, 00, 00, 00)
  600. increment = "20 days"
  601. for i in range(24):
  602. start = increment_datetime_by_string(a, increment, i)
  603. end = None
  604. map = RasterDataset(None)
  605. map.set_absolute_time(start, end)
  606. maps.append(map)
  607. gran = compute_absolute_time_granularity(maps)
  608. if increment != gran:
  609. core.fatal("Wrong granularity reference %s != gran %s" % (
  610. increment, gran))
  611. print "Test 15"
  612. maps = []
  613. a = datetime(2001, 12, 01, 00, 00, 00)
  614. increment = "5 months"
  615. for i in range(24):
  616. start = increment_datetime_by_string(a, increment, i)
  617. end = None
  618. map = RasterDataset(None)
  619. map.set_absolute_time(start, end)
  620. maps.append(map)
  621. gran = compute_absolute_time_granularity(maps)
  622. if increment != gran:
  623. core.fatal("Wrong granularity reference %s != gran %s" % (
  624. increment, gran))
  625. # Test absolute time interval and points
  626. print "Test 16"
  627. maps = []
  628. a = datetime(2001, 12, 31, 12, 30, 30)
  629. increment = "3600 seconds"
  630. for i in range(24):
  631. start = increment_datetime_by_string(a, increment, i)
  632. end = increment_datetime_by_string(a, increment, i + 1)
  633. map = RasterDataset(None)
  634. map.set_absolute_time(start, end)
  635. maps.append(map)
  636. a = datetime(2002, 02, 01, 12, 30, 30)
  637. for i in range(24):
  638. start = increment_datetime_by_string(a, increment, i)
  639. end = None
  640. map = RasterDataset(None)
  641. map.set_absolute_time(start, end)
  642. maps.append(map)
  643. gran = compute_absolute_time_granularity(maps)
  644. if increment != gran:
  645. core.fatal("Wrong granularity reference %s != gran %s" % (
  646. increment, gran))
  647. print "Test 17"
  648. maps = []
  649. a = datetime(2001, 1, 1)
  650. increment = "2 days"
  651. for i in range(8):
  652. start = increment_datetime_by_string(a, increment, i)
  653. end = increment_datetime_by_string(a, increment, i + 1)
  654. map = RasterDataset(None)
  655. map.set_absolute_time(start, end)
  656. maps.append(map)
  657. a = datetime(2001, 02, 02)
  658. for i in range(8):
  659. start = increment_datetime_by_string(a, increment, i)
  660. end = None
  661. map = RasterDataset(None)
  662. map.set_absolute_time(start, end)
  663. maps.append(map)
  664. gran = compute_absolute_time_granularity(maps)
  665. if increment != gran:
  666. core.fatal("Wrong granularity reference %s != gran %s" % (
  667. increment, gran))
  668. ###############################################################################
  669. def test_spatial_extent_intersection():
  670. # Generate the extents
  671. A = SpatialExtent(
  672. north=80, south=20, east=60, west=10, bottom=-50, top=50)
  673. A.print_info()
  674. B = SpatialExtent(
  675. north=80, south=20, east=60, west=10, bottom=-50, top=50)
  676. B.print_info()
  677. C = A.intersect(B)
  678. C.print_info()
  679. if C.get_north() != B.get_north() or C.get_south() != B.get_south() or \
  680. C.get_west() != B.get_west() or C.get_east() != B.get_east() or \
  681. C.get_bottom() != B.get_bottom() or C.get_top() != B.get_top():
  682. core.fatal("Wrong intersection computation")
  683. B = SpatialExtent(
  684. north=40, south=30, east=60, west=10, bottom=-50, top=50)
  685. B.print_info()
  686. C = A.intersect(B)
  687. C.print_info()
  688. if C.get_north() != B.get_north() or C.get_south() != B.get_south() or \
  689. C.get_west() != B.get_west() or C.get_east() != B.get_east() or \
  690. C.get_bottom() != B.get_bottom() or C.get_top() != B.get_top():
  691. core.fatal("Wrong intersection computation")
  692. B = SpatialExtent(
  693. north=40, south=30, east=60, west=30, bottom=-50, top=50)
  694. B.print_info()
  695. C = A.intersect(B)
  696. C.print_info()
  697. if C.get_north() != B.get_north() or C.get_south() != B.get_south() or \
  698. C.get_west() != B.get_west() or C.get_east() != B.get_east() or \
  699. C.get_bottom() != B.get_bottom() or C.get_top() != B.get_top():
  700. core.fatal("Wrong intersection computation")
  701. B = SpatialExtent(
  702. north=40, south=30, east=60, west=30, bottom=-30, top=50)
  703. B.print_info()
  704. C = A.intersect(B)
  705. C.print_info()
  706. if C.get_north() != B.get_north() or C.get_south() != B.get_south() or \
  707. C.get_west() != B.get_west() or C.get_east() != B.get_east() or \
  708. C.get_bottom() != B.get_bottom() or C.get_top() != B.get_top():
  709. core.fatal("Wrong intersection computation")
  710. B = SpatialExtent(
  711. north=40, south=30, east=60, west=30, bottom=-30, top=30)
  712. B.print_info()
  713. C = A.intersect(B)
  714. C.print_info()
  715. if C.get_north() != B.get_north() or C.get_south() != B.get_south() or \
  716. C.get_west() != B.get_west() or C.get_east() != B.get_east() or \
  717. C.get_bottom() != B.get_bottom() or C.get_top() != B.get_top():
  718. core.fatal("Wrong intersection computation")
  719. ###############################################################################
  720. def test_spatial_relations():
  721. # Generate the extents
  722. A = SpatialExtent(
  723. north=80, south=20, east=60, west=10, bottom=-50, top=50)
  724. A.print_info()
  725. B = SpatialExtent(
  726. north=80, south=20, east=60, west=10, bottom=-50, top=50)
  727. B.print_info()
  728. relation = A.spatial_relation(B)
  729. print relation
  730. if relation != "equivalent":
  731. core.fatal("Wrong spatial relation: %s" % (relation))
  732. B = SpatialExtent(
  733. north=70, south=20, east=60, west=10, bottom=-50, top=50)
  734. B.print_info()
  735. relation = A.spatial_relation_2d(B)
  736. print relation
  737. if relation != "cover":
  738. core.fatal("Wrong spatial relation: %s" % (relation))
  739. relation = A.spatial_relation(B)
  740. print relation
  741. if relation != "cover":
  742. core.fatal("Wrong spatial relation: %s" % (relation))
  743. B = SpatialExtent(
  744. north=70, south=30, east=60, west=10, bottom=-50, top=50)
  745. B.print_info()
  746. relation = A.spatial_relation_2d(B)
  747. print relation
  748. if relation != "cover":
  749. core.fatal("Wrong spatial relation: %s" % (relation))
  750. relation = A.spatial_relation(B)
  751. print relation
  752. if relation != "cover":
  753. core.fatal("Wrong spatial relation: %s" % (relation))
  754. relation = B.spatial_relation_2d(A)
  755. print relation
  756. if relation != "covered":
  757. core.fatal("Wrong spatial relation: %s" % (relation))
  758. relation = B.spatial_relation(A)
  759. print relation
  760. if relation != "covered":
  761. core.fatal("Wrong spatial relation: %s" % (relation))
  762. B = SpatialExtent(
  763. north=70, south=30, east=50, west=10, bottom=-50, top=50)
  764. B.print_info()
  765. relation = A.spatial_relation_2d(B)
  766. print relation
  767. if relation != "cover":
  768. core.fatal("Wrong spatial relation: %s" % (relation))
  769. relation = B.spatial_relation_2d(A)
  770. print relation
  771. if relation != "covered":
  772. core.fatal("Wrong spatial relation: %s" % (relation))
  773. relation = A.spatial_relation(B)
  774. print relation
  775. if relation != "cover":
  776. core.fatal("Wrong spatial relation: %s" % (relation))
  777. B = SpatialExtent(
  778. north=70, south=30, east=50, west=20, bottom=-50, top=50)
  779. relation = B.spatial_relation(A)
  780. print relation
  781. if relation != "covered":
  782. core.fatal("Wrong spatial relation: %s" % (relation))
  783. B = SpatialExtent(
  784. north=70, south=30, east=50, west=20, bottom=-50, top=50)
  785. B.print_info()
  786. relation = A.spatial_relation_2d(B)
  787. print relation
  788. if relation != "contain":
  789. core.fatal("Wrong spatial relation: %s" % (relation))
  790. relation = A.spatial_relation(B)
  791. print relation
  792. if relation != "cover":
  793. core.fatal("Wrong spatial relation: %s" % (relation))
  794. B = SpatialExtent(
  795. north=70, south=30, east=50, west=20, bottom=-40, top=50)
  796. B.print_info()
  797. relation = A.spatial_relation(B)
  798. print relation
  799. if relation != "cover":
  800. core.fatal("Wrong spatial relation: %s" % (relation))
  801. B = SpatialExtent(
  802. north=70, south=30, east=50, west=20, bottom=-40, top=40)
  803. B.print_info()
  804. relation = A.spatial_relation(B)
  805. print relation
  806. if relation != "contain":
  807. core.fatal("Wrong spatial relation: %s" % (relation))
  808. relation = B.spatial_relation(A)
  809. print relation
  810. if relation != "in":
  811. core.fatal("Wrong spatial relation: %s" % (relation))
  812. B = SpatialExtent(
  813. north=90, south=30, east=50, west=20, bottom=-40, top=40)
  814. B.print_info()
  815. relation = A.spatial_relation_2d(B)
  816. print relation
  817. if relation != "overlap":
  818. core.fatal("Wrong spatial relation: %s" % (relation))
  819. relation = A.spatial_relation(B)
  820. print relation
  821. if relation != "overlap":
  822. core.fatal("Wrong spatial relation: %s" % (relation))
  823. B = SpatialExtent(north=90, south=5, east=70, west=5, bottom=-40, top=40)
  824. A.print_info()
  825. B.print_info()
  826. relation = A.spatial_relation_2d(B)
  827. print relation
  828. if relation != "in":
  829. core.fatal("Wrong spatial relation: %s" % (relation))
  830. relation = A.spatial_relation(B)
  831. print relation
  832. if relation != "overlap":
  833. core.fatal("Wrong spatial relation: %s" % (relation))
  834. B = SpatialExtent(north=90, south=5, east=70, west=5, bottom=-40, top=60)
  835. A.print_info()
  836. B.print_info()
  837. relation = A.spatial_relation(B)
  838. print relation
  839. if relation != "overlap":
  840. core.fatal("Wrong spatial relation: %s" % (relation))
  841. B = SpatialExtent(north=90, south=5, east=70, west=5, bottom=-60, top=60)
  842. A.print_info()
  843. B.print_info()
  844. relation = A.spatial_relation(B)
  845. print relation
  846. if relation != "in":
  847. core.fatal("Wrong spatial relation: %s" % (relation))
  848. A = SpatialExtent(
  849. north=80, south=60, east=60, west=10, bottom=-50, top=50)
  850. A.print_info()
  851. B = SpatialExtent(
  852. north=60, south=20, east=60, west=10, bottom=-50, top=50)
  853. B.print_info()
  854. relation = A.spatial_relation_2d(B)
  855. print relation
  856. if relation != "meet":
  857. core.fatal("Wrong spatial relation: %s" % (relation))
  858. relation = A.spatial_relation(B)
  859. print relation
  860. if relation != "meet":
  861. core.fatal("Wrong spatial relation: %s" % (relation))
  862. A = SpatialExtent(
  863. north=60, south=40, east=60, west=10, bottom=-50, top=50)
  864. A.print_info()
  865. B = SpatialExtent(
  866. north=80, south=60, east=60, west=10, bottom=-50, top=50)
  867. B.print_info()
  868. relation = A.spatial_relation_2d(B)
  869. print relation
  870. if relation != "meet":
  871. core.fatal("Wrong spatial relation: %s" % (relation))
  872. relation = A.spatial_relation(B)
  873. print relation
  874. if relation != "meet":
  875. core.fatal("Wrong spatial relation: %s" % (relation))
  876. A = SpatialExtent(
  877. north=80, south=40, east=60, west=40, bottom=-50, top=50)
  878. A.print_info()
  879. B = SpatialExtent(
  880. north=80, south=40, east=40, west=20, bottom=-50, top=50)
  881. B.print_info()
  882. relation = A.spatial_relation_2d(B)
  883. print relation
  884. if relation != "meet":
  885. core.fatal("Wrong spatial relation: %s" % (relation))
  886. relation = A.spatial_relation(B)
  887. print relation
  888. if relation != "meet":
  889. core.fatal("Wrong spatial relation: %s" % (relation))
  890. A = SpatialExtent(
  891. north=80, south=40, east=40, west=20, bottom=-50, top=50)
  892. A.print_info()
  893. B = SpatialExtent(
  894. north=90, south=30, east=60, west=40, bottom=-50, top=50)
  895. B.print_info()
  896. relation = A.spatial_relation_2d(B)
  897. print relation
  898. if relation != "meet":
  899. core.fatal("Wrong spatial relation: %s" % (relation))
  900. relation = A.spatial_relation(B)
  901. print relation
  902. if relation != "meet":
  903. core.fatal("Wrong spatial relation: %s" % (relation))
  904. A = SpatialExtent(
  905. north=80, south=40, east=40, west=20, bottom=-50, top=50)
  906. A.print_info()
  907. B = SpatialExtent(
  908. north=70, south=50, 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 != "meet":
  913. core.fatal("Wrong spatial relation: %s" % (relation))
  914. relation = A.spatial_relation(B)
  915. print relation
  916. if relation != "meet":
  917. core.fatal("Wrong spatial relation: %s" % (relation))
  918. A = SpatialExtent(
  919. north=80, south=40, east=40, west=20, bottom=-50, top=50)
  920. A.print_info()
  921. B = SpatialExtent(
  922. north=60, south=20, east=60, west=40, bottom=-50, top=50)
  923. B.print_info()
  924. relation = A.spatial_relation_2d(B)
  925. print relation
  926. if relation != "meet":
  927. core.fatal("Wrong spatial relation: %s" % (relation))
  928. relation = A.spatial_relation(B)
  929. print relation
  930. if relation != "meet":
  931. core.fatal("Wrong spatial relation: %s" % (relation))
  932. A = SpatialExtent(
  933. north=80, south=40, east=40, west=20, bottom=-50, top=50)
  934. A.print_info()
  935. B = SpatialExtent(
  936. north=40, south=20, east=60, west=40, bottom=-50, top=50)
  937. B.print_info()
  938. relation = A.spatial_relation_2d(B)
  939. print relation
  940. if relation != "disjoint":
  941. core.fatal("Wrong spatial relation: %s" % (relation))
  942. relation = A.spatial_relation(B)
  943. print relation
  944. if relation != "disjoint":
  945. core.fatal("Wrong spatial relation: %s" % (relation))
  946. A = SpatialExtent(
  947. north=80, south=40, east=40, west=20, bottom=-50, top=50)
  948. A.print_info()
  949. B = SpatialExtent(
  950. north=60, south=20, east=60, west=40, bottom=-60, top=60)
  951. B.print_info()
  952. relation = A.spatial_relation(B)
  953. print relation
  954. if relation != "meet":
  955. core.fatal("Wrong spatial relation: %s" % (relation))
  956. A = SpatialExtent(
  957. north=80, south=40, east=40, west=20, bottom=-50, top=50)
  958. A.print_info()
  959. B = SpatialExtent(
  960. north=90, south=30, east=60, west=40, bottom=-40, top=40)
  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=80, south=40, east=60, west=20, 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=0, top=50)
  975. A.print_info()
  976. B = SpatialExtent(north=80, south=50, east=60, west=30, bottom=-50, top=0)
  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=0, top=50)
  983. A.print_info()
  984. B = SpatialExtent(north=70, south=50, east=50, west=30, bottom=-50, top=0)
  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=0, top=50)
  991. A.print_info()
  992. B = SpatialExtent(north=90, south=30, east=70, west=10, bottom=-50, top=0)
  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=0, top=50)
  999. A.print_info()
  1000. B = SpatialExtent(north=70, south=30, east=50, west=10, bottom=-50, top=0)
  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. ###
  1007. A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
  1008. A.print_info()
  1009. B = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
  1010. B.print_info()
  1011. relation = A.spatial_relation(B)
  1012. print relation
  1013. if relation != "meet":
  1014. core.fatal("Wrong spatial relation: %s" % (relation))
  1015. A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
  1016. A.print_info()
  1017. B = SpatialExtent(north=80, south=50, east=60, west=30, bottom=0, top=50)
  1018. B.print_info()
  1019. relation = A.spatial_relation(B)
  1020. print relation
  1021. if relation != "meet":
  1022. core.fatal("Wrong spatial relation: %s" % (relation))
  1023. A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
  1024. A.print_info()
  1025. B = SpatialExtent(north=70, south=50, east=50, west=30, bottom=0, top=50)
  1026. B.print_info()
  1027. relation = A.spatial_relation(B)
  1028. print relation
  1029. if relation != "meet":
  1030. core.fatal("Wrong spatial relation: %s" % (relation))
  1031. A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
  1032. A.print_info()
  1033. B = SpatialExtent(north=90, south=30, east=70, west=10, bottom=0, top=50)
  1034. B.print_info()
  1035. relation = A.spatial_relation(B)
  1036. print relation
  1037. if relation != "meet":
  1038. core.fatal("Wrong spatial relation: %s" % (relation))
  1039. A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
  1040. A.print_info()
  1041. B = SpatialExtent(north=70, south=30, east=50, west=10, bottom=0, top=50)
  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. ###############################################################################
  1048. def test_temporal_topology_builder():
  1049. map_listA = []
  1050. _map = RasterDataset(ident="1@a")
  1051. _map.set_absolute_time(datetime(2001, 01, 01), datetime(2001, 02, 01))
  1052. map_listA.append(copy.copy(_map))
  1053. _map = RasterDataset(ident="2@a")
  1054. _map.set_absolute_time(datetime(2001, 02, 01), datetime(2001, 03, 01))
  1055. map_listA.append(copy.copy(_map))
  1056. _map = RasterDataset(ident="3@a")
  1057. _map.set_absolute_time(datetime(2001, 03, 01), datetime(2001, 04, 01))
  1058. map_listA.append(copy.copy(_map))
  1059. _map = RasterDataset(ident="4@a")
  1060. _map.set_absolute_time(datetime(2001, 04, 01), datetime(2001, 05, 01))
  1061. map_listA.append(copy.copy(_map))
  1062. _map = RasterDataset(ident="5@a")
  1063. _map.set_absolute_time(datetime(2001, 05, 01), datetime(2001, 06, 01))
  1064. map_listA.append(copy.copy(_map))
  1065. tb = SpatioTemporalTopologyBuilder()
  1066. tb.build(map_listA)
  1067. count = 0
  1068. for _map in tb:
  1069. print "[%s]" % (_map.get_name())
  1070. _map.print_topology_info()
  1071. if _map.get_id() != map_listA[count].get_id():
  1072. core.fatal("Error building temporal topology <%s> != <%s>" %
  1073. (_map.get_id(), map_listA[count].get_id()))
  1074. count += 1
  1075. map_listB = []
  1076. _map = RasterDataset(ident="1@b")
  1077. _map.set_absolute_time(datetime(2001, 01, 14), datetime(2001, 03, 14))
  1078. map_listB.append(copy.copy(_map))
  1079. _map = RasterDataset(ident="2@b")
  1080. _map.set_absolute_time(datetime(2001, 02, 01), datetime(2001, 04, 01))
  1081. map_listB.append(copy.copy(_map))
  1082. _map = RasterDataset(ident="3@b")
  1083. _map.set_absolute_time(datetime(2001, 02, 14), datetime(2001, 04, 30))
  1084. map_listB.append(copy.copy(_map))
  1085. _map = RasterDataset(ident="4@b")
  1086. _map.set_absolute_time(datetime(2001, 04, 02), datetime(2001, 04, 30))
  1087. map_listB.append(copy.copy(_map))
  1088. _map = RasterDataset(ident="5@b")
  1089. _map.set_absolute_time(datetime(2001, 05, 01), datetime(2001, 05, 14))
  1090. map_listB.append(copy.copy(_map))
  1091. tb = SpatioTemporalTopologyBuilder()
  1092. tb.build(map_listB)
  1093. # Probing some relations
  1094. if map_listB[0].get_overlapped()[0] != map_listB[1]:
  1095. core.fatal("Error building temporal topology")
  1096. if map_listB[0].get_overlapped()[1] != map_listB[2]:
  1097. core.fatal("Error building temporal topology")
  1098. if map_listB[2].get_contains()[0] != map_listB[3]:
  1099. core.fatal("Error building temporal topology")
  1100. if map_listB[3].get_during()[0] != map_listB[2]:
  1101. core.fatal("Error building temporal topology")
  1102. count = 0
  1103. for _map in tb:
  1104. print "[%s]" % (_map.get_map_id
  1105. ())
  1106. _map.print_topology_shell_info()
  1107. if _map.get_id() != map_listB[count].get_id():
  1108. core.fatal("Error building temporal topology <%s> != <%s>" %
  1109. (_map.get_id(), map_listB[count].get_id()))
  1110. count += 1
  1111. tb = SpatioTemporalTopologyBuilder()
  1112. tb.build(map_listA, map_listB)
  1113. count = 0
  1114. for _map in tb:
  1115. print "[%s]" % (_map.get_map_id())
  1116. _map.print_topology_shell_info()
  1117. if _map.get_id() != map_listA[count].get_id():
  1118. core.fatal("Error building temporal topology <%s> != <%s>" %
  1119. (_map.get_id(), map_listA[count].get_id()))
  1120. count += 1
  1121. count = 0
  1122. for _map in map_listB:
  1123. print "[%s]" % (_map.get_map_id())
  1124. _map.print_topology_shell_info()
  1125. # Probing some relations
  1126. if map_listA[3].get_follows()[0] != map_listB[1]:
  1127. core.fatal("Error building temporal topology")
  1128. if map_listA[3].get_precedes()[0] != map_listB[4]:
  1129. core.fatal("Error building temporal topology")
  1130. if map_listA[3].get_overlaps()[0] != map_listB[2]:
  1131. core.fatal("Error building temporal topology")
  1132. if map_listA[3].get_contains()[0] != map_listB[3]:
  1133. core.fatal("Error building temporal topology")
  1134. if map_listA[2].get_during()[0] != map_listB[1]:
  1135. core.fatal("Error building temporal topology")
  1136. if map_listA[2].get_during()[1] != map_listB[2]:
  1137. core.fatal("Error building temporal topology")
  1138. ###############################################################################
  1139. def test_map_list_sorting():
  1140. map_list = []
  1141. _map = RasterDataset(ident="1@a")
  1142. _map.set_absolute_time(datetime(2001, 02, 01), datetime(2001, 03, 01))
  1143. map_list.append(copy.copy(_map))
  1144. _map = RasterDataset(ident="2@a")
  1145. _map.set_absolute_time(datetime(2001, 01, 01), datetime(2001, 02, 01))
  1146. map_list.append(copy.copy(_map))
  1147. _map = RasterDataset(ident="3@a")
  1148. _map.set_absolute_time(datetime(2001, 03, 01), datetime(2001, 04, 01))
  1149. map_list.append(copy.copy(_map))
  1150. print "Original"
  1151. for _map in map_list:
  1152. print _map.get_valid_time()[0], _map.get_valid_time()[1]
  1153. print "Sorted by start time"
  1154. new_list = sorted(map_list, key=AbstractDatasetComparisonKeyStartTime)
  1155. for _map in new_list:
  1156. print _map.get_valid_time()[0], _map.get_valid_time()[1]
  1157. if new_list[0] != map_list[1]:
  1158. core.fatal("Sorting by start time failed")
  1159. if new_list[1] != map_list[0]:
  1160. core.fatal("Sorting by start time failed")
  1161. if new_list[2] != map_list[2]:
  1162. core.fatal("Sorting by start time failed")
  1163. print "Sorted by end time"
  1164. new_list = sorted(map_list, key=AbstractDatasetComparisonKeyEndTime)
  1165. for _map in new_list:
  1166. print _map.get_valid_time()[0], _map.get_valid_time()[1]
  1167. if new_list[0] != map_list[1]:
  1168. core.fatal("Sorting by end time failed")
  1169. if new_list[1] != map_list[0]:
  1170. core.fatal("Sorting by end time failed")
  1171. if new_list[2] != map_list[2]:
  1172. core.fatal("Sorting by end time failed")
  1173. ###############################################################################
  1174. def test_1d_rtree():
  1175. """Testing the rtree ctypes wrapper"""
  1176. tree = vector.RTreeCreateTree(-1, 0, 1)
  1177. for i in xrange(10):
  1178. rect = vector.RTreeAllocRect(tree)
  1179. vector.RTreeSetRect1D(rect, tree, float(i - 2), float(i + 2))
  1180. vector.RTreeInsertRect(rect, i + 1, tree)
  1181. rect = vector.RTreeAllocRect(tree)
  1182. vector.RTreeSetRect1D(rect, tree, 2.0, 7.0)
  1183. list_ = gis.ilist()
  1184. num = vector.RTreeSearch2(tree, rect, byref(list_))
  1185. vector.RTreeFreeRect(rect)
  1186. # print rectangle ids
  1187. print "Number of overlapping rectangles", num
  1188. for i in xrange(list_.n_values):
  1189. print "id", list_.value[i]
  1190. vector.RTreeDestroyTree(tree)
  1191. ###############################################################################
  1192. def test_2d_rtree():
  1193. """Testing the rtree ctypes wrapper"""
  1194. tree = vector.RTreeCreateTree(-1, 0, 2)
  1195. for i in xrange(10):
  1196. rect = vector.RTreeAllocRect(tree)
  1197. vector.RTreeSetRect2D(rect, tree,
  1198. float(i - 2), float(i + 2),
  1199. float(i - 2), float(i + 2))
  1200. vector.RTreeInsertRect(rect, i + 1, tree)
  1201. rect = vector.RTreeAllocRect(tree)
  1202. vector.RTreeSetRect2D(rect, tree, 2.0, 7.0, 2.0, 7.0)
  1203. list_ = gis.ilist()
  1204. num = vector.RTreeSearch2(tree, rect, byref(list_))
  1205. vector.RTreeFreeRect(rect)
  1206. # print rectangle ids
  1207. print "Number of overlapping rectangles", num
  1208. for i in xrange(list_.n_values):
  1209. print "id", list_.value[i]
  1210. vector.RTreeDestroyTree(tree)
  1211. ###############################################################################
  1212. def test_3d_rtree():
  1213. """Testing the rtree ctypes wrapper"""
  1214. tree = vector.RTreeCreateTree(-1, 0, 3)
  1215. for i in xrange(10):
  1216. rect = vector.RTreeAllocRect(tree)
  1217. vector.RTreeSetRect3D(rect, tree,
  1218. float(i - 2), float(i + 2),
  1219. float(i - 2), float(i + 2),
  1220. float(i - 2), float(i + 2))
  1221. vector.RTreeInsertRect(rect, i + 1, tree)
  1222. print i + 1
  1223. vector.RTreePrintRect(rect, 1, tree)
  1224. rect = vector.RTreeAllocRect(tree)
  1225. vector.RTreeSetRect3D(rect, tree, 2.0, 7.0, 2.0, 7.0, 2.0, 7.0)
  1226. print "Select"
  1227. vector.RTreePrintRect(rect, 1, tree)
  1228. list_ = gis.ilist()
  1229. num = vector.RTreeSearch2(tree, rect, byref(list_))
  1230. vector.RTreeFreeRect(rect)
  1231. # print rectangle ids
  1232. print "Number of overlapping rectangles", num
  1233. for i in xrange(list_.n_values):
  1234. print "id", list_.value[i]
  1235. vector.RTreeDestroyTree(tree)
  1236. ###############################################################################
  1237. def test_4d_rtree():
  1238. """Testing the rtree ctypes wrapper"""
  1239. tree = vector.RTreeCreateTree(-1, 0, 4)
  1240. for i in xrange(10):
  1241. # Allocate the boundary
  1242. rect = vector.RTreeAllocRect(tree)
  1243. vector.RTreeSetRect4D(rect, tree,
  1244. float(i - 2), float(i + 2),
  1245. float(i - 2), float(i + 2),
  1246. float(i - 2), float(i + 2),
  1247. float(i - 2), float(i + 2))
  1248. vector.RTreeInsertRect(rect, i + 1, tree)
  1249. rect = vector.RTreeAllocRect(tree)
  1250. vector.RTreeSetRect4D(rect, tree, 2.0, 7.0, 2.0,
  1251. 7.0, 2.0, 7.0, 2.0, 7.0)
  1252. list_ = gis.ilist()
  1253. num = vector.RTreeSearch2(tree, rect, byref(list_))
  1254. vector.RTreeFreeRect(rect)
  1255. # print rectangle ids
  1256. print "Number of overlapping rectangles", num
  1257. for i in xrange(list_.n_values):
  1258. print "id", list_.value[i]
  1259. vector.RTreeDestroyTree(tree)
  1260. ###############################################################################
  1261. if __name__ == "__main__":
  1262. init()
  1263. test_increment_datetime_by_string()
  1264. test_adjust_datetime_to_granularity()
  1265. test_spatial_extent_intersection()
  1266. test_compute_absolute_time_granularity()
  1267. test_compute_datetime_delta()
  1268. test_spatial_extent_intersection()
  1269. test_spatial_relations()
  1270. test_temporal_topology_builder()
  1271. test_map_list_sorting()
  1272. test_1d_rtree()
  1273. test_2d_rtree()
  1274. test_3d_rtree()
  1275. test_4d_rtree()