space_time_datasets.py 25 KB

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