abstract_space_time_dataset.py 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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. Usage:
  6. @code
  7. import grass.temporal as tgis
  8. ...
  9. @endcode
  10. (C) 2008-2011 by the GRASS Development Team
  11. This program is free software under the GNU General Public
  12. License (>=v2). Read the file COPYING that comes with GRASS
  13. for details.
  14. @author Soeren Gebbert
  15. """
  16. from abstract_dataset import *
  17. from temporal_granularity import *
  18. ###############################################################################
  19. class abstract_space_time_dataset(abstract_dataset):
  20. """Abstract space time dataset class
  21. This class represents a space time dataset. Convenient functions
  22. to select, update, insert or delete objects of this type in the SQL
  23. temporal database exists as well as functions to register or unregister
  24. raster maps.
  25. Parts of the temporal logic are implemented in the SQL temporal database,
  26. like the computation of the temporal and spatial extent as well as the
  27. collecting of metadata.
  28. """
  29. def __init__(self, ident):
  30. self.reset(ident)
  31. def get_new_map_instance(self, ident=None):
  32. """Return a new instance of a map dataset which is associated with the type of this class
  33. @param ident: The unique identifier of the new object
  34. """
  35. raise IOError("This method must be implemented in the subclasses")
  36. def get_map_register(self):
  37. """Return the name of the map register table"""
  38. raise IOError("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 in this space time dataset.
  42. @param name: The name of the register table
  43. """
  44. raise IOError("This method must be implemented in the subclasses")
  45. def set_initial_values(self, temporal_type, semantic_type, \
  46. title=None, description=None):
  47. """Set the initial values of the space time dataset
  48. @param temporal_type: The temporal type of this space time dataset (absolute or relative)
  49. @param semantic_type: The semantic type of this dataset
  50. @param title: The title
  51. @param description: The description of this dataset
  52. """
  53. if temporal_type == "absolute":
  54. self.set_time_to_absolute()
  55. elif temporal_type == "relative":
  56. self.set_time_to_relative()
  57. else:
  58. core.fatal(_("Unknown temporal type \"%s\"") % (temporal_type))
  59. self.base.set_semantic_type(semantic_type)
  60. self.metadata.set_title(title)
  61. self.metadata.set_description(description)
  62. def get_semantic_type(self):
  63. """Return the semantic type of this dataset"""
  64. return self.base.get_semantic_type()
  65. def get_initial_values(self):
  66. """Return the initial values: temporal_type, semantic_type, title, description"""
  67. temporal_type = self.get_temporal_type()
  68. semantic_type = self.base.get_semantic_type()
  69. title = self.metadata.get_title()
  70. description = self.metadata.get_description()
  71. return temporal_type, semantic_type, title, description
  72. def get_granularity(self):
  73. """Return the granularity"""
  74. temporal_type = self.get_temporal_type()
  75. if temporal_type == "absolute":
  76. granularity = self.absolute_time.get_granularity()
  77. elif temporal_type == "relative":
  78. granularity = self.relative_time.get_granularity()
  79. return granularity
  80. def set_granularity(self, granularity):
  81. temporal_type = self.get_temporal_type()
  82. if temporal_type == "absolute":
  83. self.set_time_to_absolute()
  84. self.absolute_time.set_granularity(granularity)
  85. elif temporal_type == "relative":
  86. self.set_time_to_relative()
  87. self.relative_time.set_granularity(granularity)
  88. else:
  89. core.fatal(_("Unknown temporal type \"%s\"") % (temporal_type))
  90. def get_map_time(self):
  91. """Return the type of the map time, interval, point, maixed or invalid"""
  92. temporal_type = self.get_temporal_type()
  93. if temporal_type == "absolute":
  94. map_time = self.absolute_time.get_map_time()
  95. elif temporal_type == "relative":
  96. map_time = self.relative_time.get_map_time()
  97. return map_time
  98. def print_temporal_relation_matrix(self, maps):
  99. """Print the temporal relation matrix of all registered maps to stdout
  100. The temproal relation matrix includes the temporal relations between
  101. all registered maps. The relations are strings stored in a list of lists.
  102. @param dbif: The database interface to be used
  103. """
  104. for i in range(len(maps)):
  105. reltations = ""
  106. count = 0
  107. for j in range(i + 1, len(maps)):
  108. relation = maps[j].temporal_relation(maps[i])
  109. # print maps[j].base.get_name(), maps[i].base.get_name(), relation
  110. if count == 0:
  111. relations = relation
  112. else:
  113. relations += "," + relation
  114. count += 1
  115. # Break if the the next map follows
  116. if relation == "follows":
  117. break
  118. # Break if the the next map is after
  119. if relation == "after":
  120. break
  121. if i < len(maps) - 1:
  122. print maps[i].base.get_name(), relations
  123. else:
  124. print maps[i].base.get_name()
  125. def get_temporal_relation_matrix(self, maps):
  126. """Return the temporal relation matrix of all registered maps as listof lists
  127. The map list must be ordered by start time
  128. The temproal relation matrix includes the temporal relations between
  129. all registered maps. The relations are strings stored in a list of lists.
  130. @param maps: a ordered by start_time list of map objects
  131. """
  132. matrix = []
  133. # Create the temporal relation matrix
  134. # Add the map names first
  135. row = []
  136. for map in maps:
  137. row.append(map.get_id())
  138. matrix.append(row)
  139. for mapA in maps:
  140. row = []
  141. for mapB in maps:
  142. row.append(mapA.temporal_relation(mapB))
  143. matrix.append(row)
  144. return matrix
  145. def count_temporal_types(self, maps):
  146. """Return the temporal type of the registered maps as dictionary
  147. The map list must be ordered by start time
  148. The temporal type can be:
  149. * point -> only the start time is present
  150. * interval -> start and end time
  151. * invalid -> No valid time point or interval found
  152. @param maps: A sorted (start_time) list of abstract_dataset objects
  153. """
  154. time_invalid = 0
  155. time_point = 0
  156. time_interval = 0
  157. tcount = {}
  158. for i in range(len(maps)):
  159. # Check for point and interval data
  160. if maps[i].is_time_absolute():
  161. start, end, tz = maps[i].get_absolute_time()
  162. if maps[i].is_time_relative():
  163. start, end = maps[i].get_relative_time()
  164. if start != None and end != None:
  165. time_interval += 1
  166. elif start != None and end == None:
  167. time_point += 1
  168. else:
  169. time_invalid += 1
  170. tcount["point"] = time_point
  171. tcount["interval"] = time_interval
  172. tcount["invalid"] = time_invalid
  173. return tcount
  174. def count_gaps(self, maps):
  175. """Count the number of gaps between temporal neighbours
  176. @param maps: A sorted (start_time) list of abstract_dataset objects
  177. @return The numbers of gaps between temporal neighbours
  178. """
  179. gaps = 0
  180. # Check for gaps
  181. for i in range(len(maps)):
  182. if i < len(maps) - 1:
  183. relation = maps[i + 1].temporal_relation(maps[i])
  184. if relation == "after":
  185. gaps += 1
  186. return gaps
  187. def count_temporal_relations(self, maps):
  188. """Count the temporal relations between the registered maps.
  189. The map list must be ordered by start time
  190. @param maps: A sorted (start_time) list of abstract_dataset objects
  191. @return A dictionary with counted temporal relationships
  192. """
  193. tcount = {}
  194. for i in range(len(maps)):
  195. # Check for point and interval data
  196. for j in range(i + 1, len(maps)):
  197. relation = maps[j].temporal_relation(maps[i])
  198. if tcount.has_key(relation):
  199. tcount[relation] = tcount[relation] + 1
  200. else:
  201. tcount[relation] = 1
  202. # Break if the the next map follows
  203. if relation == "follows":
  204. break
  205. # Break if the the next map is after
  206. if relation == "after":
  207. break
  208. return tcount
  209. def check_temporal_topology(self, maps=None, dbif=None):
  210. """Check the temporal topology
  211. Correct topology means, that time intervals are not overlap or
  212. that intervals does not contain other intervals. Equal time intervals or
  213. points of time are not allowed.
  214. The map list must be ordered by start time
  215. Allowed and not allowed temporal relationships for correct topology
  216. after -> allowed
  217. before -> allowed
  218. follows -> allowed
  219. precedes -> allowed
  220. equivalent -> not allowed
  221. during -> not allowed
  222. contains -> not allowed
  223. overlaps -> not allowed
  224. overlapped -> not allowed
  225. starts -> not allowed
  226. finishes -> not allowed
  227. started -> not allowed
  228. finished -> not allowed
  229. @param maps: A sorted (start_time) list of abstract_dataset objects
  230. @return True if topology is correct
  231. """
  232. if maps == None:
  233. maps = get_registered_maps_as_objects(where=None, order="start_time", dbif=dbif)
  234. relations = self.count_temporal_relations(maps)
  235. map_time = self.get_map_time()
  236. if map_time == "interval" or map_time == "mixed":
  237. if relations.has_key("equivalent"):
  238. return False
  239. if relations.has_key("during"):
  240. return False
  241. if relations.has_key("contains"):
  242. return False
  243. if relations.has_key("overlaps"):
  244. return False
  245. if relations.has_key("overlapped"):
  246. return False
  247. if relations.has_key("starts"):
  248. return False
  249. if relations.has_key("finishes"):
  250. return False
  251. if relations.has_key("started"):
  252. return False
  253. if relations.has_key("finished"):
  254. return False
  255. elif map_time == "point":
  256. if relations.has_key("equivalent"):
  257. return False
  258. else:
  259. return False
  260. return True
  261. def sample_by_dataset_topology(self, stds, method=None, dbif=None):
  262. """Sample this space time dataset with the temporal topology of a second space time dataset
  263. The sample dataset must have "interval" as temporal map type, so all sample maps have valid interval time.
  264. Return all registered maps as ordered (by start_time) object list with
  265. "gap" map objects (id==None). Each list entry is a list of map objects
  266. which are potentially located in temporal relation to the actual granule of the second space time dataset.
  267. Each entry in the object list is a dict. The actual sampler map and its temporal enxtent (the actual granule) and
  268. the list of samples are stored:
  269. list = self.sample_by_dataset_topology(stds=sampler, method=["during","overlap","contain","equal"])
  270. for entry in list:
  271. granule = entry["granule"]
  272. maplist = entry["samples"]
  273. for map in maplist:
  274. map.select()
  275. map.print_info()
  276. A valid temporal topology (no overlapping or inclusion allowed) is needed to get correct results.
  277. Gaps between maps are identified as unregistered maps with id==None.
  278. The map objects are initialized with the id and the temporal extent of the granule (temporal type, start time, end time).
  279. In case more map informations are needed, use the select() method for each listed object.
  280. @param stds: The space time dataset to be used for temporal sampling
  281. @param method: This option specifies what sample method should be used. In case the registered maps are of temporal point type,
  282. only the start time is used for sampling. In case of mixed of interval data the user can chose between:
  283. * start: Select maps of which the start time is located in the selection granule
  284. map : s
  285. granule: s-----------------e
  286. map : s--------------------e
  287. granule: s-----------------e
  288. map : s--------e
  289. granule: s-----------------e
  290. * during: Select maps which are temporal during the selection granule
  291. map : s-----------e
  292. granule: s-----------------e
  293. * overlap: Select maps which temporal overlap the selection granule
  294. map : s-----------e
  295. granule: s-----------------e
  296. map : s-----------e
  297. granule: s----------e
  298. * contain: Select maps which temporally contain the selection granule
  299. map : s-----------------e
  300. granule: s-----------e
  301. * equal: Select maps which temporally equal to the selection granule
  302. map : s-----------e
  303. granule: s-----------e
  304. All these methods can be combined. Method must be of type tuple including the identification strings.
  305. @param dbif: The database interface to be used
  306. In case nothing found None is returned
  307. """
  308. use_start = False
  309. use_during = False
  310. use_overlap = False
  311. use_contain = False
  312. use_equal = False
  313. # Inititalize the methods
  314. if method:
  315. for name in method:
  316. if name == "start":
  317. use_start = True
  318. if name == "during":
  319. use_during = True
  320. if name == "overlap":
  321. use_overlap = True
  322. if name == "contain":
  323. use_contain = True
  324. if name == "equal":
  325. use_equal = True
  326. else:
  327. use_during = True
  328. use_overlap = True
  329. use_contain = True
  330. use_equal = True
  331. if self.get_temporal_type() != stds.get_temporal_type():
  332. core.error(_("The space time datasets must be of the same temporal type"))
  333. return None
  334. if stds.get_map_time() != "interval":
  335. core.error(_("The temporal map type of the sample dataset must be interval"))
  336. return None
  337. # In case points of time are available, disable the interval specific methods
  338. if self.get_map_time() == "point":
  339. use_start = True
  340. use_during = False
  341. use_overlap = False
  342. use_contain = False
  343. use_equal = False
  344. connect = False
  345. if dbif == None:
  346. dbif = sql_database_interface()
  347. dbif.connect()
  348. connect = True
  349. obj_list = []
  350. sample_maps = stds.get_registered_maps_as_objects_with_gaps(where=None, dbif=dbif)
  351. for granule in sample_maps:
  352. start, end = granule.get_valid_time()
  353. where = "("
  354. if use_start:
  355. where += "(start_time >= '%s' and start_time < '%s') " % (start, end)
  356. if use_during:
  357. if use_start:
  358. where += " OR "
  359. where += "((start_time > '%s' and end_time < '%s') OR " % (start, end)
  360. where += "(start_time >= '%s' and end_time < '%s') OR " % (start, end)
  361. where += "(start_time > '%s' and end_time <= '%s'))" % (start, end)
  362. if use_overlap:
  363. if use_start or use_during:
  364. where += " OR "
  365. where += "((start_time < '%s' and end_time > '%s' and end_time < '%s') OR " % (start, start, end)
  366. where += "(start_time < '%s' and start_time > '%s' and end_time > '%s'))" % (end, start, end)
  367. if use_contain:
  368. if use_start or use_during or use_overlap:
  369. where += " OR "
  370. where += "((start_time < '%s' and end_time > '%s') OR " % (start, end)
  371. where += "(start_time <= '%s' and end_time > '%s') OR " % (start, end)
  372. where += "(start_time < '%s' and end_time >= '%s'))" % (start, end)
  373. if use_equal:
  374. if use_start or use_during or use_overlap or use_contain:
  375. where += " OR "
  376. where += "(start_time = '%s' and end_time = '%s')" % (start, end)
  377. where += ")"
  378. rows = self.get_registered_maps("id", where, "start_time", dbif)
  379. result = {}
  380. result["granule"] = granule
  381. maplist = None
  382. if rows:
  383. maplist = []
  384. for row in rows:
  385. map = self.get_new_map_instance(row["id"])
  386. if self.is_time_absolute():
  387. map.set_absolute_time(start, end)
  388. elif self.is_time_relative():
  389. map.set_relative_time(start, end)
  390. maplist.append(copy.copy(map))
  391. result["samples"] = maplist
  392. else:
  393. maplist = []
  394. map = self.get_new_map_instance(None)
  395. if self.is_time_absolute():
  396. map.set_absolute_time(start, end)
  397. elif self.is_time_relative():
  398. map.set_relative_time(start, end)
  399. maplist.append(copy.copy(map))
  400. result["samples"] = maplist
  401. obj_list.append(copy.copy(result))
  402. if connect == True:
  403. dbif.close()
  404. return obj_list
  405. def get_registered_maps_as_objects_by_granularity(self, gran=None, dbif=None):
  406. """Return all registered maps as ordered (by start_time) object list with
  407. "gap" map objects (id==None) for temporal topological operations using the
  408. granularity of the space time dataset as increment. Each list entry is a list of map objects
  409. which are potentially located in the actual granule.
  410. A valid temporal topology (no overlapping or inclusion allowed) is needed to get correct results.
  411. The dataset must have "interval" as temporal map type, so all maps have valid interval time.
  412. Gaps between maps are identified as unregistered maps with id==None.
  413. The objects are initialized with the id and the temporal extent (temporal type, start time, end time).
  414. In case more map informations are needed, use the select() method for each listed object.
  415. @param gran: The granularity to be used
  416. @param dbif: The database interface to be used
  417. In case nothing found None is returned
  418. """
  419. connect = False
  420. if dbif == None:
  421. dbif = sql_database_interface()
  422. dbif.connect()
  423. connect = True
  424. obj_list = []
  425. if gran == None:
  426. gran = self.get_granularity()
  427. start, end = self.get_valid_time()
  428. while start < end:
  429. if self.is_time_absolute():
  430. next = increment_datetime_by_string(start, gran)
  431. else:
  432. next = start + gran
  433. where = "(start_time <= '%s' and end_time >= '%s')" % (start, next)
  434. rows = self.get_registered_maps("id", where, "start_time", dbif)
  435. if rows:
  436. if len(rows) > 1:
  437. 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."))
  438. maplist = []
  439. for row in rows:
  440. # Take the first map
  441. map = self.get_new_map_instance(rows[0]["id"])
  442. if self.is_time_absolute():
  443. map.set_absolute_time(start, next)
  444. elif self.is_time_relative():
  445. map.set_relative_time(start, next)
  446. maplist.append(copy.copy(map))
  447. obj_list.append(copy.copy(maplist))
  448. start = next
  449. if connect == True:
  450. dbif.close()
  451. return obj_list
  452. def get_registered_maps_as_objects_with_gaps(self, where=None, dbif=None):
  453. """Return all registered maps as ordered (by start_time) object list with
  454. "gap" map objects (id==None) for temporal topological operations
  455. Gaps between maps are identified as maps with id==None
  456. The objects are initialized with the id and the temporal extent (temporal type, start time, end time).
  457. In case more map informations are needed, use the select() method for each listed object.
  458. @param where: The SQL where statement to select a subset of the registered maps without "WHERE"
  459. @param dbif: The database interface to be used
  460. In case nothing found None is returned
  461. """
  462. connect = False
  463. if dbif == None:
  464. dbif = sql_database_interface()
  465. dbif.connect()
  466. connect = True
  467. obj_list = []
  468. maps = self.get_registered_maps_as_objects(where, "start_time", dbif)
  469. if maps and len(maps) > 0:
  470. for i in range(len(maps)):
  471. obj_list.append(maps[i])
  472. # Detect and insert gaps
  473. if i < len(maps) - 1:
  474. relation = maps[i + 1].temporal_relation(maps[i])
  475. if relation == "after":
  476. start1, end1 = maps[i].get_valid_time()
  477. start2, end2 = maps[i + 1].get_valid_time()
  478. end = start2
  479. if end1:
  480. start = end1
  481. else:
  482. start = start1
  483. map = self.get_new_map_instance(None)
  484. if self.is_time_absolute():
  485. map.set_absolute_time(start, end)
  486. elif self.is_time_relative():
  487. map.set_relative_time(start, end)
  488. obj_list.append(copy.copy(map))
  489. if connect == True:
  490. dbif.close()
  491. return obj_list
  492. def get_registered_maps_as_objects(self, where=None, order="start_time", dbif=None):
  493. """Return all registered maps as ordered object list for temporal topological operations
  494. The objects are initialized with the id and the temporal extent (temporal type, start time, end time).
  495. In case more map informations are needed, use the select() method for each listed object.
  496. @param where: The SQL where statement to select a subset of the registered maps without "WHERE"
  497. @param order: The SQL order statement to be used to order the objects in the list without "ORDER BY"
  498. @param dbif: The database interface to be used
  499. In case nothing found None is returned
  500. """
  501. connect = False
  502. if dbif == None:
  503. dbif = sql_database_interface()
  504. dbif.connect()
  505. connect = True
  506. obj_list = []
  507. rows = self.get_registered_maps("id,start_time,end_time", where, order, dbif)
  508. count = 0
  509. if rows:
  510. for row in rows:
  511. core.percent(count, len(rows), 1)
  512. map = self.get_new_map_instance(row["id"])
  513. if self.is_time_absolute():
  514. map.set_absolute_time(row["start_time"], row["end_time"])
  515. elif self.is_time_relative():
  516. map.set_relative_time(row["start_time"], row["end_time"])
  517. obj_list.append(copy.copy(map))
  518. count += 1
  519. core.percent(1, 1, 1)
  520. if connect == True:
  521. dbif.close()
  522. return obj_list
  523. def get_registered_maps(self, columns=None, where = None, order = None, dbif=None):
  524. """Return sqlite rows of all registered maps.
  525. In case columsn are not specified, each row includes all columns specified in the datatype specific view
  526. @param columns: Columns to be selected as SQL compliant string
  527. @param where: The SQL where statement to select a subset of the registered maps without "WHERE"
  528. @param order: The SQL order statement to be used to order the objects in the list without "ORDER BY"
  529. @param dbif: The database interface to be used
  530. In case nothing found None is returned
  531. """
  532. connect = False
  533. if dbif == None:
  534. dbif = sql_database_interface()
  535. dbif.connect()
  536. connect = True
  537. rows = None
  538. if self.get_map_register():
  539. # Use the correct temporal table
  540. if self.get_temporal_type() == "absolute":
  541. map_view = self.get_new_map_instance(None).get_type() + "_view_abs_time"
  542. else:
  543. map_view = self.get_new_map_instance(None).get_type() + "_view_rel_time"
  544. if columns:
  545. sql = "SELECT %s FROM %s WHERE %s.id IN (SELECT id FROM %s)" % (columns, map_view, map_view, self.get_map_register())
  546. else:
  547. sql = "SELECT * FROM %s WHERE %s.id IN (SELECT id FROM %s)" % (map_view, map_view, self.get_map_register())
  548. if where:
  549. sql += " AND %s" % (where)
  550. if order:
  551. sql += " ORDER BY %s" % (order)
  552. try:
  553. dbif.cursor.execute(sql)
  554. rows = dbif.cursor.fetchall()
  555. except:
  556. if connect == True:
  557. dbif.close()
  558. core.error(_("Unable to get map ids from register table <%s>") % (self.get_map_register()))
  559. raise
  560. if connect == True:
  561. dbif.close()
  562. return rows
  563. def delete(self, dbif=None):
  564. """Delete a space time dataset from the temporal database
  565. This method removes the space time dataset from the temporal database and drops its map register table
  566. @param dbif: The database interface to be used
  567. """
  568. # First we need to check if maps are registered in this dataset and
  569. # unregister them
  570. core.verbose(_("Delete space time %s dataset <%s> from temporal database") % (self.get_new_map_instance(ident=None).get_type(), self.get_id()))
  571. connect = False
  572. if dbif == None:
  573. dbif = sql_database_interface()
  574. dbif.connect()
  575. connect = True
  576. # SELECT all needed informations from the database
  577. self.select(dbif)
  578. core.verbose(_("Drop map register table: %s") % (self.get_map_register()))
  579. if self.get_map_register():
  580. rows = self.get_registered_maps("id", None, None, dbif)
  581. # Unregister each registered map in the table
  582. if rows:
  583. num_maps = len(rows)
  584. count = 0
  585. for row in rows:
  586. core.percent(count, num_maps, 1)
  587. # Unregister map
  588. map = self.get_new_map_instance(row["id"])
  589. self.unregister_map(map, dbif)
  590. count += 1
  591. core.percent(1, 1, 1)
  592. try:
  593. # Drop the map register table
  594. sql = "DROP TABLE " + self.get_map_register()
  595. dbif.cursor.execute(sql)
  596. dbif.connection.commit()
  597. except:
  598. if connect == True:
  599. dbif.close()
  600. core.error(_("Unable to drop table <%s>") % (self.get_map_register()))
  601. raise
  602. # Remove the primary key, the foreign keys will be removed by trigger
  603. self.base.delete(dbif)
  604. self.reset(None)
  605. if connect == True:
  606. dbif.close()
  607. def register_map(self, map, dbif=None):
  608. """ Register a map in the space time dataset.
  609. This method takes care of the registration of a map
  610. in a space time dataset.
  611. In case the map is already registered this function will break with a warning
  612. and return False
  613. @param dbif: The database interface to be used
  614. """
  615. connect = False
  616. if dbif == None:
  617. dbif = sql_database_interface()
  618. dbif.connect()
  619. connect = True
  620. if map.is_in_db(dbif) == False:
  621. dbif.close()
  622. core.fatal(_("Only maps with absolute or relative valid time can be registered"))
  623. core.verbose(_("Register %s map <%s> in space time %s dataset <%s>") % (map.get_type(), map.get_id(), map.get_type(), self.get_id()))
  624. # First select all data from the database
  625. map.select(dbif)
  626. if not map.check_valid_time():
  627. core.fatal(_("Map <%s> has invalid time") % map.get_id())
  628. map_id = map.base.get_id()
  629. map_name = map.base.get_name()
  630. map_mapset = map.base.get_mapset()
  631. map_register_table = map.get_stds_register()
  632. #print "Map register table", map_register_table
  633. # Get basic info
  634. stds_name = self.base.get_name()
  635. stds_mapset = self.base.get_mapset()
  636. stds_register_table = self.get_map_register()
  637. #print "STDS register table", stds_register_table
  638. if stds_mapset != map_mapset:
  639. dbif.close()
  640. core.fatal(_("Only maps from the same mapset can be registered"))
  641. # Check if map is already registred
  642. if stds_register_table:
  643. if dbmi.paramstyle == "qmark":
  644. sql = "SELECT id FROM " + stds_register_table + " WHERE id = (?)"
  645. else:
  646. sql = "SELECT id FROM " + stds_register_table + " WHERE id = (%s)"
  647. dbif.cursor.execute(sql, (map_id,))
  648. row = dbif.cursor.fetchone()
  649. # In case of no entry make a new one
  650. if row and row[0] == map_id:
  651. if connect == True:
  652. dbif.close()
  653. core.warning(_("Map <%s> is already registered.") % (map_id))
  654. return False
  655. # Create tables
  656. sql_path = get_sql_template_path()
  657. # We need to create the stmap raster register table bevor we can register the map
  658. if map_register_table == None:
  659. # Create a unique id
  660. uuid_rand = "map_" + str(uuid.uuid4()).replace("-", "")
  661. map_register_table = uuid_rand + "_" + self.get_type() + "_register"
  662. # Read the SQL template
  663. sql = open(os.path.join(sql_path, "map_stds_register_table_template.sql"), 'r').read()
  664. # Create the raster, raster3d and vector tables
  665. sql = sql.replace("GRASS_MAP", map.get_type())
  666. sql = sql.replace("MAP_NAME", map_name + "_" + map_mapset )
  667. sql = sql.replace("TABLE_NAME", uuid_rand )
  668. sql = sql.replace("MAP_ID", map_id)
  669. sql = sql.replace("STDS", self.get_type())
  670. try:
  671. if dbmi.__name__ == "sqlite3":
  672. dbif.cursor.executescript(sql)
  673. else:
  674. dbif.cursor.execute(sql)
  675. except:
  676. if connect == True:
  677. dbif.close()
  678. core.error(_("Unable to create the space time %s dataset register table for <%s>") % \
  679. (map.get_type(), map.get_id()))
  680. raise
  681. # Set the stds register table name and put it into the DB
  682. map.set_stds_register(map_register_table)
  683. map.metadata.update(dbif)
  684. core.verbose(_("Created register table <%s> for %s map <%s>") % \
  685. (map_register_table, map.get_type(), map.get_id()))
  686. # We need to create the table and register it
  687. if stds_register_table == None:
  688. # Create table name
  689. stds_register_table = stds_name + "_" + stds_mapset + "_" + map.get_type() + "_register"
  690. # Read the SQL template
  691. sql = open(os.path.join(sql_path, "stds_map_register_table_template.sql"), 'r').read()
  692. # Create the raster, raster3d and vector tables
  693. sql = sql.replace("GRASS_MAP", map.get_type())
  694. sql = sql.replace("SPACETIME_NAME", stds_name + "_" + stds_mapset )
  695. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  696. sql = sql.replace("STDS", self.get_type())
  697. sql_script = ""
  698. sql_script += "BEGIN TRANSACTION;\n"
  699. sql_script += sql
  700. sql_script += "\n"
  701. sql_script += "END TRANSACTION;"
  702. try:
  703. if dbmi.__name__ == "sqlite3":
  704. dbif.cursor.executescript(sql_script)
  705. else:
  706. dbif.cursor.execute(sql_script)
  707. dbif.connection.commit()
  708. except:
  709. if connect == True:
  710. dbif.close()
  711. core.error(_("Unable to create the space time %s dataset register table for <%s>") % \
  712. (map.get_type(), map.get_id()))
  713. raise
  714. # Set the map register table name and put it into the DB
  715. self.set_map_register(stds_register_table)
  716. self.metadata.update(dbif)
  717. core.verbose(_("Created register table <%s> for space time %s dataset <%s>") % \
  718. (stds_register_table, map.get_type(), self.get_id()))
  719. # Register the stds in the map stds register table
  720. # Check if the entry is already there
  721. if dbmi.paramstyle == "qmark":
  722. sql = "SELECT id FROM " + map_register_table + " WHERE id = ?"
  723. else:
  724. sql = "SELECT id FROM " + map_register_table + " WHERE id = %s"
  725. dbif.cursor.execute(sql, (self.base.get_id(),))
  726. row = dbif.cursor.fetchone()
  727. # In case of no entry make a new one
  728. if row == None:
  729. if dbmi.paramstyle == "qmark":
  730. sql = "INSERT INTO " + map_register_table + " (id) " + "VALUES (?)"
  731. else:
  732. sql = "INSERT INTO " + map_register_table + " (id) " + "VALUES (%s)"
  733. #print sql
  734. dbif.cursor.execute(sql, (self.base.get_id(),))
  735. # Now put the raster name in the stds map register table
  736. if dbmi.paramstyle == "qmark":
  737. sql = "INSERT INTO " + stds_register_table + " (id) " + "VALUES (?)"
  738. else:
  739. sql = "INSERT INTO " + stds_register_table + " (id) " + "VALUES (%s)"
  740. #print sql
  741. dbif.cursor.execute(sql, (map_id,))
  742. if connect == True:
  743. dbif.close()
  744. return True
  745. def unregister_map(self, map, dbif = None):
  746. """Unregister a map from the space time dataset.
  747. This method takes care of the unregistration of a map
  748. from a space time dataset.
  749. @param map: The map object to unregister
  750. @param dbif: The database interface to be used
  751. """
  752. connect = False
  753. if dbif == None:
  754. dbif = sql_database_interface()
  755. dbif.connect()
  756. connect = True
  757. if map.is_in_db(dbif) == False:
  758. dbif.close()
  759. core.fatal(_("Unable to find map <%s> in temporal database") % (map.get_id()))
  760. core.verbose(_("Unregister %s map <%s>") % (map.get_type(), map.get_id()))
  761. # First select all data from the database
  762. map.select(dbif)
  763. map_id = map.base.get_id()
  764. map_register_table = map.get_stds_register()
  765. stds_register_table = self.get_map_register()
  766. # Check if the map is registered in the space time raster dataset
  767. if dbmi.paramstyle == "qmark":
  768. sql = "SELECT id FROM " + map_register_table + " WHERE id = ?"
  769. else:
  770. sql = "SELECT id FROM " + map_register_table + " WHERE id = %s"
  771. dbif.cursor.execute(sql, (self.base.get_id(),))
  772. row = dbif.cursor.fetchone()
  773. # Break if the map is not registered
  774. if row == None:
  775. core.warning(_("Map <%s> is not registered in space time dataset") %(map_id, self.base.get_id()))
  776. if connect == True:
  777. dbif.close()
  778. return False
  779. # Remove the space time raster dataset from the raster dataset register
  780. if map_register_table != None:
  781. if dbmi.paramstyle == "qmark":
  782. sql = "DELETE FROM " + map_register_table + " WHERE id = ?"
  783. else:
  784. sql = "DELETE FROM " + map_register_table + " WHERE id = %s"
  785. dbif.cursor.execute(sql, (self.base.get_id(),))
  786. # Remove the raster map from the space time raster dataset register
  787. if stds_register_table != None:
  788. if dbmi.paramstyle == "qmark":
  789. sql = "DELETE FROM " + stds_register_table + " WHERE id = ?"
  790. else:
  791. sql = "DELETE FROM " + stds_register_table + " WHERE id = %s"
  792. dbif.cursor.execute(sql, (map_id,))
  793. if connect == True:
  794. dbif.close()
  795. def update_from_registered_maps(self, dbif = None):
  796. """This methods updates the spatial and temporal extent as well as
  797. type specific metadata. It should always been called after maps are registered
  798. or unregistered/deleted from the space time dataset.
  799. The update of the temporal extent checks if the end time is set correctly.
  800. In case the registered maps have no valid end time (None) the maximum start time
  801. will be used. If the end time is earlier than the maximum start time, it will
  802. be replaced by the maximum start time.
  803. An other solution to automate this is to use the diactivated trigger
  804. in the SQL files. But this will result in a huge performance issue
  805. in case many maps are registred (>1000).
  806. @param dbif: The database interface to be used
  807. """
  808. core.verbose(_("Update metadata, spatial and temporal extent from all registered maps of <%s>") % (self.get_id()))
  809. # Nothing to do if the register is not present
  810. if not self.get_map_register():
  811. return
  812. connect = False
  813. if dbif == None:
  814. dbif = sql_database_interface()
  815. dbif.connect()
  816. connect = True
  817. map_time = None
  818. use_start_time = False
  819. # Get basic info
  820. stds_name = self.base.get_name()
  821. stds_mapset = self.base.get_mapset()
  822. sql_path = get_sql_template_path()
  823. #We create a transaction
  824. sql_script = ""
  825. sql_script += "BEGIN TRANSACTION;\n"
  826. # Update the spatial and temporal extent from registered maps
  827. # Read the SQL template
  828. sql = open(os.path.join(sql_path, "update_stds_spatial_temporal_extent_template.sql"), 'r').read()
  829. sql = sql.replace("GRASS_MAP", self.get_new_map_instance(None).get_type())
  830. sql = sql.replace("SPACETIME_NAME", stds_name + "_" + stds_mapset )
  831. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  832. sql = sql.replace("STDS", self.get_type())
  833. sql_script += sql
  834. sql_script += "\n"
  835. # Update type specific metadata
  836. sql = open(os.path.join(sql_path, "update_" + self.get_type() + "_metadata_template.sql"), 'r').read()
  837. sql = sql.replace("GRASS_MAP", self.get_new_map_instance(None).get_type())
  838. sql = sql.replace("SPACETIME_NAME", stds_name + "_" + stds_mapset )
  839. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  840. sql = sql.replace("STDS", self.get_type())
  841. sql_script += sql
  842. sql_script += "\n"
  843. sql_script += "END TRANSACTION;"
  844. if dbmi.__name__ == "sqlite3":
  845. dbif.cursor.executescript(sql_script)
  846. else:
  847. dbif.cursor.execute(sql_script)
  848. # Read and validate the selected end time
  849. self.select()
  850. if self.is_time_absolute():
  851. start_time, end_time, tz = self.get_absolute_time()
  852. else:
  853. start_time, end_time = self.get_relative_time()
  854. # In case no end time is set, use the maximum start time of all registered maps as end time
  855. if end_time == None:
  856. use_start_time = True
  857. else:
  858. # Check if the end time is smaller than the maximum start time
  859. if self.is_time_absolute():
  860. sql = """SELECT max(start_time) FROM GRASS_MAP_absolute_time WHERE GRASS_MAP_absolute_time.id IN
  861. (SELECT id FROM SPACETIME_NAME_GRASS_MAP_register);"""
  862. sql = sql.replace("GRASS_MAP", self.get_new_map_instance(None).get_type())
  863. sql = sql.replace("SPACETIME_NAME", stds_name + "_" + stds_mapset )
  864. else:
  865. sql = """SELECT max(start_time) FROM GRASS_MAP_relative_time WHERE GRASS_MAP_relative_time.id IN
  866. (SELECT id FROM SPACETIME_NAME_GRASS_MAP_register);"""
  867. sql = sql.replace("GRASS_MAP", self.get_new_map_instance(None).get_type())
  868. sql = sql.replace("SPACETIME_NAME", stds_name + "_" + stds_mapset )
  869. dbif.cursor.execute(sql)
  870. row = dbif.cursor.fetchone()
  871. if row != None:
  872. # This seems to be a bug in sqlite3 Python driver
  873. if dbmi.__name__ == "sqlite3":
  874. tstring = row[0]
  875. # Convert the unicode string into the datetime format
  876. if self.is_time_absolute():
  877. if tstring.find(":") > 0:
  878. time_format = "%Y-%m-%d %H:%M:%S"
  879. else:
  880. time_format = "%Y-%m-%d"
  881. max_start_time = datetime.strptime(tstring, time_format)
  882. else:
  883. max_start_time = row[0]
  884. else:
  885. max_start_time = row[0]
  886. if end_time < max_start_time:
  887. use_start_time = True
  888. # Set the maximum start time as end time
  889. if use_start_time:
  890. if self.is_time_absolute():
  891. sql = """UPDATE STDS_absolute_time SET end_time =
  892. (SELECT max(start_time) FROM GRASS_MAP_absolute_time WHERE GRASS_MAP_absolute_time.id IN
  893. (SELECT id FROM SPACETIME_NAME_GRASS_MAP_register)
  894. ) WHERE id = 'SPACETIME_ID';"""
  895. sql = sql.replace("GRASS_MAP", self.get_new_map_instance(None).get_type())
  896. sql = sql.replace("SPACETIME_NAME", stds_name + "_" + stds_mapset )
  897. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  898. sql = sql.replace("STDS", self.get_type())
  899. elif self.is_time_relative():
  900. sql = """UPDATE STDS_relative_time SET end_time =
  901. (SELECT max(start_time) FROM GRASS_MAP_relative_time WHERE GRASS_MAP_relative_time.id IN
  902. (SELECT id FROM SPACETIME_NAME_GRASS_MAP_register)
  903. ) WHERE id = 'SPACETIME_ID';"""
  904. sql = sql.replace("GRASS_MAP", self.get_new_map_instance(None).get_type())
  905. sql = sql.replace("SPACETIME_NAME", stds_name + "_" + stds_mapset )
  906. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  907. sql = sql.replace("STDS", self.get_type())
  908. if dbmi.__name__ == "sqlite3":
  909. dbif.cursor.executescript(sql)
  910. else:
  911. dbif.cursor.execute(sql)
  912. # Count the temporal map types
  913. maps = self.get_registered_maps_as_objects(dbif=dbif)
  914. tlist = self.count_temporal_types(maps)
  915. if tlist["interval"] > 0 and tlist["point"] == 0 and tlist["invalid"] == 0:
  916. map_time = "interval"
  917. elif tlist["interval"] == 0 and tlist["point"] > 0 and tlist["invalid"] == 0:
  918. map_time = "point"
  919. elif tlist["interval"] > 0 and tlist["point"] > 0 and tlist["invalid"] == 0:
  920. map_time = "mixed"
  921. else:
  922. map_time = "invalid"
  923. # Compute the granularity
  924. if map_time != "invalid":
  925. # Smallest supported temporal resolution
  926. if self.is_time_absolute():
  927. gran = compute_absolute_time_granularity(maps)
  928. elif self.is_time_relative():
  929. gran = compute_relative_time_granularity(maps)
  930. else:
  931. gran = None
  932. # Set the map time type and update the time objects
  933. if self.is_time_absolute():
  934. self.absolute_time.select(dbif)
  935. self.metadata.select(dbif)
  936. if self.metadata.get_number_of_maps() > 0:
  937. self.absolute_time.set_map_time(map_time)
  938. self.absolute_time.set_granularity(gran)
  939. else:
  940. self.absolute_time.set_map_time(None)
  941. self.absolute_time.set_granularity(None)
  942. self.absolute_time.update_all(dbif)
  943. else:
  944. self.relative_time.select(dbif)
  945. self.metadata.select(dbif)
  946. if self.metadata.get_number_of_maps() > 0:
  947. self.relative_time.set_map_time(map_time)
  948. self.relative_time.set_granularity(gran)
  949. else:
  950. self.relative_time.set_map_time(None)
  951. self.relative_time.set_granularity(None)
  952. self.relative_time.update_all(dbif)
  953. if connect == True:
  954. dbif.close()