open_stds.py 8.4 KB

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