core.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS core functions to be used in library modules and scripts.
  4. This module provides the functionality to create the temporal
  5. SQL database and to establish a connection to the database.
  6. Usage:
  7. @code
  8. >>> import grass.temporal as tgis
  9. >>> # Create the temporal database
  10. >>> tgis.init()
  11. >>> # Establish a database connection
  12. >>> dbif, connected = tgis.init_dbif(None)
  13. >>> dbif.connect()
  14. >>> # Execute a SQL statement
  15. >>> dbif.execute_transaction("SELECT datetime(0, 'unixepoch', 'localtime');")
  16. >>> # Mogrify an SQL statement
  17. >>> dbif.mogrify_sql_statement(["SELECT name from raster_base where name = ?",
  18. ... ("precipitation",)])
  19. "SELECT name from raster_base where name = 'precipitation'"
  20. >>> dbif.close()
  21. @endcode
  22. (C) 2008-2011 by the GRASS Development Team
  23. This program is free software under the GNU General Public
  24. License (>=v2). Read the file COPYING that comes with GRASS
  25. for details.
  26. @author Soeren Gebbert
  27. """
  28. import os
  29. import copy
  30. import sys
  31. import grass.script.core as core
  32. # Import all supported database backends
  33. # Ignore import errors sicne they are checked later
  34. try:
  35. import sqlite3
  36. except ImportError:
  37. pass
  38. # Postgresql is optional, existence is checked when needed
  39. try:
  40. import psycopg2
  41. import psycopg2.extras
  42. except:
  43. pass
  44. # Global variable that defines the backend
  45. # of the temporal GIS
  46. # It can either be "sqlite" or "pg"
  47. tgis_backed = None
  48. # This variable specifies if the ctypes interface to the grass
  49. # libraries should be used to read map specific data. If set to False
  50. # the grass scripting library will be used to get map informations.
  51. # The advantage of the ctypes inteface is speed, the disadvantage is that
  52. # the GRASS C functions may call G_fatal_error() which exits the process.
  53. # That is not catchable in Python.
  54. use_ctypes_map_access = True
  55. ###############################################################################
  56. def get_temporal_dbmi_init_string():
  57. kv = core.parse_command("t.connect", flags="pg")
  58. grassenv = core.gisenv()
  59. global tgis_backed
  60. if tgis_backed == "sqlite":
  61. if "database" in kv:
  62. string = kv["database"]
  63. string = string.replace("$GISDBASE", grassenv["GISDBASE"])
  64. string = string.replace(
  65. "$LOCATION_NAME", grassenv["LOCATION_NAME"])
  66. return string
  67. else:
  68. core.fatal(_("Unable to initialize the temporal GIS DBMI "
  69. "interface. Use t.connect to specify the driver "
  70. "and the database string"))
  71. elif tgis_backed == "pg":
  72. if "database" in kv:
  73. string = kv["database"]
  74. return string
  75. else:
  76. core.fatal(_("Unable to initialize the temporal GIS DBMI "
  77. "interface. Use t.connect to specify the driver "
  78. "and the database string"))
  79. ###############################################################################
  80. def set_use_ctypes_map_access(use_ctype = True):
  81. """!Define the map access method for the temporal GIS library
  82. Using ctypes to read map metadata is much faster
  83. then using the grass.script interface that calls grass modules.
  84. The disadvantage is that GRASS C-library function will call
  85. G_fatal_error() that will exit the calling process.
  86. GUI developer should set this flag to False.
  87. @param use_ctype: True use ctypes interface, False use grass.script interface
  88. """
  89. global use_ctypes_map_access
  90. use_ctypes_map_access = use_ctypes
  91. ###############################################################################
  92. def get_use_ctypes_map_access(use_ctype = True):
  93. """!Return true if ctypes is used for map access """
  94. global use_ctypes_map_access
  95. return use_ctypes_map_access
  96. ###############################################################################
  97. def get_sql_template_path():
  98. base = os.getenv("GISBASE")
  99. base_etc = os.path.join(base, "etc")
  100. return os.path.join(base_etc, "sql")
  101. ###############################################################################
  102. def init():
  103. """!This function set the correct database backend from the environmental variables
  104. and creates the grass location database structure for raster,
  105. vector and raster3d maps as well as for the space-time datasets strds,
  106. str3ds and stvds in case it not exists.
  107. ATTENTION: This functions must be called before any spatio-temporal processing
  108. can be started
  109. """
  110. # We need to set the correct database backend from the environment variables
  111. global tgis_backed
  112. core.run_command("t.connect", flags="c")
  113. kv = core.parse_command("t.connect", flags="pg")
  114. if "driver" in kv:
  115. if kv["driver"] == "sqlite":
  116. tgis_backed = kv["driver"]
  117. try:
  118. import sqlite3
  119. except ImportError:
  120. core.error("Unable to locate the sqlite SQL Python interface module sqlite3.")
  121. raise
  122. dbmi = sqlite3
  123. elif kv["driver"] == "pg":
  124. tgis_backed = kv["driver"]
  125. try:
  126. import psycopg2
  127. except ImportError:
  128. core.error("Unable to locate the Postgresql SQL Python interface module psycopg2.")
  129. raise
  130. dbmi = psycopg2
  131. else:
  132. core.fatal(_("Unable to initialize the temporal DBMI interface. Use "
  133. "t.connect to specify the driver and the database string"))
  134. dbmi = sqlite3
  135. else:
  136. # Set the default sqlite3 connection in case nothing was defined
  137. core.run_command("t.connect", flags="d")
  138. database = get_temporal_dbmi_init_string()
  139. db_exists = False
  140. # Check if the database already exists
  141. if tgis_backed == "sqlite":
  142. # Check path of the sqlite database
  143. if os.path.exists(database):
  144. # Connect to database
  145. connection = dbmi.connect(database)
  146. cursor = connection.cursor()
  147. # Check for raster_base table
  148. cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='raster_base';")
  149. name = cursor.fetchone()[0]
  150. if name == "raster_base":
  151. db_exists = True
  152. connection.commit()
  153. cursor.close()
  154. elif tgis_backed == "pg":
  155. # Connect to database
  156. connection = dbmi.connect(database)
  157. cursor = connection.cursor()
  158. # Check for raster_base table
  159. cursor.execute("SELECT EXISTS(SELECT * FROM information_schema.tables "
  160. "WHERE table_name=%s)", ('raster_base',))
  161. db_exists = cursor.fetchone()[0]
  162. connection.commit()
  163. cursor.close()
  164. if db_exists == True:
  165. return
  166. core.message(_("Create temporal database: %s" % (database)))
  167. # Read all SQL scripts and templates
  168. map_tables_template_sql = open(os.path.join(
  169. get_sql_template_path(), "map_tables_template.sql"), 'r').read()
  170. raster_metadata_sql = open(os.path.join(
  171. get_sql_template_path(), "raster_metadata_table.sql"), 'r').read()
  172. raster3d_metadata_sql = open(os.path.join(get_sql_template_path(
  173. ), "raster3d_metadata_table.sql"), 'r').read()
  174. vector_metadata_sql = open(os.path.join(
  175. get_sql_template_path(), "vector_metadata_table.sql"), 'r').read()
  176. stds_tables_template_sql = open(os.path.join(
  177. get_sql_template_path(), "stds_tables_template.sql"), 'r').read()
  178. strds_metadata_sql = open(os.path.join(
  179. get_sql_template_path(), "strds_metadata_table.sql"), 'r').read()
  180. str3ds_metadata_sql = open(os.path.join(
  181. get_sql_template_path(), "str3ds_metadata_table.sql"), 'r').read()
  182. stvds_metadata_sql = open(os.path.join(
  183. get_sql_template_path(), "stvds_metadata_table.sql"), 'r').read()
  184. # Create the raster, raster3d and vector tables
  185. raster_tables_sql = map_tables_template_sql.replace("GRASS_MAP", "raster")
  186. vector_tables_sql = map_tables_template_sql.replace("GRASS_MAP", "vector")
  187. raster3d_tables_sql = map_tables_template_sql.replace(
  188. "GRASS_MAP", "raster3d")
  189. # Create the space-time raster, raster3d and vector dataset tables
  190. strds_tables_sql = stds_tables_template_sql.replace("STDS", "strds")
  191. stvds_tables_sql = stds_tables_template_sql.replace("STDS", "stvds")
  192. str3ds_tables_sql = stds_tables_template_sql.replace("STDS", "str3ds")
  193. # Connect to database
  194. connection = dbmi.connect(database)
  195. cursor = connection.cursor()
  196. if tgis_backed == "sqlite":
  197. sqlite3_delete_trigger_sql = open(os.path.join(get_sql_template_path(
  198. ), "sqlite3_delete_trigger.sql"), 'r').read()
  199. # Execute the SQL statements for sqlite
  200. # Create the global tables for the native grass datatypes
  201. cursor.executescript(raster_tables_sql)
  202. cursor.executescript(raster_metadata_sql)
  203. cursor.executescript(vector_tables_sql)
  204. cursor.executescript(vector_metadata_sql)
  205. cursor.executescript(raster3d_tables_sql)
  206. cursor.executescript(raster3d_metadata_sql)
  207. # Create the tables for the new space-time datatypes
  208. cursor.executescript(strds_tables_sql)
  209. cursor.executescript(strds_metadata_sql)
  210. cursor.executescript(stvds_tables_sql)
  211. cursor.executescript(stvds_metadata_sql)
  212. cursor.executescript(str3ds_tables_sql)
  213. cursor.executescript(str3ds_metadata_sql)
  214. cursor.executescript(sqlite3_delete_trigger_sql)
  215. elif tgis_backed == "pg":
  216. # Execute the SQL statements for postgresql
  217. # Create the global tables for the native grass datatypes
  218. cursor.execute(raster_tables_sql)
  219. cursor.execute(raster_metadata_sql)
  220. cursor.execute(vector_tables_sql)
  221. cursor.execute(vector_metadata_sql)
  222. cursor.execute(raster3d_tables_sql)
  223. cursor.execute(raster3d_metadata_sql)
  224. # Create the tables for the new space-time datatypes
  225. cursor.execute(strds_tables_sql)
  226. cursor.execute(strds_metadata_sql)
  227. cursor.execute(stvds_tables_sql)
  228. cursor.execute(stvds_metadata_sql)
  229. cursor.execute(str3ds_tables_sql)
  230. cursor.execute(str3ds_metadata_sql)
  231. connection.commit()
  232. cursor.close()
  233. ###############################################################################
  234. class SQLDatabaseInterfaceConnection():
  235. """!This class represents the database interface connection
  236. The following DBMS are supported:
  237. * sqlite via the sqlite3 standard library
  238. * postgresql via psycopg2
  239. """
  240. def __init__(self):
  241. self.connected = False
  242. global tgis_backend
  243. if tgis_backed == "sqlite":
  244. self.dbmi = sqlite3
  245. else:
  246. self.dbmi = psycopg2
  247. def connect(self):
  248. """!Connect to the DBMI to execute SQL statements
  249. Supported backends are sqlite3 and postgresql
  250. """
  251. init = get_temporal_dbmi_init_string()
  252. #print "Connect to", self.database
  253. if self.dbmi.__name__ == "sqlite3":
  254. self.connection = self.dbmi.connect(init,
  255. detect_types = self.dbmi.PARSE_DECLTYPES | self.dbmi.PARSE_COLNAMES)
  256. self.connection.row_factory = self.dbmi.Row
  257. self.connection.isolation_level = None
  258. self.cursor = self.connection.cursor()
  259. self.cursor.execute("PRAGMA synchronous = OFF")
  260. self.cursor.execute("PRAGMA journal_mode = MEMORY")
  261. elif self.dbmi.__name__ == "psycopg2":
  262. self.connection = self.dbmi.connect(init)
  263. #self.connection.set_isolation_level(dbmi.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
  264. self.cursor = self.connection.cursor(
  265. cursor_factory = self.dbmi.extras.DictCursor)
  266. self.connected = True
  267. def close(self):
  268. """!Close the DBMI connection"""
  269. #print "Close connection to", self.database
  270. self.connection.commit()
  271. self.cursor.close()
  272. self.connected = False
  273. def mogrify_sql_statement(self, content):
  274. """!Return the SQL statement and arguments as executable SQL string
  275. @param content: The content as tuple with two entries, the first
  276. entry is the SQL statement with DBMI specific
  277. place holder (?), the second entry is the argument
  278. list that should substitue the place holder.
  279. Usage:
  280. @code
  281. >>> dbif = SQLDatabaseInterfaceConnection()
  282. >>> dbif.mogrify_sql_statement(["SELECT ctime FROM raster_base WHERE id = ?",
  283. ... ["soil@PERMANENT",]])
  284. "SELECT ctime FROM raster_base WHERE id = 'soil@PERMANENT'"
  285. @endcode
  286. """
  287. sql = content[0]
  288. args = content[1]
  289. if self.dbmi.__name__ == "psycopg2":
  290. if len(args) == 0:
  291. return sql
  292. else:
  293. if self.connected:
  294. try:
  295. return self.cursor.mogrify(sql, args)
  296. except:
  297. print sql, args
  298. raise
  299. else:
  300. self.connect()
  301. statement = self.cursor.mogrify(sql, args)
  302. self.close()
  303. return statement
  304. elif self.dbmi.__name__ == "sqlite3":
  305. if len(args) == 0:
  306. return sql
  307. else:
  308. # Unfortunately as sqlite does not support
  309. # the transformation of sql strings and qmarked or
  310. # named arguments we must make our hands dirty
  311. # and do it by ourself. :(
  312. # Doors are open for SQL injection because of the
  313. # limited python sqlite3 implementation!!!
  314. pos = 0
  315. count = 0
  316. maxcount = 100
  317. statement = sql
  318. while count < maxcount:
  319. pos = statement.find("?", pos + 1)
  320. if pos == -1:
  321. break
  322. if args[count] is None:
  323. statement = "%sNULL%s" % (statement[0:pos],
  324. statement[pos + 1:])
  325. elif isinstance(args[count], (int, long)):
  326. statement = "%s%d%s" % (statement[0:pos], args[count],
  327. statement[pos + 1:])
  328. elif isinstance(args[count], float):
  329. statement = "%s%f%s" % (statement[0:pos], args[count],
  330. statement[pos + 1:])
  331. else:
  332. # Default is a string, this works for datetime
  333. # objects too
  334. statement = "%s\'%s\'%s" % (statement[0:pos],
  335. str(args[count]),
  336. statement[pos + 1:])
  337. count += 1
  338. return statement
  339. def execute_transaction(self, statement):
  340. """!Execute a transactional SQL statement
  341. The BEGIN and END TRANSACTION statements will be added automatically
  342. to the sql statement
  343. @param statement The executable SQL statement or SQL script
  344. """
  345. connect = False
  346. if not self.connected:
  347. self.connect()
  348. connect = True
  349. sql_script = ""
  350. sql_script += "BEGIN TRANSACTION;\n"
  351. sql_script += statement
  352. sql_script += "END TRANSACTION;"
  353. try:
  354. if self.dbmi.__name__ == "sqlite3":
  355. self.cursor.executescript(statement)
  356. else:
  357. self.cursor.execute(statement)
  358. self.connection.commit()
  359. except:
  360. if connect:
  361. self.close()
  362. core.error(_("Unable to execute transaction:\n %(sql)s" % \
  363. {"sql":statement}))
  364. raise
  365. if connect:
  366. self.close()
  367. ###############################################################################
  368. def init_dbif(dbif):
  369. """!This method checks if the database interface connection exists,
  370. if not a new one will be created, connected and True will be returned
  371. Usage code sample:
  372. @code
  373. dbif, connect = tgis.init_dbif(None)
  374. sql = dbif.mogrify_sql_statement(["SELECT * FROM raster_base WHERE ? = ?"],
  375. ["id", "soil@PERMANENT"])
  376. dbif.execute_transaction(sql)
  377. if connect:
  378. dbif.close()
  379. @endcode
  380. """
  381. if dbif is None:
  382. dbif = SQLDatabaseInterfaceConnection()
  383. dbif.connect()
  384. return dbif, True
  385. return dbif, False
  386. ###############################################################################
  387. if __name__ == "__main__":
  388. import doctest
  389. doctest.testmod()