abstract_space_time_dataset.py 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  1. # -*- coding: utf-8 -*-
  2. """!@package grass.temporal
  3. @brief GRASS Python scripting module (temporal GIS functions)
  4. Temporal GIS related functions to be used in temporal GIS Python library package.
  5. (C) 2011-2012 by the GRASS Development Team
  6. This program is free software under the GNU General Public
  7. License (>=v2). Read the file COPYING that comes with GRASS
  8. for details.
  9. @author Soeren Gebbert
  10. """
  11. from abstract_dataset import *
  12. from temporal_granularity import *
  13. from temporal_relationships import *
  14. ###############################################################################
  15. class AbstractSpaceTimeDataset(AbstractDataset):
  16. """!Abstract space time dataset class
  17. This class represents a space time dataset. Convenient functions
  18. to select, update, insert or delete objects of this type in the SQL
  19. temporal database exists as well as functions to register or unregister
  20. raster maps.
  21. Parts of the temporal logic are implemented in the SQL temporal database,
  22. like the computation of the temporal and spatial extent as well as the
  23. collecting of metadata.
  24. """
  25. def __init__(self, ident):
  26. self.reset(ident)
  27. self.map_counter = 0
  28. def get_new_map_instance(self, ident=None):
  29. """!Return a new instance of a map dataset which is associated
  30. with the type of this class
  31. @param ident: The unique identifier of the new object
  32. """
  33. raise ImplementationError(
  34. "This method must be implemented in the subclasses")
  35. def get_map_register(self):
  36. """!Return the name of the map register table"""
  37. raise ImplementationError(
  38. "This method must be implemented in the subclasses")
  39. def set_map_register(self, name):
  40. """!Set the name of the map register table
  41. This table stores all map names which are registered
  42. in this space time dataset.
  43. @param name: The name of the register table
  44. """
  45. raise ImplementationError(
  46. "This method must be implemented in the subclasses")
  47. def print_self(self):
  48. """!Print the content of the internal structure to stdout"""
  49. self.base.print_self()
  50. if self.is_time_absolute():
  51. self.absolute_time.print_self()
  52. if self.is_time_relative():
  53. self.relative_time.print_self()
  54. self.spatial_extent.print_self()
  55. self.metadata.print_self()
  56. def print_info(self):
  57. """!Print information about this class in human readable style"""
  58. if self.get_type() == "strds":
  59. # 1 2 3 4 5 6 7
  60. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  61. print " +-------------------- Space Time Raster Dataset -----------------------------+"
  62. if self.get_type() == "str3ds":
  63. # 1 2 3 4 5 6 7
  64. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  65. print " +-------------------- Space Time Raster3d Dataset ---------------------------+"
  66. if self.get_type() == "stvds":
  67. # 1 2 3 4 5 6 7
  68. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  69. print " +-------------------- Space Time Vector Dataset -----------------------------+"
  70. print " | |"
  71. self.base.print_info()
  72. if self.is_time_absolute():
  73. self.absolute_time.print_info()
  74. if self.is_time_relative():
  75. self.relative_time.print_info()
  76. self.spatial_extent.print_info()
  77. self.metadata.print_info()
  78. print " +----------------------------------------------------------------------------+"
  79. def print_shell_info(self):
  80. """!Print information about this class in shell style"""
  81. self.base.print_shell_info()
  82. if self.is_time_absolute():
  83. self.absolute_time.print_shell_info()
  84. if self.is_time_relative():
  85. self.relative_time.print_shell_info()
  86. self.spatial_extent.print_shell_info()
  87. self.metadata.print_shell_info()
  88. def set_initial_values(self, temporal_type, semantic_type,
  89. title=None, description=None):
  90. """!Set the initial values of the space time dataset
  91. @param temporal_type: The temporal type of this space
  92. time dataset (absolute or relative)
  93. @param semantic_type: The semantic type of this dataset
  94. @param title: The title
  95. @param description: The description of this dataset
  96. """
  97. if temporal_type == "absolute":
  98. self.set_time_to_absolute()
  99. elif temporal_type == "relative":
  100. self.set_time_to_relative()
  101. else:
  102. core.fatal(_("Unknown temporal type \"%s\"") % (temporal_type))
  103. self.base.set_semantic_type(semantic_type)
  104. self.metadata.set_title(title)
  105. self.metadata.set_description(description)
  106. def get_semantic_type(self):
  107. """!Return the semantic type of this dataset"""
  108. return self.base.get_semantic_type()
  109. def get_initial_values(self):
  110. """!Return the initial values: temporal_type,
  111. semantic_type, title, description"""
  112. temporal_type = self.get_temporal_type()
  113. semantic_type = self.base.get_semantic_type()
  114. title = self.metadata.get_title()
  115. description = self.metadata.get_description()
  116. return temporal_type, semantic_type, title, description
  117. def get_granularity(self):
  118. """!Return the granularity
  119. Granularity can be of absolute time or relative time.
  120. In case of absolute time a string containing an integer
  121. value and the time unit (years, months, days, hours, minuts, seconds).
  122. In case of relative time an integer value is expected.
  123. """
  124. temporal_type = self.get_temporal_type()
  125. if temporal_type == "absolute":
  126. granularity = self.absolute_time.get_granularity()
  127. elif temporal_type == "relative":
  128. granularity = self.relative_time.get_granularity()
  129. return granularity
  130. def set_granularity(self, granularity):
  131. """!Set the granularity
  132. Granularity can be of absolute time or relative time.
  133. In case of absolute time a string containing an integer
  134. value and the time unit (years, months, days, hours, minuts, seconds).
  135. In case of relative time an integer value is expected.
  136. """
  137. temporal_type = self.get_temporal_type()
  138. if temporal_type == "absolute":
  139. self.set_time_to_absolute()
  140. self.absolute_time.set_granularity(granularity)
  141. elif temporal_type == "relative":
  142. self.set_time_to_relative()
  143. self.relative_time.set_granularity(granularity)
  144. else:
  145. core.fatal(_("Unknown temporal type \"%s\"") % (temporal_type))
  146. def set_relative_time_unit(self, unit):
  147. """!Set the relative time unit which may be of type:
  148. years, months, days, hours, minutes or seconds
  149. All maps registered in a (relative time)
  150. space time dataset must have the same unit
  151. """
  152. temporal_type = self.get_temporal_type()
  153. if temporal_type == "relative":
  154. if not self.check_relative_time_unit(unit):
  155. core.fatal(_("Unsupported temporal unit: %s") % (unit))
  156. self.relative_time.set_unit(unit)
  157. def get_map_time(self):
  158. """!Return the type of the map time, interval, point, mixed or invalid"""
  159. temporal_type = self.get_temporal_type()
  160. if temporal_type == "absolute":
  161. map_time = self.absolute_time.get_map_time()
  162. elif temporal_type == "relative":
  163. map_time = self.relative_time.get_map_time()
  164. return map_time
  165. def count_temporal_types(self, maps=None, dbif=None):
  166. """!Return the temporal type of the registered maps as dictionary
  167. The map list must be ordered by start time
  168. The temporal type can be:
  169. * point -> only the start time is present
  170. * interval -> start and end time
  171. * invalid -> No valid time point or interval found
  172. @param maps: A sorted (start_time) list of AbstractDataset objects
  173. @param dbif: The database interface to be used
  174. """
  175. if maps is None:
  176. maps = get_registered_maps_as_objects(
  177. where=None, order="start_time", dbif=dbif)
  178. time_invalid = 0
  179. time_point = 0
  180. time_interval = 0
  181. tcount = {}
  182. for i in range(len(maps)):
  183. # Check for point and interval data
  184. if maps[i].is_time_absolute():
  185. start, end, tz = maps[i].get_absolute_time()
  186. if maps[i].is_time_relative():
  187. start, end, unit = maps[i].get_relative_time()
  188. if start is not None and end is not None:
  189. time_interval += 1
  190. elif start is not None and end is None:
  191. time_point += 1
  192. else:
  193. time_invalid += 1
  194. tcount["point"] = time_point
  195. tcount["interval"] = time_interval
  196. tcount["invalid"] = time_invalid
  197. return tcount
  198. def count_gaps(self, maps=None, dbif=None):
  199. """!Count the number of gaps between temporal neighbors
  200. @param maps: A sorted (start_time) list of AbstractDataset objects
  201. @param dbif: The database interface to be used
  202. @return The numbers of gaps between temporal neighbors
  203. """
  204. if maps is None:
  205. maps = self.get_registered_maps_as_objects(
  206. where=None, order="start_time", dbif=dbif)
  207. gaps = 0
  208. # Check for gaps
  209. for i in range(len(maps)):
  210. if i < len(maps) - 1:
  211. relation = maps[i + 1].temporal_relation(maps[i])
  212. if relation == "after":
  213. gaps += 1
  214. return gaps
  215. def print_temporal_relationships(self, maps=None, dbif=None):
  216. """!Print the temporal relation matrix of all registered maps to stdout
  217. The temporal relation matrix includes the temporal relations between
  218. all registered maps. The relations are strings stored in a list of lists.
  219. @param maps: a ordered by start_time list of map objects
  220. @param dbif: The database interface to be used
  221. """
  222. if maps is None:
  223. maps = self.get_registered_maps_as_objects(
  224. where=None, order="start_time", dbif=dbif)
  225. print_temporal_topology_relationships(maps, maps)
  226. def count_temporal_relations(self, maps=None, dbif=None):
  227. """!Count the temporal relations between the registered maps.
  228. The map list must be ordered by start time.
  229. Temporal relations are counted by analysing the sparse
  230. upper right side temporal relationships matrix.
  231. @param maps: A sorted (start_time) list of AbstractDataset objects
  232. @param dbif: The database interface to be used
  233. @return A dictionary with counted temporal relationships
  234. """
  235. if maps is None:
  236. maps = self.get_registered_maps_as_objects(
  237. where=None, order="start_time", dbif=dbif)
  238. return count_temporal_topology_relationships(maps, maps)
  239. def check_temporal_topology(self, maps=None, dbif=None):
  240. """!Check the temporal topology
  241. Correct topology means, that time intervals are not overlap or
  242. that intervals does not contain other intervals.
  243. Equal time intervals are not allowed.
  244. The map list must be ordered by start time
  245. Allowed and not allowed temporal relationships for correct topology:
  246. @verbatim
  247. * after -> allowed
  248. * precedes -> allowed
  249. * follows -> allowed
  250. * precedes -> allowed
  251. * equivalent -> not allowed
  252. * during -> not allowed
  253. * contains -> not allowed
  254. * overlaps -> not allowed
  255. * overlapped -> not allowed
  256. * starts -> not allowed
  257. * finishes -> not allowed
  258. * started -> not allowed
  259. * finished -> not allowed
  260. @endverbatim
  261. @param maps: A sorted (start_time) list of AbstractDataset objects
  262. @return True if topology is correct
  263. """
  264. if maps is None:
  265. maps = self.get_registered_maps_as_objects(
  266. where=None, order="start_time", dbif=dbif)
  267. relations = count_temporal_topology_relationships(maps, maps)
  268. map_time = self.get_map_time()
  269. if map_time == "interval" or map_time == "mixed":
  270. if "equivalent" in relations:
  271. return False
  272. if "during" in relations:
  273. return False
  274. if "contains" in relations:
  275. return False
  276. if "overlaps" in relations:
  277. return False
  278. if "overlapped" in relations:
  279. return False
  280. if "starts" in relations:
  281. return False
  282. if "finishes" in relations:
  283. return False
  284. if "started" in relations:
  285. return False
  286. if "finished" in relations:
  287. return False
  288. elif map_time == "point":
  289. if "equivalent" in relations:
  290. return False
  291. else:
  292. return False
  293. return True
  294. def sample_by_dataset(self, stds, method=None, spatial=False, dbif=None):
  295. """!Sample this space time dataset with the temporal topology
  296. of a second space time dataset
  297. The sample dataset must have "interval" as temporal map type,
  298. so all sample maps have valid interval time.
  299. In case spatial is True, the spatial overlap between
  300. temporal related maps is performed. Only
  301. temporal related and spatial overlapping maps are returned.
  302. Return all registered maps as ordered (by start_time) object list with
  303. "gap" map objects (id==None). Each list entry is a list of map objects
  304. which are potentially located in temporal relation to the actual
  305. granule of the second space time dataset.
  306. Each entry in the object list is a dict. The actual sampler
  307. map and its temporal extent (the actual granule) and
  308. the list of samples are stored:
  309. @code
  310. list = self.sample_by_dataset(stds=sampler, method=[
  311. "during","overlap","contain","equal"])
  312. for entry in list:
  313. granule = entry["granule"]
  314. maplist = entry["samples"]
  315. for map in maplist:
  316. map.select()
  317. map.print_info()
  318. @endcode
  319. A valid temporal topology (no overlapping or inclusion allowed)
  320. is needed to get correct results in case of gaps in the sample dataset.
  321. Gaps between maps are identified as unregistered maps with id==None.
  322. The map objects are initialized with the id and the temporal
  323. extent of the granule (temporal type, start time, end time).
  324. In case more map information are needed, use the select()
  325. method for each listed object.
  326. @param stds: The space time dataset to be used for temporal sampling
  327. @param method: This option specifies what sample method should be used.
  328. In case the registered maps are of temporal point type,
  329. only the start time is used for sampling. In case of mixed
  330. of interval data the user can chose between:
  331. * start: Select maps of which the start time is
  332. located in the selection granule
  333. @verbatim
  334. map : s
  335. granule: s-----------------e
  336. map : s--------------------e
  337. granule: s-----------------e
  338. map : s--------e
  339. granule: s-----------------e
  340. @endverbatim
  341. * during: Select maps which are temporal
  342. during the selection granule
  343. @verbatim
  344. map : s-----------e
  345. granule: s-----------------e
  346. @endverbatim
  347. * overlap: Select maps which temporal overlap
  348. the selection granule
  349. @verbatim
  350. map : s-----------e
  351. granule: s-----------------e
  352. map : s-----------e
  353. granule: s----------e
  354. @endverbatim
  355. * contain: Select maps which temporally contain
  356. the selection granule
  357. @verbatim
  358. map : s-----------------e
  359. granule: s-----------e
  360. @endverbatim
  361. * equal: Select maps which temporally equal
  362. to the selection granule
  363. @verbatim
  364. map : s-----------e
  365. granule: s-----------e
  366. @endverbatim
  367. * follows: Select maps which temporally follow
  368. the selection granule
  369. @verbatim
  370. map : s-----------e
  371. granule: s-----------e
  372. @endverbatim
  373. * precedes: Select maps which temporally precedes
  374. the selection granule
  375. @verbatim
  376. map : s-----------e
  377. granule: s-----------e
  378. @endverbatim
  379. All these methods can be combined. Method must be of
  380. type tuple including the identification strings.
  381. @param spatial: If set True additional the spatial overlapping
  382. is used for selection -> spatio-temporal relation.
  383. The returned map objects will have temporal and
  384. spatial extents
  385. @param dbif: The database interface to be used
  386. In case nothing found None is returned
  387. """
  388. use_start = False
  389. use_during = False
  390. use_overlap = False
  391. use_contain = False
  392. use_equal = False
  393. use_follows = False
  394. use_precedes = False
  395. # Initialize the methods
  396. if method is not None:
  397. for name in method:
  398. if name == "start":
  399. use_start = True
  400. if name == "during":
  401. use_during = True
  402. if name == "overlap":
  403. use_overlap = True
  404. if name == "contain":
  405. use_contain = True
  406. if name == "equal":
  407. use_equal = True
  408. if name == "follows":
  409. use_follows = True
  410. if name == "precedes":
  411. use_precedes = True
  412. else:
  413. use_during = True
  414. use_overlap = True
  415. use_contain = True
  416. use_equal = True
  417. if self.get_temporal_type() != stds.get_temporal_type():
  418. core.error(_("The space time datasets must be of "
  419. "the same temporal type"))
  420. return None
  421. if stds.get_map_time() != "interval":
  422. core.error(_("The temporal map type of the sample "
  423. "dataset must be interval"))
  424. return None
  425. # In case points of time are available, disable the interval specific methods
  426. if self.get_map_time() == "point":
  427. use_start = True
  428. use_during = False
  429. use_overlap = False
  430. use_contain = False
  431. use_equal = False
  432. use_follows = False
  433. use_precedes = False
  434. dbif, connect = init_dbif(dbif)
  435. obj_list = []
  436. sample_maps = stds.get_registered_maps_as_objects_with_gaps(
  437. where=None, dbif=dbif)
  438. for granule in sample_maps:
  439. # Read the spatial extent
  440. if spatial:
  441. granule.spatial_extent.select(dbif)
  442. start, end = granule.get_valid_time()
  443. where = create_temporal_relation_sql_where_statement(
  444. start, end, use_start,
  445. use_during, use_overlap, use_contain, use_equal, use_follows, use_precedes)
  446. maps = self.get_registered_maps_as_objects(
  447. where, "start_time", dbif)
  448. result = {}
  449. result["granule"] = granule
  450. num_samples = 0
  451. maplist = []
  452. if maps is not None:
  453. for map in maps:
  454. # Read the spatial extent
  455. if spatial:
  456. map.spatial_extent.select(dbif)
  457. # Ignore spatial disjoint maps
  458. if not granule.spatial_overlapping(map):
  459. continue
  460. num_samples += 1
  461. maplist.append(copy.copy(map))
  462. # Fill with empty map in case no spatio-temporal relations found
  463. if maps is None or num_samples == 0:
  464. map = self.get_new_map_instance(None)
  465. if self.is_time_absolute():
  466. map.set_absolute_time(start, end)
  467. elif self.is_time_relative():
  468. map.set_relative_time(start, end,
  469. self.get_relative_time_unit())
  470. maplist.append(copy.copy(map))
  471. result["samples"] = maplist
  472. obj_list.append(copy.copy(result))
  473. if connect:
  474. dbif.close()
  475. return obj_list
  476. def get_registered_maps_as_objects_by_granularity(self, gran=None, dbif=None):
  477. """!Return all registered maps as ordered (by start_time) object list with
  478. "gap" map objects (id==None) for temporal topological operations using
  479. the granularity of the space time dataset as increment.
  480. Each list entry is a list of map objects
  481. which are potentially located in the actual granule.
  482. A valid temporal topology (no overlapping or inclusion allowed)
  483. is needed to get correct results.
  484. The dataset must have "interval" as temporal map type,
  485. so all maps have valid interval time.
  486. Gaps between maps are identified as unregistered maps with id==None.
  487. The objects are initialized with the id and the temporal
  488. extent (temporal type, start time, end time).
  489. In case more map information are needed, use the select()
  490. method for each listed object.
  491. @param gran: The granularity to be used
  492. @param dbif: The database interface to be used
  493. @return ordered object list, in case nothing found None is returned
  494. """
  495. dbif, connect = init_dbif(dbif)
  496. obj_list = []
  497. if gran is None:
  498. gran = self.get_granularity()
  499. start, end = self.get_valid_time()
  500. while start < end:
  501. if self.is_time_absolute():
  502. next = increment_datetime_by_string(start, gran)
  503. else:
  504. next = start + gran
  505. where = "(start_time <= '%s' and end_time >= '%s')" % (start, next)
  506. rows = self.get_registered_maps("id", where, "start_time", dbif)
  507. if rows is not None:
  508. if len(rows) > 1:
  509. core.warning(_("More than one map found in a granule. Temporal granularity seems to be invalid or the chosen granularity is not a greatest common divider of all intervals and gaps in the dataset."))
  510. maplist = []
  511. for row in rows:
  512. # Take the first map
  513. map = self.get_new_map_instance(rows[0]["id"])
  514. if self.is_time_absolute():
  515. map.set_absolute_time(start, next)
  516. elif self.is_time_relative():
  517. map.set_relative_time(start, next, self.get_relative_time_unit())
  518. maplist.append(copy.copy(map))
  519. obj_list.append(copy.copy(maplist))
  520. start = next
  521. if connect:
  522. dbif.close()
  523. return obj_list
  524. def get_registered_maps_as_objects_with_gaps(self, where=None, dbif=None):
  525. """!Return all registered maps as ordered (by start_time) object list with
  526. "gap" map objects (id==None) for temporal topological operations
  527. Gaps between maps are identified as maps with id==None
  528. The objects are initialized with the id and the
  529. temporal extent (temporal type, start time, end time).
  530. In case more map information are needed, use the select()
  531. method for each listed object.
  532. @param where: The SQL where statement to select a
  533. subset of the registered maps without "WHERE"
  534. @param dbif: The database interface to be used
  535. @return ordered object list, in case nothing found None is returned
  536. """
  537. dbif, connect = init_dbif(dbif)
  538. obj_list = []
  539. maps = self.get_registered_maps_as_objects(where, "start_time", dbif)
  540. if maps is not None and len(maps) > 0:
  541. for i in range(len(maps)):
  542. obj_list.append(maps[i])
  543. # Detect and insert gaps
  544. if i < len(maps) - 1:
  545. relation = maps[i + 1].temporal_relation(maps[i])
  546. if relation == "after":
  547. start1, end1 = maps[i].get_valid_time()
  548. start2, end2 = maps[i + 1].get_valid_time()
  549. end = start2
  550. if end1 is not None:
  551. start = end1
  552. else:
  553. start = start1
  554. map = self.get_new_map_instance(None)
  555. if self.is_time_absolute():
  556. map.set_absolute_time(start, end)
  557. elif self.is_time_relative():
  558. map.set_relative_time(start, end,
  559. self.get_relative_time_unit())
  560. obj_list.append(copy.copy(map))
  561. if connect:
  562. dbif.close()
  563. return obj_list
  564. def get_registered_maps_as_objects(self, where=None, order="start_time",
  565. dbif=None):
  566. """!Return all registered maps as ordered object list for
  567. temporal topological operations
  568. The objects are initialized with the id and the temporal extent
  569. (temporal type, start time, end time).
  570. In case more map information are needed, use the select()
  571. method for each listed object.
  572. @param where: The SQL where statement to select a subset of
  573. the registered maps without "WHERE"
  574. @param order: The SQL order statement to be used to order the
  575. objects in the list without "ORDER BY"
  576. @param dbif: The database interface to be used
  577. In case nothing found None is returned
  578. """
  579. dbif, connect = init_dbif(dbif)
  580. obj_list = []
  581. rows = self.get_registered_maps(
  582. "id,start_time,end_time", where, order, dbif)
  583. count = 0
  584. if rows is not None:
  585. for row in rows:
  586. core.percent(count, len(rows), 1)
  587. map = self.get_new_map_instance(row["id"])
  588. if self.is_time_absolute():
  589. map.set_absolute_time(row["start_time"], row["end_time"])
  590. elif self.is_time_relative():
  591. map.set_relative_time(row["start_time"], row["end_time"],
  592. self.get_relative_time_unit())
  593. obj_list.append(copy.copy(map))
  594. count += 1
  595. core.percent(1, 1, 1)
  596. if connect:
  597. dbif.close()
  598. return obj_list
  599. def get_registered_maps(self, columns=None, where=None, order=None, dbif=None):
  600. """!Return SQL rows of all registered maps.
  601. In case columns are not specified, each row includes all columns
  602. specified in the datatype specific view.
  603. @param columns: Columns to be selected as SQL compliant string
  604. @param where: The SQL where statement to select a subset
  605. of the registered maps without "WHERE"
  606. @param order: The SQL order statement to be used to order the
  607. objects in the list without "ORDER BY"
  608. @param dbif: The database interface to be used
  609. @return SQL rows of all registered maps,
  610. In case nothing found None is returned
  611. """
  612. dbif, connect = init_dbif(dbif)
  613. rows = None
  614. if self.get_map_register() is not None:
  615. # Use the correct temporal table
  616. if self.get_temporal_type() == "absolute":
  617. map_view = self.get_new_map_instance(
  618. None).get_type() + "_view_abs_time"
  619. else:
  620. map_view = self.get_new_map_instance(
  621. None).get_type() + "_view_rel_time"
  622. if columns is not None and columns != "":
  623. sql = "SELECT %s FROM %s WHERE %s.id IN (SELECT id FROM %s)" %\
  624. (columns, map_view, map_view, self.get_map_register())
  625. else:
  626. sql = "SELECT * FROM %s WHERE %s.id IN (SELECT id FROM %s)" % \
  627. (map_view, map_view, self.get_map_register())
  628. if where is not None and where != "":
  629. sql += " AND (%s)" % (where.split(";")[0])
  630. if order is not None and order != "":
  631. sql += " ORDER BY %s" % (order.split(";")[0])
  632. try:
  633. dbif.cursor.execute(sql)
  634. rows = dbif.cursor.fetchall()
  635. except:
  636. if connect:
  637. dbif.close()
  638. core.error(_("Unable to get map ids from register table <%s>")
  639. % (self.get_map_register()))
  640. raise
  641. if connect:
  642. dbif.close()
  643. return rows
  644. def delete(self, dbif=None, execute=True):
  645. """!Delete a space time dataset from the temporal database
  646. This method removes the space time dataset from the temporal
  647. database and drops its map register table
  648. @param dbif: The database interface to be used
  649. @param execute: If True the SQL DELETE and DROP table
  650. statements will be executed.
  651. If False the prepared SQL statements are returned
  652. and must be executed by the caller.
  653. @return The SQL statements if execute == False, else an empty string
  654. """
  655. # First we need to check if maps are registered in this dataset and
  656. # unregister them
  657. core.verbose(_("Delete space time %s dataset <%s> from temporal "
  658. "database") % \
  659. (self.get_new_map_instance(ident=None).get_type(),
  660. self.get_id()))
  661. statement = ""
  662. dbif, connect = init_dbif(dbif)
  663. # SELECT all needed information from the database
  664. self.metadata.select(dbif)
  665. if self.get_map_register() is not None:
  666. core.verbose(_("Drop map register table: %s") % (
  667. self.get_map_register()))
  668. rows = self.get_registered_maps("id", None, None, dbif)
  669. # Unregister each registered map in the table
  670. if rows is not None:
  671. num_maps = len(rows)
  672. count = 0
  673. for row in rows:
  674. core.percent(count, num_maps, 1)
  675. # Unregister map
  676. map = self.get_new_map_instance(row["id"])
  677. statement += self.unregister_map(
  678. map=map, dbif=dbif, execute=False)
  679. count += 1
  680. core.percent(1, 1, 1)
  681. # Safe the DROP table statement
  682. statement += "DROP TABLE " + self.get_map_register() + ";\n"
  683. # Remove the primary key, the foreign keys will be removed by trigger
  684. statement += self.base.get_delete_statement()
  685. if execute:
  686. dbif.execute_transaction(statement)
  687. self.reset(None)
  688. if connect:
  689. dbif.close()
  690. if execute:
  691. return ""
  692. return statement
  693. def register_map(self, map, dbif=None):
  694. """!Register a map in the space time dataset.
  695. This method takes care of the registration of a map
  696. in a space time dataset.
  697. In case the map is already registered this function
  698. will break with a warning and return False.
  699. @param dbif: The database interface to be used
  700. """
  701. dbif, connect = init_dbif(dbif)
  702. if map.is_in_db(dbif) == False:
  703. dbif.close()
  704. core.fatal(_("Only maps with absolute or relative valid time can "
  705. "be registered"))
  706. if map.get_layer():
  707. core.verbose(_("Register %s map <%s> with layer %s in space "
  708. "time %s dataset <%s>") % (map.get_type(),
  709. map.get_map_id(),
  710. map.get_layer(),
  711. map.get_type(),
  712. self.get_id()))
  713. else:
  714. core.verbose(_("Register %s map <%s> in space time %s "
  715. "dataset <%s>") % (map.get_type(), map.get_map_id(),
  716. map.get_type(), self.get_id()))
  717. # First select all data from the database
  718. map.select(dbif)
  719. if not map.check_valid_time():
  720. if map.get_layer():
  721. core.fatal(_("Map <%s> with layer %s has invalid time")
  722. % (map.get_map_id(), map.get_layer()))
  723. else:
  724. core.fatal(_("Map <%s> has invalid time") % (map.get_map_id()))
  725. map_id = map.base.get_id()
  726. map_name = map.base.get_name()
  727. map_mapset = map.base.get_mapset()
  728. map_register_table = map.get_stds_register()
  729. map_rel_time_unit = map.get_relative_time_unit()
  730. map_ttype = map.get_temporal_type()
  731. #print "Map register table", map_register_table
  732. # Get basic info
  733. stds_name = self.base.get_name()
  734. stds_mapset = self.base.get_mapset()
  735. stds_register_table = self.get_map_register()
  736. stds_ttype = self.get_temporal_type()
  737. # The gathered SQL statemets are stroed here
  738. statement = ""
  739. # Check temporal types
  740. if stds_ttype != map_ttype:
  741. if map.get_layer():
  742. core.fatal(_("Temporal type of space time dataset <%s> and "
  743. "map <%s> with layer %s are different") % \
  744. (self.get_id(), map.get_map_id(), map.get_layer()))
  745. else:
  746. core.fatal(_("Temporal type of space time dataset <%s> and "
  747. "map <%s> are different") % \
  748. (self.get_id(), map.get_map_id()))
  749. # In case no map has been registered yet, set the
  750. # relative time unit from the first map
  751. if (self.metadata.get_number_of_maps() is None or \
  752. self.metadata.get_number_of_maps() == 0) and \
  753. self.map_counter == 0 and self.is_time_relative():
  754. self.set_relative_time_unit(map_rel_time_unit)
  755. statement += self.relative_time.get_update_all_statement_mogrified(
  756. dbif)
  757. core.verbose(_("Set temporal unit for space time %s dataset "
  758. "<%s> to %s") % (map.get_type(), self.get_id(),
  759. map_rel_time_unit))
  760. stds_rel_time_unit = self.get_relative_time_unit()
  761. # Check the relative time unit
  762. if self.is_time_relative() and (stds_rel_time_unit != map_rel_time_unit):
  763. if map.get_layer():
  764. core.fatal(_("Relative time units of space time dataset "
  765. "<%s> and map <%s> with layer %s are different") %\
  766. (self.get_id(), map.get_map_id(), map.get_layer()))
  767. else:
  768. core.fatal(_("Relative time units of space time dataset "
  769. "<%s> and map <%s> are different") % \
  770. (self.get_id(), map.get_map_id()))
  771. #print "STDS register table", stds_register_table
  772. if stds_mapset != map_mapset:
  773. dbif.close()
  774. core.fatal(_("Only maps from the same mapset can be registered"))
  775. # Check if map is already registered
  776. if stds_register_table is not None:
  777. if dbmi.paramstyle == "qmark":
  778. sql = "SELECT id FROM " + \
  779. stds_register_table + " WHERE id = (?)"
  780. else:
  781. sql = "SELECT id FROM " + \
  782. stds_register_table + " WHERE id = (%s)"
  783. try:
  784. dbif.cursor.execute(sql, (map_id,))
  785. row = dbif.cursor.fetchone()
  786. except:
  787. row = None
  788. core.warning(_("Error in strds_register_table request"))
  789. raise
  790. if row is not None and row[0] == map_id:
  791. if connect == True:
  792. dbif.close()
  793. if map.get_layer() is not None:
  794. core.warning(_("Map <%s> with layer %s is already "
  795. "registered.") % (map.get_map_id(),
  796. map.get_layer()))
  797. else:
  798. core.warning(_("Map <%s> is already registered.")
  799. % (map.get_map_id()))
  800. return ""
  801. # Create tables
  802. sql_path = get_sql_template_path()
  803. # We need to create the map raster register table precedes we can register the map
  804. if map_register_table is None:
  805. # Create a unique id
  806. uuid_rand = "map_" + str(uuid.uuid4()).replace("-", "")
  807. map_register_table = uuid_rand + "_" + \
  808. self.get_type() + "_register"
  809. # Read the SQL template
  810. sql = open(os.path.join(sql_path,
  811. "map_stds_register_table_template.sql"),
  812. 'r').read()
  813. # Create the raster, raster3d and vector tables
  814. sql = sql.replace("GRASS_MAP", map.get_type())
  815. sql = sql.replace("MAP_NAME", map_name + "_" + map_mapset)
  816. sql = sql.replace("TABLE_NAME", uuid_rand)
  817. sql = sql.replace("MAP_ID", map_id)
  818. sql = sql.replace("STDS", self.get_type())
  819. statement += sql
  820. # Set the stds register table name and put it into the DB
  821. map.set_stds_register(map_register_table)
  822. statement += map.metadata.get_update_statement_mogrified(dbif)
  823. if map.get_layer():
  824. core.verbose(_("Created register table <%s> for "
  825. "%s map <%s> with layer %s") %
  826. (map_register_table, map.get_type(),
  827. map.get_map_id(), map.get_layer()))
  828. else:
  829. core.verbose(_("Created register table <%s> for %s map <%s>") %
  830. (map_register_table, map.get_type(),
  831. map.get_map_id()))
  832. # We need to create the table and register it
  833. if stds_register_table is None:
  834. # Create table name
  835. stds_register_table = stds_name + "_" + \
  836. stds_mapset + "_" + map.get_type() + "_register"
  837. # Read the SQL template
  838. sql = open(os.path.join(sql_path,
  839. "stds_map_register_table_template.sql"),
  840. 'r').read()
  841. # Create the raster, raster3d and vector tables
  842. sql = sql.replace("GRASS_MAP", map.get_type())
  843. sql = sql.replace("SPACETIME_NAME", stds_name + "_" + stds_mapset)
  844. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  845. sql = sql.replace("STDS", self.get_type())
  846. statement += sql
  847. # Set the map register table name and put it into the DB
  848. self.set_map_register(stds_register_table)
  849. statement += self.metadata.get_update_statement_mogrified(dbif)
  850. core.verbose(_("Created register table <%s> for space "
  851. "time %s dataset <%s>") %
  852. (stds_register_table, map.get_type(), self.get_id()))
  853. # We need to execute the statement at this time
  854. if statement != "":
  855. dbif.execute_transaction(statement)
  856. statement = ""
  857. # Register the stds in the map stds register table
  858. # Check if the entry is already there
  859. if dbmi.paramstyle == "qmark":
  860. sql = "SELECT id FROM " + map_register_table + " WHERE id = ?"
  861. else:
  862. sql = "SELECT id FROM " + map_register_table + " WHERE id = %s"
  863. try:
  864. dbif.cursor.execute(sql, (self.base.get_id(),))
  865. row = dbif.cursor.fetchone()
  866. except:
  867. row = None
  868. # In case of no entry make a new one
  869. if row is None:
  870. if dbmi.paramstyle == "qmark":
  871. sql = "INSERT INTO " + map_register_table + \
  872. " (id) " + "VALUES (?);\n"
  873. else:
  874. sql = "INSERT INTO " + map_register_table + \
  875. " (id) " + "VALUES (%s);\n"
  876. statement += dbif.mogrify_sql_statement(
  877. (sql, (self.base.get_id(),)))
  878. # Now put the raster name in the stds map register table
  879. if dbmi.paramstyle == "qmark":
  880. sql = "INSERT INTO " + stds_register_table + \
  881. " (id) " + "VALUES (?);\n"
  882. else:
  883. sql = "INSERT INTO " + stds_register_table + \
  884. " (id) " + "VALUES (%s);\n"
  885. statement += dbif.mogrify_sql_statement((sql, (map_id,)))
  886. # Now execute the insert transaction
  887. dbif.execute_transaction(statement)
  888. if connect:
  889. dbif.close()
  890. # increase the counter
  891. self.map_counter += 1
  892. def unregister_map(self, map, dbif=None, execute=True):
  893. """!Unregister a map from the space time dataset.
  894. This method takes care of the un-registration of a map
  895. from a space time dataset.
  896. @param map: The map object to unregister
  897. @param dbif: The database interface to be used
  898. @param execute: If True the SQL DELETE and DROP table
  899. statements will be executed.
  900. If False the prepared SQL statements are
  901. returned and must be executed by the caller.
  902. @return The SQL statements if execute == False, else an empty
  903. string, None in case of a failure
  904. """
  905. statement = ""
  906. dbif, connect = init_dbif(dbif)
  907. # First select needed data from the database
  908. map.metadata.select(dbif)
  909. map_id = map.get_id()
  910. map_register_table = map.get_stds_register()
  911. stds_register_table = self.get_map_register()
  912. if map.get_layer() is not None:
  913. core.verbose(_("Unregister %s map <%s> with layer %s") % \
  914. (map.get_type(), map.get_map_id(), map.get_layer()))
  915. else:
  916. core.verbose(_("Unregister %s map <%s>") % (
  917. map.get_type(), map.get_map_id()))
  918. # Check if the map is registered in the space time raster dataset
  919. if map_register_table is not None:
  920. if dbmi.paramstyle == "qmark":
  921. sql = "SELECT id FROM " + map_register_table + " WHERE id = ?"
  922. else:
  923. sql = "SELECT id FROM " + map_register_table + " WHERE id = %s"
  924. try:
  925. dbif.cursor.execute(sql, (self.base.get_id(),))
  926. row = dbif.cursor.fetchone()
  927. except:
  928. row = None
  929. # Break if the map is not registered
  930. if row is None:
  931. if map.get_layer() is not None:
  932. core.warning(_("Map <%s> with layer %s is not registered "
  933. "in space time dataset <%s>") % \
  934. (map.get_map_id(), map.get_layer(),
  935. self.base.get_id()))
  936. else:
  937. core.warning(_("Map <%s> is not registered in space "
  938. "time dataset <%s>") % (map.get_map_id(),
  939. self.base.get_id()))
  940. if connect == True:
  941. dbif.close()
  942. return ""
  943. # Remove the space time raster dataset from the raster dataset register
  944. if map_register_table is not None:
  945. if dbmi.paramstyle == "qmark":
  946. sql = "DELETE FROM " + map_register_table + " WHERE id = ?;\n"
  947. else:
  948. sql = "DELETE FROM " + map_register_table + " WHERE id = %s;\n"
  949. statement += dbif.mogrify_sql_statement(
  950. (sql, (self.base.get_id(),)))
  951. # Remove the raster map from the space time raster dataset register
  952. if stds_register_table is not None:
  953. if dbmi.paramstyle == "qmark":
  954. sql = "DELETE FROM " + stds_register_table + " WHERE id = ?;\n"
  955. else:
  956. sql = "DELETE FROM " + \
  957. stds_register_table + " WHERE id = %s;\n"
  958. statement += dbif.mogrify_sql_statement((sql, (map_id,)))
  959. if execute:
  960. dbif.execute_transaction(statement)
  961. if connect:
  962. dbif.close()
  963. # decrease the counter
  964. self.map_counter -= 1
  965. if execute:
  966. return ""
  967. return statement
  968. def update_from_registered_maps(self, dbif=None):
  969. """!This methods updates the spatial and temporal extent as well as
  970. type specific metadata. It should always been called after maps are registered
  971. or unregistered/deleted from the space time dataset.
  972. The update of the temporal extent checks if the end time is set correctly.
  973. In case the registered maps have no valid end time (None) the maximum start time
  974. will be used. If the end time is earlier than the maximum start time, it will
  975. be replaced by the maximum start time.
  976. An other solution to automate this is to use the deactivated trigger
  977. in the SQL files. But this will result in a huge performance issue
  978. in case many maps are registered (>1000).
  979. @param dbif: The database interface to be used
  980. """
  981. core.verbose(_("Update metadata, spatial and temporal extent from "
  982. "all registered maps of <%s>") % (self.get_id()))
  983. # Nothing to do if the register is not present
  984. if not self.get_map_register():
  985. return
  986. dbif, connect = init_dbif(dbif)
  987. map_time = None
  988. use_start_time = False
  989. # Get basic info
  990. stds_name = self.base.get_name()
  991. stds_mapset = self.base.get_mapset()
  992. sql_path = get_sql_template_path()
  993. #We create a transaction
  994. sql_script = ""
  995. # Update the spatial and temporal extent from registered maps
  996. # Read the SQL template
  997. sql = open(os.path.join(sql_path,
  998. "update_stds_spatial_temporal_extent_template.sql"),
  999. 'r').read()
  1000. sql = sql.replace(
  1001. "GRASS_MAP", self.get_new_map_instance(None).get_type())
  1002. sql = sql.replace("SPACETIME_NAME", stds_name + "_" + stds_mapset)
  1003. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  1004. sql = sql.replace("STDS", self.get_type())
  1005. sql_script += sql
  1006. sql_script += "\n"
  1007. # Update type specific metadata
  1008. sql = open(os.path.join(sql_path, "update_" +
  1009. self.get_type() + "_metadata_template.sql"), 'r').read()
  1010. sql = sql.replace(
  1011. "GRASS_MAP", self.get_new_map_instance(None).get_type())
  1012. sql = sql.replace("SPACETIME_NAME", stds_name + "_" + stds_mapset)
  1013. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  1014. sql = sql.replace("STDS", self.get_type())
  1015. sql_script += sql
  1016. sql_script += "\n"
  1017. dbif.execute_transaction(sql_script)
  1018. # Read and validate the selected end time
  1019. self.select()
  1020. if self.is_time_absolute():
  1021. start_time, end_time, tz = self.get_absolute_time()
  1022. else:
  1023. start_time, end_time, unit = self.get_relative_time()
  1024. # In case no end time is set, use the maximum start time of
  1025. # all registered maps as end time
  1026. if end_time is None:
  1027. use_start_time = True
  1028. else:
  1029. # Check if the end time is smaller than the maximum start time
  1030. if self.is_time_absolute():
  1031. sql = """SELECT max(start_time) FROM GRASS_MAP_absolute_time
  1032. WHERE GRASS_MAP_absolute_time.id IN
  1033. (SELECT id FROM SPACETIME_NAME_GRASS_MAP_register);"""
  1034. sql = sql.replace("GRASS_MAP", self.get_new_map_instance(
  1035. None).get_type())
  1036. sql = sql.replace("SPACETIME_NAME",
  1037. stds_name + "_" + stds_mapset)
  1038. else:
  1039. sql = """SELECT max(start_time) FROM GRASS_MAP_relative_time
  1040. WHERE GRASS_MAP_relative_time.id IN
  1041. (SELECT id FROM SPACETIME_NAME_GRASS_MAP_register);"""
  1042. sql = sql.replace("GRASS_MAP", self.get_new_map_instance(
  1043. None).get_type())
  1044. sql = sql.replace("SPACETIME_NAME",
  1045. stds_name + "_" + stds_mapset)
  1046. dbif.cursor.execute(sql)
  1047. row = dbif.cursor.fetchone()
  1048. if row is not None:
  1049. # This seems to be a bug in sqlite3 Python driver
  1050. if dbmi.__name__ == "sqlite3":
  1051. tstring = row[0]
  1052. # Convert the unicode string into the datetime format
  1053. if self.is_time_absolute():
  1054. if tstring.find(":") > 0:
  1055. time_format = "%Y-%m-%d %H:%M:%S"
  1056. else:
  1057. time_format = "%Y-%m-%d"
  1058. max_start_time = datetime.strptime(
  1059. tstring, time_format)
  1060. else:
  1061. max_start_time = row[0]
  1062. else:
  1063. max_start_time = row[0]
  1064. if end_time < max_start_time:
  1065. use_start_time = True
  1066. # Set the maximum start time as end time
  1067. if use_start_time:
  1068. if self.is_time_absolute():
  1069. sql = """UPDATE STDS_absolute_time SET end_time =
  1070. (SELECT max(start_time) FROM GRASS_MAP_absolute_time WHERE
  1071. GRASS_MAP_absolute_time.id IN
  1072. (SELECT id FROM SPACETIME_NAME_GRASS_MAP_register)
  1073. ) WHERE id = 'SPACETIME_ID';"""
  1074. sql = sql.replace("GRASS_MAP", self.get_new_map_instance(
  1075. None).get_type())
  1076. sql = sql.replace("SPACETIME_NAME",
  1077. stds_name + "_" + stds_mapset)
  1078. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  1079. sql = sql.replace("STDS", self.get_type())
  1080. elif self.is_time_relative():
  1081. sql = """UPDATE STDS_relative_time SET end_time =
  1082. (SELECT max(start_time) FROM GRASS_MAP_relative_time WHERE
  1083. GRASS_MAP_relative_time.id IN
  1084. (SELECT id FROM SPACETIME_NAME_GRASS_MAP_register)
  1085. ) WHERE id = 'SPACETIME_ID';"""
  1086. sql = sql.replace("GRASS_MAP", self.get_new_map_instance(
  1087. None).get_type())
  1088. sql = sql.replace("SPACETIME_NAME",
  1089. stds_name + "_" + stds_mapset)
  1090. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  1091. sql = sql.replace("STDS", self.get_type())
  1092. dbif.execute_transaction(sql)
  1093. # Count the temporal map types
  1094. maps = self.get_registered_maps_as_objects(dbif=dbif)
  1095. tlist = self.count_temporal_types(maps)
  1096. if tlist["interval"] > 0 and tlist["point"] == 0 and \
  1097. tlist["invalid"] == 0:
  1098. map_time = "interval"
  1099. elif tlist["interval"] == 0 and tlist["point"] > 0 and \
  1100. tlist["invalid"] == 0:
  1101. map_time = "point"
  1102. elif tlist["interval"] > 0 and tlist["point"] > 0 and \
  1103. tlist["invalid"] == 0:
  1104. map_time = "mixed"
  1105. else:
  1106. map_time = "invalid"
  1107. # Compute the granularity
  1108. if map_time != "invalid":
  1109. # Smallest supported temporal resolution
  1110. if self.is_time_absolute():
  1111. gran = compute_absolute_time_granularity(maps)
  1112. elif self.is_time_relative():
  1113. gran = compute_relative_time_granularity(maps)
  1114. else:
  1115. gran = None
  1116. # Set the map time type and update the time objects
  1117. if self.is_time_absolute():
  1118. self.absolute_time.select(dbif)
  1119. self.metadata.select(dbif)
  1120. if self.metadata.get_number_of_maps() > 0:
  1121. self.absolute_time.set_map_time(map_time)
  1122. self.absolute_time.set_granularity(gran)
  1123. else:
  1124. self.absolute_time.set_map_time(None)
  1125. self.absolute_time.set_granularity(None)
  1126. self.absolute_time.update_all(dbif)
  1127. else:
  1128. self.relative_time.select(dbif)
  1129. self.metadata.select(dbif)
  1130. if self.metadata.get_number_of_maps() > 0:
  1131. self.relative_time.set_map_time(map_time)
  1132. self.relative_time.set_granularity(gran)
  1133. else:
  1134. self.relative_time.set_map_time(None)
  1135. self.relative_time.set_granularity(None)
  1136. self.relative_time.update_all(dbif)
  1137. if connect:
  1138. dbif.close()
  1139. ###############################################################################
  1140. def create_temporal_relation_sql_where_statement(
  1141. start, end, use_start=True, use_during=False,
  1142. use_overlap=False, use_contain=False, use_equal=False,
  1143. use_follows=False, use_precedes=False):
  1144. """!Create a SQL WHERE statement for temporal relation selection of maps in space time datasets
  1145. @param start: The start time
  1146. @param end: The end time
  1147. @param use_start: Select maps of which the start time is located in the selection granule
  1148. @verbatim
  1149. map : s
  1150. granule: s-----------------e
  1151. map : s--------------------e
  1152. granule: s-----------------e
  1153. map : s--------e
  1154. granule: s-----------------e
  1155. @endverbatim
  1156. @param use_during: during: Select maps which are temporal during the selection granule
  1157. @verbatim
  1158. map : s-----------e
  1159. granule: s-----------------e
  1160. @endverbatim
  1161. @param use_overlap: Select maps which temporal overlap the selection granule
  1162. @verbatim
  1163. map : s-----------e
  1164. granule: s-----------------e
  1165. map : s-----------e
  1166. granule: s----------e
  1167. @endverbatim
  1168. @param use_contain: Select maps which temporally contain the selection granule
  1169. @verbatim
  1170. map : s-----------------e
  1171. granule: s-----------e
  1172. @endverbatim
  1173. @param use_equal: Select maps which temporally equal to the selection granule
  1174. @verbatim
  1175. map : s-----------e
  1176. granule: s-----------e
  1177. @endverbatim
  1178. @param use_follows: Select maps which temporally follow the selection granule
  1179. @verbatim
  1180. map : s-----------e
  1181. granule: s-----------e
  1182. @endverbatim
  1183. @param use_precedes: Select maps which temporally precedes the selection granule
  1184. @verbatim
  1185. map : s-----------e
  1186. granule: s-----------e
  1187. @endverbatim
  1188. Usage:
  1189. @code
  1190. >>> # Relative time
  1191. >>> start = 1
  1192. >>> end = 2
  1193. >>> create_temporal_relation_sql_where_statement(start, end,
  1194. ... use_start=False)
  1195. >>> create_temporal_relation_sql_where_statement(start, end)
  1196. '((start_time >= 1 and start_time < 2) )'
  1197. >>> create_temporal_relation_sql_where_statement(start, end,
  1198. ... use_start=True)
  1199. '((start_time >= 1 and start_time < 2) )'
  1200. >>> create_temporal_relation_sql_where_statement(start, end,
  1201. ... use_start=False, use_during=True)
  1202. '(((start_time > 1 and end_time < 2) OR (start_time >= 1 and end_time < 2) OR (start_time > 1 and end_time <= 2)))'
  1203. >>> create_temporal_relation_sql_where_statement(start, end,
  1204. ... use_start=False, use_overlap=True)
  1205. '(((start_time < 1 and end_time > 1 and end_time < 2) OR (start_time < 2 and start_time > 1 and end_time > 2)))'
  1206. >>> create_temporal_relation_sql_where_statement(start, end,
  1207. ... use_start=False, use_contain=True)
  1208. '(((start_time < 1 and end_time > 2) OR (start_time <= 1 and end_time > 2) OR (start_time < 1 and end_time >= 2)))'
  1209. >>> create_temporal_relation_sql_where_statement(start, end,
  1210. ... use_start=False, use_equal=True)
  1211. '((start_time = 1 and end_time = 2))'
  1212. >>> create_temporal_relation_sql_where_statement(start, end,
  1213. ... use_start=False, use_follows=True)
  1214. '((start_time = 2))'
  1215. >>> create_temporal_relation_sql_where_statement(start, end,
  1216. ... use_start=False, use_precedes=True)
  1217. '((end_time = 1))'
  1218. >>> create_temporal_relation_sql_where_statement(start, end,
  1219. ... use_start=True, use_during=True, use_overlap=True, use_contain=True,
  1220. ... use_equal=True, use_follows=True, use_precedes=True)
  1221. '((start_time >= 1 and start_time < 2) OR ((start_time > 1 and end_time < 2) OR (start_time >= 1 and end_time < 2) OR (start_time > 1 and end_time <= 2)) OR ((start_time < 1 and end_time > 1 and end_time < 2) OR (start_time < 2 and start_time > 1 and end_time > 2)) OR ((start_time < 1 and end_time > 2) OR (start_time <= 1 and end_time > 2) OR (start_time < 1 and end_time >= 2)) OR (start_time = 1 and end_time = 2) OR (start_time = 2) OR (end_time = 1))'
  1222. >>> # Absolute time
  1223. >>> start = datetime(2001, 1, 1, 12, 30)
  1224. >>> end = datetime(2001, 3, 31, 14, 30)
  1225. >>> create_temporal_relation_sql_where_statement(start, end,
  1226. ... use_start=False)
  1227. >>> create_temporal_relation_sql_where_statement(start, end)
  1228. "((start_time >= '2001-01-01 12:30:00' and start_time < '2001-03-31 14:30:00') )"
  1229. >>> create_temporal_relation_sql_where_statement(start, end,
  1230. ... use_start=True)
  1231. "((start_time >= '2001-01-01 12:30:00' and start_time < '2001-03-31 14:30:00') )"
  1232. >>> create_temporal_relation_sql_where_statement(start, end,
  1233. ... use_start=False, use_during=True)
  1234. "(((start_time > '2001-01-01 12:30:00' and end_time < '2001-03-31 14:30:00') OR (start_time >= '2001-01-01 12:30:00' and end_time < '2001-03-31 14:30:00') OR (start_time > '2001-01-01 12:30:00' and end_time <= '2001-03-31 14:30:00')))"
  1235. >>> create_temporal_relation_sql_where_statement(start, end,
  1236. ... use_start=False, use_overlap=True)
  1237. "(((start_time < '2001-01-01 12:30:00' and end_time > '2001-01-01 12:30:00' and end_time < '2001-03-31 14:30:00') OR (start_time < '2001-03-31 14:30:00' and start_time > '2001-01-01 12:30:00' and end_time > '2001-03-31 14:30:00')))"
  1238. >>> create_temporal_relation_sql_where_statement(start, end,
  1239. ... use_start=False, use_contain=True)
  1240. "(((start_time < '2001-01-01 12:30:00' and end_time > '2001-03-31 14:30:00') OR (start_time <= '2001-01-01 12:30:00' and end_time > '2001-03-31 14:30:00') OR (start_time < '2001-01-01 12:30:00' and end_time >= '2001-03-31 14:30:00')))"
  1241. >>> create_temporal_relation_sql_where_statement(start, end,
  1242. ... use_start=False, use_equal=True)
  1243. "((start_time = '2001-01-01 12:30:00' and end_time = '2001-03-31 14:30:00'))"
  1244. >>> create_temporal_relation_sql_where_statement(start, end,
  1245. ... use_start=False, use_follows=True)
  1246. "((start_time = '2001-03-31 14:30:00'))"
  1247. >>> create_temporal_relation_sql_where_statement(start, end,
  1248. ... use_start=False, use_precedes=True)
  1249. "((end_time = '2001-01-01 12:30:00'))"
  1250. >>> create_temporal_relation_sql_where_statement(start, end,
  1251. ... use_start=True, use_during=True, use_overlap=True, use_contain=True,
  1252. ... use_equal=True, use_follows=True, use_precedes=True)
  1253. "((start_time >= '2001-01-01 12:30:00' and start_time < '2001-03-31 14:30:00') OR ((start_time > '2001-01-01 12:30:00' and end_time < '2001-03-31 14:30:00') OR (start_time >= '2001-01-01 12:30:00' and end_time < '2001-03-31 14:30:00') OR (start_time > '2001-01-01 12:30:00' and end_time <= '2001-03-31 14:30:00')) OR ((start_time < '2001-01-01 12:30:00' and end_time > '2001-01-01 12:30:00' and end_time < '2001-03-31 14:30:00') OR (start_time < '2001-03-31 14:30:00' and start_time > '2001-01-01 12:30:00' and end_time > '2001-03-31 14:30:00')) OR ((start_time < '2001-01-01 12:30:00' and end_time > '2001-03-31 14:30:00') OR (start_time <= '2001-01-01 12:30:00' and end_time > '2001-03-31 14:30:00') OR (start_time < '2001-01-01 12:30:00' and end_time >= '2001-03-31 14:30:00')) OR (start_time = '2001-01-01 12:30:00' and end_time = '2001-03-31 14:30:00') OR (start_time = '2001-03-31 14:30:00') OR (end_time = '2001-01-01 12:30:00'))"
  1254. @endcode
  1255. """
  1256. where = "("
  1257. if use_start:
  1258. if isinstance(start, datetime):
  1259. where += "(start_time >= '%s' and start_time < '%s') " % (start, end)
  1260. else:
  1261. where += "(start_time >= %i and start_time < %i) " % (start, end)
  1262. if use_during:
  1263. if use_start:
  1264. where += " OR "
  1265. if isinstance(start, datetime):
  1266. where += "((start_time > '%s' and end_time < '%s') OR " % (start, end)
  1267. where += "(start_time >= '%s' and end_time < '%s') OR " % (start, end)
  1268. where += "(start_time > '%s' and end_time <= '%s'))" % (start, end)
  1269. else:
  1270. where += "((start_time > %i and end_time < %i) OR " % (start, end)
  1271. where += "(start_time >= %i and end_time < %i) OR " % (start, end)
  1272. where += "(start_time > %i and end_time <= %i))" % (start, end)
  1273. if use_overlap:
  1274. if use_start or use_during:
  1275. where += " OR "
  1276. if isinstance(start, datetime):
  1277. where += "((start_time < '%s' and end_time > '%s' and end_time < '%s') OR " % (start, start, end)
  1278. where += "(start_time < '%s' and start_time > '%s' and end_time > '%s'))" % (end, start, end)
  1279. else:
  1280. where += "((start_time < %i and end_time > %i and end_time < %i) OR " % (start, start, end)
  1281. where += "(start_time < %i and start_time > %i and end_time > %i))" % (end, start, end)
  1282. if use_contain:
  1283. if use_start or use_during or use_overlap:
  1284. where += " OR "
  1285. if isinstance(start, datetime):
  1286. where += "((start_time < '%s' and end_time > '%s') OR " % (start, end)
  1287. where += "(start_time <= '%s' and end_time > '%s') OR " % (start, end)
  1288. where += "(start_time < '%s' and end_time >= '%s'))" % (start, end)
  1289. else:
  1290. where += "((start_time < %i and end_time > %i) OR " % (start, end)
  1291. where += "(start_time <= %i and end_time > %i) OR " % (start, end)
  1292. where += "(start_time < %i and end_time >= %i))" % (start, end)
  1293. if use_equal:
  1294. if use_start or use_during or use_overlap or use_contain:
  1295. where += " OR "
  1296. if isinstance(start, datetime):
  1297. where += "(start_time = '%s' and end_time = '%s')" % (start, end)
  1298. else:
  1299. where += "(start_time = %i and end_time = %i)" % (start, end)
  1300. if use_follows:
  1301. if use_start or use_during or use_overlap or use_contain or use_equal:
  1302. where += " OR "
  1303. if isinstance(start, datetime):
  1304. where += "(start_time = '%s')" % (end)
  1305. else:
  1306. where += "(start_time = %i)" % (end)
  1307. if use_precedes:
  1308. if use_start or use_during or use_overlap or use_contain or use_equal \
  1309. or use_follows:
  1310. where += " OR "
  1311. if isinstance(start, datetime):
  1312. where += "(end_time = '%s')" % (start)
  1313. else:
  1314. where += "(end_time = %i)" % (start)
  1315. where += ")"
  1316. # Catch empty where statement
  1317. if where == "()":
  1318. where = None
  1319. return where
  1320. ###############################################################################
  1321. if __name__ == "__main__":
  1322. import doctest
  1323. doctest.testmod()