abstract_map_dataset.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. # -*- coding: utf-8 -*-
  2. """!@package grass.temporal
  3. @brief GRASS Python scripting module (temporal GIS functions)
  4. Temporal GIS related functions to be used in temporal GIS Python library package.
  5. Usage:
  6. >>> import grass.temporal as tgis
  7. >>> amd = tgis.AbstractMapDataset()
  8. (C) 2008-2011 by the GRASS Development Team
  9. This program is free software under the GNU General Public
  10. License (>=v2). Read the file COPYING that comes with GRASS
  11. for details.
  12. @author Soeren Gebbert
  13. """
  14. from abstract_dataset import *
  15. from datetime_math import *
  16. class AbstractMapDataset(AbstractDataset):
  17. """!This is the base class for all maps (raster, vector, raster3d).
  18. The temporal extent, the spatial extent and the metadata of maps
  19. are stored in the temporal database. Maps can be registered in the
  20. temporal database, updated and deleted.
  21. This class provides all functionalities that are needed to manage maps
  22. in the temporal database. That are:
  23. - insert() to register the map and therefore its spatio-temporal extent
  24. and metadata in the temporal database
  25. - update() to update the map spatio-temporal extent and metadata in the
  26. temporal database
  27. - unregister() to unregister the map from each space time dataset in
  28. which this map is registered
  29. - delete() to remove the map from the temporal database
  30. - Methods to set relative and absolute time stamps
  31. - Abstract methods that must be implemented in the map specific
  32. subclasses
  33. """
  34. def __init__(self):
  35. AbstractDataset.__init__(self)
  36. def get_new_stds_instance(self, ident):
  37. """!Return a new space time dataset instance that store maps with the
  38. type of this map object (rast, rast3d or vect)
  39. @param ident The identifier of the space time dataset
  40. @return The new space time dataset instance
  41. """
  42. raise ImplementationError(
  43. "This method must be implemented in the subclasses")
  44. def get_stds_register(self):
  45. """!Return the space time dataset register table name
  46. Maps can be registered in several different space time datasets.
  47. This method returns the name of the register table in the
  48. temporal database.
  49. @return The name of the stds register table
  50. """
  51. raise ImplementationError(
  52. "This method must be implemented in the subclasses")
  53. def set_stds_register(self, name):
  54. """!Set the space time dataset register table name.
  55. This table stores all space time datasets in
  56. which this map is registered.
  57. @param name The name of the register table
  58. """
  59. raise ImplementationError(
  60. "This method must be implemented in the subclasses")
  61. def check_resolution_with_current_region(self):
  62. """!Check if the raster or voxel resolution is
  63. finer than the current resolution
  64. - Return "finer" in case the raster/voxel resolution is finer
  65. than the current region
  66. - Return "coarser" in case the raster/voxel resolution is coarser
  67. than the current region
  68. Vector maps have no resolution, since they store the coordinates
  69. directly.
  70. @return "finer" or "coarser"
  71. """
  72. raise ImplementationError(
  73. "This method must be implemented in the subclasses")
  74. def has_grass_timestamp(self):
  75. """!Check if a grass file based time stamp exists for this map.
  76. @return True is the grass file based time stamped exists for this
  77. map
  78. """
  79. raise ImplementationError(
  80. "This method must be implemented in the subclasses")
  81. def write_timestamp_to_grass(self):
  82. """!Write the timestamp of this map into the map metadata
  83. in the grass file system based spatial database.
  84. """
  85. raise ImplementationError(
  86. "This method must be implemented in the subclasses")
  87. def remove_timestamp_from_grass(self):
  88. """!Remove the timestamp from the grass file
  89. system based spatial database
  90. """
  91. raise ImplementationError(
  92. "This method must be implemented in the subclasses")
  93. def map_exists(self):
  94. """!Return True in case the map exists in the grass spatial database
  95. @return True if map exists, False otherwise
  96. """
  97. raise ImplementationError(
  98. "This method must be implemented in the subclasses")
  99. def read_info(self):
  100. """!Read the map info from the grass file system based database and
  101. store the content into a dictionary
  102. """
  103. raise ImplementationError(
  104. "This method must be implemented in the subclasses")
  105. def load(self):
  106. """!Load the content of this object from the grass
  107. file system based database"""
  108. raise ImplementationError(
  109. "This method must be implemented in the subclasses")
  110. def _convert_timestamp(self):
  111. """!Convert the valid time into a grass datetime library
  112. compatible timestamp string
  113. This methods works for relative and absolute time
  114. @return the grass timestamp string
  115. """
  116. start = ""
  117. if self.is_time_absolute():
  118. start_time, end_time, tz = self.get_absolute_time()
  119. start = datetime_to_grass_datetime_string(start_time)
  120. if end_time is not None:
  121. end = datetime_to_grass_datetime_string(end_time)
  122. start += " / %s" % (end)
  123. else:
  124. start_time, end_time, unit = self.get_relative_time()
  125. start = "%i %s" % (int(start_time), unit)
  126. if end_time is not None:
  127. end = "%i %s" % (int(end_time), unit)
  128. start += " / %s" % (end)
  129. return start
  130. def get_map_id(self):
  131. """!Return the map id. The map id is the unique identifier
  132. in grass and must not be equal to the
  133. primary key identifier (id) of the map in the database.
  134. Since vector maps may have layer information,
  135. the unique id is a combination of name, layer and mapset.
  136. Use get_map_id() every time your need to access the grass map
  137. in the file system but not to identify
  138. map information in the temporal database.
  139. @return The map id "name@mapset"
  140. """
  141. return self.base.get_map_id()
  142. def build_id(self, name, mapset, layer=None):
  143. """!Convenient method to build the unique identifier
  144. Existing layer and mapset definitions in the name
  145. string will be reused
  146. @param name The name of the map
  147. @param mapset The mapset in which the map is located
  148. @param layer The layer of the vector map, use None in case no
  149. layer exists
  150. @return the id of the map as "name(:layer)@mapset"
  151. while layer is optional
  152. """
  153. # Check if the name includes any mapset
  154. if name.find("@") >= 0:
  155. name, mapset = name.split("@")
  156. # Check for layer number in map name
  157. if name.find(":") >= 0:
  158. name, layer = name.split(":")
  159. if layer is not None:
  160. return "%s:%s@%s" % (name, layer, mapset)
  161. else:
  162. return "%s@%s" % (name, mapset)
  163. def get_layer(self):
  164. """!Return the layer of the map
  165. @return the layer of the map or None in case no layer is defined
  166. """
  167. return self.base.get_layer()
  168. def print_info(self):
  169. """!Print information about this object in human readable style"""
  170. if self.get_type() == "raster":
  171. # 1 2 3 4 5 6 7
  172. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  173. print " +-------------------- Raster Dataset ----------------------------------------+"
  174. if self.get_type() == "raster3d":
  175. # 1 2 3 4 5 6 7
  176. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  177. print " +-------------------- 3D Raster Dataset -------------------------------------+"
  178. if self.get_type() == "vector":
  179. # 1 2 3 4 5 6 7
  180. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  181. print " +-------------------- Vector Dataset ----------------------------------------+"
  182. print " | |"
  183. self.base.print_info()
  184. self.temporal_extent.print_info()
  185. if self.is_topology_build():
  186. self.print_topology_info()
  187. self.spatial_extent.print_info()
  188. self.metadata.print_info()
  189. datasets = self.get_registered_datasets()
  190. count = 0
  191. string = ""
  192. if datasets is not None:
  193. for ds in datasets:
  194. if count > 0 and count % 3 == 0:
  195. string += "\n | ............................ "
  196. count = 0
  197. if count == 0:
  198. string += ds["id"]
  199. else:
  200. string += ",%s" % ds["id"]
  201. count += 1
  202. print " | Registered datasets ........ " + string
  203. print " +----------------------------------------------------------------------------+"
  204. def print_shell_info(self):
  205. """!Print information about this object in shell style"""
  206. self.base.print_shell_info()
  207. self.temporal_extent.print_shell_info()
  208. self.spatial_extent.print_shell_info()
  209. self.metadata.print_shell_info()
  210. datasets = self.get_registered_datasets()
  211. count = 0
  212. string = ""
  213. if datasets is not None:
  214. for ds in datasets:
  215. if count == 0:
  216. string += ds["id"]
  217. else:
  218. string += ",%s" % ds["id"]
  219. count += 1
  220. print "registered_datasets=" + string
  221. if self.is_topology_build():
  222. self.print_topology_shell_info()
  223. def insert(self, dbif=None, execute=True):
  224. """!Insert the map content into the database from the internal
  225. structure
  226. This functions assures that the timestamp is written to the
  227. grass file system based database in addition to the temporal
  228. database entry.
  229. @param dbif The database interface to be used
  230. @param execute If True the SQL statements will be executed.
  231. If False the prepared SQL statements are
  232. returned and must be executed by the caller.
  233. @return The SQL insert statement in case execute=False, or an
  234. empty string otherwise
  235. """
  236. self.write_timestamp_to_grass()
  237. return AbstractDataset.insert(self, dbif, execute)
  238. def update(self, dbif=None, execute=True):
  239. """!Update the map content in the database from the internal structure
  240. excluding None variables
  241. This functions assures that the timestamp is written to the
  242. grass file system based database in addition to the temporal
  243. database entry.
  244. @param dbif The database interface to be used
  245. @param execute If True the SQL statements will be executed.
  246. If False the prepared SQL statements are
  247. returned and must be executed by the caller.
  248. @return The SQL insert statement in case execute=False, or an
  249. empty string otherwise
  250. """
  251. self.write_timestamp_to_grass()
  252. return AbstractDataset.update(self, dbif, execute)
  253. def update_all(self, dbif=None, execute=True):
  254. """!Update the map content in the database from the internal structure
  255. including None variables
  256. This functions assures that the timestamp is written to the
  257. grass file system based database in addition to the temporal
  258. database entry.
  259. @param dbif The database interface to be used
  260. @param execute If True the SQL statements will be executed.
  261. If False the prepared SQL statements are
  262. returned and must be executed by the caller.
  263. @return The SQL insert statement in case execute=False, or an
  264. empty string otherwise
  265. """
  266. self.write_timestamp_to_grass()
  267. return AbstractDataset.update_all(self, dbif, execute)
  268. def set_absolute_time(self, start_time, end_time=None, timezone=None):
  269. """!Set the absolute time with start time and end time
  270. The end time is optional and must be set to None in case of time
  271. instance.
  272. This method only modifies this object and does not commit
  273. the modifications to the temporal database.
  274. @param start_time a datetime object specifying the start time of the
  275. map
  276. @param end_time a datetime object specifying the end time of the
  277. map, None in case or time instance
  278. @param timezone Thee timezone of the map (not used)
  279. """
  280. if start_time and not isinstance(start_time, datetime):
  281. if self.get_layer() is not None:
  282. core.fatal(_("Start time must be of type datetime for %(type)s"
  283. " map <%(id)s> with layer: %(l)s") % {
  284. 'type': self.get_type(), 'id': self.get_map_id(),
  285. 'l': self.get_layer()})
  286. else:
  287. core.fatal(_("Start time must be of type datetime for "
  288. "%(type)s map <%(id)s>") % {
  289. 'type': self.get_type(), 'id': self.get_map_id()})
  290. if end_time and not isinstance(end_time, datetime):
  291. if self.get_layer():
  292. core.fatal(_("End time must be of type datetime for %(type)s "
  293. "map <%(id)s> with layer: %(l)s") % {
  294. 'type': self.get_type(), 'id': self.get_map_id(),
  295. 'l': self.get_layer()})
  296. else:
  297. core.fatal(_("End time must be of type datetime for "
  298. "%(type)s map <%(id)s>") % {
  299. 'type': self.get_type(), 'id': self.get_map_id()})
  300. if start_time is not None and end_time is not None:
  301. if start_time > end_time:
  302. if self.get_layer():
  303. core.fatal(_("End time must be greater than start time for"
  304. " %(type)s map <%(id)s> with layer: %(l)s") % {
  305. 'type': self.get_type(),
  306. 'id': self.get_map_id(),
  307. 'l': self.get_layer()})
  308. else:
  309. core.fatal(_("End time must be greater than start time "
  310. "for %(type)s map <%(id)s>") % {
  311. 'type': self.get_type(),
  312. 'id': self.get_map_id()})
  313. else:
  314. # Do not create an interval in case start and end time are
  315. # equal
  316. if start_time == end_time:
  317. end_time = None
  318. self.base.set_ttype("absolute")
  319. self.absolute_time.set_start_time(start_time)
  320. self.absolute_time.set_end_time(end_time)
  321. self.absolute_time.set_timezone(timezone)
  322. return True
  323. def update_absolute_time(self, start_time, end_time=None,
  324. timezone=None, dbif=None):
  325. """!Update the absolute time
  326. The end time is optional and must be set to None in case of time
  327. instance.
  328. This functions assures that the timestamp is written to the
  329. grass file system based database in addition to the temporal
  330. database entry.
  331. @param start_time a datetime object specifying the start time of
  332. the map
  333. @param end_time a datetime object specifying the end time of the
  334. map, None in case or time instance
  335. @param timezone Thee timezone of the map (not used)
  336. @param dbif The database interface to be used
  337. """
  338. dbif, connected = init_dbif(dbif)
  339. self.set_absolute_time(start_time, end_time, timezone)
  340. self.absolute_time.update_all(dbif)
  341. self.base.update(dbif)
  342. if connected:
  343. dbif.close()
  344. self.write_timestamp_to_grass()
  345. def set_relative_time(self, start_time, end_time, unit):
  346. """!Set the relative time interval
  347. The end time is optional and must be set to None in case of time
  348. instance.
  349. This method only modifies this object and does not commit
  350. the modifications to the temporal database.
  351. @param start_time An integer value
  352. @param end_time An integer value, None in case or time instance
  353. @param unit The unit of the relative time. Supported units:
  354. year(s), month(s), day(s), hour(s), minute(s), second(s)
  355. @return True for success and False otherwise
  356. """
  357. if not self.check_relative_time_unit(unit):
  358. if self.get_layer() is not None:
  359. core.error(_("Unsupported relative time unit type for %(type)s"
  360. " map <%(id)s> with layer %(l)s: %(u)s") % {
  361. 'type': self.get_type(), 'id': self.get_id(),
  362. 'l': self.get_layer(), 'u': unit})
  363. else:
  364. core.error(_("Unsupported relative time unit type for %(type)s"
  365. " map <%(id)s>: %(u)s") % {
  366. 'type': self.get_type(), 'id': self.get_id(),
  367. 'u': unit})
  368. return False
  369. if start_time is not None and end_time is not None:
  370. if int(start_time) > int(end_time):
  371. if self.get_layer() is not None:
  372. core.error(_("End time must be greater than start time for"
  373. " %(type)s map <%(id)s> with layer %(l)s") % \
  374. {'type': self.get_type(), 'id': self.get_id(),
  375. 'l': self.get_layer()})
  376. else:
  377. core.error(_("End time must be greater than start time for"
  378. " %(type)s map <%(id)s>") % {
  379. 'type': self.get_type(), 'id': self.get_id()})
  380. return False
  381. else:
  382. # Do not create an interval in case start and end time are
  383. # equal
  384. if start_time == end_time:
  385. end_time = None
  386. self.base.set_ttype("relative")
  387. self.relative_time.set_unit(unit)
  388. self.relative_time.set_start_time(int(start_time))
  389. if end_time is not None:
  390. self.relative_time.set_end_time(int(end_time))
  391. else:
  392. self.relative_time.set_end_time(None)
  393. return True
  394. def update_relative_time(self, start_time, end_time, unit, dbif=None):
  395. """!Update the relative time interval
  396. The end time is optional and must be set to None in case of time
  397. instance.
  398. This functions assures that the timestamp is written to the
  399. grass file system based database in addition to the temporal
  400. database entry.
  401. @param start_time An integer value
  402. @param end_time An integer value, None in case or time instance
  403. @param unit The relative time unit
  404. @param dbif The database interface to be used
  405. """
  406. dbif, connected = init_dbif(dbif)
  407. if self.set_relative_time(start_time, end_time, unit):
  408. self.relative_time.update_all(dbif)
  409. self.base.update(dbif)
  410. if connected:
  411. dbif.close()
  412. self.write_timestamp_to_grass()
  413. def temporal_buffer(self, increment, update=False, dbif=None):
  414. """!Create a temporal buffer based on an increment
  415. For absolute time the increment must be a string of type "integer
  416. unit"
  417. Unit can be year, years, month, months, day, days, hour, hours,
  418. minute, minutes, day or days.
  419. @param increment This is the increment, a string in case of absolute
  420. time or an integer in case of relative time
  421. @param update Perform an immediate database update to store the
  422. modified temporal extent, otherwise only this object
  423. will be modified
  424. Usage:
  425. @code
  426. >>> import grass.temporal as tgis
  427. >>> maps = []
  428. >>> for i in range(5):
  429. ... map = tgis.RasterDataset(None)
  430. ... if i%2 == 0:
  431. ... check = map.set_relative_time(i, i + 1, 'years')
  432. ... else:
  433. ... check = map.set_relative_time(i, None, 'years')
  434. ... map.temporal_buffer(3)
  435. ... maps.append(map)
  436. >>> for map in maps:
  437. ... map.temporal_extent.print_info()
  438. +-------------------- Relative time -----------------------------------------+
  439. | Start time:................. -3
  440. | End time:................... 4
  441. | Relative time unit:......... years
  442. +-------------------- Relative time -----------------------------------------+
  443. | Start time:................. -2
  444. | End time:................... 4
  445. | Relative time unit:......... years
  446. +-------------------- Relative time -----------------------------------------+
  447. | Start time:................. -1
  448. | End time:................... 6
  449. | Relative time unit:......... years
  450. +-------------------- Relative time -----------------------------------------+
  451. | Start time:................. 0
  452. | End time:................... 6
  453. | Relative time unit:......... years
  454. +-------------------- Relative time -----------------------------------------+
  455. | Start time:................. 1
  456. | End time:................... 8
  457. | Relative time unit:......... years
  458. >>> maps = []
  459. >>> for i in range(1,5):
  460. ... map = tgis.RasterDataset(None)
  461. ... if i%2 == 0:
  462. ... check = map.set_absolute_time(datetime(2001,i,1), datetime(2001, i + 1, 1))
  463. ... else:
  464. ... check = map.set_absolute_time(datetime(2001,i,1), None)
  465. ... map.temporal_buffer("7 days")
  466. ... maps.append(map)
  467. >>> for map in maps:
  468. ... map.temporal_extent.print_info()
  469. +-------------------- Absolute time -----------------------------------------+
  470. | Start time:................. 2000-12-25 00:00:00
  471. | End time:................... 2001-01-08 00:00:00
  472. +-------------------- Absolute time -----------------------------------------+
  473. | Start time:................. 2001-01-25 00:00:00
  474. | End time:................... 2001-03-08 00:00:00
  475. +-------------------- Absolute time -----------------------------------------+
  476. | Start time:................. 2001-02-22 00:00:00
  477. | End time:................... 2001-03-08 00:00:00
  478. +-------------------- Absolute time -----------------------------------------+
  479. | Start time:................. 2001-03-25 00:00:00
  480. | End time:................... 2001-05-08 00:00:00
  481. @endcode
  482. """
  483. if self.is_time_absolute():
  484. start, end, tz = self.get_absolute_time()
  485. new_start = decrement_datetime_by_string(start, increment)
  486. if end == None:
  487. new_end = increment_datetime_by_string(start, increment)
  488. else:
  489. new_end = increment_datetime_by_string(end, increment)
  490. if update:
  491. self.update_absolute_time(new_start, new_end, tz, dbif=dbif)
  492. else:
  493. self.set_absolute_time(new_start, new_end, tz)
  494. else:
  495. start, end, unit = self.get_relative_time()
  496. new_start = start - increment
  497. if end == None:
  498. new_end = start + increment
  499. else:
  500. new_end = end + increment
  501. if update:
  502. self.update_relative_time(new_start, new_end, unit, dbif=dbif)
  503. else:
  504. self.set_relative_time(new_start, new_end, unit)
  505. def set_spatial_extent(self, north, south, east, west, top=0, bottom=0):
  506. """!Set the spatial extent of the map
  507. This method only modifies this object and does not commit
  508. the modifications to the temporal database.
  509. @param north The northern edge
  510. @param south The southern edge
  511. @param east The eastern edge
  512. @param west The western edge
  513. @param top The top edge
  514. @param bottom The bottom edge
  515. """
  516. self.spatial_extent.set_spatial_extent(
  517. north, south, east, west, top, bottom)
  518. def check_for_correct_time(self):
  519. """!Check for correct time"""
  520. if self.is_time_absolute():
  521. start, end, tz = self.get_absolute_time()
  522. else:
  523. start, end, unit = self.get_relative_time()
  524. if start is not None:
  525. if end is not None:
  526. if start >= end:
  527. if self.get_layer() is not None:
  528. core.error(_("Map <%(id)s> with layer %(layer)s has "
  529. "incorrect time interval, start time is "
  530. "greater than end time") % {
  531. 'id': self.get_map_id(),
  532. 'layer': self.get_layer()})
  533. else:
  534. core.error(_("Map <%s> has incorrect time interval, "
  535. "start time is greater than end time") % \
  536. (self.get_map_id()))
  537. return False
  538. else:
  539. core.error(_("Map <%s> has incorrect start time") %
  540. (self.get_map_id()))
  541. return False
  542. return True
  543. def delete(self, dbif=None, update=True, execute=True):
  544. """!Delete a map entry from database if it exists
  545. Remove dependent entries:
  546. * Remove the map entry in each space time dataset in which this map
  547. is registered
  548. * Remove the space time dataset register table
  549. @param dbif The database interface to be used
  550. @param update Call for each unregister statement the update from
  551. registered maps of the space time dataset.
  552. This can slow down the un-registration process
  553. significantly.
  554. @param execute If True the SQL DELETE and DROP table statements will
  555. be executed.
  556. If False the prepared SQL statements are
  557. returned and must be executed by the caller.
  558. @return The SQL statements if execute=False, else an empty string,
  559. None in case of a failure
  560. """
  561. dbif, connected = init_dbif(dbif)
  562. statement = ""
  563. if self.is_in_db(dbif):
  564. # SELECT all needed information from the database
  565. self.metadata.select(dbif)
  566. # First we unregister from all dependent space time datasets
  567. statement += self.unregister(
  568. dbif=dbif, update=update, execute=False)
  569. # Remove the strds register table
  570. if self.get_stds_register() is not None:
  571. statement += "DROP TABLE " + self.get_stds_register() + ";\n"
  572. # Commented because of performance issue calling g.message thousend times
  573. #core.verbose(_("Delete %s dataset <%s> from temporal database")
  574. # % (self.get_type(), self.get_id()))
  575. # Delete yourself from the database, trigger functions will
  576. # take care of dependencies
  577. statement += self.base.get_delete_statement()
  578. if execute:
  579. dbif.execute_transaction(statement)
  580. # Remove the timestamp from the file system
  581. self.remove_timestamp_from_grass()
  582. self.reset(None)
  583. if connected:
  584. dbif.close()
  585. if execute:
  586. return ""
  587. return statement
  588. def unregister(self, dbif=None, update=True, execute=True):
  589. """! Remove the map entry in each space time dataset in which this map
  590. is registered
  591. @param dbif The database interface to be used
  592. @param update Call for each unregister statement the update from
  593. registered maps of the space time dataset. This can
  594. slow down the un-registration process significantly.
  595. @param execute If True the SQL DELETE and DROP table statements
  596. will be executed.
  597. If False the prepared SQL statements are
  598. returned and must be executed by the caller.
  599. @return The SQL statements if execute=False, else an empty string
  600. """
  601. # Commented because of performance issue calling g.message thousend times
  602. #if self.get_layer() is not None:
  603. # core.verbose(_("Unregister %(type)s map <%(map)s> with "
  604. # "layer %(layer)s from space time datasets" % \
  605. # {'type':self.get_type(), 'map':self.get_map_id(),
  606. # 'layer':self.get_layer()}))
  607. #else:
  608. # core.verbose(_("Unregister %(type)s map <%(map)s> "
  609. # "from space time datasets"
  610. # % {'type':self.get_type(), 'map':self.get_map_id()}))
  611. statement = ""
  612. dbif, connected = init_dbif(dbif)
  613. # Get all datasets in which this map is registered
  614. rows = self.get_registered_datasets(dbif)
  615. # For each stds in which the map is registered
  616. if rows is not None:
  617. for row in rows:
  618. # Create a space time dataset object to remove the map
  619. # from its register
  620. stds = self.get_new_stds_instance(row["id"])
  621. stds.metadata.select(dbif)
  622. statement += stds.unregister_map(self, dbif, False)
  623. # Take care to update the space time dataset after
  624. # the map has been unregistered
  625. if update == True and execute == True:
  626. stds.update_from_registered_maps(dbif)
  627. if execute:
  628. dbif.execute_transaction(statement)
  629. if connected:
  630. dbif.close()
  631. if execute:
  632. return ""
  633. return statement
  634. def get_registered_datasets(self, dbif=None):
  635. """!Return all space time dataset ids in which this map is registered
  636. as dictionary like rows with column "id" or None if this map is not
  637. registered in any space time dataset.
  638. @param dbif The database interface to be used
  639. @return The SQL rows with the ids of all space time datasets in
  640. which this map is registered
  641. """
  642. dbif, connected = init_dbif(dbif)
  643. rows = None
  644. try:
  645. if self.get_stds_register() is not None:
  646. # Select all stds tables in which this map is registered
  647. sql = "SELECT id FROM " + self.get_stds_register()
  648. dbif.cursor.execute(sql)
  649. rows = dbif.cursor.fetchall()
  650. except:
  651. core.error(_("Unable to select space time dataset register table "
  652. "<%s>") % (self.get_stds_register()))
  653. if connected:
  654. dbif.close()
  655. return rows
  656. ###############################################################################
  657. if __name__ == "__main__":
  658. import doctest
  659. doctest.testmod()