base.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. elif dbmi.__name__ == "psycopg2":
  189. self.connection = dbmi.connect(self.database)
  190. self.connection.set_isolation_level(dbmi.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
  191. self.cursor = self.connection.cursor(cursor_factory=dbmi.extras.DictCursor)
  192. def close(self):
  193. """Close the DBMI connection"""
  194. #print "Close connection to", self.database
  195. self.connection.commit()
  196. self.cursor.close()
  197. def get_delete_statement(self):
  198. return "DELETE FROM " + self.get_table_name() + " WHERE id = \'" + str(self.ident) + "\'"
  199. def delete(self, dbif=None):
  200. """Delete the entry of this object from the temporal database"""
  201. sql = self.get_delete_statement()
  202. #print sql
  203. if dbif:
  204. dbif.cursor.execute(sql)
  205. else:
  206. self.connect()
  207. self.cursor.execute(sql)
  208. self.close()
  209. def get_is_in_db_statement(self):
  210. return "SELECT id FROM " + self.get_table_name() + " WHERE id = \'" + str(self.ident) + "\'"
  211. def is_in_db(self, dbif=None):
  212. """Check if this object is present in the temporal database
  213. @param dbif: The database interface to be used
  214. """
  215. sql = self.get_is_in_db_statement()
  216. #print sql
  217. if dbif:
  218. dbif.cursor.execute(sql)
  219. row = dbif.cursor.fetchone()
  220. else:
  221. self.connect()
  222. self.cursor.execute(sql)
  223. row = self.cursor.fetchone()
  224. self.close()
  225. # Nothing found
  226. if row == None:
  227. return False
  228. return True
  229. def get_select_statement(self):
  230. return self.serialize("SELECT", self.get_table_name(), "WHERE id = \'" + str(self.ident) + "\'")
  231. def select(self, dbif=None):
  232. """Select the content from the temporal database and store it
  233. in the internal dictionary structure
  234. @param dbif: The database interface to be used
  235. """
  236. sql, args = self.get_select_statement()
  237. #print sql
  238. #print args
  239. if dbif:
  240. if len(args) == 0:
  241. dbif.cursor.execute(sql)
  242. else:
  243. dbif.cursor.execute(sql, args)
  244. row = dbif.cursor.fetchone()
  245. else:
  246. self.connect()
  247. if len(args) == 0:
  248. self.cursor.execute(sql)
  249. else:
  250. self.cursor.execute(sql, args)
  251. row = self.cursor.fetchone()
  252. self.close()
  253. # Nothing found
  254. if row == None:
  255. return False
  256. if len(row) > 0:
  257. self.deserialize(row)
  258. else:
  259. core.fatal(_("Object <%s> not found in the temporal database") % self.get_id())
  260. return True
  261. def get_insert_statement(self):
  262. return self.serialize("INSERT", self.get_table_name())
  263. def insert(self, dbif=None):
  264. """Serialize the content of this object and store it in the temporal
  265. database using the internal identifier
  266. @param dbif: The database interface to be used
  267. """
  268. sql, args = self.get_insert_statement()
  269. #print sql
  270. #print args
  271. if dbif:
  272. dbif.cursor.execute(sql, args)
  273. else:
  274. self.connect()
  275. self.cursor.execute(sql, args)
  276. self.close()
  277. def get_update_statement(self):
  278. return self.serialize("UPDATE", self.get_table_name(), "WHERE id = \'" + str(self.ident) + "\'")
  279. def update(self, dbif=None):
  280. """Serialize the content of this object and update it in the temporal
  281. database using the internal identifier
  282. Only object entries which are exists (not None) are updated
  283. @param dbif: The database interface to be used
  284. """
  285. if self.ident == None:
  286. raise IOError("Missing identifer");
  287. sql, args = self.get_update_statement()
  288. #print sql
  289. #print args
  290. if dbif:
  291. dbif.cursor.execute(sql, args)
  292. else:
  293. self.connect()
  294. self.cursor.execute(sql, args)
  295. self.close()
  296. def get_update_all_statement(self):
  297. return self.serialize("UPDATE ALL", self.get_table_name(), "WHERE id = \'" + str(self.ident) + "\'")
  298. def update_all(self, dbif=None):
  299. """Serialize the content of this object, including None objects, and update it in the temporal
  300. database using the internal identifier
  301. @param dbif: The database interface to be used
  302. """
  303. if self.ident == None:
  304. raise IOError("Missing identifer");
  305. sql, args = self.get_update_all_statement()
  306. #print sql
  307. #print args
  308. if dbif:
  309. dbif.cursor.execute(sql, args)
  310. else:
  311. self.connect()
  312. self.cursor.execute(sql, args)
  313. self.close()
  314. ###############################################################################
  315. class dataset_base(sql_database_interface):
  316. """This is the base class for all maps and spacetime datasets storing basic identification information"""
  317. def __init__(self, table=None, ident=None, name=None, mapset=None, creator=None, ctime=None,\
  318. mtime=None, ttype=None, revision=1):
  319. sql_database_interface.__init__(self, table, ident)
  320. self.set_id(ident)
  321. self.set_name(name)
  322. self.set_mapset(mapset)
  323. self.set_creator(creator)
  324. self.set_ctime(ctime)
  325. self.set_ttype(ttype)
  326. #self.set_mtime(mtime)
  327. #self.set_revision(revision)
  328. def set_id(self, ident):
  329. """Convenient method to set the unique identifier (primary key)
  330. @param ident: The unique identififer should be a combination of the dataset name and the mapset name@mapset
  331. """
  332. self.ident = ident
  333. self.D["id"] = ident
  334. def set_name(self, name):
  335. """Set the name of the dataset
  336. @param name: The name of the dataset
  337. """
  338. self.D["name"] = name
  339. def set_mapset(self, mapset):
  340. """Set the mapset of the dataset
  341. @param mapsets: The name of the mapset in whoch this dataset is stored
  342. """
  343. self.D["mapset"] = mapset
  344. def set_creator(self, creator):
  345. """Set the creator of the dataset
  346. @param creator: The name of the creator
  347. """
  348. self.D["creator"] = creator
  349. def set_ctime(self, ctime=None):
  350. """Set the creation time of the dataset, if nothing set the current time is used
  351. @param ctime: The current time of type datetime
  352. """
  353. if ctime == None:
  354. self.D["creation_time"] = datetime.now()
  355. else:
  356. self.D["creation_time"] = ctime
  357. def set_ttype(self, ttype):
  358. """Set the temporal type of the dataset: absolute or relative, if nothing set absolute time will assumed
  359. @param ttype: The temporal type of the dataset "absolute or relative"
  360. """
  361. if ttype == None or (ttype != "absolute" and ttype != "relative"):
  362. self.D["temporal_type"] = "absolute"
  363. else:
  364. self.D["temporal_type"] = ttype
  365. # def set_mtime(self, mtime=None):
  366. # """Set the modification time of the map, if nothing set the current time is used"""
  367. # if mtime == None:
  368. # self.D["modification_time"] = datetime.now()
  369. # else:
  370. # self.D["modification_time"] = mtime
  371. # def set_revision(self, revision=1):
  372. # """Set the revision of the map: if nothing set revision 1 will assumed"""
  373. # self.D["revision"] = revision
  374. def get_id(self):
  375. """Convenient method to get the unique identifier (primary key)
  376. @return None if not found
  377. """
  378. if self.D.has_key("id"):
  379. return self.D["id"]
  380. else:
  381. return None
  382. def get_name(self):
  383. """Get the name of the dataset
  384. @return None if not found"""
  385. if self.D.has_key("name"):
  386. return self.D["name"]
  387. else:
  388. return None
  389. def get_mapset(self):
  390. """Get the name of mapset of this dataset
  391. @return None if not found"""
  392. if self.D.has_key("mapset"):
  393. return self.D["mapset"]
  394. else:
  395. return None
  396. def get_creator(self):
  397. """Get the creator of the dataset
  398. @return None if not found"""
  399. if self.D.has_key("creator"):
  400. return self.D["creator"]
  401. else:
  402. return None
  403. def get_ctime(self):
  404. """Get the creation time of the dataset, datatype is datetime
  405. @return None if not found"""
  406. if self.D.has_key("creation_time"):
  407. return self.D["creation_time"]
  408. else:
  409. return None
  410. def get_ttype(self):
  411. """Get the temporal type of the map
  412. @return None if not found"""
  413. if self.D.has_key("temporal_type"):
  414. return self.D["temporal_type"]
  415. else:
  416. return None
  417. # def get_mtime(self):
  418. # """Get the modification time of the map, datatype is datetime
  419. # @return None if not found"""
  420. # if self.D.has_key("modification_time"):
  421. # return self.D["modification_time"]
  422. # else:
  423. # return None
  424. # def get_revision(self):
  425. # """Get the revision of the map
  426. # @return None if not found"""
  427. # if self.D.has_key("revision"):
  428. # return self.D["revision"]
  429. # else:
  430. # return None
  431. def print_info(self):
  432. """Print information about this class in human readable style"""
  433. # 0123456789012345678901234567890
  434. print " +-------------------- Basic information -------------------------------------+"
  435. print " | Id: ........................ " + str(self.get_id())
  436. print " | Name: ...................... " + str(self.get_name())
  437. print " | Mapset: .................... " + str(self.get_mapset())
  438. print " | Creator: ................... " + str(self.get_creator())
  439. print " | Creation time: ............. " + str(self.get_ctime())
  440. # print " | Modification time: ......... " + str(self.get_mtime())
  441. print " | Temporal type: ............. " + str(self.get_ttype())
  442. # print " | Revision in database: ...... " + str(self.get_revision())
  443. def print_shell_info(self):
  444. """Print information about this class in shell style"""
  445. print "id=" + str(self.get_id())
  446. print "name=" + str(self.get_name())
  447. print "mapset=" + str(self.get_mapset())
  448. print "creator=" + str(self.get_creator())
  449. print "creation_time=" + str(self.get_ctime())
  450. # print "modification_time=" + str(self.get_mtime())
  451. print "temporal_type=" + str(self.get_ttype())
  452. # print "revision=" + str(self.get_revision())
  453. ###############################################################################
  454. class raster_base(dataset_base):
  455. def __init__(self, ident=None, name=None, mapset=None, creator=None, creation_time=None,\
  456. modification_time=None, temporal_type=None, revision=1):
  457. dataset_base.__init__(self, "raster_base", ident, name, mapset, creator, creation_time,\
  458. modification_time, temporal_type, revision)
  459. class raster3d_base(dataset_base):
  460. def __init__(self, ident=None, name=None, mapset=None, creator=None, creation_time=None,\
  461. modification_time=None, temporal_type=None, revision=1):
  462. dataset_base.__init__(self, "raster3d_base", ident, name, mapset, creator, creation_time,\
  463. modification_time, temporal_type, revision)
  464. class vector_base(dataset_base):
  465. def __init__(self, ident=None, name=None, mapset=None, creator=None, creation_time=None,\
  466. modification_time=None, temporal_type=None, revision=1):
  467. dataset_base.__init__(self, "vector_base", ident, name, mapset, creator, creation_time,\
  468. modification_time, temporal_type, revision)
  469. ###############################################################################
  470. class stds_base(dataset_base):
  471. def __init__(self, table=None, ident=None, name=None, mapset=None, semantic_type=None, creator=None, creation_time=None,\
  472. modification_time=None, temporal_type=None, revision=1):
  473. dataset_base.__init__(self, table, ident, name, mapset, creator, creation_time,\
  474. modification_time, temporal_type, revision)
  475. self.set_semantic_type(semantic_type)
  476. def set_semantic_type(self, semantic_type):
  477. """Set the semantic type of the space time dataset"""
  478. self.D["semantic_type"] = semantic_type
  479. def get_semantic_type(self):
  480. """Get the semantic type of the space time dataset
  481. @return None if not found"""
  482. if self.D.has_key("semantic_type"):
  483. return self.D["semantic_type"]
  484. else:
  485. return None
  486. def print_info(self):
  487. """Print information about this class in human readable style"""
  488. dataset_base.print_info(self)
  489. # 0123456789012345678901234567890
  490. print " | Semantic type:.............. " + str(self.get_semantic_type())
  491. def print_shell_info(self):
  492. """Print information about this class in shell style"""
  493. dataset_base.print_shell_info(self)
  494. print "semantic_type=" + str(self.get_semantic_type())
  495. ###############################################################################
  496. class strds_base(stds_base):
  497. def __init__(self, ident=None, name=None, mapset=None, semantic_type=None, creator=None, creation_time=None,\
  498. modification_time=None, temporal_type=None, revision=1):
  499. stds_base.__init__(self, "strds_base", ident, name, mapset, semantic_type, creator, creation_time,\
  500. modification_time, temporal_type, revision)
  501. class str3ds_base(stds_base):
  502. def __init__(self, ident=None, name=None, mapset=None, semantic_type=None, creator=None, creation_time=None,\
  503. modification_time=None, temporal_type=None, revision=1):
  504. stds_base.__init__(self, "str3ds_base", ident, name, mapset, semantic_type, creator, creation_time,\
  505. modification_time, temporal_type, revision)
  506. class stvds_base(stds_base):
  507. def __init__(self, ident=None, name=None, mapset=None, semantic_type=None, creator=None, creation_time=None,\
  508. modification_time=None, temporal_type=None, revision=1):
  509. stds_base.__init__(self, "stvds_base", ident, name, mapset, semantic_type, creator, creation_time,\
  510. modification_time, temporal_type, revision)