open_stds.py 9.2 KB

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