core.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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():
  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. global has_command_column
  113. core.run_command("t.connect", flags="c")
  114. kv = core.parse_command("t.connect", flags="pg")
  115. if "driver" in kv:
  116. if kv["driver"] == "sqlite":
  117. tgis_backed = kv["driver"]
  118. try:
  119. import sqlite3
  120. except ImportError:
  121. core.error("Unable to locate the sqlite SQL Python interface module sqlite3.")
  122. raise
  123. dbmi = sqlite3
  124. elif kv["driver"] == "pg":
  125. tgis_backed = kv["driver"]
  126. try:
  127. import psycopg2
  128. except ImportError:
  129. core.error("Unable to locate the Postgresql SQL Python interface module psycopg2.")
  130. raise
  131. dbmi = psycopg2
  132. else:
  133. core.fatal(_("Unable to initialize the temporal DBMI interface. Use "
  134. "t.connect to specify the driver and the database string"))
  135. dbmi = sqlite3
  136. else:
  137. # Set the default sqlite3 connection in case nothing was defined
  138. core.run_command("t.connect", flags="d")
  139. database = get_temporal_dbmi_init_string()
  140. db_exists = False
  141. # Check if the database already exists
  142. if tgis_backed == "sqlite":
  143. # Check path of the sqlite database
  144. if os.path.exists(database):
  145. # Connect to database
  146. connection = dbmi.connect(database)
  147. cursor = connection.cursor()
  148. # Check for raster_base table
  149. cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='raster_base';")
  150. name = cursor.fetchone()[0]
  151. if name == "raster_base":
  152. db_exists = True
  153. # Try to add the command column to the space time dataset metadata tables
  154. try:
  155. c.execute('ALTER TABLE strds_metadata ADD COLUMN command VARCHAR;')
  156. except:
  157. pass
  158. try:
  159. c.execute('ALTER TABLE str3ds_metadata ADD COLUMN command VARCHAR;')
  160. except:
  161. pass
  162. try:
  163. c.execute('ALTER TABLE stvds_metadata ADD COLUMN command VARCHAR;')
  164. except:
  165. pass
  166. connection.commit()
  167. cursor.close()
  168. elif tgis_backed == "pg":
  169. # Connect to database
  170. connection = dbmi.connect(database)
  171. cursor = connection.cursor()
  172. # Check for raster_base table
  173. cursor.execute("SELECT EXISTS(SELECT * FROM information_schema.tables "
  174. "WHERE table_name=%s)", ('raster_base',))
  175. db_exists = cursor.fetchone()[0]
  176. if db_exists:
  177. # Try to add the command column to the space time dataset metadata tables
  178. try:
  179. c.execute('ALTER TABLE strds_metadata ADD COLUMN command VARCHAR;')
  180. except:
  181. pass
  182. try:
  183. c.execute('ALTER TABLE str3ds_metadata ADD COLUMN command VARCHAR;')
  184. except:
  185. pass
  186. try:
  187. c.execute('ALTER TABLE stvds_metadata ADD COLUMN command VARCHAR;')
  188. except:
  189. pass
  190. connection.commit()
  191. cursor.close()
  192. if db_exists == True:
  193. return
  194. core.message(_("Create temporal database: %s" % (database)))
  195. # Read all SQL scripts and templates
  196. map_tables_template_sql = open(os.path.join(
  197. get_sql_template_path(), "map_tables_template.sql"), 'r').read()
  198. raster_metadata_sql = open(os.path.join(
  199. get_sql_template_path(), "raster_metadata_table.sql"), 'r').read()
  200. raster3d_metadata_sql = open(os.path.join(get_sql_template_path(
  201. ), "raster3d_metadata_table.sql"), 'r').read()
  202. vector_metadata_sql = open(os.path.join(
  203. get_sql_template_path(), "vector_metadata_table.sql"), 'r').read()
  204. stds_tables_template_sql = open(os.path.join(
  205. get_sql_template_path(), "stds_tables_template.sql"), 'r').read()
  206. strds_metadata_sql = open(os.path.join(
  207. get_sql_template_path(), "strds_metadata_table.sql"), 'r').read()
  208. str3ds_metadata_sql = open(os.path.join(
  209. get_sql_template_path(), "str3ds_metadata_table.sql"), 'r').read()
  210. stvds_metadata_sql = open(os.path.join(
  211. get_sql_template_path(), "stvds_metadata_table.sql"), 'r').read()
  212. # Create the raster, raster3d and vector tables
  213. raster_tables_sql = map_tables_template_sql.replace("GRASS_MAP", "raster")
  214. vector_tables_sql = map_tables_template_sql.replace("GRASS_MAP", "vector")
  215. raster3d_tables_sql = map_tables_template_sql.replace(
  216. "GRASS_MAP", "raster3d")
  217. # Create the space-time raster, raster3d and vector dataset tables
  218. strds_tables_sql = stds_tables_template_sql.replace("STDS", "strds")
  219. stvds_tables_sql = stds_tables_template_sql.replace("STDS", "stvds")
  220. str3ds_tables_sql = stds_tables_template_sql.replace("STDS", "str3ds")
  221. if tgis_backed == "sqlite":
  222. # We need to create the sqlite3 database path if it does not exists
  223. tgis_dir = os.path.dirname(database)
  224. if not os.path.exists(tgis_dir):
  225. os.makedirs(tgis_dir)
  226. # Connect to database
  227. connection = dbmi.connect(database)
  228. cursor = connection.cursor()
  229. sqlite3_delete_trigger_sql = open(os.path.join(get_sql_template_path(
  230. ), "sqlite3_delete_trigger.sql"), 'r').read()
  231. # Execute the SQL statements for sqlite
  232. # Create the global tables for the native grass datatypes
  233. cursor.executescript(raster_tables_sql)
  234. cursor.executescript(raster_metadata_sql)
  235. cursor.executescript(vector_tables_sql)
  236. cursor.executescript(vector_metadata_sql)
  237. cursor.executescript(raster3d_tables_sql)
  238. cursor.executescript(raster3d_metadata_sql)
  239. # Create the tables for the new space-time datatypes
  240. cursor.executescript(strds_tables_sql)
  241. cursor.executescript(strds_metadata_sql)
  242. cursor.executescript(stvds_tables_sql)
  243. cursor.executescript(stvds_metadata_sql)
  244. cursor.executescript(str3ds_tables_sql)
  245. cursor.executescript(str3ds_metadata_sql)
  246. cursor.executescript(sqlite3_delete_trigger_sql)
  247. elif tgis_backed == "pg":
  248. # Connect to database
  249. connection = dbmi.connect(database)
  250. cursor = connection.cursor()
  251. # Execute the SQL statements for postgresql
  252. # Create the global tables for the native grass datatypes
  253. cursor.execute(raster_tables_sql)
  254. cursor.execute(raster_metadata_sql)
  255. cursor.execute(vector_tables_sql)
  256. cursor.execute(vector_metadata_sql)
  257. cursor.execute(raster3d_tables_sql)
  258. cursor.execute(raster3d_metadata_sql)
  259. # Create the tables for the new space-time datatypes
  260. cursor.execute(strds_tables_sql)
  261. cursor.execute(strds_metadata_sql)
  262. cursor.execute(stvds_tables_sql)
  263. cursor.execute(stvds_metadata_sql)
  264. cursor.execute(str3ds_tables_sql)
  265. cursor.execute(str3ds_metadata_sql)
  266. connection.commit()
  267. cursor.close()
  268. ###############################################################################
  269. class SQLDatabaseInterfaceConnection():
  270. """!This class represents the database interface connection
  271. The following DBMS are supported:
  272. * sqlite via the sqlite3 standard library
  273. * postgresql via psycopg2
  274. """
  275. def __init__(self):
  276. self.connected = False
  277. global tgis_backend
  278. if tgis_backed == "sqlite":
  279. self.dbmi = sqlite3
  280. else:
  281. self.dbmi = psycopg2
  282. def connect(self):
  283. """!Connect to the DBMI to execute SQL statements
  284. Supported backends are sqlite3 and postgresql
  285. """
  286. init = get_temporal_dbmi_init_string()
  287. #print "Connect to", self.database
  288. if self.dbmi.__name__ == "sqlite3":
  289. self.connection = self.dbmi.connect(init,
  290. detect_types = self.dbmi.PARSE_DECLTYPES | self.dbmi.PARSE_COLNAMES)
  291. self.connection.row_factory = self.dbmi.Row
  292. self.connection.isolation_level = None
  293. self.cursor = self.connection.cursor()
  294. self.cursor.execute("PRAGMA synchronous = OFF")
  295. self.cursor.execute("PRAGMA journal_mode = MEMORY")
  296. elif self.dbmi.__name__ == "psycopg2":
  297. self.connection = self.dbmi.connect(init)
  298. #self.connection.set_isolation_level(dbmi.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
  299. self.cursor = self.connection.cursor(
  300. cursor_factory = self.dbmi.extras.DictCursor)
  301. self.connected = True
  302. def close(self):
  303. """!Close the DBMI connection"""
  304. #print "Close connection to", self.database
  305. self.connection.commit()
  306. self.cursor.close()
  307. self.connected = False
  308. def mogrify_sql_statement(self, content):
  309. """!Return the SQL statement and arguments as executable SQL string
  310. @param content: The content as tuple with two entries, the first
  311. entry is the SQL statement with DBMI specific
  312. place holder (?), the second entry is the argument
  313. list that should substitue the place holder.
  314. Usage:
  315. @code
  316. >>> dbif = SQLDatabaseInterfaceConnection()
  317. >>> dbif.mogrify_sql_statement(["SELECT ctime FROM raster_base WHERE id = ?",
  318. ... ["soil@PERMANENT",]])
  319. "SELECT ctime FROM raster_base WHERE id = 'soil@PERMANENT'"
  320. @endcode
  321. """
  322. sql = content[0]
  323. args = content[1]
  324. if self.dbmi.__name__ == "psycopg2":
  325. if len(args) == 0:
  326. return sql
  327. else:
  328. if self.connected:
  329. try:
  330. return self.cursor.mogrify(sql, args)
  331. except:
  332. print sql, args
  333. raise
  334. else:
  335. self.connect()
  336. statement = self.cursor.mogrify(sql, args)
  337. self.close()
  338. return statement
  339. elif self.dbmi.__name__ == "sqlite3":
  340. if len(args) == 0:
  341. return sql
  342. else:
  343. # Unfortunately as sqlite does not support
  344. # the transformation of sql strings and qmarked or
  345. # named arguments we must make our hands dirty
  346. # and do it by ourself. :(
  347. # Doors are open for SQL injection because of the
  348. # limited python sqlite3 implementation!!!
  349. pos = 0
  350. count = 0
  351. maxcount = 100
  352. statement = sql
  353. while count < maxcount:
  354. pos = statement.find("?", pos + 1)
  355. if pos == -1:
  356. break
  357. if args[count] is None:
  358. statement = "%sNULL%s" % (statement[0:pos],
  359. statement[pos + 1:])
  360. elif isinstance(args[count], (int, long)):
  361. statement = "%s%d%s" % (statement[0:pos], args[count],
  362. statement[pos + 1:])
  363. elif isinstance(args[count], float):
  364. statement = "%s%f%s" % (statement[0:pos], args[count],
  365. statement[pos + 1:])
  366. else:
  367. # Default is a string, this works for datetime
  368. # objects too
  369. statement = "%s\'%s\'%s" % (statement[0:pos],
  370. str(args[count]),
  371. statement[pos + 1:])
  372. count += 1
  373. return statement
  374. def execute_transaction(self, statement):
  375. """!Execute a transactional SQL statement
  376. The BEGIN and END TRANSACTION statements will be added automatically
  377. to the sql statement
  378. @param statement The executable SQL statement or SQL script
  379. """
  380. connect = False
  381. if not self.connected:
  382. self.connect()
  383. connect = True
  384. sql_script = ""
  385. sql_script += "BEGIN TRANSACTION;\n"
  386. sql_script += statement
  387. sql_script += "END TRANSACTION;"
  388. try:
  389. if self.dbmi.__name__ == "sqlite3":
  390. self.cursor.executescript(statement)
  391. else:
  392. self.cursor.execute(statement)
  393. self.connection.commit()
  394. except:
  395. if connect:
  396. self.close()
  397. core.error(_("Unable to execute transaction:\n %(sql)s" % \
  398. {"sql":statement}))
  399. raise
  400. if connect:
  401. self.close()
  402. ###############################################################################
  403. def init_dbif(dbif):
  404. """!This method checks if the database interface connection exists,
  405. if not a new one will be created, connected and True will be returned
  406. Usage code sample:
  407. @code
  408. dbif, connect = tgis.init_dbif(None)
  409. sql = dbif.mogrify_sql_statement(["SELECT * FROM raster_base WHERE ? = ?"],
  410. ["id", "soil@PERMANENT"])
  411. dbif.execute_transaction(sql)
  412. if connect:
  413. dbif.close()
  414. @endcode
  415. """
  416. if dbif is None:
  417. dbif = SQLDatabaseInterfaceConnection()
  418. dbif.connect()
  419. return dbif, True
  420. return dbif, False
  421. ###############################################################################
  422. if __name__ == "__main__":
  423. import doctest
  424. doctest.testmod()