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