temporal_framework.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. GRASS GIS Temporal Framework
  2. ================================
  3. Introduction
  4. ------------
  5. The GRASS GIS Temporal Framework implements the temporal GIS functionality of GRASS GIS
  6. and provides an API to implement spatio-temporal processing modules. The framework
  7. introduces space time datasets that represent time series of raster, 3D raster or vector maps.
  8. This framework provides the following functionalities:
  9. - Assign time stamp to maps and register maps in the temporal database
  10. - Modification of time stamps
  11. - Creation, renaming and deletion of space time datasets
  12. - Registration and un-registration of maps in space time datasets
  13. - Query of maps that are registered in space time datasets using SQL where statements
  14. - Analysis of the spatio-temporal topology of space time datasets
  15. - Sampling of space time datasets
  16. - Computation of temporal and spatial relationships between registered maps
  17. - Higher level functions that are shared between modules
  18. Most of the functions described above are member functions of dedicated map layer and space time dataset classes.
  19. Temporal API
  20. ------------
  21. The temporal framework API consists of several dedicated modules. Each module contains one or several classes
  22. as well as function definition. The API can be roughly divided in a low level and high level part. However, several
  23. functions from the low level module are used in module implementation as well.
  24. Low level API
  25. ^^^^^^^^^^^^^
  26. The low level API implements the core functionality of the temporal framework. Core functionality is
  27. for example the database interface, the temporal database creation and initialization,
  28. the SQL object serialization, all classes that represent table entries, datetime mathematics and many more.
  29. :mod:`~temporal.core`
  30. """""""""""""""""""""
  31. The core functionality of the temporal framework:
  32. - Initialization function :func:`~temporal.core.init()`
  33. - Definition of global variables
  34. - Several global functions to access TGIS specific variables
  35. - Interfaces to the TGIS C-library and PyGRASS messenger objects
  36. - Database interface connection class :class:`~temporal.core.SQLDatabaseInterfaceConnection`
  37. to sqlite3 and postgresql database backends
  38. - Functions to create the temporal database
  39. :mod:`~temporal.base`
  40. """""""""""""""""""""
  41. Implements of basic dataset information and SQL conversion of such information:
  42. - Definition of the SQL serialize class :class:`~temporal.base.DictSQLSerializer`
  43. that converts the content of temporal
  44. classes into SQL SELECT, INSERT or UPDATE statements
  45. - Definition of :class:`~temporal.base.SQLDatabaseInterface`
  46. that is the base class for all temporal datatype subclasses
  47. - Contains classes for all datasets [#allds]_ that contain
  48. basic information (id, name, mapset, creator, ...)
  49. :mod:`~temporal.spatial_extent`
  50. """""""""""""""""""""""""""""""
  51. Implements of 2d and 3d spatial extents of all datasets:
  52. - Implements class :class:`~temporal.spatial_extent.SpatialExtent`
  53. that is the base class for all dataset specific spatial extent classes
  54. It provides spatial topological logic and operations for 2D and 3D extents
  55. - Implements spatial extent classes for all datasets [#allds]_
  56. :mod:`~temporal.temporal_extent`
  57. """"""""""""""""""""""""""""""""
  58. Implements of the temporal extent of all datasets for relative and absolute time:
  59. - Implements class :class:`~temporal.temporal_extent.TemporalExtent`
  60. that is the base class for all dataset specific temporal extent classes
  61. It provides temporal topological logic and operations
  62. - Implements temporal extent classes for relative time and absolute time for
  63. all datasets [#allds]_
  64. :mod:`~temporal.metadata`
  65. """""""""""""""""""""""""
  66. Implements the metadata base classes and datatype specific derivatives fpr all datasets [#allds]_.
  67. :mod:`~temporal.spatial_topology_dataset_connector`
  68. """""""""""""""""""""""""""""""""""""""""""""""""""
  69. Implements the interface to link datasets by spatial topological relations
  70. :mod:`~temporal.temporal_topology_dataset_connector`
  71. """"""""""""""""""""""""""""""""""""""""""""""""""""
  72. Implements the interface to link datasets by temporal topological relations
  73. :mod:`~temporal.c_libraries_interface`
  74. """"""""""""""""""""""""""""""""""""""
  75. The RPC C-library interface for exit safe and fast access to raster, vector and 3D raster information.
  76. :mod:`~temporal.temporal_granularity`
  77. """""""""""""""""""""""""""""""""""""
  78. The computation of the temporal granularity for a list
  79. of :class:`~temporal.abstract_dataset.AbstractDataset`
  80. objects for absolute and relative is implemented here.
  81. :mod:`~temporal.datetime_math`
  82. """"""""""""""""""""""""""""""
  83. This module contains function to parse, convert and process datetime objects
  84. in the temporal framework.
  85. Spatio-temporal algebra classes for space time raster and vector datasets are defined in:
  86. - :mod:`~temporal.temporal_algebra`
  87. - :mod:`~temporal.temporal_operator`
  88. - :mod:`~temporal.temporal_raster_base_algebra`
  89. - :mod:`~temporal.temporal_raster_algebra`
  90. - :mod:`~temporal.temporal_raster3d_algebra`
  91. - :mod:`~temporal.temporal_vector_algebra`
  92. High level API
  93. ^^^^^^^^^^^^^^
  94. The high level API utilizes the low level API. Its classes and functions are usually used to implement
  95. temporal processing algorithms and temporal GRASS modules.
  96. :mod:`~temporal.abstract_dataset`
  97. """""""""""""""""""""""""""""""""
  98. - Implements the base class for all datasets [#allds]_ :class:`~temporal.abstract_dataset.AbstractDataset`.
  99. - Implements the the select, insert and update functionality as well as
  100. convenient functions to access the base, extent and metadata information
  101. :mod:`~temporal.abstract_map_dataset`
  102. """""""""""""""""""""""""""""""""""""
  103. - Implements the base class :class:`~temporal.abstract_map_dataset.AbstractMapDataset`
  104. for all map layer specific classes
  105. - Provides the interface to all map layer specific information in the temporal database
  106. :mod:`~temporal.abstract_space_time_dataset`
  107. """"""""""""""""""""""""""""""""""""""""""""
  108. - Implements the base class :class:`~temporal.abstract_space_time_dataset.AbstractSpaceTimeDataset`
  109. for all Space Time Datasets classes
  110. - Contains the creation and deletion functionality, the map registration and un-registration,
  111. access methods to map layer objects and so on
  112. - Provides the interface to all Space Time Dataset specific information in the temporal database
  113. :mod:`~temporal.space_time_datasets`
  114. """"""""""""""""""""""""""""""""""""
  115. This module contains all classes that represent specific datasets [#allds]_.
  116. A module developer uses these map layer and Space Time Dataset object
  117. representations to perform spatio-temporal tasks.
  118. :mod:`~temporal.spatio_temporal_relationships`
  119. """"""""""""""""""""""""""""""""""""""""""""""
  120. The logic to compute spatio-temporal topology for a single list or two lists of :class:`~temporal.abstract_dataset.AbstractDataset` objects
  121. is implemented in this module.
  122. The class :class:`~temporal.spatio_temporal_relationships.SpatioTemporalTopologyBuilder`
  123. provides a convenient interface for topology computation.
  124. :mod:`~temporal.gui_support`
  125. """"""""""""""""""""""""""""
  126. Helper functions to support the listing of space time datasets in the automatically generated GUI.
  127. Shared Module functionality
  128. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  129. Functionality that is shared between different temporal GRASS modules, such as
  130. map listing, space time dataset creation, map registration and un-registration,
  131. aggregation, extraction, map calculation, statistics as well as import and export of
  132. space time datasets.
  133. :mod:`~temporal.aggregation`
  134. """"""""""""""""""""""""""""
  135. Aggregation of Space Time Raster Datasets based on topological relations.
  136. Used in *t.rast.aggregate* and *t.rast.aggregate.ds*
  137. :mod:`~temporal.extract`
  138. """"""""""""""""""""""""
  139. Extraction of subsets from Space Time Datasets including
  140. map algebra and vector selection statements.
  141. Used in *t.rast.extract*, *t.rast3d.extract* and *t.vect.extract*.
  142. :mod:`~temporal.factory`
  143. """"""""""""""""""""""""
  144. Factory functions to create datasets of all types [#allds]_.
  145. :mod:`~temporal.open_stds`
  146. """"""""""""""""""""""""""
  147. Convenient functions to open existing Space Time Datasets or
  148. to create new ones. Used in almost all temporal modules.
  149. :mod:`~temporal.list_stds`
  150. """"""""""""""""""""""""""
  151. Convenient functions to list datasets of all types [#allds]_
  152. registered in the temporal database.
  153. :mod:`~temporal.mapcalc`
  154. """"""""""""""""""""""""
  155. Simple temporal algebra for Space Time Raster and 3d Raster
  156. datasets. Used in *t.rast.mapcalc* and *t.rast3d.mapcalc*
  157. :mod:`~temporal.register`
  158. """""""""""""""""""""""""
  159. Convenient functions to register a single or multiple map layer in the temporal database and
  160. SpaceTime Datasets. Used in several modules, most important *t.register*.
  161. :mod:`~temporal.sampling`
  162. """""""""""""""""""""""""
  163. Sampling functions used in several modules.
  164. :mod:`~temporal.stds_export`
  165. """"""""""""""""""""""""""""
  166. Functions to export of Space Time Datasets, used in *t.rast.export*, *t.rast3d.export* and *t.vect.export*.
  167. :mod:`~temporal.stds_import`
  168. """"""""""""""""""""""""""""
  169. Functions to import Space Time Datasets, used in *t.rast.import*, *t.rast3d.import* and *t.vect.import*.
  170. :mod:`~temporal.univar_statistics`
  171. """"""""""""""""""""""""""""""""""
  172. Simple statistical analysis functions for Space Time Datasets, used in *t.rast.univar*, *t.rast3d.univar*
  173. and *t.vect.univar*.
  174. .. [#allds] : Raster Map Layer, 3d Raster Map Layer, Vector Map Layer, Space time Raster Datasets (STRDS),
  175. Space Time 3d Raster Datasets (STR3DS) and Space Time Vector Datasets (STVDS)
  176. Here the full list of all temporal modules:
  177. .. toctree::
  178. :maxdepth: 2
  179. temporal
  180. Examples
  181. --------
  182. Howto start example
  183. ^^^^^^^^^^^^^^^^^^^
  184. This simple example shows how to open a space time raster dataset
  185. to access its registered maps.
  186. .. code-block:: python
  187. # Lets import the temporal framework and
  188. # the script framework
  189. import grass.temporal as tgis
  190. import grass.script as grass
  191. # Make sure the temporal database exists
  192. # and set the temporal GIS environment
  193. tgis.init()
  194. # We create the temporal database interface for fast processing
  195. dbif = tgis.SQLDatabaseInterfaceConnection()
  196. dbif.connect()
  197. # The id of a space time raster dataset is build from its name and its mapset
  198. id = "test@PERMANENT"
  199. # We create a space time raster dataset object
  200. strds = tgis.SpaceTimeRasterDataset(id)
  201. # Check if the space time raster dataset is in the temporal database
  202. if strds.is_in_db(dbif=dbif) == False:
  203. dbif.close()
  204. grass.fatal(_("Space time %s dataset <%s> not found") % (
  205. strds.get_new_map_instance(None).get_type(), id))
  206. # Fill the object with the content from the temporal database
  207. strds.select(dbif=dbif)
  208. # Print information about the space time raster dataset to stdout
  209. strds.print_info()
  210. # Get all maps that are registered in the strds and print
  211. # information about the maps to stdout
  212. maps = strds.get_registered_maps_as_objects(dbif=dbif)
  213. # We iterate over the temporal sorted map list
  214. for map in maps:
  215. # We fill the map object with the content
  216. # from the temporal database. We use the existing
  217. # database connection, otherwise a new connection
  218. # will be established for each map object
  219. # which slows the processing down
  220. map.select(dbif=dbif)
  221. map.print_info()
  222. # Close the database connection
  223. dbif.close()
  224. Creation of a space time dataset
  225. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  226. This example shows howto create a space time dataset. The code is generic and works
  227. for different space time datasets (raster, 3D raster and vector):
  228. .. code-block:: python
  229. # Lets import the temporal framework and
  230. # the script framework
  231. import grass.temporal as tgis
  232. import grass.script as grass
  233. # The id of the new space time dataset
  234. id="test@PERMANENT"
  235. # The title of the new space time dataset
  236. title="This is a test dataset"
  237. # The description of the space time dataset
  238. description="The description"
  239. # The type of the space time dataset (strds, str3ds or stvds)
  240. type="strds"
  241. # The temporal type of the space time dataset (absolute or relative)
  242. temporal_type="absolute"
  243. # Make sure the temporal database exists
  244. # and set the temporal GIS environment
  245. tgis.init()
  246. # We use the dataset factory to create an new space time dataset instance of a specific type
  247. stds = tgis.dataset_factory(type, id)
  248. # We need a dtabase connection to insert the content of the space time dataset
  249. dbif = tgis.SQLDatabaseInterfaceConnection()
  250. dbif.connect()
  251. # First we check if the dataset is already in the database
  252. if stds.is_in_db(dbif=dbif) and overwrite == False:
  253. dbif.close()
  254. grass.fatal(_("Space time %s dataset <%s> is already in the database. "
  255. "Use the overwrite flag.") %
  256. (stds.get_new_map_instance(None).get_type(), name))
  257. # We delete the exiting dataset and create a new one in case we are allowed to overwrite it
  258. if stds.is_in_db(dbif=dbif) and overwrite == True:
  259. grass.warning(_("Overwrite space time %s dataset <%s> "
  260. "and unregister all maps.") %
  261. (stds.get_new_map_instance(None).get_type(), name))
  262. stds.delete(dbif=dbif)
  263. stds = stds.get_new_instance(id)
  264. # We set the initial values. This function also created the command history.
  265. stds.set_initial_values(temporal_type=temporaltype, semantic_type="mean",
  266. title=title, description=description)
  267. # Now we can insert the new space time dataset in the database
  268. stds.insert(dbif=dbif)
  269. # Close the database connection
  270. dbif.close()
  271. Temporal shifting
  272. ^^^^^^^^^^^^^^^^^
  273. .. code-block:: python
  274. import grass.script as grass
  275. import grass.temporal as tgis
  276. id="test@PERMANENT"
  277. type="strds"
  278. # Make sure the temporal database exists
  279. tgis.init()
  280. dbif = tgis.SQLDatabaseInterfaceConnection()
  281. dbif.connect()
  282. stds = tgis.dataset_factory(type, id)
  283. if stds.is_in_db(dbif) == False:
  284. dbif.close()
  285. grass.fatal(_("Space time dataset <%s> not found in temporal database") % (id))
  286. stds.select(dbif=dbif)
  287. stds.snap(dbif=dbif)
  288. stds.update_command_string(dbif=dbif)
  289. dbif.close()
  290. References
  291. ----------
  292. * Gebbert, S., Pebesma, E., 2014. *TGRASS: A temporal GIS for field based environmental modeling*. Environmental Modelling & Software. 2(1):201-219. `doi:10.1016/j.envsoft.2013.11.001 <http://dx.doi.org/10.1016/j.envsoft.2013.11.001>`_
  293. * `TGRASS related articles in the GRASS GIS Wiki <http://grasswiki.osgeo.org/wiki/Temporal_data_processing>`_
  294. :Authors: Soeren Gebbert
  295. :TODO: add more documentation