open.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS related functions to be used in Python scripts.
  4. Usage:
  5. @code
  6. import grass.temporal as tgis
  7. tgis.register_maps_in_space_time_dataset(type, name, maps)
  8. ...
  9. @endcode
  10. (C) 2008-2011 by the GRASS Development Team
  11. This program is free software under the GNU General Public
  12. License (>=v2). Read the file COPYING that comes with GRASS
  13. for details.
  14. @author Soeren Gebbert
  15. """
  16. from space_time_datasets import *
  17. from factory import *
  18. ###############################################################################
  19. def open_old_space_time_dataset(name, type, dbif=None):
  20. """!This function opens an existing space time dataset and return the
  21. created and intialized object of the specified type.
  22. This function will raise a ScriptError in case the type is wrong,
  23. or the space time dataset was not found.
  24. @param name The name of the space time dataset, if the name does not
  25. contain the mapset (name@mapset) then the current mapset
  26. will be used to identifiy the space time dataset
  27. @param type The type of the space time dataset (strd, str3ds, stvds,
  28. raster, vector, raster3d)
  29. @param dbif The optional database interface to be used
  30. """
  31. mapset = core.gisenv()["MAPSET"]
  32. # Check if the dataset name contains the mapset as well
  33. if name.find("@") < 0:
  34. id = name + "@" + mapset
  35. else:
  36. id = name
  37. if type == "strds" or type == "rast" or type == "raster":
  38. sp = dataset_factory("strds", id)
  39. elif type == "str3ds" or type == "rast3d" or type == "raster3d":
  40. sp = dataset_factory("str3ds", id)
  41. elif type == "stvds" or type == "vect" or type == "vector":
  42. sp = dataset_factory("stvds", id)
  43. else:
  44. core.fatal(_("Unkown type: %s") % (type))
  45. dbif, connected = init_dbif(dbif)
  46. if not sp.is_in_db(dbif):
  47. dbif.close()
  48. core.fatal(_("Space time %(sp)s dataset <%(name)s> no found") %
  49. {'sp': sp.get_new_map_instance(None).get_type(),
  50. 'name': name})
  51. # Read content from temporal database
  52. sp.select(dbif)
  53. if connected:
  54. dbif.close()
  55. return sp
  56. ###############################################################################
  57. def check_new_space_time_dataset(name, type, dbif=None, overwrite=False):
  58. """!Check if a new space time dataset of a specific type can be created
  59. @param name The name of the new space time dataset
  60. @param type The type of the new space time dataset (strd, str3ds, stvds,
  61. raster, vector, raster3d)
  62. @param dbif The temporal database interface to be used
  63. @param overwrite Flag to allow overwriting
  64. @return A space time dataset object that must be filled with
  65. content before insertion in the temporal database
  66. This function will raise a ScriptError in case of an error.
  67. """
  68. #Get the current mapset to create the id of the space time dataset
  69. mapset = core.gisenv()["MAPSET"]
  70. if name.find("@") < 0:
  71. id = name + "@" + mapset
  72. else:
  73. n, m = name.split("@")
  74. if mapset != m:
  75. core.fatal(_("Space time datasets can only be created in the "
  76. "current mapset"))
  77. id = name
  78. if type == "strds" or type == "rast" or type == "raster":
  79. sp = dataset_factory("strds", id)
  80. elif type == "str3ds" or type == "rast3d" or type == "raster3d":
  81. sp = dataset_factory("str3ds", id)
  82. elif type == "stvds" or type == "vect" or type == "vector":
  83. sp = dataset_factory("stvds", id)
  84. else:
  85. core.error(_("Unkown type: %s") % (type))
  86. return None
  87. dbif, connected = init_dbif(dbif)
  88. if sp.is_in_db(dbif) and overwrite is False:
  89. core.fatal(_("Space time %(sp)s dataset <%(name)s> is already in the"
  90. " database. Use the overwrite flag.") % {
  91. 'sp': sp.get_new_map_instance(None).get_type(),
  92. 'name': name})
  93. if connected:
  94. dbif.close()
  95. return sp
  96. ###############################################################################
  97. def open_new_space_time_dataset(name, type, temporaltype, title, descr, semantic,
  98. dbif=None, overwrite=False):
  99. """!Create a new space time dataset of a specific type
  100. @param name The name of the new space time dataset
  101. @param type The type of the new space time dataset (strd, str3ds, stvds,
  102. raster, vector, raster3d)
  103. @param temporaltype The temporal type (relative or absolute)
  104. @param title The title
  105. @param descr The dataset description
  106. @param semantic Semantical information
  107. @param dbif The temporal database interface to be used
  108. @param overwrite Flag to allow overwriting
  109. @param dry Do not create the space time dataset in the temporal database,
  110. make a dry run with including all checks
  111. @return The new created space time dataset
  112. This function will raise a ScriptError in case of an error.
  113. """
  114. dbif, connected = init_dbif(dbif)
  115. sp = check_new_space_time_dataset(name, type, dbif, overwrite)
  116. if sp.is_in_db(dbif):
  117. core.warning(_("Overwrite space time %(sp)s dataset <%(name)s> and "
  118. "unregister all maps.") % {
  119. 'sp': sp.get_new_map_instance(None).get_type(),
  120. 'name': name})
  121. id = sp.get_id()
  122. sp.delete(dbif)
  123. sp = sp.get_new_instance(id)
  124. core.verbose(_("Create new space time %s dataset.") %
  125. sp.get_new_map_instance(None).get_type())
  126. sp.set_initial_values(temporal_type=temporaltype, semantic_type=semantic,
  127. title=title, description=descr)
  128. sp.insert(dbif)
  129. if connected:
  130. dbif.close()
  131. return sp
  132. ############################################################################
  133. def check_new_map_dataset(name, layer=None, mapset=None,
  134. type="raster", overwrite=False, dbif=None):
  135. """!Check if a new map dataset of a specific type can be created in
  136. the temporal database
  137. @param name The name of the new map dataset
  138. @param layer The layer of the new map dataset
  139. @param mapset The current mapset the new map dataset is created in,
  140. this argument is optional, if not provided g.gisenv
  141. will be called to reveive the current mapset
  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 ScriptError in case of an error.
  147. """
  148. if not mapset:
  149. mapset = core.gisenv()["MAPSET"]
  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. core.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, mapset=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 mapset The current mapset the new map dataset is created in,
  172. this argument is optional, if not provided g.gisenv
  173. will be called to reveive the current mapset
  174. @param type The type of the new map dataset (raster, vector, raster3d)
  175. @param dbif The temporal database interface to be used
  176. @param overwrite Flag to allow overwriting
  177. @return A map dataset object
  178. This function will raise a ScriptError in case of an error.
  179. """
  180. if not mapset:
  181. mapset = core.gisenv()["MAPSET"]
  182. dbif, connected = init_dbif(dbif)
  183. new_map = check_new_map_dataset(name, layer, mapset, "raster",
  184. overwrite, dbif)
  185. # Check if new map is in the temporal database
  186. if new_map.is_in_db(dbif):
  187. # Remove the existing temporal database entry
  188. map_id = new_map.get_id()
  189. new_map.delete(dbif)
  190. new_map = new_map.get_new_instance(map_id)
  191. if temporal_extent:
  192. new_map.set_temporal_extent(temporal_extent)
  193. if connected:
  194. dbif.close()
  195. return new_map