base.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS base classes to be used in other
  4. Python temporal gis packages.
  5. This packages includes all base classes to stor basic information like id, name,
  6. mapset creation and modification time as well as sql serialization and deserialization
  7. and the sql database interface.
  8. Usage:
  9. @code
  10. import grass.temporal as tgis
  11. rbase = tgis.raster_base(ident="soil")
  12. ...
  13. @endcode
  14. (C) 2008-2011 by the GRASS Development Team
  15. This program is free software under the GNU General Public
  16. License (>=v2). Read the file COPYING that comes with GRASS
  17. for details.
  18. @author Soeren Gebbert
  19. """
  20. from core import *
  21. ###############################################################################
  22. class dict_sql_serializer(object):
  23. def __init__(self):
  24. self.D = {}
  25. def serialize(self, type, table, where=None):
  26. """Convert the internal dictionary into a string of semicolon separated SQL statements
  27. The keys are the colum names and the values are the row entries
  28. @type must be SELECT. INSERT, UPDATE
  29. @table The name of the table to select, insert or update
  30. @where The optinal where statment
  31. @return the sql string
  32. """
  33. sql = ""
  34. args = []
  35. # Create ordered select statement
  36. if type == "SELECT":
  37. sql += 'SELECT '
  38. count = 0
  39. for key in self.D.keys():
  40. if count == 0:
  41. sql += ' %s ' % key
  42. else:
  43. sql += ' , %s ' % key
  44. count += 1
  45. sql += ' FROM ' + table + ' '
  46. if where:
  47. sql += where
  48. # Create insert statement
  49. if type =="INSERT":
  50. count = 0
  51. sql += 'INSERT INTO ' + table + ' ('
  52. for key in self.D.keys():
  53. if count == 0:
  54. sql += ' %s ' % key
  55. else:
  56. sql += ' ,%s ' % key
  57. count += 1
  58. count = 0
  59. sql += ') VALUES ('
  60. for key in self.D.keys():
  61. if count == 0:
  62. if dbmi.paramstyle == "qmark":
  63. sql += '?'
  64. else:
  65. sql += '%s'
  66. else:
  67. if dbmi.paramstyle == "qmark":
  68. sql += ' ,?'
  69. else:
  70. sql += ' ,%s'
  71. count += 1
  72. args.append(self.D[key])
  73. sql += ') '
  74. if where:
  75. sql += where
  76. # Create update statement for existing entries
  77. if type =="UPDATE":
  78. count = 0
  79. sql += 'UPDATE ' + table + ' SET '
  80. for key in self.D.keys():
  81. # Update only entries which are not None
  82. if self.D[key] != None:
  83. if count == 0:
  84. if dbmi.paramstyle == "qmark":
  85. sql += ' %s = ? ' % key
  86. else:
  87. sql += ' %s ' % key
  88. sql += '= %s '
  89. else:
  90. if dbmi.paramstyle == "qmark":
  91. sql += ' ,%s = ? ' % key
  92. else:
  93. sql += ' ,%s ' % key
  94. sql += '= %s '
  95. count += 1
  96. args.append(self.D[key])
  97. if where:
  98. sql += where
  99. # Create update statement for all entries
  100. if type =="UPDATE ALL":
  101. count = 0
  102. sql += 'UPDATE ' + table + ' SET '
  103. for key in self.D.keys():
  104. if count == 0:
  105. if dbmi.paramstyle == "qmark":
  106. sql += ' %s = ? ' % key
  107. else:
  108. sql += ' %s ' % key
  109. sql += '= %s '
  110. else:
  111. if dbmi.paramstyle == "qmark":
  112. sql += ' ,%s = ? ' % key
  113. else:
  114. sql += ' ,%s ' % key
  115. sql += '= %s '
  116. count += 1
  117. args.append(self.D[key])
  118. if where:
  119. sql += where
  120. return sql, tuple(args)
  121. def deserialize(self, row):
  122. """Convert the content of the dbmi dictionary like row into the internal dictionary
  123. @param row: The dixtionary like row to stoe in the internal dict
  124. """
  125. self.D = {}
  126. for key in row.keys():
  127. self.D[key] = row[key]
  128. def clear(self):
  129. """Inititalize the internal storage"""
  130. self.D = {}
  131. def print_self(self):
  132. print self.D
  133. def test(self):
  134. t = dict_sql_serializer()
  135. t.D["id"] = "soil@PERMANENT"
  136. t.D["name"] = "soil"
  137. t.D["mapset"] = "PERMANENT"
  138. t.D["creator"] = "soeren"
  139. t.D["creation_time"] = datetime.now()
  140. t.D["modification_time"] = datetime.now()
  141. t.D["revision"] = 1
  142. sql, values = t.serialize(type="SELECT", table="raster_base")
  143. print sql, '\n', values
  144. sql, values = t.serialize(type="INSERT", table="raster_base")
  145. print sql, '\n', values
  146. sql, values = t.serialize(type="UPDATE", table="raster_base")
  147. print sql, '\n', values
  148. ###############################################################################
  149. class sql_database_interface(dict_sql_serializer):
  150. """This class represents the SQL database interface
  151. Functions to insert, select and update the internal structure of this class
  152. in the temporal database are implemented. The following DBMS are supported:
  153. * sqlite via the sqlite3 standard library
  154. * postgresql via psycopg2
  155. This is the base class for raster, raster3d, vector and space time datasets
  156. data management classes:
  157. * Identification information (base)
  158. * Spatial extent
  159. * Temporal extent
  160. * Metadata
  161. """
  162. def __init__(self, table=None, ident=None, database=None):
  163. """Constructor of this class
  164. @param table: The name of the table
  165. @param ident: The identifier (primary key) of this object in the database table
  166. @param database: A specific string used in the dbmi connect method. This should be the path to the database , user name, ...
  167. """
  168. dict_sql_serializer.__init__(self)
  169. self.table = table # Name of the table, set in the subclass
  170. if database == None:
  171. self.database = get_temporal_dbmi_init_string()
  172. else:
  173. self.database = database
  174. self.ident = ident
  175. def get_table_name(self):
  176. """Return the name of the table in which the internal data are inserted, updated or selected"""
  177. return self.table
  178. def connect(self):
  179. """Connect to the DBMI to execute SQL statements
  180. Supported backends are sqlite3 and postgresql
  181. """
  182. #print "Connect to", self.database
  183. if dbmi.__name__ == "sqlite3":
  184. self.connection = dbmi.connect(self.database, detect_types=dbmi.PARSE_DECLTYPES|dbmi.PARSE_COLNAMES)
  185. self.connection.row_factory = dbmi.Row
  186. self.connection.isolation_level = None
  187. self.cursor = self.connection.cursor()
  188. self.cursor.execute("PRAGMA synchronous = OFF")
  189. self.cursor.execute("PRAGMA journal_mode = MEMORY")
  190. elif dbmi.__name__ == "psycopg2":
  191. self.connection = dbmi.connect(self.database)
  192. self.connection.set_isolation_level(dbmi.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
  193. self.cursor = self.connection.cursor(cursor_factory=dbmi.extras.DictCursor)
  194. def close(self):
  195. """Close the DBMI connection"""
  196. #print "Close connection to", self.database
  197. self.connection.commit()
  198. self.cursor.close()
  199. def get_delete_statement(self):
  200. return "DELETE FROM " + self.get_table_name() + " WHERE id = \'" + str(self.ident) + "\'"
  201. def delete(self, dbif=None):
  202. """Delete the entry of this object from the temporal database"""
  203. sql = self.get_delete_statement()
  204. #print sql
  205. if dbif:
  206. dbif.cursor.execute(sql)
  207. else:
  208. self.connect()
  209. self.cursor.execute(sql)
  210. self.close()
  211. def get_is_in_db_statement(self):
  212. return "SELECT id FROM " + self.get_table_name() + " WHERE id = \'" + str(self.ident) + "\'"
  213. def is_in_db(self, dbif=None):
  214. """Check if this object is present in the temporal database
  215. @param dbif: The database interface to be used
  216. """
  217. sql = self.get_is_in_db_statement()
  218. #print sql
  219. if dbif:
  220. dbif.cursor.execute(sql)
  221. row = dbif.cursor.fetchone()
  222. else:
  223. self.connect()
  224. self.cursor.execute(sql)
  225. row = self.cursor.fetchone()
  226. self.close()
  227. # Nothing found
  228. if row == None:
  229. return False
  230. return True
  231. def get_select_statement(self):
  232. return self.serialize("SELECT", self.get_table_name(), "WHERE id = \'" + str(self.ident) + "\'")
  233. def select(self, dbif=None):
  234. """Select the content from the temporal database and store it
  235. in the internal dictionary structure
  236. @param dbif: The database interface to be used
  237. """
  238. sql, args = self.get_select_statement()
  239. #print sql
  240. #print args
  241. if dbif:
  242. if len(args) == 0:
  243. dbif.cursor.execute(sql)
  244. else:
  245. dbif.cursor.execute(sql, args)
  246. row = dbif.cursor.fetchone()
  247. else:
  248. self.connect()
  249. if len(args) == 0:
  250. self.cursor.execute(sql)
  251. else:
  252. self.cursor.execute(sql, args)
  253. row = self.cursor.fetchone()
  254. self.close()
  255. # Nothing found
  256. if row == None:
  257. return False
  258. if len(row) > 0:
  259. self.deserialize(row)
  260. else:
  261. core.fatal(_("Object <%s> not found in the temporal database") % self.get_id())
  262. return True
  263. def get_insert_statement(self):
  264. return self.serialize("INSERT", self.get_table_name())
  265. def insert(self, dbif=None):
  266. """Serialize the content of this object and store it in the temporal
  267. database using the internal identifier
  268. @param dbif: The database interface to be used
  269. """
  270. sql, args = self.get_insert_statement()
  271. #print sql
  272. #print args
  273. if dbif:
  274. dbif.cursor.execute(sql, args)
  275. else:
  276. self.connect()
  277. self.cursor.execute(sql, args)
  278. self.close()
  279. def get_update_statement(self):
  280. return self.serialize("UPDATE", self.get_table_name(), "WHERE id = \'" + str(self.ident) + "\'")
  281. def update(self, dbif=None):
  282. """Serialize the content of this object and update it in the temporal
  283. database using the internal identifier
  284. Only object entries which are exists (not None) are updated
  285. @param dbif: The database interface to be used
  286. """
  287. if self.ident == None:
  288. raise IOError("Missing identifer");
  289. sql, args = self.get_update_statement()
  290. #print sql
  291. #print args
  292. if dbif:
  293. dbif.cursor.execute(sql, args)
  294. else:
  295. self.connect()
  296. self.cursor.execute(sql, args)
  297. self.close()
  298. def get_update_all_statement(self):
  299. return self.serialize("UPDATE ALL", self.get_table_name(), "WHERE id = \'" + str(self.ident) + "\'")
  300. def update_all(self, dbif=None):
  301. """Serialize the content of this object, including None objects, and update it in the temporal
  302. database using the internal identifier
  303. @param dbif: The database interface to be used
  304. """
  305. if self.ident == None:
  306. raise IOError("Missing identifer");
  307. sql, args = self.get_update_all_statement()
  308. #print sql
  309. #print args
  310. if dbif:
  311. dbif.cursor.execute(sql, args)
  312. else:
  313. self.connect()
  314. self.cursor.execute(sql, args)
  315. self.close()
  316. ###############################################################################
  317. class dataset_base(sql_database_interface):
  318. """This is the base class for all maps and spacetime datasets storing basic identification information"""
  319. def __init__(self, table=None, ident=None, name=None, mapset=None, creator=None, ctime=None,\
  320. mtime=None, ttype=None, revision=1):
  321. sql_database_interface.__init__(self, table, ident)
  322. self.set_id(ident)
  323. self.set_name(name)
  324. self.set_mapset(mapset)
  325. self.set_creator(creator)
  326. self.set_ctime(ctime)
  327. self.set_ttype(ttype)
  328. #self.set_mtime(mtime)
  329. #self.set_revision(revision)
  330. def set_id(self, ident):
  331. """Convenient method to set the unique identifier (primary key)
  332. @param ident: The unique identififer should be a combination of the dataset name and the mapset name@mapset
  333. """
  334. self.ident = ident
  335. self.D["id"] = ident
  336. def set_name(self, name):
  337. """Set the name of the dataset
  338. @param name: The name of the dataset
  339. """
  340. self.D["name"] = name
  341. def set_mapset(self, mapset):
  342. """Set the mapset of the dataset
  343. @param mapsets: The name of the mapset in whoch this dataset is stored
  344. """
  345. self.D["mapset"] = mapset
  346. def set_creator(self, creator):
  347. """Set the creator of the dataset
  348. @param creator: The name of the creator
  349. """
  350. self.D["creator"] = creator
  351. def set_ctime(self, ctime=None):
  352. """Set the creation time of the dataset, if nothing set the current time is used
  353. @param ctime: The current time of type datetime
  354. """
  355. if ctime == None:
  356. self.D["creation_time"] = datetime.now()
  357. else:
  358. self.D["creation_time"] = ctime
  359. def set_ttype(self, ttype):
  360. """Set the temporal type of the dataset: absolute or relative, if nothing set absolute time will assumed
  361. @param ttype: The temporal type of the dataset "absolute or relative"
  362. """
  363. if ttype == None or (ttype != "absolute" and ttype != "relative"):
  364. self.D["temporal_type"] = "absolute"
  365. else:
  366. self.D["temporal_type"] = ttype
  367. # def set_mtime(self, mtime=None):
  368. # """Set the modification time of the map, if nothing set the current time is used"""
  369. # if mtime == None:
  370. # self.D["modification_time"] = datetime.now()
  371. # else:
  372. # self.D["modification_time"] = mtime
  373. # def set_revision(self, revision=1):
  374. # """Set the revision of the map: if nothing set revision 1 will assumed"""
  375. # self.D["revision"] = revision
  376. def get_id(self):
  377. """Convenient method to get the unique identifier (primary key)
  378. @return None if not found
  379. """
  380. if self.D.has_key("id"):
  381. return self.D["id"]
  382. else:
  383. return None
  384. def get_name(self):
  385. """Get the name of the dataset
  386. @return None if not found"""
  387. if self.D.has_key("name"):
  388. return self.D["name"]
  389. else:
  390. return None
  391. def get_mapset(self):
  392. """Get the name of mapset of this dataset
  393. @return None if not found"""
  394. if self.D.has_key("mapset"):
  395. return self.D["mapset"]
  396. else:
  397. return None
  398. def get_creator(self):
  399. """Get the creator of the dataset
  400. @return None if not found"""
  401. if self.D.has_key("creator"):
  402. return self.D["creator"]
  403. else:
  404. return None
  405. def get_ctime(self):
  406. """Get the creation time of the dataset, datatype is datetime
  407. @return None if not found"""
  408. if self.D.has_key("creation_time"):
  409. return self.D["creation_time"]
  410. else:
  411. return None
  412. def get_ttype(self):
  413. """Get the temporal type of the map
  414. @return None if not found"""
  415. if self.D.has_key("temporal_type"):
  416. return self.D["temporal_type"]
  417. else:
  418. return None
  419. # def get_mtime(self):
  420. # """Get the modification time of the map, datatype is datetime
  421. # @return None if not found"""
  422. # if self.D.has_key("modification_time"):
  423. # return self.D["modification_time"]
  424. # else:
  425. # return None
  426. # def get_revision(self):
  427. # """Get the revision of the map
  428. # @return None if not found"""
  429. # if self.D.has_key("revision"):
  430. # return self.D["revision"]
  431. # else:
  432. # return None
  433. def print_info(self):
  434. """Print information about this class in human readable style"""
  435. # 0123456789012345678901234567890
  436. print " +-------------------- Basic information -------------------------------------+"
  437. print " | Id: ........................ " + str(self.get_id())
  438. print " | Name: ...................... " + str(self.get_name())
  439. print " | Mapset: .................... " + str(self.get_mapset())
  440. print " | Creator: ................... " + str(self.get_creator())
  441. print " | Creation time: ............. " + str(self.get_ctime())
  442. # print " | Modification time: ......... " + str(self.get_mtime())
  443. print " | Temporal type: ............. " + str(self.get_ttype())
  444. # print " | Revision in database: ...... " + str(self.get_revision())
  445. def print_shell_info(self):
  446. """Print information about this class in shell style"""
  447. print "id=" + str(self.get_id())
  448. print "name=" + str(self.get_name())
  449. print "mapset=" + str(self.get_mapset())
  450. print "creator=" + str(self.get_creator())
  451. print "creation_time=" + str(self.get_ctime())
  452. # print "modification_time=" + str(self.get_mtime())
  453. print "temporal_type=" + str(self.get_ttype())
  454. # print "revision=" + str(self.get_revision())
  455. ###############################################################################
  456. class raster_base(dataset_base):
  457. def __init__(self, ident=None, name=None, mapset=None, creator=None, creation_time=None,\
  458. modification_time=None, temporal_type=None, revision=1):
  459. dataset_base.__init__(self, "raster_base", ident, name, mapset, creator, creation_time,\
  460. modification_time, temporal_type, revision)
  461. class raster3d_base(dataset_base):
  462. def __init__(self, ident=None, name=None, mapset=None, creator=None, creation_time=None,\
  463. modification_time=None, temporal_type=None, revision=1):
  464. dataset_base.__init__(self, "raster3d_base", ident, name, mapset, creator, creation_time,\
  465. modification_time, temporal_type, revision)
  466. class vector_base(dataset_base):
  467. def __init__(self, ident=None, name=None, mapset=None, creator=None, creation_time=None,\
  468. modification_time=None, temporal_type=None, revision=1):
  469. dataset_base.__init__(self, "vector_base", ident, name, mapset, creator, creation_time,\
  470. modification_time, temporal_type, revision)
  471. ###############################################################################
  472. class stds_base(dataset_base):
  473. def __init__(self, table=None, ident=None, name=None, mapset=None, semantic_type=None, creator=None, creation_time=None,\
  474. modification_time=None, temporal_type=None, revision=1):
  475. dataset_base.__init__(self, table, ident, name, mapset, creator, creation_time,\
  476. modification_time, temporal_type, revision)
  477. self.set_semantic_type(semantic_type)
  478. def set_semantic_type(self, semantic_type):
  479. """Set the semantic type of the space time dataset"""
  480. self.D["semantic_type"] = semantic_type
  481. def get_semantic_type(self):
  482. """Get the semantic type of the space time dataset
  483. @return None if not found"""
  484. if self.D.has_key("semantic_type"):
  485. return self.D["semantic_type"]
  486. else:
  487. return None
  488. def print_info(self):
  489. """Print information about this class in human readable style"""
  490. dataset_base.print_info(self)
  491. # 0123456789012345678901234567890
  492. print " | Semantic type:.............. " + str(self.get_semantic_type())
  493. def print_shell_info(self):
  494. """Print information about this class in shell style"""
  495. dataset_base.print_shell_info(self)
  496. print "semantic_type=" + str(self.get_semantic_type())
  497. ###############################################################################
  498. class strds_base(stds_base):
  499. def __init__(self, ident=None, name=None, mapset=None, semantic_type=None, creator=None, creation_time=None,\
  500. modification_time=None, temporal_type=None, revision=1):
  501. stds_base.__init__(self, "strds_base", ident, name, mapset, semantic_type, creator, creation_time,\
  502. modification_time, temporal_type, revision)
  503. class str3ds_base(stds_base):
  504. def __init__(self, ident=None, name=None, mapset=None, semantic_type=None, creator=None, creation_time=None,\
  505. modification_time=None, temporal_type=None, revision=1):
  506. stds_base.__init__(self, "str3ds_base", ident, name, mapset, semantic_type, creator, creation_time,\
  507. modification_time, temporal_type, revision)
  508. class stvds_base(stds_base):
  509. def __init__(self, ident=None, name=None, mapset=None, semantic_type=None, creator=None, creation_time=None,\
  510. modification_time=None, temporal_type=None, revision=1):
  511. stds_base.__init__(self, "stvds_base", ident, name, mapset, semantic_type, creator, creation_time,\
  512. modification_time, temporal_type, revision)