space_time_datasets.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS related functions to be used in Python scripts.
  4. Usage:
  5. @code
  6. import grass.temporal as tgis
  7. strds = tgis.space_time_raster_dataset("soils_1950_2010")
  8. ...
  9. @endcode
  10. (C) 2008-2011 by the GRASS Development Team
  11. This program is free software under the GNU General Public
  12. License (>=v2). Read the file COPYING that comes with GRASS
  13. for details.
  14. @author Soeren Gebbert
  15. """
  16. import getpass
  17. import grass.script.raster as raster
  18. import grass.script.vector as vector
  19. import grass.script.raster3d as raster3d
  20. from datetime_math import *
  21. from abstract_datasets import *
  22. ###############################################################################
  23. class raster_dataset(abstract_map_dataset):
  24. """Raster dataset class
  25. This class provides functions to select, update, insert or delete raster
  26. map informations and valid time stamps into the SQL temporal database.
  27. """
  28. def __init__(self, ident):
  29. self.reset(ident)
  30. def get_type(self):
  31. return "raster"
  32. def get_new_instance(self, ident):
  33. """Return a new instance with the type of this class"""
  34. return raster_dataset(ident)
  35. def get_new_stds_instance(self, ident):
  36. """Return a new space time dataset instance in which maps are stored with the type of this class"""
  37. return space_time_raster_dataset(ident)
  38. def get_stds_register(self):
  39. """Return the space time dataset register table name in which stds are listed in which this map is registered"""
  40. return self.metadata.get_strds_register()
  41. def set_stds_register(self, name):
  42. """Set the space time dataset register table name in which stds are listed in which this map is registered"""
  43. self.metadata.set_strds_register(name)
  44. def reset(self, ident):
  45. """Reset the internal structure and set the identifier"""
  46. self.ident = ident
  47. self.base = raster_base(ident=ident)
  48. self.absolute_time = raster_absolute_time(ident=ident)
  49. self.relative_time = raster_relative_time(ident=ident)
  50. self.spatial_extent = raster_spatial_extent(ident=ident)
  51. self.metadata = raster_metadata(ident=ident)
  52. def load(self):
  53. """Load all info from an existing raster map into the internal structure"""
  54. # Get the data from an existing raster map
  55. kvp = raster.raster_info(self.ident)
  56. # Fill base information
  57. self.base.set_name(self.ident.split("@")[0])
  58. self.base.set_mapset(self.ident.split("@")[1])
  59. self.base.set_creator(str(getpass.getuser()))
  60. # Fill spatial extent
  61. self.set_spatial_extent(north=kvp["north"], south=kvp["south"], \
  62. east=kvp["east"], west=kvp["west"])
  63. # Fill metadata
  64. self.metadata.set_nsres(kvp["nsres"])
  65. self.metadata.set_ewres(kvp["ewres"])
  66. self.metadata.set_datatype(kvp["datatype"])
  67. self.metadata.set_min(kvp["min"])
  68. self.metadata.set_max(kvp["max"])
  69. rows = int((kvp["north"] - kvp["south"])/kvp["nsres"] + 0.5)
  70. cols = int((kvp["east"] - kvp["west"])/kvp["ewres"] + 0.5)
  71. ncells = cols * rows
  72. self.metadata.set_cols(cols)
  73. self.metadata.set_rows(rows)
  74. self.metadata.set_number_of_cells(ncells)
  75. ###############################################################################
  76. class raster3d_dataset(abstract_map_dataset):
  77. """Raster3d dataset class
  78. This class provides functions to select, update, insert or delete raster3d
  79. map informations and valid time stamps into the SQL temporal database.
  80. """
  81. def __init__(self, ident):
  82. self.reset(ident)
  83. def get_type(self):
  84. return "raster3d"
  85. def get_new_instance(self, ident):
  86. """Return a new instance with the type of this class"""
  87. return raster3d_dataset(ident)
  88. def get_new_stds_instance(self, ident):
  89. """Return a new space time dataset instance in which maps are stored with the type of this class"""
  90. return space_time_raster3d_dataset(ident)
  91. def get_stds_register(self):
  92. """Return the space time dataset register table name in which stds are listed in which this map is registered"""
  93. return self.metadata.get_str3ds_register()
  94. def set_stds_register(self, name):
  95. """Set the space time dataset register table name in which stds are listed in which this map is registered"""
  96. self.metadata.set_str3ds_register(name)
  97. def reset(self, ident):
  98. """Reset the internal structure and set the identifier"""
  99. self.ident = ident
  100. self.base = raster3d_base(ident=ident)
  101. self.absolute_time = raster3d_absolute_time(ident=ident)
  102. self.relative_time = raster3d_relative_time(ident=ident)
  103. self.spatial_extent = raster3d_spatial_extent(ident=ident)
  104. self.metadata = raster3d_metadata(ident=ident)
  105. def load(self):
  106. """Load all info from an existing raster3d map into the internal structure"""
  107. # Get the data from an existing raster map
  108. kvp = raster3d.raster3d_info(self.ident)
  109. # Fill base information
  110. self.base.set_name(self.ident.split("@")[0])
  111. self.base.set_mapset(self.ident.split("@")[1])
  112. self.base.set_creator(str(getpass.getuser()))
  113. # Fill spatial extent
  114. self.set_spatial_extent(north=kvp["north"], south=kvp["south"], \
  115. east=kvp["east"], west=kvp["west"],\
  116. top=kvp["top"], bottom=kvp["bottom"])
  117. # Fill metadata
  118. self.metadata.set_nsres(kvp["nsres"])
  119. self.metadata.set_ewres(kvp["ewres"])
  120. self.metadata.set_tbres(kvp["tbres"])
  121. self.metadata.set_datatype(kvp["datatype"])
  122. self.metadata.set_min(kvp["min"])
  123. self.metadata.set_max(kvp["max"])
  124. rows = int((kvp["north"] - kvp["south"])/kvp["nsres"] + 0.5)
  125. cols = int((kvp["east"] - kvp["west"])/kvp["ewres"] + 0.5)
  126. depths = int((kvp["top"] - kvp["bottom"])/kvp["tbres"] + 0.5)
  127. ncells = cols * rows * depths
  128. self.metadata.set_cols(cols)
  129. self.metadata.set_rows(rows)
  130. self.metadata.set_depths(depths)
  131. self.metadata.set_number_of_cells(ncells)
  132. ###############################################################################
  133. class vector_dataset(abstract_map_dataset):
  134. """Vector dataset class
  135. This class provides functions to select, update, insert or delete vector
  136. map informations and valid time stamps into the SQL temporal database.
  137. """
  138. def __init__(self, ident):
  139. self.reset(ident)
  140. def get_type(self):
  141. return "vector"
  142. def get_new_instance(self, ident):
  143. """Return a new instance with the type of this class"""
  144. return vector_dataset(ident)
  145. def get_new_stds_instance(self, ident):
  146. """Return a new space time dataset instance in which maps are stored with the type of this class"""
  147. return space_time_vector_dataset(ident)
  148. def get_stds_register(self):
  149. """Return the space time dataset register table name in which stds are listed in which this map is registered"""
  150. return self.metadata.get_stvds_register()
  151. def set_stds_register(self, name):
  152. """Set the space time dataset register table name in which stds are listed in which this map is registered"""
  153. self.metadata.set_stvds_register(name)
  154. def reset(self, ident):
  155. """Reset the internal structure and set the identifier"""
  156. self.ident = ident
  157. self.base = vector_base(ident=ident)
  158. self.absolute_time = vector_absolute_time(ident=ident)
  159. self.relative_time = vector_relative_time(ident=ident)
  160. self.spatial_extent = vector_spatial_extent(ident=ident)
  161. self.metadata = vector_metadata(ident=ident)
  162. def load(self):
  163. """Load all info from an existing vector map into the internal structure"""
  164. # Get the data from an existing raster map
  165. kvp = vector.vector_info(self.ident)
  166. # Fill base information
  167. self.base.set_name(self.ident.split("@")[0])
  168. self.base.set_mapset(self.ident.split("@")[1])
  169. self.base.set_creator(str(getpass.getuser()))
  170. # Fill spatial extent
  171. self.set_spatial_extent(north=kvp["north"], south=kvp["south"], \
  172. east=kvp["east"], west=kvp["west"],\
  173. top=kvp["top"], bottom=kvp["bottom"])
  174. # Fill metadata .. no metadata yet
  175. ###############################################################################
  176. class space_time_raster_dataset(abstract_space_time_dataset):
  177. """Space time raster dataset class
  178. """
  179. def __init__(self, ident):
  180. abstract_space_time_dataset.__init__(self, ident)
  181. def get_type(self):
  182. return "strds"
  183. def get_new_instance(self, ident):
  184. """Return a new instance with the type of this class"""
  185. return space_time_raster_dataset(ident)
  186. def get_new_map_instance(self, ident):
  187. """Return a new instance of a map dataset which is associated with the type of this class"""
  188. return raster_dataset(ident)
  189. def get_map_register(self):
  190. """Return the name of the map register table"""
  191. return self.metadata.get_raster_register()
  192. def set_map_register(self, name):
  193. """Set the name of the map register table"""
  194. self.metadata.set_raster_register(name)
  195. def reset(self, ident):
  196. """Reset the internal structure and set the identifier"""
  197. self.ident = ident
  198. self.base = strds_base(ident=ident)
  199. if ident != None:
  200. self.base.set_name(self.ident.split("@")[0])
  201. self.base.set_mapset(self.ident.split("@")[1])
  202. self.base.set_creator(str(getpass.getuser()))
  203. self.absolute_time = strds_absolute_time(ident=ident)
  204. self.relative_time = strds_relative_time(ident=ident)
  205. self.spatial_extent = strds_spatial_extent(ident=ident)
  206. self.metadata = strds_metadata(ident=ident)
  207. ###############################################################################
  208. class space_time_raster3d_dataset(abstract_space_time_dataset):
  209. """Space time raster3d dataset class
  210. """
  211. def __init__(self, ident):
  212. abstract_space_time_dataset.__init__(self, ident)
  213. def get_type(self):
  214. return "str3ds"
  215. def get_new_instance(self, ident):
  216. """Return a new instance with the type of this class"""
  217. return space_time_raster3d_dataset(ident)
  218. def get_new_map_instance(self, ident):
  219. """Return a new instance of a map dataset which is associated with the type of this class"""
  220. return raster3d_dataset(ident)
  221. def get_map_register(self):
  222. """Return the name of the map register table"""
  223. return self.metadata.get_raster3d_register()
  224. def set_map_register(self, name):
  225. """Set the name of the map register table"""
  226. self.metadata.set_raster3d_register(name)
  227. def reset(self, ident):
  228. """Reset the internal structure and set the identifier"""
  229. self.ident = ident
  230. self.base = str3ds_base(ident=ident)
  231. if ident != None:
  232. self.base.set_name(self.ident.split("@")[0])
  233. self.base.set_mapset(self.ident.split("@")[1])
  234. self.base.set_creator(str(getpass.getuser()))
  235. self.absolute_time = str3ds_absolute_time(ident=ident)
  236. self.relative_time = str3ds_relative_time(ident=ident)
  237. self.spatial_extent = str3ds_spatial_extent(ident=ident)
  238. self.metadata = str3ds_metadata(ident=ident)
  239. ###############################################################################
  240. class space_time_vector_dataset(abstract_space_time_dataset):
  241. """Space time vector dataset class
  242. """
  243. def __init__(self, ident):
  244. abstract_space_time_dataset.__init__(self, ident)
  245. def get_type(self):
  246. return "stvds"
  247. def get_new_instance(self, ident):
  248. """Return a new instance with the type of this class"""
  249. return space_time_vector_dataset(ident)
  250. def get_new_map_instance(self, ident):
  251. """Return a new instance of a map dataset which is associated with the type of this class"""
  252. return vector_dataset(ident)
  253. def get_map_register(self):
  254. """Return the name of the map register table"""
  255. return self.metadata.get_vector_register()
  256. def set_map_register(self, name):
  257. """Set the name of the map register table"""
  258. self.metadata.set_vector_register(name)
  259. def reset(self, ident):
  260. """Reset the internal structure and set the identifier"""
  261. self.ident = ident
  262. self.base = stvds_base(ident=ident)
  263. if ident != None:
  264. self.base.set_name(self.ident.split("@")[0])
  265. self.base.set_mapset(self.ident.split("@")[1])
  266. self.base.set_creator(str(getpass.getuser()))
  267. self.absolute_time = stvds_absolute_time(ident=ident)
  268. self.relative_time = stvds_relative_time(ident=ident)
  269. self.spatial_extent = stvds_spatial_extent(ident=ident)
  270. self.metadata = stvds_metadata(ident=ident)
  271. ###############################################################################
  272. def register_maps_in_space_time_dataset(type, name, maps, start=None, increment=None, dbif = None, interval=False):
  273. """Use this method to register maps in space time datasets. This function is generic and
  274. can handle raster, vector and raster3d maps as well as there space time datasets.
  275. Additionally a start time string and an increment string can be specified
  276. to assign a time interval automatically to the maps.
  277. It takes care of the correct update of the space time datasets from all
  278. registered maps.
  279. @param type: The type of the maps raster, raster3d or vector
  280. @param name: The name of the space time dataset
  281. @param maps: A comma separated list of map names
  282. @param start: The start date and time of the first raster map, in case the map has no date (format absolute: "yyyy-mm-dd HH:MM:SS" or "yyyy-mm-dd", format relative 5.0)
  283. @param increment: Time increment between maps for time stamp creation (format absolute: NNN seconds, minutes, hours, days, weeks, months, years; format relative: 1.0)
  284. @param dbif: The database interface to be used
  285. @param interval: If True, time intervals are created in case the start time and an increment is provided
  286. """
  287. # We may need the mapset
  288. mapset = core.gisenv()["MAPSET"]
  289. # Check if the dataset name contains the mapset as well
  290. if name.find("@") < 0:
  291. id = name + "@" + mapset
  292. else:
  293. id = name
  294. if type == "raster":
  295. sp = space_time_raster_dataset(id)
  296. if type == "raster3d":
  297. sp = space_time_raster3d_dataset(id)
  298. if type == "vector":
  299. sp = space_time_vector_dataset(id)
  300. connect = False
  301. if dbif == None:
  302. dbif = sql_database_interface()
  303. dbif.connect()
  304. connect = True
  305. # Read content from temporal database
  306. sp.select(dbif)
  307. if sp.is_in_db(dbif) == False:
  308. dbif.close()
  309. core.fatal("Space time " + sp.get_new_map_instance(None).get_type() + " dataset <" + name + "> not found")
  310. if maps.find(",") == -1:
  311. maplist = (maps,)
  312. else:
  313. maplist = tuple(maps.split(","))
  314. num_maps = len(maplist)
  315. count = 0
  316. for mapname in maplist:
  317. core.percent(count, num_maps, 1)
  318. mapname = mapname.strip()
  319. # Check if the map name contains the mapset as well
  320. if mapname.find("@") < 0:
  321. mapid = mapname + "@" + mapset
  322. else:
  323. mapid = mapname
  324. # Get a new instance of the space time dataset map type
  325. map = sp.get_new_map_instance(mapid)
  326. # In case the map is already registered print a message and continue to the next map
  327. # Put the map into the database
  328. if map.is_in_db(dbif) == False:
  329. # Break in case no valid time is provided
  330. if start == "" or start == None:
  331. dbif.close()
  332. core.fatal("Unable to register " + map.get_type() + " map <" + map.get_id() + ">. The map has no valid time and the start time is not set.")
  333. # Load the data from the grass file database
  334. map.load()
  335. if sp.get_temporal_type() == "absolute":
  336. map.set_time_to_absolute()
  337. else:
  338. map.set_time_to_relative()
  339. # Put it into the temporal database
  340. map.insert(dbif)
  341. else:
  342. map.select(dbif)
  343. if map.get_temporal_type() != sp.get_temporal_type():
  344. dbif.close()
  345. core.fatal("Unable to register " + map.get_type() + " map <" + map.get_id() + ">. The temporal types are different.")
  346. # Set the valid time
  347. if start:
  348. assign_valid_time_to_map(ttype=sp.get_temporal_type(), map=map, start=start, end=None, increment=increment, mult=count, dbif=dbif, interval=interval)
  349. # Finally Register map in the space time dataset
  350. sp.register_map(map, dbif)
  351. count += 1
  352. # Update the space time tables
  353. sp.update_from_registered_maps(dbif)
  354. if connect == True:
  355. dbif.close()
  356. core.percent(num_maps, num_maps, 1)
  357. ###############################################################################
  358. def unregister_maps_from_space_time_datasets(type, name, maps, dbif = None):
  359. """Unregister maps from a single space time dataset or, in case no dataset name is provided,
  360. unregister from all datasets within the maps are registered.
  361. @param type: The type of the maps raster, vector or raster3d
  362. @param name: Name of an existing space time raster dataset. If no name is provided the raster map(s) are unregistered from all space time datasets in which they are registered.
  363. @param maps: A comma separated list of map names
  364. @param dbif: The database interface to be used
  365. """
  366. mapset = core.gisenv()["MAPSET"]
  367. if dbif == None:
  368. dbif = sql_database_interface()
  369. dbif.connect()
  370. connect = True
  371. # In case a space time dataset is specified
  372. if name:
  373. # Check if the dataset name contains the mapset as well
  374. if name.find("@") < 0:
  375. id = name + "@" + mapset
  376. else:
  377. id = name
  378. if type == "raster":
  379. sp = space_time_raster_dataset(id)
  380. if type == "raster3d":
  381. sp = space_time_raster3d_dataset(id)
  382. if type == "vector":
  383. sp = space_time_vector_dataset(id)
  384. if sp.is_in_db(dbif) == False:
  385. dbif.close()
  386. core.fatal("Space time " + sp.get_new_map_instance(None).get_type() + " dataset <" + name + "> not found")
  387. # Build the list of maps
  388. if maps.find(",") == -1:
  389. maplist = (maps,)
  390. else:
  391. maplist = tuple(maps.split(","))
  392. num_maps = len(maplist)
  393. count = 0
  394. for mapname in maplist:
  395. core.percent(count, num_maps, 1)
  396. mapname = mapname.strip()
  397. # Check if the map name contains the mapset as well
  398. if mapname.find("@") < 0:
  399. mapid = mapname + "@" + mapset
  400. else:
  401. mapid = mapname
  402. # Create a new instance with the map type
  403. if type == "raster":
  404. map = raster_dataset(mapid)
  405. if type == "raster3d":
  406. map = raster3d_dataset(mapid)
  407. if type == "vector":
  408. map = vector_dataset(mapid)
  409. # Unregister map if in database
  410. if map.is_in_db(dbif) == True:
  411. if name:
  412. sp.select(dbif)
  413. sp.unregister_map(map, dbif)
  414. else:
  415. map.select(dbif)
  416. map.unregister(dbif)
  417. count += 1
  418. if name:
  419. sp.update_from_registered_maps(dbif)
  420. if connect == True:
  421. dbif.close()
  422. core.percent(num_maps, num_maps, 1)
  423. ###############################################################################
  424. def assign_valid_time_to_maps(type, maps, ttype, start, end=None, increment=None, dbif = None, interval=False):
  425. """Use this method to assign valid time (absolute or relative) to raster,
  426. raster3d and vector datasets.
  427. It takes care of the correct update of the space time datasets from all
  428. registered maps.
  429. Valid end time and increment are mutual exclusive.
  430. @param type: The type of the maps raster, raster3d or vector
  431. @param maps: A comma separated list of map names
  432. @param start: The start date and time of the first raster map (format absolute: "yyyy-mm-dd HH:MM:SS" or "yyyy-mm-dd", format relative 5.0)
  433. @param end: The end date and time of the first raster map (format absolute: "yyyy-mm-dd HH:MM:SS" or "yyyy-mm-dd", format relative 5.0)
  434. @param increment: Time increment between maps for time stamp creation (format absolute: NNN seconds, minutes, hours, days, weeks, months, years; format relative: 1.0)
  435. @param dbif: The database interface to be used
  436. @param interval: If True, time intervals are created in case the start time and an increment is provided
  437. """
  438. if end and increment:
  439. if dbif:
  440. dbif.close()
  441. core.fatal(_("Valid end time and increment are mutual exclusive"))
  442. # List of space time datasets to be updated
  443. splist = {}
  444. # We may need the mapset
  445. mapset = core.gisenv()["MAPSET"]
  446. if dbif == None:
  447. dbif = sql_database_interface()
  448. dbif.connect()
  449. connect = True
  450. if maps.find(",") == -1:
  451. maplist = (maps,)
  452. else:
  453. maplist = tuple(maps.split(","))
  454. num_maps = len(maplist)
  455. count = 0
  456. for mapname in maplist:
  457. core.percent(count, num_maps, 1)
  458. mapname = mapname.strip()
  459. # Check if the map name contains the mapset as well
  460. if mapname.find("@") < 0:
  461. mapid = mapname + "@" + mapset
  462. else:
  463. mapid = mapname
  464. if type == "raster":
  465. map = raster_dataset(mapid)
  466. if type == "raster3d":
  467. map = raster3d_dataset(mapid)
  468. if type == "vector":
  469. map = vector_dataset(mapid)
  470. if map.is_in_db(dbif) == False:
  471. # Load the data from the grass file database
  472. map.load()
  473. if ttype == "absolute":
  474. map.set_time_to_absolute()
  475. else:
  476. map.set_time_to_relative()
  477. # Put it into the temporal database
  478. map.insert(dbif)
  479. else:
  480. map.select(dbif)
  481. sprows = map.get_registered_datasets(dbif)
  482. # Make an entry in the dataset list, using a dict make sure that
  483. # each dataset is listed only once
  484. if sprows != None:
  485. for dataset in sprows:
  486. splist[dataset["id"]] = True
  487. # Set the valid time
  488. assign_valid_time_to_map(ttype=ttype, map=map, start=start, end=end, increment=increment, mult=count, dbif=dbif, interval=interval)
  489. count += 1
  490. # Update all the space time datasets in which registered maps are changed there valid time
  491. for name in splist.keys():
  492. sp = map.get_new_stds_instance(name)
  493. sp.select(dbif)
  494. sp.update_from_registered_maps(dbif)
  495. if connect == True:
  496. dbif.close()
  497. core.percent(num_maps, num_maps, 1)
  498. ###############################################################################
  499. def assign_valid_time_to_map(ttype, map, start, end, increment=None, mult=1, dbif = None, interval=False):
  500. """Assign the valid time to a map dataset
  501. @param ttype: The temporal type which should be assigned and which the time format is of
  502. @param map: A map dataset object derived from abstract_map_dataset
  503. @param start: The start date and time of the first raster map (format absolute: "yyyy-mm-dd HH:MM:SS" or "yyyy-mm-dd", format relative 5.0)
  504. @param end: The end date and time of the first raster map (format absolute: "yyyy-mm-dd HH:MM:SS" or "yyyy-mm-dd", format relative 5.0)
  505. @param increment: Time increment between maps for time stamp creation (format absolute: NNN seconds, minutes, hours, days, weeks, months, years; format relative: 1.0)
  506. @param multi: A multiplier for the increment
  507. @param dbif: The database interface to use for sql queries
  508. @param interval: If True, time intervals are created in case the start time and an increment is provided
  509. """
  510. connect = False
  511. if dbif == None:
  512. dbif = sql_database_interface()
  513. dbif.connect()
  514. connect = True
  515. if ttype == "absolute":
  516. # Create the start time object
  517. if start.find(":") > 0:
  518. time_format = "%Y-%m-%d %H:%M:%S"
  519. else:
  520. time_format = "%Y-%m-%d"
  521. start_time = datetime.strptime(start, time_format)
  522. end_time = None
  523. if end:
  524. end_time = datetime.strptime(end, time_format)
  525. # Add the increment
  526. if increment:
  527. start_time = increment_datetime_by_string(start_time, increment, mult)
  528. if interval:
  529. end_time = increment_datetime_by_string(start_time, increment, 1)
  530. core.verbose(_("Set absolute valid time for map <%s> to %s - %s") % (map.get_id(), str(start_time), str(end_time)))
  531. map.update_absolute_time(start_time, end_time, None, dbif)
  532. else:
  533. start_time = float(start)
  534. end_time = None
  535. if end:
  536. end_time = float(end)
  537. if increment:
  538. start_time = start_time + mult * float(increment)
  539. if interval:
  540. end_time = start_time + float(increment)
  541. core.verbose(_("Set relative valid time for map <%s> to %f - %s") % (map.get_id(), start_time, str(end_time)))
  542. map.update_relative_time(start_time, end_time, dbif)
  543. if connect == True:
  544. dbif.close()