core.py 7.0 KB

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