core.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS core functions to be used in Python sripts.
  4. Usage:
  5. @code
  6. import grass.temporal as tgis
  7. tgis.create_temporal_database()
  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. import os
  17. import copy
  18. from datetime import datetime, date, time, timedelta
  19. import grass.script.core as core
  20. ###############################################################################
  21. def get_temporal_dbmi_default_path(grassenv):
  22. dbpath = os.path.join(grassenv["GISDBASE"], grassenv["LOCATION_NAME"])
  23. dbpath = os.path.join(dbpath, "PERMANENT")
  24. return os.path.join(dbpath, "grass.db")
  25. # The chosen DBMI backend can be defined on runtime
  26. # Check the grass environment before import
  27. grassenv = core.gisenv()
  28. if grassenv.has_key("TDBMI"):
  29. if grassenv["TDBMI"] == "sqlite3":
  30. import sqlite3 as dbmi
  31. elif grassenv["TDBMI"] == "psycopg2":
  32. import psycopg2 as dbmi
  33. # Needed for dictionary like cursors
  34. import psycopg2.extras
  35. else:
  36. core.fatal(_("Unable to initialize the temporal DBMI interface: %s. \nPlease set g.gisenv set=\"TDBMI=sqlite3\" or g.gisenv set=\"TDBMI=psycopg2\"") % grassenv["TDBMI"])
  37. else:
  38. # Use the default sqlite variable
  39. import sqlite3 as dbmi
  40. core.run_command("g.gisenv", set="TDBMI=sqlite3")
  41. core.run_command("g.gisenv", set="TDBMI_INIT=%s" % get_temporal_dbmi_default_path(grassenv))
  42. ###############################################################################
  43. def get_temporal_dbmi_init_string():
  44. grassenv = core.gisenv()
  45. if dbmi.__name__ == "sqlite3":
  46. if grassenv.has_key("TDBMI_INIT"):
  47. return grassenv["TDBMI_INIT"]
  48. else:
  49. return get_temporal_dbmi_default_path(grassenv)
  50. elif dbmi.__name__ == "psycopg2":
  51. if grassenv.has_key("TDBMI_INIT"):
  52. return grassenv["TDBMI_INIT"]
  53. else:
  54. return "dbname=grass_test user=soeren password=abcdefgh"
  55. ###############################################################################
  56. def get_sql_template_path():
  57. base = os.getenv("GISBASE")
  58. base_etc = os.path.join(base, "etc")
  59. return os.path.join(base_etc, "sql")
  60. ###############################################################################
  61. def create_temporal_database():
  62. """This function creates the grass location database structure for raster, vector and raster3d maps
  63. as well as for the space-time datasets strds, str3ds and stvds
  64. This functions must be called befor any spatio-temporal processing is started
  65. """
  66. database = get_temporal_dbmi_init_string()
  67. db_exists = False
  68. # Check if the database already exists
  69. if dbmi.__name__ == "sqlite3":
  70. # Check path of the sqlite database
  71. if os.path.exists(database):
  72. db_exists = True
  73. elif dbmi.__name__ == "psycopg2":
  74. # Connect to database
  75. connection = dbmi.connect(database)
  76. cursor = connection.cursor()
  77. # Check for raster_base table
  78. cursor.execute("SELECT EXISTS(SELECT * FROM information_schema.tables WHERE table_name=%s)", ('raster_base',))
  79. db_exists = cursor.fetchone()[0]
  80. connection.commit()
  81. cursor.close()
  82. if db_exists == True:
  83. return
  84. # Read all SQL scripts and templates
  85. map_tables_template_sql = open(os.path.join(get_sql_template_path(), "map_tables_template.sql"), 'r').read()
  86. raster_metadata_sql = open(os.path.join(get_sql_template_path(), "raster_metadata_table.sql"), 'r').read()
  87. raster3d_metadata_sql = open(os.path.join(get_sql_template_path(), "raster3d_metadata_table.sql"), 'r').read()
  88. vector_metadata_sql = open(os.path.join(get_sql_template_path(), "vector_metadata_table.sql"), 'r').read()
  89. stds_tables_template_sql = open(os.path.join(get_sql_template_path(), "stds_tables_template.sql"), 'r').read()
  90. strds_metadata_sql = open(os.path.join(get_sql_template_path(), "strds_metadata_table.sql"), 'r').read()
  91. str3ds_metadata_sql = open(os.path.join(get_sql_template_path(), "str3ds_metadata_table.sql"), 'r').read()
  92. stvds_metadata_sql = open(os.path.join(get_sql_template_path(), "stvds_metadata_table.sql"), 'r').read()
  93. # Create the raster, raster3d and vector tables
  94. raster_tables_sql = map_tables_template_sql.replace("GRASS_MAP", "raster")
  95. vector_tables_sql = map_tables_template_sql.replace("GRASS_MAP", "vector")
  96. raster3d_tables_sql = map_tables_template_sql.replace("GRASS_MAP", "raster3d")
  97. # Create the space-time raster, raster3d and vector dataset tables
  98. strds_tables_sql = stds_tables_template_sql.replace("STDS", "strds")
  99. stvds_tables_sql = stds_tables_template_sql.replace("STDS", "stvds")
  100. str3ds_tables_sql = stds_tables_template_sql.replace("STDS", "str3ds")
  101. # Connect to database
  102. connection = dbmi.connect(database)
  103. cursor = connection.cursor()
  104. if dbmi.__name__ == "sqlite3":
  105. sqlite3_delete_trigger_sql = open(os.path.join(get_sql_template_path(), "sqlite3_delete_trigger.sql"), 'r').read()
  106. # Execute the SQL statements for sqlite
  107. # Create the global tables for the native grass datatypes
  108. cursor.executescript(raster_tables_sql)
  109. cursor.executescript(raster_metadata_sql)
  110. cursor.executescript(vector_tables_sql)
  111. cursor.executescript(vector_metadata_sql)
  112. cursor.executescript(raster3d_tables_sql)
  113. cursor.executescript(raster3d_metadata_sql)
  114. # Create the tables for the new space-time datatypes
  115. cursor.executescript(strds_tables_sql)
  116. cursor.executescript(strds_metadata_sql)
  117. cursor.executescript(stvds_tables_sql)
  118. cursor.executescript(stvds_metadata_sql)
  119. cursor.executescript(str3ds_tables_sql)
  120. cursor.executescript(str3ds_metadata_sql)
  121. cursor.executescript(sqlite3_delete_trigger_sql)
  122. elif dbmi.__name__ == "psycopg2":
  123. # Execute the SQL statements for postgresql
  124. # Create the global tables for the native grass datatypes
  125. cursor.execute(raster_tables_sql)
  126. cursor.execute(raster_metadata_sql)
  127. cursor.execute(vector_tables_sql)
  128. cursor.execute(vector_metadata_sql)
  129. cursor.execute(raster3d_tables_sql)
  130. cursor.execute(raster3d_metadata_sql)
  131. # Create the tables for the new space-time datatypes
  132. cursor.execute(strds_tables_sql)
  133. cursor.execute(strds_metadata_sql)
  134. cursor.execute(stvds_tables_sql)
  135. cursor.execute(stvds_metadata_sql)
  136. cursor.execute(str3ds_tables_sql)
  137. cursor.execute(str3ds_metadata_sql)
  138. connection.commit()
  139. cursor.close()