open_stds.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. """
  2. Functions to open or create space time datasets
  3. Usage:
  4. .. code-block:: python
  5. import grass.temporal as tgis
  6. tgis.register_maps_in_space_time_dataset(type, name, maps)
  7. (C) 2012-2014 by the GRASS Development Team
  8. This program is free software under the GNU General Public
  9. License (>=v2). Read the file COPYING that comes with GRASS
  10. for details.
  11. :authors: Soeren Gebbert
  12. """
  13. from factory import *
  14. ###############################################################################
  15. def open_old_stds(name, type, dbif=None):
  16. """This function opens an existing space time dataset and return the
  17. created and intialized object of the specified type.
  18. This function will call exit() or raise a
  19. grass.pygrass.messages.FatalError in case the type is wrong,
  20. or the space time dataset was not found.
  21. :param name: The name of the space time dataset, if the name does not
  22. contain the mapset (name@mapset) then the current mapset
  23. will be used to identifiy the space time dataset
  24. :param type: The type of the space time dataset (strd, str3ds, stvds,
  25. raster, vector, raster3d)
  26. :param dbif: The optional database interface to be used
  27. """
  28. mapset = get_current_mapset()
  29. msgr = get_tgis_message_interface()
  30. # Check if the dataset name contains the mapset as well
  31. if name.find("@") < 0:
  32. id = name + "@" + mapset
  33. else:
  34. id = name
  35. if type == "strds" or type == "rast" or type == "raster":
  36. sp = dataset_factory("strds", id)
  37. elif type == "str3ds" or type == "rast3d" or type == "raster3d":
  38. sp = dataset_factory("str3ds", id)
  39. elif type == "stvds" or type == "vect" or type == "vector":
  40. sp = dataset_factory("stvds", id)
  41. else:
  42. msgr.fatal(_("Unkown type: %s") % (type))
  43. dbif, connected = init_dbif(dbif)
  44. if not sp.is_in_db(dbif):
  45. dbif.close()
  46. msgr.fatal(_("Space time %(sp)s dataset <%(name)s> no found") %
  47. {'sp': sp.get_new_map_instance(None).get_type(),
  48. 'name': name})
  49. # Read content from temporal database
  50. sp.select(dbif)
  51. if connected:
  52. dbif.close()
  53. return sp
  54. ###############################################################################
  55. def check_new_stds(name, type, dbif=None, overwrite=False):
  56. """Check if a new space time dataset of a specific type can be created
  57. :param name: The name of the new space time dataset
  58. :param type: The type of the new space time dataset (strd, str3ds,
  59. stvds, raster, vector, raster3d)
  60. :param dbif: The temporal database interface to be used
  61. :param overwrite: Flag to allow overwriting
  62. :return: A space time dataset object that must be filled with
  63. content before insertion in the temporal database
  64. This function will raise a FatalError in case of an error.
  65. """
  66. # Get the current mapset to create the id of the space time dataset
  67. mapset = get_current_mapset()
  68. msgr = get_tgis_message_interface()
  69. if name.find("@") < 0:
  70. id = name + "@" + mapset
  71. else:
  72. n, m = name.split("@")
  73. if mapset != m:
  74. msgr.fatal(_("Space time datasets can only be created in the "
  75. "current mapset"))
  76. id = name
  77. if type == "strds" or type == "rast" or type == "raster":
  78. sp = dataset_factory("strds", id)
  79. elif type == "str3ds" or type == "rast3d" or type == "raster3d":
  80. sp = dataset_factory("str3ds", id)
  81. elif type == "stvds" or type == "vect" or type == "vector":
  82. sp = dataset_factory("stvds", id)
  83. else:
  84. msgr.error(_("Unkown type: %s") % (type))
  85. return None
  86. dbif, connected = init_dbif(dbif)
  87. if sp.is_in_db(dbif) and overwrite is False:
  88. msgr.fatal(_("Space time %(sp)s dataset <%(name)s> is already in the"
  89. " database. Use the overwrite flag.") % {
  90. 'sp': sp.get_new_map_instance(None).get_type(),
  91. 'name': name})
  92. if connected:
  93. dbif.close()
  94. return sp
  95. ###############################################################################
  96. def open_new_stds(name, type, temporaltype, title, descr, semantic,
  97. dbif=None, overwrite=False):
  98. """Create a new space time dataset of a specific type
  99. :param name: The name of the new space time dataset
  100. :param type: The type of the new space time dataset (strd, str3ds,
  101. stvds, raster, vector, raster3d)
  102. :param temporaltype: The temporal type (relative or absolute)
  103. :param title: The title
  104. :param descr: The dataset description
  105. :param semantic: Semantical information
  106. :param dbif: The temporal database interface to be used
  107. :param overwrite: Flag to allow overwriting
  108. :return: The new created space time dataset
  109. This function will raise a FatalError in case of an error.
  110. """
  111. dbif, connected = init_dbif(dbif)
  112. msgr = get_tgis_message_interface()
  113. sp = check_new_stds(name, type, dbif, overwrite)
  114. if sp.is_in_db(dbif):
  115. msgr.warning(_("Overwriting space time %(sp)s dataset <%(name)s> and "
  116. "unregistering all maps") % {
  117. 'sp': sp.get_new_map_instance(None).get_type(),
  118. 'name': name})
  119. id = sp.get_id()
  120. sp.delete(dbif)
  121. sp = sp.get_new_instance(id)
  122. msgr.verbose(_("Creating a new space time %s dataset") %
  123. sp.get_new_map_instance(None).get_type())
  124. sp.set_initial_values(temporal_type=temporaltype, semantic_type=semantic,
  125. title=title, description=descr)
  126. sp.insert(dbif)
  127. if connected:
  128. dbif.close()
  129. return sp
  130. ############################################################################
  131. def check_new_map_dataset(name, layer=None, type="raster",
  132. overwrite=False, dbif=None):
  133. """Check if a new map dataset of a specific type can be created in
  134. the temporal database
  135. :param name: The name of the new map dataset
  136. :param layer: The layer of the new map dataset
  137. :param type: The type of the new map dataset (raster, vector, raster3d)
  138. :param dbif: The temporal database interface to be used
  139. :param overwrite: Flag to allow overwriting
  140. :return: A map dataset object
  141. This function will raise a FatalError in case of an error.
  142. """
  143. mapset = get_current_mapset()
  144. msgr = get_tgis_message_interface()
  145. dbif, connected = init_dbif(dbif)
  146. map_id = AbstractMapDataset.build_id(name, mapset, layer)
  147. new_map = dataset_factory(type, map_id)
  148. # Check if new map is in the temporal database
  149. if new_map.is_in_db(dbif):
  150. if not overwrite:
  151. if connected:
  152. dbif.close()
  153. msgr.fatal(_("Map <%s> is already in temporal database,"
  154. " use overwrite flag to overwrite") % (map_id))
  155. if connected:
  156. dbif.close()
  157. return new_map
  158. ############################################################################
  159. def open_new_map_dataset(name, layer=None, type="raster",
  160. temporal_extent=None, overwrite=False,
  161. dbif=None):
  162. """Create a new map dataset object of a specific type that can be
  163. registered in the temporal database
  164. :param name: The name of the new map dataset
  165. :param layer: The layer of the new map dataset
  166. :param type: The type of the new map dataset (raster, vector, raster3d)
  167. :param dbif: The temporal database interface to be used
  168. :param overwrite: Flag to allow overwriting
  169. :return: A map dataset object
  170. """
  171. mapset = get_current_mapset()
  172. dbif, connected = init_dbif(dbif)
  173. new_map = check_new_map_dataset(name, layer, "raster", overwrite, dbif)
  174. # Check if new map is in the temporal database
  175. if new_map.is_in_db(dbif):
  176. # Remove the existing temporal database entry
  177. map_id = new_map.get_id()
  178. new_map.delete(dbif)
  179. new_map = new_map.get_new_instance(map_id)
  180. if temporal_extent:
  181. new_map.set_temporal_extent(temporal_extent)
  182. if connected:
  183. dbif.close()
  184. return new_map