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