abstract_space_time_dataset.py 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  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 3D Raster 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. @param dbif: The database interface to be used
  263. @return True if topology is correct
  264. """
  265. if maps is None:
  266. maps = self.get_registered_maps_as_objects(
  267. where=None, order="start_time", dbif=dbif)
  268. relations = count_temporal_topology_relationships(maps, maps)
  269. map_time = self.get_map_time()
  270. if map_time == "interval" or map_time == "mixed":
  271. if "equivalent" in relations:
  272. return False
  273. if "during" in relations:
  274. return False
  275. if "contains" in relations:
  276. return False
  277. if "overlaps" in relations:
  278. return False
  279. if "overlapped" in relations:
  280. return False
  281. if "starts" in relations:
  282. return False
  283. if "finishes" in relations:
  284. return False
  285. if "started" in relations:
  286. return False
  287. if "finished" in relations:
  288. return False
  289. elif map_time == "point":
  290. if "equivalent" in relations:
  291. return False
  292. else:
  293. return False
  294. return True
  295. def sample_by_dataset(self, stds, method=None, spatial=False, dbif=None):
  296. """!Sample this space time dataset with the temporal topology
  297. of a second space time dataset
  298. The sample dataset must have "interval" as temporal map type,
  299. so all sample maps have valid interval time.
  300. In case spatial is True, the spatial overlap between
  301. temporal related maps is performed. Only
  302. temporal related and spatial overlapping maps are returned.
  303. Return all registered maps as ordered (by start_time) object list with
  304. "gap" map objects (id==None). Each list entry is a list of map objects
  305. which are potentially located in temporal relation to the actual
  306. granule of the second space time dataset.
  307. Each entry in the object list is a dict. The actual sampler
  308. map and its temporal extent (the actual granule) and
  309. the list of samples are stored:
  310. @code
  311. list = self.sample_by_dataset(stds=sampler, method=[
  312. "during","overlap","contain","equal"])
  313. for entry in list:
  314. granule = entry["granule"]
  315. maplist = entry["samples"]
  316. for map in maplist:
  317. map.select()
  318. map.print_info()
  319. @endcode
  320. A valid temporal topology (no overlapping or inclusion allowed)
  321. is needed to get correct results in case of gaps in the sample dataset.
  322. Gaps between maps are identified as unregistered maps with id==None.
  323. The map objects are initialized with the id and the temporal
  324. extent of the granule (temporal type, start time, end time).
  325. In case more map information are needed, use the select()
  326. method for each listed object.
  327. @param stds: The space time dataset to be used for temporal sampling
  328. @param method: This option specifies what sample method should be used.
  329. In case the registered maps are of temporal point type,
  330. only the start time is used for sampling. In case of mixed
  331. of interval data the user can chose between:
  332. - start: Select maps of which the start time is
  333. located in the selection granule
  334. @verbatim
  335. map : s
  336. granule: s-----------------e
  337. map : s--------------------e
  338. granule: s-----------------e
  339. map : s--------e
  340. granule: s-----------------e
  341. @endverbatim
  342. - during: Select maps which are temporal
  343. during the selection granule
  344. @verbatim
  345. map : s-----------e
  346. granule: s-----------------e
  347. @endverbatim
  348. - overlap: Select maps which temporal overlap
  349. the selection granule
  350. @verbatim
  351. map : s-----------e
  352. granule: s-----------------e
  353. map : s-----------e
  354. granule: s----------e
  355. @endverbatim
  356. - contain: Select maps which temporally contain
  357. the selection granule
  358. @verbatim
  359. map : s-----------------e
  360. granule: s-----------e
  361. @endverbatim
  362. - equal: Select maps which temporally equal
  363. to the selection granule
  364. @verbatim
  365. map : s-----------e
  366. granule: s-----------e
  367. @endverbatim
  368. - follows: Select maps which temporally follow
  369. the selection granule
  370. @verbatim
  371. map : s-----------e
  372. granule: s-----------e
  373. @endverbatim
  374. - precedes: Select maps which temporally precedes
  375. the selection granule
  376. @verbatim
  377. map : s-----------e
  378. granule: s-----------e
  379. @endverbatim
  380. All these methods can be combined. Method must be of
  381. type tuple including the identification strings.
  382. @param spatial: If set True additional the spatial overlapping
  383. is used for selection -> spatio-temporal relation.
  384. The returned map objects will have temporal and
  385. spatial extents
  386. @param dbif: The database interface to be used
  387. In case nothing found None is returned
  388. """
  389. use_start = False
  390. use_during = False
  391. use_overlap = False
  392. use_contain = False
  393. use_equal = False
  394. use_follows = False
  395. use_precedes = False
  396. # Initialize the methods
  397. if method is not None:
  398. for name in method:
  399. if name == "start":
  400. use_start = True
  401. if name == "during":
  402. use_during = True
  403. if name == "overlap":
  404. use_overlap = True
  405. if name == "contain":
  406. use_contain = True
  407. if name == "equal":
  408. use_equal = True
  409. if name == "follows":
  410. use_follows = True
  411. if name == "precedes":
  412. use_precedes = True
  413. else:
  414. use_during = True
  415. use_overlap = True
  416. use_contain = True
  417. use_equal = True
  418. if self.get_temporal_type() != stds.get_temporal_type():
  419. core.error(_("The space time datasets must be of "
  420. "the same temporal type"))
  421. return None
  422. if stds.get_map_time() != "interval":
  423. core.error(_("The temporal map type of the sample "
  424. "dataset must be interval"))
  425. return None
  426. # In case points of time are available, disable the interval specific methods
  427. if self.get_map_time() == "point":
  428. use_start = True
  429. use_during = False
  430. use_overlap = False
  431. use_contain = False
  432. use_equal = False
  433. use_follows = False
  434. use_precedes = False
  435. dbif, connect = init_dbif(dbif)
  436. obj_list = []
  437. sample_maps = stds.get_registered_maps_as_objects_with_gaps(
  438. where=None, dbif=dbif)
  439. for granule in sample_maps:
  440. # Read the spatial extent
  441. if spatial:
  442. granule.spatial_extent.select(dbif)
  443. start, end = granule.get_valid_time()
  444. where = create_temporal_relation_sql_where_statement(
  445. start, end, use_start,
  446. use_during, use_overlap, use_contain, use_equal, use_follows, use_precedes)
  447. maps = self.get_registered_maps_as_objects(
  448. where, "start_time", dbif)
  449. result = {}
  450. result["granule"] = granule
  451. num_samples = 0
  452. maplist = []
  453. if maps is not None:
  454. for map in maps:
  455. # Read the spatial extent
  456. if spatial:
  457. map.spatial_extent.select(dbif)
  458. # Ignore spatial disjoint maps
  459. if not granule.spatial_overlapping(map):
  460. continue
  461. num_samples += 1
  462. maplist.append(copy.copy(map))
  463. # Fill with empty map in case no spatio-temporal relations found
  464. if maps is None or num_samples == 0:
  465. map = self.get_new_map_instance(None)
  466. if self.is_time_absolute():
  467. map.set_absolute_time(start, end)
  468. elif self.is_time_relative():
  469. map.set_relative_time(start, end,
  470. self.get_relative_time_unit())
  471. maplist.append(copy.copy(map))
  472. result["samples"] = maplist
  473. obj_list.append(copy.copy(result))
  474. if connect:
  475. dbif.close()
  476. return obj_list
  477. def get_registered_maps_as_objects_by_granularity(self, gran=None, dbif=None):
  478. """!Return all registered maps as ordered (by start_time) object list with
  479. "gap" map objects (id==None) for temporal topological operations using
  480. the granularity of the space time dataset as increment.
  481. Each list entry is a list of map objects
  482. which are potentially located in the actual granule.
  483. A valid temporal topology (no overlapping or inclusion allowed)
  484. is needed to get correct results.
  485. The dataset must have "interval" as temporal map type,
  486. so all maps have valid interval time.
  487. Gaps between maps are identified as unregistered maps with id==None.
  488. The objects are initialized with the id and the temporal
  489. extent (temporal type, start time, end time).
  490. In case more map information are needed, use the select()
  491. method for each listed object.
  492. @param gran: The granularity to be used
  493. @param dbif: The database interface to be used
  494. @return ordered object list, in case nothing found None is returned
  495. """
  496. dbif, connect = init_dbif(dbif)
  497. obj_list = []
  498. if self.get_map_time() == "point" or self.get_map_time() == "mixed":
  499. core.error(_("The space time %(type)s dataset <%(name)s> must have"
  500. " interval time"%\
  501. {"type":self.get_new_map_instance(None).get_type(),
  502. "name":self.get_id()}))
  503. if gran is None:
  504. gran = self.get_granularity()
  505. start, end = self.get_valid_time()
  506. while start < end:
  507. if self.is_time_absolute():
  508. next = increment_datetime_by_string(start, gran)
  509. else:
  510. next = start + gran
  511. where = "(start_time <= '%s' and end_time >= '%s')" % (start, next)
  512. rows = self.get_registered_maps("id", where, "start_time", dbif)
  513. if rows is not None and len(rows) != 0:
  514. if len(rows) > 1:
  515. core.warning(_("More than one map found in a granule. "
  516. "Temporal granularity seems to be invalid or"
  517. " the chosen granularity is not a greatest "
  518. "common divider of all intervals and gaps "
  519. "in the dataset."))
  520. maplist = []
  521. for row in rows:
  522. # Take the first map
  523. map = self.get_new_map_instance(rows[0]["id"])
  524. if self.is_time_absolute():
  525. map.set_absolute_time(start, next)
  526. elif self.is_time_relative():
  527. map.set_relative_time(start, next,
  528. self.get_relative_time_unit())
  529. maplist.append(copy.copy(map))
  530. obj_list.append(copy.copy(maplist))
  531. else:
  532. # Found a gap
  533. map = self.get_new_map_instance(None)
  534. if self.is_time_absolute():
  535. map.set_absolute_time(start, next)
  536. elif self.is_time_relative():
  537. map.set_relative_time(start, next,
  538. self.get_relative_time_unit())
  539. maplist = []
  540. maplist.append(copy.copy(map))
  541. obj_list.append(copy.copy(maplist))
  542. start = next
  543. if connect:
  544. dbif.close()
  545. if obj_list:
  546. return obj_list
  547. return None
  548. def get_registered_maps_as_objects_with_gaps(self, where=None, dbif=None):
  549. """!Return all registered maps as ordered (by start_time) object list with
  550. "gap" map objects (id==None) for temporal topological operations
  551. Gaps between maps are identified as maps with id==None
  552. The objects are initialized with the id and the
  553. temporal extent (temporal type, start time, end time).
  554. In case more map information are needed, use the select()
  555. method for each listed object.
  556. @param where: The SQL where statement to select a
  557. subset of the registered maps without "WHERE"
  558. @param dbif: The database interface to be used
  559. @return ordered object list, in case nothing found None is returned
  560. """
  561. dbif, connect = init_dbif(dbif)
  562. obj_list = []
  563. maps = self.get_registered_maps_as_objects(where, "start_time", dbif)
  564. if maps is not None and len(maps) > 0:
  565. for i in range(len(maps)):
  566. obj_list.append(maps[i])
  567. # Detect and insert gaps
  568. if i < len(maps) - 1:
  569. relation = maps[i + 1].temporal_relation(maps[i])
  570. if relation == "after":
  571. start1, end1 = maps[i].get_valid_time()
  572. start2, end2 = maps[i + 1].get_valid_time()
  573. end = start2
  574. if end1 is not None:
  575. start = end1
  576. else:
  577. start = start1
  578. map = self.get_new_map_instance(None)
  579. if self.is_time_absolute():
  580. map.set_absolute_time(start, end)
  581. elif self.is_time_relative():
  582. map.set_relative_time(start, end,
  583. self.get_relative_time_unit())
  584. obj_list.append(copy.copy(map))
  585. if connect:
  586. dbif.close()
  587. return obj_list
  588. def get_registered_maps_as_objects(self, where=None, order="start_time",
  589. dbif=None):
  590. """!Return all registered maps as ordered object list for
  591. temporal topological operations
  592. The objects are initialized with the id and the temporal extent
  593. (temporal type, start time, end time).
  594. In case more map information are needed, use the select()
  595. method for each listed object.
  596. @param where: The SQL where statement to select a subset of
  597. the registered maps without "WHERE"
  598. @param order: The SQL order statement to be used to order the
  599. objects in the list without "ORDER BY"
  600. @param dbif: The database interface to be used
  601. @return The ordered map object list,
  602. In case nothing found None is returned
  603. """
  604. dbif, connect = init_dbif(dbif)
  605. obj_list = []
  606. rows = self.get_registered_maps(
  607. "id,start_time,end_time", where, order, dbif)
  608. count = 0
  609. if rows is not None:
  610. for row in rows:
  611. core.percent(count, len(rows), 1)
  612. map = self.get_new_map_instance(row["id"])
  613. if self.is_time_absolute():
  614. map.set_absolute_time(row["start_time"], row["end_time"])
  615. elif self.is_time_relative():
  616. map.set_relative_time(row["start_time"], row["end_time"],
  617. self.get_relative_time_unit())
  618. obj_list.append(copy.copy(map))
  619. count += 1
  620. core.percent(1, 1, 1)
  621. if connect:
  622. dbif.close()
  623. return obj_list
  624. def get_registered_maps(self, columns=None, where=None, order=None, dbif=None):
  625. """!Return SQL rows of all registered maps.
  626. In case columns are not specified, each row includes all columns
  627. specified in the datatype specific view.
  628. @param columns: Columns to be selected as SQL compliant string
  629. @param where: The SQL where statement to select a subset
  630. of the registered maps without "WHERE"
  631. @param order: The SQL order statement to be used to order the
  632. objects in the list without "ORDER BY"
  633. @param dbif: The database interface to be used
  634. @return SQL rows of all registered maps,
  635. In case nothing found None is returned
  636. """
  637. dbif, connect = init_dbif(dbif)
  638. rows = None
  639. if self.get_map_register() is not None:
  640. # Use the correct temporal table
  641. if self.get_temporal_type() == "absolute":
  642. map_view = self.get_new_map_instance(
  643. None).get_type() + "_view_abs_time"
  644. else:
  645. map_view = self.get_new_map_instance(
  646. None).get_type() + "_view_rel_time"
  647. if columns is not None and columns != "":
  648. sql = "SELECT %s FROM %s WHERE %s.id IN (SELECT id FROM %s)" %\
  649. (columns, map_view, map_view, self.get_map_register())
  650. else:
  651. sql = "SELECT * FROM %s WHERE %s.id IN (SELECT id FROM %s)" % \
  652. (map_view, map_view, self.get_map_register())
  653. if where is not None and where != "":
  654. sql += " AND (%s)" % (where.split(";")[0])
  655. if order is not None and order != "":
  656. sql += " ORDER BY %s" % (order.split(";")[0])
  657. try:
  658. dbif.cursor.execute(sql)
  659. rows = dbif.cursor.fetchall()
  660. except:
  661. if connect:
  662. dbif.close()
  663. core.error(_("Unable to get map ids from register table <%s>")
  664. % (self.get_map_register()))
  665. raise
  666. if connect:
  667. dbif.close()
  668. return rows
  669. def delete(self, dbif=None, execute=True):
  670. """!Delete a space time dataset from the temporal database
  671. This method removes the space time dataset from the temporal
  672. database and drops its map register table
  673. @param dbif: The database interface to be used
  674. @param execute: If True the SQL DELETE and DROP table
  675. statements will be executed.
  676. If False the prepared SQL statements are returned
  677. and must be executed by the caller.
  678. @return The SQL statements if execute == False, else an empty string
  679. """
  680. # First we need to check if maps are registered in this dataset and
  681. # unregister them
  682. core.verbose(_("Delete space time %s dataset <%s> from temporal "
  683. "database") % \
  684. (self.get_new_map_instance(ident=None).get_type(),
  685. self.get_id()))
  686. statement = ""
  687. dbif, connect = init_dbif(dbif)
  688. # SELECT all needed information from the database
  689. self.metadata.select(dbif)
  690. if self.get_map_register() is not None:
  691. core.verbose(_("Drop map register table: %s") % (
  692. self.get_map_register()))
  693. rows = self.get_registered_maps("id", None, None, dbif)
  694. # Unregister each registered map in the table
  695. if rows is not None:
  696. num_maps = len(rows)
  697. count = 0
  698. for row in rows:
  699. core.percent(count, num_maps, 1)
  700. # Unregister map
  701. map = self.get_new_map_instance(row["id"])
  702. statement += self.unregister_map(
  703. map=map, dbif=dbif, execute=False)
  704. count += 1
  705. core.percent(1, 1, 1)
  706. # Safe the DROP table statement
  707. statement += "DROP TABLE " + self.get_map_register() + ";\n"
  708. # Remove the primary key, the foreign keys will be removed by trigger
  709. statement += self.base.get_delete_statement()
  710. if execute:
  711. dbif.execute_transaction(statement)
  712. self.reset(None)
  713. if connect:
  714. dbif.close()
  715. if execute:
  716. return ""
  717. return statement
  718. def register_map(self, map, dbif=None):
  719. """!Register a map in the space time dataset.
  720. This method takes care of the registration of a map
  721. in a space time dataset.
  722. In case the map is already registered this function
  723. will break with a warning and return False.
  724. @param dbif: The database interface to be used
  725. """
  726. dbif, connect = init_dbif(dbif)
  727. if map.is_in_db(dbif) == False:
  728. dbif.close()
  729. core.fatal(_("Only maps with absolute or relative valid time can "
  730. "be registered"))
  731. if map.get_layer():
  732. core.verbose(_("Register %s map <%s> with layer %s in space "
  733. "time %s dataset <%s>") % (map.get_type(),
  734. map.get_map_id(),
  735. map.get_layer(),
  736. map.get_type(),
  737. self.get_id()))
  738. else:
  739. core.verbose(_("Register %s map <%s> in space time %s "
  740. "dataset <%s>") % (map.get_type(), map.get_map_id(),
  741. map.get_type(), self.get_id()))
  742. # First select all data from the database
  743. map.select(dbif)
  744. if not map.check_valid_time():
  745. if map.get_layer():
  746. core.fatal(_("Map <%s> with layer %s has invalid time")
  747. % (map.get_map_id(), map.get_layer()))
  748. else:
  749. core.fatal(_("Map <%s> has invalid time") % (map.get_map_id()))
  750. map_id = map.base.get_id()
  751. map_name = map.base.get_name()
  752. map_mapset = map.base.get_mapset()
  753. map_register_table = map.get_stds_register()
  754. map_rel_time_unit = map.get_relative_time_unit()
  755. map_ttype = map.get_temporal_type()
  756. #print "Map register table", map_register_table
  757. # Get basic info
  758. stds_name = self.base.get_name()
  759. stds_mapset = self.base.get_mapset()
  760. stds_register_table = self.get_map_register()
  761. stds_ttype = self.get_temporal_type()
  762. # The gathered SQL statemets are stroed here
  763. statement = ""
  764. # Check temporal types
  765. if stds_ttype != map_ttype:
  766. if map.get_layer():
  767. core.fatal(_("Temporal type of space time dataset <%s> and "
  768. "map <%s> with layer %s are different") % \
  769. (self.get_id(), map.get_map_id(), map.get_layer()))
  770. else:
  771. core.fatal(_("Temporal type of space time dataset <%s> and "
  772. "map <%s> are different") % \
  773. (self.get_id(), map.get_map_id()))
  774. # In case no map has been registered yet, set the
  775. # relative time unit from the first map
  776. if (self.metadata.get_number_of_maps() is None or \
  777. self.metadata.get_number_of_maps() == 0) and \
  778. self.map_counter == 0 and self.is_time_relative():
  779. self.set_relative_time_unit(map_rel_time_unit)
  780. statement += self.relative_time.get_update_all_statement_mogrified(
  781. dbif)
  782. core.verbose(_("Set temporal unit for space time %s dataset "
  783. "<%s> to %s") % (map.get_type(), self.get_id(),
  784. map_rel_time_unit))
  785. stds_rel_time_unit = self.get_relative_time_unit()
  786. # Check the relative time unit
  787. if self.is_time_relative() and (stds_rel_time_unit != map_rel_time_unit):
  788. if map.get_layer():
  789. core.fatal(_("Relative time units of space time dataset "
  790. "<%s> and map <%s> with layer %s are different") %\
  791. (self.get_id(), map.get_map_id(), map.get_layer()))
  792. else:
  793. core.fatal(_("Relative time units of space time dataset "
  794. "<%s> and map <%s> are different") % \
  795. (self.get_id(), map.get_map_id()))
  796. #print "STDS register table", stds_register_table
  797. if stds_mapset != map_mapset:
  798. dbif.close()
  799. core.fatal(_("Only maps from the same mapset can be registered"))
  800. # Check if map is already registered
  801. if stds_register_table is not None:
  802. if dbmi.paramstyle == "qmark":
  803. sql = "SELECT id FROM " + \
  804. stds_register_table + " WHERE id = (?)"
  805. else:
  806. sql = "SELECT id FROM " + \
  807. stds_register_table + " WHERE id = (%s)"
  808. try:
  809. dbif.cursor.execute(sql, (map_id,))
  810. row = dbif.cursor.fetchone()
  811. except:
  812. row = None
  813. core.warning(_("Error in strds_register_table request"))
  814. raise
  815. if row is not None and row[0] == map_id:
  816. if connect == True:
  817. dbif.close()
  818. if map.get_layer() is not None:
  819. core.warning(_("Map <%s> with layer %s is already "
  820. "registered.") % (map.get_map_id(),
  821. map.get_layer()))
  822. else:
  823. core.warning(_("Map <%s> is already registered.")
  824. % (map.get_map_id()))
  825. return ""
  826. # Create tables
  827. sql_path = get_sql_template_path()
  828. # We need to create the map raster register table precedes we can register the map
  829. if map_register_table is None:
  830. # Create a unique id
  831. uuid_rand = "map_" + str(uuid.uuid4()).replace("-", "")
  832. map_register_table = uuid_rand + "_" + \
  833. self.get_type() + "_register"
  834. # Read the SQL template
  835. sql = open(os.path.join(sql_path,
  836. "map_stds_register_table_template.sql"),
  837. 'r').read()
  838. # Create the raster, raster3d and vector tables
  839. sql = sql.replace("GRASS_MAP", map.get_type())
  840. sql = sql.replace("MAP_NAME", map_name + "_" + map_mapset)
  841. sql = sql.replace("TABLE_NAME", uuid_rand)
  842. sql = sql.replace("MAP_ID", map_id)
  843. sql = sql.replace("STDS", self.get_type())
  844. statement += sql
  845. # Set the stds register table name and put it into the DB
  846. map.set_stds_register(map_register_table)
  847. statement += map.metadata.get_update_statement_mogrified(dbif)
  848. if map.get_layer():
  849. core.verbose(_("Created register table <%s> for "
  850. "%s map <%s> with layer %s") %
  851. (map_register_table, map.get_type(),
  852. map.get_map_id(), map.get_layer()))
  853. else:
  854. core.verbose(_("Created register table <%s> for %s map <%s>") %
  855. (map_register_table, map.get_type(),
  856. map.get_map_id()))
  857. # We need to create the table and register it
  858. if stds_register_table is None:
  859. # Create table name
  860. stds_register_table = stds_name + "_" + \
  861. stds_mapset + "_" + map.get_type() + "_register"
  862. # Read the SQL template
  863. sql = open(os.path.join(sql_path,
  864. "stds_map_register_table_template.sql"),
  865. 'r').read()
  866. # Create the raster, raster3d and vector tables
  867. sql = sql.replace("GRASS_MAP", map.get_type())
  868. sql = sql.replace("SPACETIME_NAME", stds_name + "_" + stds_mapset)
  869. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  870. sql = sql.replace("STDS", self.get_type())
  871. statement += sql
  872. # Set the map register table name and put it into the DB
  873. self.set_map_register(stds_register_table)
  874. statement += self.metadata.get_update_statement_mogrified(dbif)
  875. core.verbose(_("Created register table <%s> for space "
  876. "time %s dataset <%s>") %
  877. (stds_register_table, map.get_type(), self.get_id()))
  878. # We need to execute the statement at this time
  879. if statement != "":
  880. dbif.execute_transaction(statement)
  881. statement = ""
  882. # Register the stds in the map stds register table
  883. # Check if the entry is already there
  884. if dbmi.paramstyle == "qmark":
  885. sql = "SELECT id FROM " + map_register_table + " WHERE id = ?"
  886. else:
  887. sql = "SELECT id FROM " + map_register_table + " WHERE id = %s"
  888. try:
  889. dbif.cursor.execute(sql, (self.base.get_id(),))
  890. row = dbif.cursor.fetchone()
  891. except:
  892. row = None
  893. # In case of no entry make a new one
  894. if row is None:
  895. if dbmi.paramstyle == "qmark":
  896. sql = "INSERT INTO " + map_register_table + \
  897. " (id) " + "VALUES (?);\n"
  898. else:
  899. sql = "INSERT INTO " + map_register_table + \
  900. " (id) " + "VALUES (%s);\n"
  901. statement += dbif.mogrify_sql_statement(
  902. (sql, (self.base.get_id(),)))
  903. # Now put the raster name in the stds map register table
  904. if dbmi.paramstyle == "qmark":
  905. sql = "INSERT INTO " + stds_register_table + \
  906. " (id) " + "VALUES (?);\n"
  907. else:
  908. sql = "INSERT INTO " + stds_register_table + \
  909. " (id) " + "VALUES (%s);\n"
  910. statement += dbif.mogrify_sql_statement((sql, (map_id,)))
  911. # Now execute the insert transaction
  912. dbif.execute_transaction(statement)
  913. if connect:
  914. dbif.close()
  915. # increase the counter
  916. self.map_counter += 1
  917. def unregister_map(self, map, dbif=None, execute=True):
  918. """!Unregister a map from the space time dataset.
  919. This method takes care of the un-registration of a map
  920. from a space time dataset.
  921. @param map: The map object to unregister
  922. @param dbif: The database interface to be used
  923. @param execute: If True the SQL DELETE and DROP table
  924. statements will be executed.
  925. If False the prepared SQL statements are
  926. returned and must be executed by the caller.
  927. @return The SQL statements if execute == False, else an empty
  928. string, None in case of a failure
  929. """
  930. statement = ""
  931. dbif, connect = init_dbif(dbif)
  932. # First select needed data from the database
  933. map.metadata.select(dbif)
  934. map_id = map.get_id()
  935. map_register_table = map.get_stds_register()
  936. stds_register_table = self.get_map_register()
  937. if map.get_layer() is not None:
  938. core.verbose(_("Unregister %s map <%s> with layer %s") % \
  939. (map.get_type(), map.get_map_id(), map.get_layer()))
  940. else:
  941. core.verbose(_("Unregister %s map <%s>") % (
  942. map.get_type(), map.get_map_id()))
  943. # Check if the map is registered in the space time raster dataset
  944. if map_register_table is not None:
  945. if dbmi.paramstyle == "qmark":
  946. sql = "SELECT id FROM " + map_register_table + " WHERE id = ?"
  947. else:
  948. sql = "SELECT id FROM " + map_register_table + " WHERE id = %s"
  949. try:
  950. dbif.cursor.execute(sql, (self.base.get_id(),))
  951. row = dbif.cursor.fetchone()
  952. except:
  953. row = None
  954. # Break if the map is not registered
  955. if row is None:
  956. if map.get_layer() is not None:
  957. core.warning(_("Map <%s> with layer %s is not registered "
  958. "in space time dataset <%s>") % \
  959. (map.get_map_id(), map.get_layer(),
  960. self.base.get_id()))
  961. else:
  962. core.warning(_("Map <%s> is not registered in space "
  963. "time dataset <%s>") % (map.get_map_id(),
  964. self.base.get_id()))
  965. if connect == True:
  966. dbif.close()
  967. return ""
  968. # Remove the space time raster dataset from the raster dataset register
  969. if map_register_table is not None:
  970. if dbmi.paramstyle == "qmark":
  971. sql = "DELETE FROM " + map_register_table + " WHERE id = ?;\n"
  972. else:
  973. sql = "DELETE FROM " + map_register_table + " WHERE id = %s;\n"
  974. statement += dbif.mogrify_sql_statement(
  975. (sql, (self.base.get_id(),)))
  976. # Remove the raster map from the space time raster dataset register
  977. if stds_register_table is not None:
  978. if dbmi.paramstyle == "qmark":
  979. sql = "DELETE FROM " + stds_register_table + " WHERE id = ?;\n"
  980. else:
  981. sql = "DELETE FROM " + \
  982. stds_register_table + " WHERE id = %s;\n"
  983. statement += dbif.mogrify_sql_statement((sql, (map_id,)))
  984. if execute:
  985. dbif.execute_transaction(statement)
  986. if connect:
  987. dbif.close()
  988. # decrease the counter
  989. self.map_counter -= 1
  990. if execute:
  991. return ""
  992. return statement
  993. def update_from_registered_maps(self, dbif=None):
  994. """!This methods updates the spatial and temporal extent as well as
  995. type specific metadata. It should always been called after maps are registered
  996. or unregistered/deleted from the space time dataset.
  997. The update of the temporal extent checks if the end time is set correctly.
  998. In case the registered maps have no valid end time (None) the maximum start time
  999. will be used. If the end time is earlier than the maximum start time, it will
  1000. be replaced by the maximum start time.
  1001. An other solution to automate this is to use the deactivated trigger
  1002. in the SQL files. But this will result in a huge performance issue
  1003. in case many maps are registered (>1000).
  1004. @param dbif: The database interface to be used
  1005. """
  1006. core.verbose(_("Update metadata, spatial and temporal extent from "
  1007. "all registered maps of <%s>") % (self.get_id()))
  1008. # Nothing to do if the register is not present
  1009. if not self.get_map_register():
  1010. return
  1011. dbif, connect = init_dbif(dbif)
  1012. map_time = None
  1013. use_start_time = False
  1014. # Get basic info
  1015. stds_name = self.base.get_name()
  1016. stds_mapset = self.base.get_mapset()
  1017. sql_path = get_sql_template_path()
  1018. #We create a transaction
  1019. sql_script = ""
  1020. # Update the spatial and temporal extent from registered maps
  1021. # Read the SQL template
  1022. sql = open(os.path.join(sql_path,
  1023. "update_stds_spatial_temporal_extent_template.sql"),
  1024. 'r').read()
  1025. sql = sql.replace(
  1026. "GRASS_MAP", self.get_new_map_instance(None).get_type())
  1027. sql = sql.replace("SPACETIME_NAME", stds_name + "_" + stds_mapset)
  1028. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  1029. sql = sql.replace("STDS", self.get_type())
  1030. sql_script += sql
  1031. sql_script += "\n"
  1032. # Update type specific metadata
  1033. sql = open(os.path.join(sql_path, "update_" +
  1034. self.get_type() + "_metadata_template.sql"), 'r').read()
  1035. sql = sql.replace(
  1036. "GRASS_MAP", self.get_new_map_instance(None).get_type())
  1037. sql = sql.replace("SPACETIME_NAME", stds_name + "_" + stds_mapset)
  1038. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  1039. sql = sql.replace("STDS", self.get_type())
  1040. sql_script += sql
  1041. sql_script += "\n"
  1042. dbif.execute_transaction(sql_script)
  1043. # Read and validate the selected end time
  1044. self.select()
  1045. if self.is_time_absolute():
  1046. start_time, end_time, tz = self.get_absolute_time()
  1047. else:
  1048. start_time, end_time, unit = self.get_relative_time()
  1049. # In case no end time is set, use the maximum start time of
  1050. # all registered maps as end time
  1051. if end_time is None:
  1052. use_start_time = True
  1053. else:
  1054. # Check if the end time is smaller than the maximum start time
  1055. if self.is_time_absolute():
  1056. sql = """SELECT max(start_time) FROM GRASS_MAP_absolute_time
  1057. WHERE GRASS_MAP_absolute_time.id IN
  1058. (SELECT id FROM SPACETIME_NAME_GRASS_MAP_register);"""
  1059. sql = sql.replace("GRASS_MAP", self.get_new_map_instance(
  1060. None).get_type())
  1061. sql = sql.replace("SPACETIME_NAME",
  1062. stds_name + "_" + stds_mapset)
  1063. else:
  1064. sql = """SELECT max(start_time) FROM GRASS_MAP_relative_time
  1065. WHERE GRASS_MAP_relative_time.id IN
  1066. (SELECT id FROM SPACETIME_NAME_GRASS_MAP_register);"""
  1067. sql = sql.replace("GRASS_MAP", self.get_new_map_instance(
  1068. None).get_type())
  1069. sql = sql.replace("SPACETIME_NAME",
  1070. stds_name + "_" + stds_mapset)
  1071. dbif.cursor.execute(sql)
  1072. row = dbif.cursor.fetchone()
  1073. if row is not None:
  1074. # This seems to be a bug in sqlite3 Python driver
  1075. if dbmi.__name__ == "sqlite3":
  1076. tstring = row[0]
  1077. # Convert the unicode string into the datetime format
  1078. if self.is_time_absolute():
  1079. if tstring.find(":") > 0:
  1080. time_format = "%Y-%m-%d %H:%M:%S"
  1081. else:
  1082. time_format = "%Y-%m-%d"
  1083. max_start_time = datetime.strptime(
  1084. tstring, time_format)
  1085. else:
  1086. max_start_time = row[0]
  1087. else:
  1088. max_start_time = row[0]
  1089. if end_time < max_start_time:
  1090. use_start_time = True
  1091. # Set the maximum start time as end time
  1092. if use_start_time:
  1093. if self.is_time_absolute():
  1094. sql = """UPDATE STDS_absolute_time SET end_time =
  1095. (SELECT max(start_time) FROM GRASS_MAP_absolute_time WHERE
  1096. GRASS_MAP_absolute_time.id IN
  1097. (SELECT id FROM SPACETIME_NAME_GRASS_MAP_register)
  1098. ) WHERE id = 'SPACETIME_ID';"""
  1099. sql = sql.replace("GRASS_MAP", self.get_new_map_instance(
  1100. None).get_type())
  1101. sql = sql.replace("SPACETIME_NAME",
  1102. stds_name + "_" + stds_mapset)
  1103. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  1104. sql = sql.replace("STDS", self.get_type())
  1105. elif self.is_time_relative():
  1106. sql = """UPDATE STDS_relative_time SET end_time =
  1107. (SELECT max(start_time) FROM GRASS_MAP_relative_time WHERE
  1108. GRASS_MAP_relative_time.id IN
  1109. (SELECT id FROM SPACETIME_NAME_GRASS_MAP_register)
  1110. ) WHERE id = 'SPACETIME_ID';"""
  1111. sql = sql.replace("GRASS_MAP", self.get_new_map_instance(
  1112. None).get_type())
  1113. sql = sql.replace("SPACETIME_NAME",
  1114. stds_name + "_" + stds_mapset)
  1115. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  1116. sql = sql.replace("STDS", self.get_type())
  1117. dbif.execute_transaction(sql)
  1118. # Count the temporal map types
  1119. maps = self.get_registered_maps_as_objects(dbif=dbif)
  1120. tlist = self.count_temporal_types(maps)
  1121. if tlist["interval"] > 0 and tlist["point"] == 0 and \
  1122. tlist["invalid"] == 0:
  1123. map_time = "interval"
  1124. elif tlist["interval"] == 0 and tlist["point"] > 0 and \
  1125. tlist["invalid"] == 0:
  1126. map_time = "point"
  1127. elif tlist["interval"] > 0 and tlist["point"] > 0 and \
  1128. tlist["invalid"] == 0:
  1129. map_time = "mixed"
  1130. else:
  1131. map_time = "invalid"
  1132. # Compute the granularity
  1133. if map_time != "invalid":
  1134. # Smallest supported temporal resolution
  1135. if self.is_time_absolute():
  1136. gran = compute_absolute_time_granularity(maps)
  1137. elif self.is_time_relative():
  1138. gran = compute_relative_time_granularity(maps)
  1139. else:
  1140. gran = None
  1141. # Set the map time type and update the time objects
  1142. if self.is_time_absolute():
  1143. self.absolute_time.select(dbif)
  1144. self.metadata.select(dbif)
  1145. if self.metadata.get_number_of_maps() > 0:
  1146. self.absolute_time.set_map_time(map_time)
  1147. self.absolute_time.set_granularity(gran)
  1148. else:
  1149. self.absolute_time.set_map_time(None)
  1150. self.absolute_time.set_granularity(None)
  1151. self.absolute_time.update_all(dbif)
  1152. else:
  1153. self.relative_time.select(dbif)
  1154. self.metadata.select(dbif)
  1155. if self.metadata.get_number_of_maps() > 0:
  1156. self.relative_time.set_map_time(map_time)
  1157. self.relative_time.set_granularity(gran)
  1158. else:
  1159. self.relative_time.set_map_time(None)
  1160. self.relative_time.set_granularity(None)
  1161. self.relative_time.update_all(dbif)
  1162. if connect:
  1163. dbif.close()
  1164. ###############################################################################
  1165. def create_temporal_relation_sql_where_statement(
  1166. start, end, use_start=True, use_during=False,
  1167. use_overlap=False, use_contain=False, use_equal=False,
  1168. use_follows=False, use_precedes=False):
  1169. """!Create a SQL WHERE statement for temporal relation selection of maps in space time datasets
  1170. @param start: The start time
  1171. @param end: The end time
  1172. @param use_start: Select maps of which the start time is located in the selection granule
  1173. @verbatim
  1174. map : s
  1175. granule: s-----------------e
  1176. map : s--------------------e
  1177. granule: s-----------------e
  1178. map : s--------e
  1179. granule: s-----------------e
  1180. @endverbatim
  1181. @param use_during: during: Select maps which are temporal during the selection granule
  1182. @verbatim
  1183. map : s-----------e
  1184. granule: s-----------------e
  1185. @endverbatim
  1186. @param use_overlap: Select maps which temporal overlap the selection granule
  1187. @verbatim
  1188. map : s-----------e
  1189. granule: s-----------------e
  1190. map : s-----------e
  1191. granule: s----------e
  1192. @endverbatim
  1193. @param use_contain: Select maps which temporally contain the selection granule
  1194. @verbatim
  1195. map : s-----------------e
  1196. granule: s-----------e
  1197. @endverbatim
  1198. @param use_equal: Select maps which temporally equal to the selection granule
  1199. @verbatim
  1200. map : s-----------e
  1201. granule: s-----------e
  1202. @endverbatim
  1203. @param use_follows: Select maps which temporally follow the selection granule
  1204. @verbatim
  1205. map : s-----------e
  1206. granule: s-----------e
  1207. @endverbatim
  1208. @param use_precedes: Select maps which temporally precedes the selection granule
  1209. @verbatim
  1210. map : s-----------e
  1211. granule: s-----------e
  1212. @endverbatim
  1213. Usage:
  1214. @code
  1215. >>> # Relative time
  1216. >>> start = 1
  1217. >>> end = 2
  1218. >>> create_temporal_relation_sql_where_statement(start, end,
  1219. ... use_start=False)
  1220. >>> create_temporal_relation_sql_where_statement(start, end)
  1221. '((start_time >= 1 and start_time < 2) )'
  1222. >>> create_temporal_relation_sql_where_statement(start, end,
  1223. ... use_start=True)
  1224. '((start_time >= 1 and start_time < 2) )'
  1225. >>> create_temporal_relation_sql_where_statement(start, end,
  1226. ... use_start=False, use_during=True)
  1227. '(((start_time > 1 and end_time < 2) OR (start_time >= 1 and end_time < 2) OR (start_time > 1 and end_time <= 2)))'
  1228. >>> create_temporal_relation_sql_where_statement(start, end,
  1229. ... use_start=False, use_overlap=True)
  1230. '(((start_time < 1 and end_time > 1 and end_time < 2) OR (start_time < 2 and start_time > 1 and end_time > 2)))'
  1231. >>> create_temporal_relation_sql_where_statement(start, end,
  1232. ... use_start=False, use_contain=True)
  1233. '(((start_time < 1 and end_time > 2) OR (start_time <= 1 and end_time > 2) OR (start_time < 1 and end_time >= 2)))'
  1234. >>> create_temporal_relation_sql_where_statement(start, end,
  1235. ... use_start=False, use_equal=True)
  1236. '((start_time = 1 and end_time = 2))'
  1237. >>> create_temporal_relation_sql_where_statement(start, end,
  1238. ... use_start=False, use_follows=True)
  1239. '((start_time = 2))'
  1240. >>> create_temporal_relation_sql_where_statement(start, end,
  1241. ... use_start=False, use_precedes=True)
  1242. '((end_time = 1))'
  1243. >>> create_temporal_relation_sql_where_statement(start, end,
  1244. ... use_start=True, use_during=True, use_overlap=True, use_contain=True,
  1245. ... use_equal=True, use_follows=True, use_precedes=True)
  1246. '((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))'
  1247. >>> # Absolute time
  1248. >>> start = datetime(2001, 1, 1, 12, 30)
  1249. >>> end = datetime(2001, 3, 31, 14, 30)
  1250. >>> create_temporal_relation_sql_where_statement(start, end,
  1251. ... use_start=False)
  1252. >>> create_temporal_relation_sql_where_statement(start, end)
  1253. "((start_time >= '2001-01-01 12:30:00' and start_time < '2001-03-31 14:30:00') )"
  1254. >>> create_temporal_relation_sql_where_statement(start, end,
  1255. ... use_start=True)
  1256. "((start_time >= '2001-01-01 12:30:00' and start_time < '2001-03-31 14:30:00') )"
  1257. >>> create_temporal_relation_sql_where_statement(start, end,
  1258. ... use_start=False, use_during=True)
  1259. "(((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')))"
  1260. >>> create_temporal_relation_sql_where_statement(start, end,
  1261. ... use_start=False, use_overlap=True)
  1262. "(((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')))"
  1263. >>> create_temporal_relation_sql_where_statement(start, end,
  1264. ... use_start=False, use_contain=True)
  1265. "(((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')))"
  1266. >>> create_temporal_relation_sql_where_statement(start, end,
  1267. ... use_start=False, use_equal=True)
  1268. "((start_time = '2001-01-01 12:30:00' and end_time = '2001-03-31 14:30:00'))"
  1269. >>> create_temporal_relation_sql_where_statement(start, end,
  1270. ... use_start=False, use_follows=True)
  1271. "((start_time = '2001-03-31 14:30:00'))"
  1272. >>> create_temporal_relation_sql_where_statement(start, end,
  1273. ... use_start=False, use_precedes=True)
  1274. "((end_time = '2001-01-01 12:30:00'))"
  1275. >>> create_temporal_relation_sql_where_statement(start, end,
  1276. ... use_start=True, use_during=True, use_overlap=True, use_contain=True,
  1277. ... use_equal=True, use_follows=True, use_precedes=True)
  1278. "((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'))"
  1279. @endcode
  1280. """
  1281. where = "("
  1282. if use_start:
  1283. if isinstance(start, datetime):
  1284. where += "(start_time >= '%s' and start_time < '%s') " % (start, end)
  1285. else:
  1286. where += "(start_time >= %i and start_time < %i) " % (start, end)
  1287. if use_during:
  1288. if use_start:
  1289. where += " OR "
  1290. if isinstance(start, datetime):
  1291. where += "((start_time > '%s' and end_time < '%s') OR " % (start, end)
  1292. where += "(start_time >= '%s' and end_time < '%s') OR " % (start, end)
  1293. where += "(start_time > '%s' and end_time <= '%s'))" % (start, end)
  1294. else:
  1295. where += "((start_time > %i and end_time < %i) OR " % (start, end)
  1296. where += "(start_time >= %i and end_time < %i) OR " % (start, end)
  1297. where += "(start_time > %i and end_time <= %i))" % (start, end)
  1298. if use_overlap:
  1299. if use_start or use_during:
  1300. where += " OR "
  1301. if isinstance(start, datetime):
  1302. where += "((start_time < '%s' and end_time > '%s' and end_time < '%s') OR " % (start, start, end)
  1303. where += "(start_time < '%s' and start_time > '%s' and end_time > '%s'))" % (end, start, end)
  1304. else:
  1305. where += "((start_time < %i and end_time > %i and end_time < %i) OR " % (start, start, end)
  1306. where += "(start_time < %i and start_time > %i and end_time > %i))" % (end, start, end)
  1307. if use_contain:
  1308. if use_start or use_during or use_overlap:
  1309. where += " OR "
  1310. if isinstance(start, datetime):
  1311. where += "((start_time < '%s' and end_time > '%s') OR " % (start, end)
  1312. where += "(start_time <= '%s' and end_time > '%s') OR " % (start, end)
  1313. where += "(start_time < '%s' and end_time >= '%s'))" % (start, end)
  1314. else:
  1315. where += "((start_time < %i and end_time > %i) OR " % (start, end)
  1316. where += "(start_time <= %i and end_time > %i) OR " % (start, end)
  1317. where += "(start_time < %i and end_time >= %i))" % (start, end)
  1318. if use_equal:
  1319. if use_start or use_during or use_overlap or use_contain:
  1320. where += " OR "
  1321. if isinstance(start, datetime):
  1322. where += "(start_time = '%s' and end_time = '%s')" % (start, end)
  1323. else:
  1324. where += "(start_time = %i and end_time = %i)" % (start, end)
  1325. if use_follows:
  1326. if use_start or use_during or use_overlap or use_contain or use_equal:
  1327. where += " OR "
  1328. if isinstance(start, datetime):
  1329. where += "(start_time = '%s')" % (end)
  1330. else:
  1331. where += "(start_time = %i)" % (end)
  1332. if use_precedes:
  1333. if use_start or use_during or use_overlap or use_contain or use_equal \
  1334. or use_follows:
  1335. where += " OR "
  1336. if isinstance(start, datetime):
  1337. where += "(end_time = '%s')" % (start)
  1338. else:
  1339. where += "(end_time = %i)" % (start)
  1340. where += ")"
  1341. # Catch empty where statement
  1342. if where == "()":
  1343. where = None
  1344. return where
  1345. ###############################################################################
  1346. if __name__ == "__main__":
  1347. import doctest
  1348. doctest.testmod()