open_stds.py 9.0 KB

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