abstract_dataset.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. # -*- coding: utf-8 -*-
  2. """
  3. The abstract_dataset module provides the AbstractDataset class
  4. that is the base class for all map layer and Space Time Datasets.
  5. (C) 2011-2013 by the GRASS Development Team
  6. This program is free software under the GNU General Public
  7. License (>=v2). Read the file COPYING that comes with GRASS
  8. for details.
  9. :authors: Soeren Gebbert
  10. """
  11. from abc import ABCMeta, abstractmethod
  12. from .core import get_tgis_message_interface, init_dbif, get_enable_mapset_check, get_current_mapset
  13. from .temporal_topology_dataset_connector import TemporalTopologyDatasetConnector
  14. from .spatial_topology_dataset_connector import SpatialTopologyDatasetConnector
  15. ###############################################################################
  16. class AbstractDataset(SpatialTopologyDatasetConnector,
  17. TemporalTopologyDatasetConnector):
  18. """This is the base class for all datasets
  19. (raster, vector, raster3d, strds, stvds, str3ds)"""
  20. __metaclass__ = ABCMeta
  21. def __init__(self):
  22. SpatialTopologyDatasetConnector.__init__(self)
  23. TemporalTopologyDatasetConnector.__init__(self)
  24. self.msgr = get_tgis_message_interface()
  25. def reset_topology(self):
  26. """Reset any information about temporal topology"""
  27. self.reset_spatial_topology()
  28. self.reset_temporal_topology()
  29. def get_number_of_relations(self):
  30. """Return a dictionary in which the keys are the relation names and the
  31. value are the number of relations.
  32. The following relations are available:
  33. Spatial relations:
  34. - equivalent
  35. - overlap
  36. - in
  37. - contain
  38. - meet
  39. - cover
  40. - covered
  41. Temporal relations:
  42. - equal
  43. - follows
  44. - precedes
  45. - overlaps
  46. - overlapped
  47. - during (including starts, finishes)
  48. - contains (including started, finished)
  49. - starts
  50. - started
  51. - finishes
  52. - finished
  53. To access topological information the spatial, temporal or booth
  54. topologies must be build first using the SpatioTemporalTopologyBuilder.
  55. :return: The dictionary with relations as keys and number as values or
  56. None in case the topology wasn't build
  57. """
  58. if self.is_temporal_topology_build() and not self.is_spatial_topology_build():
  59. return self.get_number_of_temporal_relations()
  60. elif self.is_spatial_topology_build() and not self.is_temporal_topology_build():
  61. self.get_number_of_spatial_relations()
  62. else:
  63. return self.get_number_of_temporal_relations() + \
  64. self.get_number_of_spatial_relations()
  65. return None
  66. def set_topology_build_true(self):
  67. """Use this method when the spatio-temporal topology was build"""
  68. self.set_spatial_topology_build_true()
  69. self.set_temporal_topology_build_true()
  70. def set_topology_build_false(self):
  71. """Use this method when the spatio-temporal topology was not build"""
  72. self.set_spatial_topology_build_false()
  73. self.set_temporal_topology_build_false()
  74. def is_topology_build(self):
  75. """Check if the spatial and temporal topology was build
  76. :return: A dictionary with "spatial" and "temporal" as keys that
  77. have boolen values
  78. """
  79. d = {}
  80. d["spatial"] = self.is_spatial_topology_build()
  81. d["temporal"] = self.is_temporal_topology_build()
  82. return d
  83. def print_topology_info(self):
  84. if self.is_temporal_topology_build():
  85. self.print_temporal_topology_info()
  86. if self.is_spatial_topology_build():
  87. self.print_spatial_topology_info()
  88. def print_topology_shell_info(self):
  89. if self.is_temporal_topology_build():
  90. self.print_temporal_topology_shell_info()
  91. if self.is_spatial_topology_build():
  92. self.print_spatial_topology_shell_info()
  93. @abstractmethod
  94. def reset(self, ident):
  95. """Reset the internal structure and set the identifier
  96. This method creates the dataset specific internal objects
  97. that store the base information, the spatial and temporal extent
  98. and the metadata. It must be implemented in the dataset
  99. specific subclasses. This is the code for the
  100. vector dataset:
  101. .. code-block:: python
  102. self.base = VectorBase(ident=ident)
  103. self.absolute_time = VectorAbsoluteTime(ident=ident)
  104. self.relative_time = VectorRelativeTime(ident=ident)
  105. self.spatial_extent = VectorSpatialExtent(ident=ident)
  106. self.metadata = VectorMetadata(ident=ident)
  107. :param ident: The identifier of the dataset that "name@mapset" or
  108. in case of vector maps "name:layer@mapset"
  109. """
  110. @abstractmethod
  111. def is_stds(self):
  112. """Return True if this class is a space time dataset
  113. :return: True if this class is a space time dataset, False otherwise
  114. """
  115. @abstractmethod
  116. def get_type(self):
  117. """Return the type of this class as string
  118. The type can be "vector", "raster", "raster3d", "stvds", "strds" or "str3ds"
  119. :return: "vector", "raster", "raster3d", "stvds", "strds" or "str3ds"
  120. """
  121. @abstractmethod
  122. def get_new_instance(self, ident):
  123. """Return a new instance with the type of this class
  124. :param ident: The identifier of the new dataset instance
  125. :return: A new instance with the type of this object
  126. """
  127. @abstractmethod
  128. def spatial_overlapping(self, dataset):
  129. """Return True if the spatial extents overlap
  130. :param dataset: The abstract dataset to check spatial overlapping
  131. :return: True if self and the provided dataset spatial overlap
  132. """
  133. @abstractmethod
  134. def spatial_intersection(self, dataset):
  135. """Return the spatial intersection as spatial_extent
  136. object or None in case no intersection was found.
  137. :param dataset: The abstract dataset to intersect with
  138. :return: The intersection spatial extent
  139. """
  140. @abstractmethod
  141. def spatial_union(self, dataset):
  142. """Return the spatial union as spatial_extent
  143. object or None in case the extents does not overlap or meet.
  144. :param dataset: The abstract dataset to create a union with
  145. :return: The union spatial extent
  146. """
  147. @abstractmethod
  148. def spatial_disjoint_union(self, dataset):
  149. """Return the spatial union as spatial_extent object.
  150. :param dataset: The abstract dataset to create a union with
  151. :return: The union spatial extent
  152. """
  153. @abstractmethod
  154. def spatial_relation(self, dataset):
  155. """Return the spatial relationship between self and dataset
  156. :param dataset: The abstract dataset to compute the spatial
  157. relation with self
  158. :return: The spatial relationship as string
  159. """
  160. @abstractmethod
  161. def print_info(self):
  162. """Print information about this class in human readable style"""
  163. @abstractmethod
  164. def print_shell_info(self):
  165. """Print information about this class in shell style"""
  166. @abstractmethod
  167. def print_self(self):
  168. """Print the content of the internal structure to stdout"""
  169. def set_id(self, ident):
  170. """Set the identifier of the dataset"""
  171. self.base.set_id(ident)
  172. self.temporal_extent.set_id(ident)
  173. self.spatial_extent.set_id(ident)
  174. self.metadata.set_id(ident)
  175. if self.is_stds() is False:
  176. self.stds_register.set_id(ident)
  177. def get_id(self):
  178. """Return the unique identifier of the dataset
  179. :return: The id of the dataset "name(:layer)@mapset" as string
  180. """
  181. return self.base.get_id()
  182. def get_name(self):
  183. """Return the name
  184. :return: The name of the dataset as string
  185. """
  186. return self.base.get_name()
  187. def get_mapset(self):
  188. """Return the mapset
  189. :return: The mapset in which the dataset was created as string
  190. """
  191. return self.base.get_mapset()
  192. def get_temporal_extent_as_tuple(self):
  193. """Returns a tuple of the valid start and end time
  194. Start and end time can be either of type datetime or of type
  195. integer, depending on the temporal type.
  196. :return: A tuple of (start_time, end_time)
  197. """
  198. start = self.temporal_extent.get_start_time()
  199. end = self.temporal_extent.get_end_time()
  200. return (start, end)
  201. def get_absolute_time(self):
  202. """Returns the start time, the end
  203. time of the map as tuple
  204. The start time is of type datetime.
  205. The end time is of type datetime in case of interval time,
  206. or None on case of a time instance.
  207. :return: A tuple of (start_time, end_time)
  208. """
  209. start = self.absolute_time.get_start_time()
  210. end = self.absolute_time.get_end_time()
  211. return (start, end)
  212. def get_relative_time(self):
  213. """Returns the start time, the end
  214. time and the temporal unit of the dataset as tuple
  215. The start time is of type integer.
  216. The end time is of type integer in case of interval time,
  217. or None on case of a time instance.
  218. :return: A tuple of (start_time, end_time, unit)
  219. """
  220. start = self.relative_time.get_start_time()
  221. end = self.relative_time.get_end_time()
  222. unit = self.relative_time.get_unit()
  223. return (start, end, unit)
  224. def get_relative_time_unit(self):
  225. """Returns the relative time unit
  226. :return: The relative time unit as string, None if not present
  227. """
  228. return self.relative_time.get_unit()
  229. def check_relative_time_unit(self, unit):
  230. """Check if unit is of type year(s), month(s), day(s), hour(s),
  231. minute(s) or second(s)
  232. :param unit: The unit string
  233. :return: True if success, False otherwise
  234. """
  235. # Check unit
  236. units = ["year", "years", "month", "months", "day", "days", "hour",
  237. "hours", "minute", "minutes", "second", "seconds"]
  238. if unit not in units:
  239. return False
  240. return True
  241. def get_temporal_type(self):
  242. """Return the temporal type of this dataset
  243. The temporal type can be absolute or relative
  244. :return: The temporal type of the dataset as string
  245. """
  246. return self.base.get_ttype()
  247. def get_spatial_extent_as_tuple(self):
  248. """Return the spatial extent as tuple
  249. Top and bottom are set to 0 in case of a two dimensional spatial
  250. extent.
  251. :return: A the spatial extent as tuple (north, south, east, west,
  252. top, bottom)
  253. """
  254. return self.spatial_extent.get_spatial_extent_as_tuple()
  255. def get_spatial_extent(self):
  256. """Return the spatial extent
  257. """
  258. return self.spatial_extent
  259. def select(self, dbif=None):
  260. """Select temporal dataset entry from database and fill
  261. the internal structure
  262. The content of every dataset is stored in the temporal database.
  263. This method must be used to fill this object with the content
  264. from the temporal database.
  265. :param dbif: The database interface to be used
  266. """
  267. dbif, connected = init_dbif(dbif)
  268. self.base.select(dbif)
  269. self.temporal_extent.select(dbif)
  270. self.spatial_extent.select(dbif)
  271. self.metadata.select(dbif)
  272. if self.is_stds() is False:
  273. self.stds_register.select(dbif)
  274. if connected:
  275. dbif.close()
  276. def is_in_db(self, dbif=None):
  277. """Check if the dataset is registered in the database
  278. :param dbif: The database interface to be used
  279. :return: True if the dataset is registered in the database
  280. """
  281. return self.base.is_in_db(dbif)
  282. @abstractmethod
  283. def delete(self):
  284. """Delete dataset from database if it exists"""
  285. def insert(self, dbif=None, execute=True):
  286. """Insert dataset into database
  287. :param dbif: The database interface to be used
  288. :param execute: If True the SQL statements will be executed.
  289. If False the prepared SQL statements are returned
  290. and must be executed by the caller.
  291. :return: The SQL insert statement in case execute=False, or an
  292. empty string otherwise
  293. """
  294. if get_enable_mapset_check() is True and self.get_mapset() != get_current_mapset():
  295. self.msgr.fatal(_("Unable to insert dataset <%(ds)s> of type "
  296. "%(type)s in the temporal database. The mapset "
  297. "of the dataset does not match the current "
  298. "mapset") % {"ds": self.get_id(),
  299. "type": self.get_type()})
  300. dbif, connected = init_dbif(dbif)
  301. # Build the INSERT SQL statement
  302. statement = self.base.get_insert_statement_mogrified(dbif)
  303. statement += self.temporal_extent.get_insert_statement_mogrified(dbif)
  304. statement += self.spatial_extent.get_insert_statement_mogrified(dbif)
  305. statement += self.metadata.get_insert_statement_mogrified(dbif)
  306. if self.is_stds() is False:
  307. statement += self.stds_register.get_insert_statement_mogrified(dbif)
  308. if execute:
  309. dbif.execute_transaction(statement)
  310. if connected:
  311. dbif.close()
  312. return ""
  313. if connected:
  314. dbif.close()
  315. return statement
  316. def update(self, dbif=None, execute=True, ident=None):
  317. """Update the dataset entry in the database from the internal structure
  318. excluding None variables
  319. :param dbif: The database interface to be used
  320. :param execute: If True the SQL statements will be executed.
  321. If False the prepared SQL statements are returned
  322. and must be executed by the caller.
  323. :param ident: The identifier to be updated, useful for renaming
  324. :return: The SQL update statement in case execute=False, or an
  325. empty string otherwise
  326. """
  327. if get_enable_mapset_check() is True and self.get_mapset() != get_current_mapset():
  328. self.msgr.fatal(_("Unable to update dataset <%(ds)s> of type "
  329. "%(type)s in the temporal database. The mapset "
  330. "of the dataset does not match the current "
  331. "mapset") % {"ds": self.get_id(),
  332. "type": self.get_type()})
  333. dbif, connected = init_dbif(dbif)
  334. # Build the UPDATE SQL statement
  335. statement = self.base.get_update_statement_mogrified(dbif, ident)
  336. statement += self.temporal_extent.get_update_statement_mogrified(dbif,
  337. ident)
  338. statement += self.spatial_extent.get_update_statement_mogrified(dbif,
  339. ident)
  340. statement += self.metadata.get_update_statement_mogrified(dbif, ident)
  341. if self.is_stds() is False:
  342. statement += self.stds_register.get_update_statement_mogrified(dbif, ident)
  343. if execute:
  344. dbif.execute_transaction(statement)
  345. if connected:
  346. dbif.close()
  347. return ""
  348. if connected:
  349. dbif.close()
  350. return statement
  351. def update_all(self, dbif=None, execute=True, ident=None):
  352. """Update the dataset entry in the database from the internal structure
  353. and include None variables.
  354. :param dbif: The database interface to be used
  355. :param execute: If True the SQL statements will be executed.
  356. If False the prepared SQL statements are returned
  357. and must be executed by the caller.
  358. :param ident: The identifier to be updated, useful for renaming
  359. :return: The SQL update statement in case execute=False, or an
  360. empty string otherwise
  361. """
  362. if get_enable_mapset_check() is True and self.get_mapset() != get_current_mapset():
  363. self.msgr.fatal(_("Unable to update dataset <%(ds)s> of type "
  364. "%(type)s in the temporal database. The mapset"
  365. " of the dataset does not match the current "
  366. "mapset") % {"ds": self.get_id(),
  367. "type": self.get_type()})
  368. dbif, connected = init_dbif(dbif)
  369. # Build the UPDATE SQL statement
  370. statement = self.base.get_update_all_statement_mogrified(dbif, ident)
  371. statement += self.temporal_extent.get_update_all_statement_mogrified(dbif,
  372. ident)
  373. statement += self.spatial_extent.get_update_all_statement_mogrified(dbif,
  374. ident)
  375. statement += self.metadata.get_update_all_statement_mogrified(dbif,
  376. ident)
  377. if self.is_stds() is False:
  378. statement += self.stds_register.get_update_all_statement_mogrified(dbif, ident)
  379. if execute:
  380. dbif.execute_transaction(statement)
  381. if connected:
  382. dbif.close()
  383. return ""
  384. if connected:
  385. dbif.close()
  386. return statement
  387. def is_time_absolute(self):
  388. """Return True in case the temporal type is absolute
  389. :return: True if temporal type is absolute, False otherwise
  390. """
  391. if "temporal_type" in self.base.D:
  392. return self.base.get_ttype() == "absolute"
  393. else:
  394. return None
  395. def is_time_relative(self):
  396. """Return True in case the temporal type is relative
  397. :return: True if temporal type is relative, False otherwise
  398. """
  399. if "temporal_type" in self.base.D:
  400. return self.base.get_ttype() == "relative"
  401. else:
  402. return None
  403. def get_temporal_extent(self):
  404. """Return the temporal extent of the correct internal type
  405. """
  406. if self.is_time_absolute():
  407. return self.absolute_time
  408. if self.is_time_relative():
  409. return self.relative_time
  410. return None
  411. temporal_extent = property(fget=get_temporal_extent)
  412. def temporal_relation(self, dataset):
  413. """Return the temporal relation of self and the provided dataset
  414. :return: The temporal relation as string
  415. """
  416. return self.temporal_extent.temporal_relation(dataset.temporal_extent)
  417. def temporal_intersection(self, dataset):
  418. """Intersect self with the provided dataset and
  419. return a new temporal extent with the new start and end time
  420. :param dataset: The abstract dataset to temporal intersect with
  421. :return: The new temporal extent with start and end time,
  422. or None in case of no intersection
  423. """
  424. return self.temporal_extent.intersect(dataset.temporal_extent)
  425. def temporal_union(self, dataset):
  426. """Creates a union with the provided dataset and
  427. return a new temporal extent with the new start and end time.
  428. :param dataset: The abstract dataset to create temporal union with
  429. :return: The new temporal extent with start and end time,
  430. or None in case of no intersection
  431. """
  432. return self.temporal_extent.union(dataset.temporal_extent)
  433. def temporal_disjoint_union(self, dataset):
  434. """Creates a union with the provided dataset and
  435. return a new temporal extent with the new start and end time.
  436. :param dataset: The abstract dataset to create temporal union with
  437. :return: The new temporal extent with start and end time
  438. """
  439. return self.temporal_extent.disjoint_union(dataset.temporal_extent)
  440. ###############################################################################
  441. class AbstractDatasetComparisonKeyStartTime(object):
  442. """This comparison key can be used to sort lists of abstract datasets
  443. by start time
  444. Example:
  445. .. code-block:: python
  446. # Return all maps in a space time raster dataset as map objects
  447. map_list = strds.get_registered_maps_as_objects()
  448. # Sort the maps in the list by start time
  449. sorted_map_list = sorted(map_list, key=AbstractDatasetComparisonKeyStartTime)
  450. """
  451. def __init__(self, obj, *args):
  452. self.obj = obj
  453. def __lt__(self, other):
  454. startA, endA = self.obj.get_temporal_extent_as_tuple()
  455. startB, endB = other.obj.get_temporal_extent_as_tuple()
  456. return startA < startB
  457. def __gt__(self, other):
  458. startA, endA = self.obj.get_temporal_extent_as_tuple()
  459. startB, endB = other.obj.get_temporal_extent_as_tuple()
  460. return startA > startB
  461. def __eq__(self, other):
  462. startA, endA = self.obj.get_temporal_extent_as_tuple()
  463. startB, endB = other.obj.get_temporal_extent_as_tuple()
  464. return startA == startB
  465. def __le__(self, other):
  466. startA, endA = self.obj.get_temporal_extent_as_tuple()
  467. startB, endB = other.obj.get_temporal_extent_as_tuple()
  468. return startA <= startB
  469. def __ge__(self, other):
  470. startA, endA = self.obj.get_temporal_extent_as_tuple()
  471. startB, endB = other.obj.get_temporal_extent_as_tuple()
  472. return startA >= startB
  473. def __ne__(self, other):
  474. startA, endA = self.obj.get_temporal_extent_as_tuple()
  475. startB, endB = other.obj.get_temporal_extent_as_tuple()
  476. return startA != startB
  477. ###############################################################################
  478. class AbstractDatasetComparisonKeyEndTime(object):
  479. """This comparison key can be used to sort lists of abstract datasets
  480. by end time
  481. Example:
  482. .. code-block:: python
  483. # Return all maps in a space time raster dataset as map objects
  484. map_list = strds.get_registered_maps_as_objects()
  485. # Sort the maps in the list by end time
  486. sorted_map_list = sorted(map_list, key=AbstractDatasetComparisonKeyEndTime)
  487. """
  488. def __init__(self, obj, *args):
  489. self.obj = obj
  490. def __lt__(self, other):
  491. startA, endA = self.obj.get_temporal_extent_as_tuple()
  492. startB, endB = other.obj.get_temporal_extent_as_tuple()
  493. return endA < endB
  494. def __gt__(self, other):
  495. startA, endA = self.obj.get_temporal_extent_as_tuple()
  496. startB, endB = other.obj.get_temporal_extent_as_tuple()
  497. return endA > endB
  498. def __eq__(self, other):
  499. startA, endA = self.obj.get_temporal_extent_as_tuple()
  500. startB, endB = other.obj.get_temporal_extent_as_tuple()
  501. return endA == endB
  502. def __le__(self, other):
  503. startA, endA = self.obj.get_temporal_extent_as_tuple()
  504. startB, endB = other.obj.get_temporal_extent_as_tuple()
  505. return endA <= endB
  506. def __ge__(self, other):
  507. startA, endA = self.obj.get_temporal_extent_as_tuple()
  508. startB, endB = other.obj.get_temporal_extent_as_tuple()
  509. return endA >= endB
  510. def __ne__(self, other):
  511. startA, endA = self.obj.get_temporal_extent_as_tuple()
  512. startB, endB = other.obj.get_temporal_extent_as_tuple()
  513. return endA != endB
  514. ###############################################################################
  515. if __name__ == "__main__":
  516. import doctest
  517. doctest.testmod()