abstract_map_dataset.py 32 KB

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