open.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 open_new_space_time_dataset(name, type, temporaltype, title, descr, semantic,
  58. dbif=None, overwrite=False, dry=False):
  59. """!Create a new space time dataset of a specific type
  60. This function is sensitive to the settings in grass.core.overwrite to
  61. overwrite existing space time datasets.
  62. @param name The name of the new space time dataset
  63. @param type The type of the new space time dataset (strd, str3ds, stvds,
  64. raster, vector, raster3d)
  65. @param temporaltype The temporal type (relative or absolute)
  66. @param title The title
  67. @param descr The dataset description
  68. @param semantic Semantical information
  69. @param dbif The temporal database interface to be used
  70. @param overwrite Flag to allow overwriting
  71. @param dry Do not create the space time dataset in the temporal database,
  72. make a dry run with including all checks
  73. @return The new created space time dataset
  74. This function will raise a ScriptError in case of an error.
  75. """
  76. #Get the current mapset to create the id of the space time dataset
  77. mapset = core.gisenv()["MAPSET"]
  78. if name.find("@") < 0:
  79. id = name + "@" + mapset
  80. else:
  81. n, m = name.split("@")
  82. if mapset != m:
  83. core.fatal(_("Space time datasets can only be created in the "
  84. "current mapset"))
  85. id = name
  86. if type == "strds" or type == "rast" or type == "raster":
  87. sp = dataset_factory("strds", id)
  88. elif type == "str3ds" or type == "rast3d" or type == "raster3d":
  89. sp = dataset_factory("str3ds", id)
  90. elif type == "stvds" or type == "vect" or type == "vector":
  91. sp = dataset_factory("stvds", id)
  92. else:
  93. core.fatal(_("Unkown type: %s") % (type))
  94. dbif, connected = init_dbif(dbif)
  95. if sp.is_in_db(dbif) and overwrite is False:
  96. if connected:
  97. dbif.close()
  98. core.fatal(_("Space time %(sp)s dataset <%(name)s> is already in the"
  99. " database. Use the overwrite flag.") % {
  100. 'sp': sp.get_new_map_instance(None).get_type(),
  101. 'name': name})
  102. return None
  103. if sp.is_in_db(dbif) and overwrite is True:
  104. core.warning(_("Overwrite space time %(sp)s dataset <%(name)s> and "
  105. "unregister all maps.") % {
  106. 'sp': sp.get_new_map_instance(None).get_type(),
  107. 'name': name})
  108. if not dry:
  109. sp.delete(dbif)
  110. sp = sp.get_new_instance(id)
  111. core.verbose(_("Create new space time %s dataset.") %
  112. sp.get_new_map_instance(None).get_type())
  113. sp.set_initial_values(temporal_type=temporaltype, semantic_type=semantic,
  114. title=title, description=descr)
  115. if not dry:
  116. sp.insert(dbif)
  117. if connected:
  118. dbif.close()
  119. return sp