open_stds.py 8.4 KB

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