space_time_datasets_tools.py 33 KB

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