abstract_dataset.py 23 KB

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