open_stds.py 8.3 KB

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