space_time_datasets_tools.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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. tgis.register_maps_in_space_time_dataset(type, name, maps)
  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 space_time_datasets import *
  17. ###############################################################################
  18. def register_maps_in_space_time_dataset(
  19. type, name, maps=None, file=None, start=None,
  20. end=None, unit=None, increment=None, dbif=None,
  21. interval=False, fs="|"):
  22. """!Use this method to register maps in space time datasets.
  23. Additionally a start time string and an increment string can be specified
  24. to assign a time interval automatically to the maps.
  25. It takes care of the correct update of the space time datasets from all
  26. registered maps.
  27. @param type: The type of the maps rast, rast3d or vect
  28. @param name: The name of the space time dataset
  29. @param maps: A comma separated list of map names
  30. @param file: Input file one map with start and optional end time,
  31. one per line
  32. @param start: The start date and time of the first raster map
  33. (format absolute: "yyyy-mm-dd HH:MM:SS" or "yyyy-mm-dd",
  34. format relative is integer 5)
  35. @param end: The end date and time of the first raster map
  36. (format absolute: "yyyy-mm-dd HH:MM:SS" or "yyyy-mm-dd",
  37. format relative is integer 5)
  38. @param unit: The unit of the relative time: years, months, days,
  39. hours, minutes, seconds
  40. @param increment: Time increment between maps for time stamp creation
  41. (format absolute: NNN seconds, minutes, hours, days,
  42. weeks, months, years; format relative: 1.0)
  43. @param dbif: The database interface to be used
  44. @param interval: If True, time intervals are created in case the start
  45. time and an increment is provided
  46. @param fs: Field separator used in input file
  47. """
  48. start_time_in_file = False
  49. end_time_in_file = False
  50. if maps and file:
  51. core.fatal(_("%s= and %s= are mutually exclusive") % ("maps", "file"))
  52. if end and increment:
  53. core.fatal(_("%s= and %s= are mutually exclusive") % (
  54. "end", "increment"))
  55. if end and not start:
  56. core.fatal(_("Please specify %s= and %s=") % ("start_time",
  57. "end_time"))
  58. if not maps and not file:
  59. core.fatal(_("Please specify %s= or %s=") % ("maps", "file"))
  60. # We may need the mapset
  61. mapset = core.gisenv()["MAPSET"]
  62. # The name of the space time dataset is optional
  63. if name:
  64. # Check if the dataset name contains the mapset as well
  65. if name.find("@") < 0:
  66. id = name + "@" + mapset
  67. else:
  68. id = name
  69. if type == "rast" or type == "raster":
  70. sp = dataset_factory("strds", id)
  71. elif type == "rast3d":
  72. sp = dataset_factory("str3ds", id)
  73. elif type == "vect" or type == "vector":
  74. sp = dataset_factory("stvds", id)
  75. else:
  76. core.fatal(_("Unkown map type: %s") % (type))
  77. dbif, connect = init_dbif(None)
  78. if name:
  79. # Read content from temporal database
  80. sp.select(dbif)
  81. if not sp.is_in_db(dbif):
  82. dbif.close()
  83. core.fatal(_("Space time %s dataset <%s> no found") %
  84. (sp.get_new_map_instance(None).get_type(), name))
  85. if sp.is_time_relative() and not unit:
  86. dbif.close()
  87. core.fatal(_("Space time %s dataset <%s> with relative time found, "
  88. "but no relative unit set for %s maps") %
  89. (sp.get_new_map_instance(None).get_type(),
  90. name, sp.get_new_map_instance(None).get_type()))
  91. # We need a dummy map object to build the map ids
  92. dummy = dataset_factory(type, None)
  93. maplist = []
  94. # Map names as comma separated string
  95. if maps:
  96. if maps.find(",") < 0:
  97. maplist = [maps, ]
  98. else:
  99. maplist = maps.split(",")
  100. # Build the map list again with the ids
  101. for count in range(len(maplist)):
  102. row = {}
  103. mapid = dummy.build_id(maplist[count], mapset, None)
  104. row["id"] = mapid
  105. maplist[count] = row
  106. # Read the map list from file
  107. if file:
  108. fd = open(file, "r")
  109. line = True
  110. while True:
  111. line = fd.readline()
  112. if not line:
  113. break
  114. line_list = line.split(fs)
  115. # Detect start and end time
  116. if len(line_list) == 2:
  117. start_time_in_file = True
  118. end_time_in_file = False
  119. elif len(line_list) == 3:
  120. start_time_in_file = True
  121. end_time_in_file = True
  122. else:
  123. start_time_in_file = False
  124. end_time_in_file = False
  125. mapname = line_list[0].strip()
  126. row = {}
  127. if start_time_in_file and end_time_in_file:
  128. row["start"] = line_list[1].strip()
  129. row["end"] = line_list[2].strip()
  130. if start_time_in_file and not end_time_in_file:
  131. row["start"] = line_list[1].strip()
  132. row["id"] = dummy.build_id(mapname, mapset)
  133. maplist.append(row)
  134. num_maps = len(maplist)
  135. map_object_list = []
  136. statement = ""
  137. # Store the ids of datasets that must be updated
  138. datatsets_to_modify = {}
  139. core.message(_("Gathering map informations"))
  140. for count in range(len(maplist)):
  141. core.percent(count, num_maps, 1)
  142. # Get a new instance of the map type
  143. map = dataset_factory(type, maplist[count]["id"])
  144. # Use the time data from file
  145. if "start" in maplist[count]:
  146. start = maplist[count]["start"]
  147. if "end" in maplist[count]:
  148. end = maplist[count]["end"]
  149. is_in_db = False
  150. # Put the map into the database
  151. if not map.is_in_db(dbif):
  152. is_in_db = False
  153. # Break in case no valid time is provided
  154. if start == "" or start is None:
  155. dbif.close()
  156. if map.get_layer():
  157. core.fatal(_("Unable to register %s map <%s> with layer %s. "
  158. "The map has no valid time and the start time is not set.") %
  159. (map.get_type(), map.get_map_id(), map.get_layer()))
  160. else:
  161. core.fatal(_("Unable to register %s map <%s>. The map has no valid"
  162. " time and the start time is not set.") %
  163. (map.get_type(), map.get_map_id()))
  164. if unit:
  165. map.set_time_to_relative()
  166. else:
  167. map.set_time_to_absolute()
  168. else:
  169. is_in_db = True
  170. # Check the overwrite flag
  171. if not core.overwrite():
  172. if map.get_layer():
  173. core.warning(_("Map is already registered in temporal database. "
  174. "Unable to update %s map <%s> with layer %s. "
  175. "Overwrite flag is not set.") %
  176. (map.get_type(), map.get_map_id(), str(map.get_layer())))
  177. else:
  178. core.warning(_("Map is already registered in temporal database. "
  179. "Unable to update %s map <%s>. "
  180. "Overwrite flag is not set.") %
  181. (map.get_type(), map.get_map_id()))
  182. # Simple registration is allowed
  183. if name:
  184. map_object_list.append(map)
  185. # Jump to next map
  186. continue
  187. # Select information from temporal database
  188. map.select(dbif)
  189. # Save the datasets that must be updated
  190. datasets = map.get_registered_datasets(dbif)
  191. if datasets:
  192. for dataset in datasets:
  193. datatsets_to_modify[dataset["id"]] = dataset["id"]
  194. if name and map.get_temporal_type() != sp.get_temporal_type():
  195. dbif.close()
  196. if map.get_layer():
  197. core.fatal(_("Unable to update %s map <%s> with layer. "
  198. "The temporal types are different.") %
  199. (map.get_type(), map.get_map_id(), map.get_layer()))
  200. else:
  201. core.fatal(_("Unable to update %s map <%s>. "
  202. "The temporal types are different.") %
  203. (map.get_type(), map.get_map_id()))
  204. # Load the data from the grass file database
  205. map.load()
  206. # Set the valid time
  207. if start:
  208. # In case the time is in the input file we ignore the increment counter
  209. if start_time_in_file:
  210. count = 1
  211. assign_valid_time_to_map(ttype=map.get_temporal_type(),
  212. map=map, start=start, end=end, unit=unit,
  213. increment=increment, mult=count,
  214. interval=interval)
  215. if is_in_db:
  216. # Gather the SQL update statement
  217. statement += map.update_all(dbif=dbif, execute=False)
  218. else:
  219. # Gather the SQL insert statement
  220. statement += map.insert(dbif=dbif, execute=False)
  221. # Sqlite3 performace better for huge datasets when committing in small chunks
  222. if dbif.dbmi.__name__ == "sqlite3":
  223. if count % 100 == 0:
  224. if statement is not None and statement != "":
  225. core.message(_("Registering maps in the temporal database")
  226. )
  227. dbif.execute_transaction(statement)
  228. statement = ""
  229. # Store the maps in a list to register in a space time dataset
  230. if name:
  231. map_object_list.append(map)
  232. core.percent(num_maps, num_maps, 1)
  233. if statement is not None and statement != "":
  234. core.message(_("Register maps in the temporal database"))
  235. dbif.execute_transaction(statement)
  236. # Finally Register the maps in the space time dataset
  237. if name and map_object_list:
  238. statement = ""
  239. count = 0
  240. num_maps = len(map_object_list)
  241. core.message(_("Register maps in the space time raster dataset"))
  242. for map in map_object_list:
  243. core.percent(count, num_maps, 1)
  244. sp.register_map(map=map, dbif=dbif)
  245. count += 1
  246. # Update the space time tables
  247. if name and map_object_list:
  248. core.message(_("Update space time raster dataset"))
  249. sp.update_from_registered_maps(dbif)
  250. # Update affected datasets
  251. if datatsets_to_modify:
  252. for dataset in datatsets_to_modify:
  253. if type == "rast" or type == "raster":
  254. ds = dataset_factory("strds", dataset)
  255. elif type == "rast3d":
  256. ds = dataset_factory("str3ds", dataset)
  257. elif type == "vect" or type == "vector":
  258. ds = dataset_factory("stvds", dataset)
  259. ds.select(dbif)
  260. ds.update_from_registered_maps(dbif)
  261. if connect == True:
  262. dbif.close()
  263. core.percent(num_maps, num_maps, 1)
  264. ###############################################################################
  265. def assign_valid_time_to_map(ttype, map, start, end, unit, increment=None, mult=1, interval=False):
  266. """!Assign the valid time to a map dataset
  267. @param ttype: The temporal type which should be assigned
  268. and which the time format is of
  269. @param map: A map dataset object derived from abstract_map_dataset
  270. @param start: The start date and time of the first raster map
  271. (format absolute: "yyyy-mm-dd HH:MM:SS" or "yyyy-mm-dd",
  272. format relative is integer 5)
  273. @param end: The end date and time of the first raster map
  274. (format absolute: "yyyy-mm-dd HH:MM:SS" or "yyyy-mm-dd",
  275. format relative is integer 5)
  276. @param unit: The unit of the relative time: years, months,
  277. days, hours, minutes, seconds
  278. @param increment: Time increment between maps for time stamp creation
  279. (format absolute: NNN seconds, minutes, hours, days,
  280. weeks, months, years; format relative is integer 1)
  281. @param multi: A multiplier for the increment
  282. @param interval: If True, time intervals are created in case the start
  283. time and an increment is provided
  284. """
  285. if ttype == "absolute":
  286. start_time = string_to_datetime(start)
  287. if start_time is None:
  288. core.fatal(_("Unable to convert string \"%s\"into a "
  289. "datetime object") % (start))
  290. end_time = None
  291. if end:
  292. end_time = string_to_datetime(end)
  293. if end_time is None:
  294. dbif.close()
  295. core.fatal(_("Unable to convert string \"%s\"into a "
  296. "datetime object") % (end))
  297. # Add the increment
  298. if increment:
  299. start_time = increment_datetime_by_string(
  300. start_time, increment, mult)
  301. if start_time is None:
  302. core.fatal(_("Error in increment computation"))
  303. if interval:
  304. end_time = increment_datetime_by_string(
  305. start_time, increment, 1)
  306. if end_time is None:
  307. core.fatal(_("Error in increment computation"))
  308. if map.get_layer():
  309. core.verbose(_("Set absolute valid time for map <%(id)s> with "
  310. "layer %(layer)s to %(start)s - %(end)s") %
  311. {'id': map.get_map_id(), 'layer': map.get_layer(),
  312. 'start': str(start_time), 'end': str(end_time)})
  313. else:
  314. core.verbose(_("Set absolute valid time for map <%s> to %s - %s") %
  315. (map.get_map_id(), str(start_time), str(end_time)))
  316. map.set_absolute_time(start_time, end_time, None)
  317. else:
  318. start_time = int(start)
  319. end_time = None
  320. if end:
  321. end_time = int(end)
  322. if increment:
  323. start_time = start_time + mult * int(increment)
  324. if interval:
  325. end_time = start_time + int(increment)
  326. if map.get_layer():
  327. core.verbose(_("Set relative valid time for map <%s> with layer %s "
  328. "to %i - %s with unit %s") %
  329. (map.get_map_id(), map.get_layer(), start_time,
  330. str(end_time), unit))
  331. else:
  332. core.verbose(_("Set relative valid time for map <%s> to %i - %s "
  333. "with unit %s") % (map.get_map_id(), start_time,
  334. str(end_time), unit))
  335. map.set_relative_time(start_time, end_time, unit)
  336. ###############################################################################
  337. def dataset_factory(type, id):
  338. """!A factory functions to create space time or map datasets
  339. @param type: the dataset type: rast or raster, rast3d,
  340. vect or vector, strds, str3ds, stvds
  341. @param id: The id of the dataset ("name@mapset")
  342. """
  343. if type == "strds":
  344. sp = SpaceTimeRasterDataset(id)
  345. elif type == "str3ds":
  346. sp = SpaceTimeRaster3DDataset(id)
  347. elif type == "stvds":
  348. sp = SpaceTimeVectorDataset(id)
  349. elif type == "rast" or type == "raster":
  350. sp = RasterDataset(id)
  351. elif type == "rast3d":
  352. sp = Raster3DDataset(id)
  353. elif type == "vect" or type == "vector":
  354. sp = VectorDataset(id)
  355. else:
  356. core.error(_("Unknown dataset type: %s") % type)
  357. return None
  358. return sp
  359. ###############################################################################
  360. def list_maps_of_stds(type, input, columns, order, where, separator, method, header):
  361. """! List the maps of a space time dataset using diffetent methods
  362. @param type: The type of the maps raster, raster3d or vector
  363. @param input: Name of a space time raster dataset
  364. @param columns: A comma separated list of columns to be printed to stdout
  365. @param order: A comma separated list of columns to order the
  366. space time dataset by category
  367. @param where: A where statement for selected listing without "WHERE"
  368. e.g: start_time < "2001-01-01" and end_time > "2001-01-01"
  369. @param separator: The field separator character between the columns
  370. @param method: String identifier to select a method out of cols,
  371. comma,delta or deltagaps
  372. - "cols": Print preselected columns specified by columns
  373. - "comma": Print the map ids (name@mapset) as comma separated string
  374. - "delta": Print the map ids (name@mapset) with start time,
  375. end time, relative length of intervals and the relative
  376. distance to the begin
  377. - "deltagaps": Same as "delta" with additional listing of gaps.
  378. Gaps can be simply identified as the id is "None"
  379. - "gran": List map using the granularity of the space time dataset,
  380. columns are identical to deltagaps
  381. @param header: Set True to print column names
  382. """
  383. mapset = core.gisenv()["MAPSET"]
  384. if input.find("@") >= 0:
  385. id = input
  386. else:
  387. id = input + "@" + mapset
  388. sp = dataset_factory(type, id)
  389. if not sp.is_in_db():
  390. core.fatal(_("Dataset <%s> not found in temporal database") % (id))
  391. sp.select()
  392. if separator is None or separator == "":
  393. separator = "\t"
  394. # This method expects a list of objects for gap detection
  395. if method == "delta" or method == "deltagaps" or method == "gran":
  396. if type == "stvds":
  397. columns = "id,name,layer,mapset,start_time,end_time"
  398. else:
  399. columns = "id,name,mapset,start_time,end_time"
  400. if method == "deltagaps":
  401. maps = sp.get_registered_maps_as_objects_with_gaps(where, None)
  402. elif method == "delta":
  403. maps = sp.get_registered_maps_as_objects(where, "start_time", None)
  404. elif method == "gran":
  405. maps = sp.get_registered_maps_as_objects_by_granularity(None)
  406. if header:
  407. string = ""
  408. string += "%s%s" % ("id", separator)
  409. string += "%s%s" % ("name", separator)
  410. if type == "stvds":
  411. string += "%s%s" % ("layer", separator)
  412. string += "%s%s" % ("mapset", separator)
  413. string += "%s%s" % ("start_time", separator)
  414. string += "%s%s" % ("end_time", separator)
  415. string += "%s%s" % ("interval_length", separator)
  416. string += "%s" % ("distance_from_begin")
  417. print string
  418. if maps and len(maps) > 0:
  419. if isinstance(maps[0], list):
  420. if len(maps[0]) > 0:
  421. first_time, dummy = maps[0][0].get_valid_time()
  422. else:
  423. core.warning(_("Empty map list."))
  424. return
  425. else:
  426. first_time, dummy = maps[0].get_valid_time()
  427. for mymap in maps:
  428. if isinstance(mymap, list):
  429. if len(mymap) > 0:
  430. map = mymap[0]
  431. else:
  432. core.fatal(_("Empty entry in map list, this should not happen."))
  433. else:
  434. map = mymap
  435. start, end = map.get_valid_time()
  436. if end:
  437. delta = end - start
  438. else:
  439. delta = None
  440. delta_first = start - first_time
  441. if map.is_time_absolute():
  442. if end:
  443. delta = time_delta_to_relative_time(delta)
  444. delta_first = time_delta_to_relative_time(delta_first)
  445. string = ""
  446. string += "%s%s" % (map.get_id(), separator)
  447. string += "%s%s" % (map.get_name(), separator)
  448. if type == "stvds":
  449. string += "%s%s" % (map.get_layer(), separator)
  450. string += "%s%s" % (map.get_mapset(), separator)
  451. string += "%s%s" % (start, separator)
  452. string += "%s%s" % (end, separator)
  453. string += "%s%s" % (delta, separator)
  454. string += "%s" % (delta_first)
  455. print string
  456. else:
  457. # In comma separated mode only map ids are needed
  458. if method == "comma":
  459. columns = "id"
  460. rows = sp.get_registered_maps(columns, where, order, None)
  461. if rows:
  462. if method == "comma":
  463. string = ""
  464. count = 0
  465. for row in rows:
  466. if count == 0:
  467. string += row["id"]
  468. else:
  469. string += ",%s" % row["id"]
  470. count += 1
  471. print string
  472. elif method == "cols":
  473. # Print the column names if requested
  474. if header:
  475. output = ""
  476. count = 0
  477. collist = columns.split(",")
  478. for key in collist:
  479. if count > 0:
  480. output += separator + str(key)
  481. else:
  482. output += str(key)
  483. count += 1
  484. print output
  485. for row in rows:
  486. output = ""
  487. count = 0
  488. for col in row:
  489. if count > 0:
  490. output += separator + str(col)
  491. else:
  492. output += str(col)
  493. count += 1
  494. print output
  495. ###############################################################################
  496. def sample_stds_by_stds_topology(intype, sampletype, inputs, sampler, header,
  497. separator, method, spatial=False,
  498. print_only=True):
  499. """!Sample the input space time datasets with a sample
  500. space time dataset, return the created map matrix and optionally
  501. print the result to stdout
  502. In case multiple maps are located in the current granule,
  503. the map names are separated by comma.
  504. In case a layer is present, the names map ids are extended
  505. in this form: name:layer@mapset
  506. Attention: Do not use the comma as separator for printing
  507. @param intype: Type of the input space time dataset (strds, stvds or str3ds)
  508. @param samtype: Type of the sample space time dataset (strds, stvds or str3ds)
  509. @param inputs: Name or comma separated names of space time datasets
  510. @param sampler: Name of a space time dataset used for temporal sampling
  511. @param header: Set True to print column names
  512. @param separator: The field separator character between the columns
  513. @param method: The method to be used for temporal sampling
  514. (start,during,contain,overlap,equal)
  515. @param spatial: Perform spatial overlapping check
  516. @param print_only: If set True (default) then the result of the sampling will be
  517. printed to stdout, if set to False the resulting map matrix
  518. will be returned.
  519. @return The map matrix or None if nothing found
  520. """
  521. mapset = core.gisenv()["MAPSET"]
  522. # Make a method list
  523. method = method.split(",")
  524. # Split the inputs
  525. input_list = inputs.split(",")
  526. sts = []
  527. for input in input_list:
  528. if input.find("@") >= 0:
  529. id = input
  530. else:
  531. id = input + "@" + mapset
  532. st = dataset_factory(intype, id)
  533. sts.append(st)
  534. if sampler.find("@") >= 0:
  535. sid = sampler
  536. else:
  537. sid = sampler + "@" + mapset
  538. sst = dataset_factory(sampletype, sid)
  539. dbif = SQLDatabaseInterfaceConnection()
  540. dbif.connect()
  541. for st in sts:
  542. if st.is_in_db(dbif) == False:
  543. core.fatal(_("Dataset <%s> not found in temporal database") % (id))
  544. st.select(dbif)
  545. if sst.is_in_db(dbif) == False:
  546. core.fatal(_("Dataset <%s> not found in temporal database") % (sid))
  547. sst.select(dbif)
  548. if separator is None or separator == "" or separator.find(",") >= 0:
  549. separator = " | "
  550. mapmatrizes = []
  551. for st in sts:
  552. mapmatrix = st.sample_by_dataset(sst, method, spatial, dbif)
  553. if mapmatrix and len(mapmatrix) > 0:
  554. mapmatrizes.append(mapmatrix)
  555. if len(mapmatrizes) > 0:
  556. # Simply return the map matrix
  557. if not print_only:
  558. dbif.close()
  559. return mapmatrizes
  560. if header:
  561. string = ""
  562. string += "%s%s" % (sst.get_id(), separator)
  563. for st in sts:
  564. string += "%s%s" % (st.get_id(), separator)
  565. string += "%s%s" % ("start_time", separator)
  566. string += "%s%s" % ("end_time", separator)
  567. string += "%s%s" % ("interval_length", separator)
  568. string += "%s" % ("distance_from_begin")
  569. print string
  570. first_time, dummy = mapmatrizes[0][0]["granule"].get_valid_time()
  571. for i in range(len(mapmatrizes[0])):
  572. mapname_list = []
  573. for mapmatrix in mapmatrizes:
  574. mapnames = ""
  575. count = 0
  576. entry = mapmatrix[i]
  577. for sample in entry["samples"]:
  578. if count == 0:
  579. mapnames += str(sample.get_id())
  580. else:
  581. mapnames += ",%s" % str(sample.get_id())
  582. count += 1
  583. mapname_list.append(mapnames)
  584. entry = mapmatrizes[0][i]
  585. map = entry["granule"]
  586. start, end = map.get_valid_time()
  587. if end:
  588. delta = end - start
  589. else:
  590. delta = None
  591. delta_first = start - first_time
  592. if map.is_time_absolute():
  593. if end:
  594. delta = time_delta_to_relative_time(delta)
  595. delta_first = time_delta_to_relative_time(delta_first)
  596. string = ""
  597. string += "%s%s" % (map.get_id(), separator)
  598. for mapnames in mapname_list:
  599. string += "%s%s" % (mapnames, separator)
  600. string += "%s%s" % (start, separator)
  601. string += "%s%s" % (end, separator)
  602. string += "%s%s" % (delta, separator)
  603. string += "%s" % (delta_first)
  604. print string
  605. dbif.close()
  606. if len(mapmatrizes) > 0:
  607. return mapmatrizes
  608. return None
  609. ###############################################################################
  610. def tlist_grouped(type, group_type = False):
  611. """!List of temporal elements grouped by mapsets.
  612. Returns a dictionary where the keys are mapset
  613. names and the values are lists of space time datasets in that
  614. mapset. Example:
  615. @code
  616. >>> tgis.tlist_grouped('strds')['PERMANENT']
  617. ['precipitation', 'temperature']
  618. @endcode
  619. @param type element type (strds, str3ds, stvds)
  620. @return directory of mapsets/elements
  621. """
  622. result = {}
  623. mapset = None
  624. if type == 'stds':
  625. types = ['strds', 'str3ds', 'stvds']
  626. else:
  627. types = [type]
  628. for type in types:
  629. try:
  630. tlist_result = tlist(type)
  631. except core.ScriptError, e:
  632. warning(e)
  633. continue
  634. for line in tlist_result:
  635. try:
  636. name, mapset = line.split('@')
  637. except ValueError:
  638. warning(_("Invalid element '%s'") % line)
  639. continue
  640. if mapset not in result:
  641. if group_type:
  642. result[mapset] = {}
  643. else:
  644. result[mapset] = []
  645. if group_type:
  646. if type in result[mapset]:
  647. result[mapset][type].append(name)
  648. else:
  649. result[mapset][type] = [name, ]
  650. else:
  651. result[mapset].append(name)
  652. return result
  653. ###############################################################################
  654. def tlist(type):
  655. """!Return a list of space time datasets of absolute and relative time
  656. @param type element type (strds, str3ds, stvds)
  657. @return a list of space time dataset ids
  658. """
  659. id = None
  660. sp = dataset_factory(type, id)
  661. dbif = SQLDatabaseInterfaceConnection()
  662. dbif.connect()
  663. output = []
  664. temporal_type = ["absolute", 'relative']
  665. for type in temporal_type:
  666. # Table name
  667. if type == "absolute":
  668. table = sp.get_type() + "_view_abs_time"
  669. else:
  670. table = sp.get_type() + "_view_rel_time"
  671. # Create the sql selection statement
  672. sql = "SELECT id FROM " + table
  673. sql += " ORDER BY id"
  674. dbif.cursor.execute(sql)
  675. rows = dbif.cursor.fetchall()
  676. # Append the ids of the space time datasets
  677. for row in rows:
  678. for col in row:
  679. output.append(str(col))
  680. dbif.close()
  681. return output
  682. ###############################################################################
  683. def create_space_time_dataset(name, type, temporaltype, title, descr, semantic,
  684. dbif=None, overwrite=False):
  685. """!Create a new space time dataset
  686. This function is sensitive to the settings in grass.core.overwrite to
  687. overwrute existing space time datasets.
  688. @param name: The name of the new space time dataset
  689. @param type: The type (strds, stvds, str3ds) of the new space time dataset
  690. @param temporaltype: The temporal type (relative or absolute)
  691. @param title: The title
  692. @param descr: The dataset description
  693. @param semantic: Semantical information
  694. @param dbif: The temporal database interface to be used
  695. @return The new created space time dataset
  696. This function will raise a ScriptError in case of an error.
  697. """
  698. #Get the current mapset to create the id of the space time dataset
  699. mapset = core.gisenv()["MAPSET"]
  700. id = name + "@" + mapset
  701. sp = dataset_factory(type, id)
  702. dbif, connect = init_dbif(dbif)
  703. if sp.is_in_db(dbif) and overwrite == False:
  704. if connect:
  705. dbif.close()
  706. core.fatal(_("Space time %s dataset <%s> is already in the database. "
  707. "Use the overwrite flag.") %
  708. (sp.get_new_map_instance(None).get_type(), name))
  709. return None
  710. if sp.is_in_db(dbif) and overwrite == True:
  711. core.info(_("Overwrite space time %s dataset <%s> "
  712. "and unregister all maps.") %
  713. (sp.get_new_map_instance(None).get_type(), name))
  714. sp.delete(dbif)
  715. sp = sp.get_new_instance(id)
  716. core.verbose(_("Create space time %s dataset.") %
  717. sp.get_new_map_instance(None).get_type())
  718. sp.set_initial_values(temporal_type=temporaltype, semantic_type=semantic,
  719. title=title, description=descr)
  720. sp.insert(dbif)
  721. if connect:
  722. dbif.close()
  723. return sp