abstract_dataset.py 24 KB

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