space_time_datasets.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS related functions to be used in Python scripts.
  4. Usage:
  5. @code
  6. import grass.temporal as tgis
  7. strds = tgis.space_time_raster_dataset("soils_1950_2010")
  8. ...
  9. @endcode
  10. (C) 2008-2011 by the GRASS Development Team
  11. This program is free software under the GNU General Public
  12. License (>=v2). Read the file COPYING that comes with GRASS
  13. for details.
  14. @author Soeren Gebbert
  15. """
  16. import getpass
  17. import grass.script.raster as raster
  18. import grass.script.vector as vector
  19. import grass.script.raster3d as raster3d
  20. from datetime_math import *
  21. from abstract_map_dataset import *
  22. from abstract_space_time_dataset import *
  23. ###############################################################################
  24. class raster_dataset(abstract_map_dataset):
  25. """Raster dataset class
  26. This class provides functions to select, update, insert or delete raster
  27. map information and valid time stamps into the SQL temporal database.
  28. """
  29. def __init__(self, ident):
  30. self.reset(ident)
  31. def get_type(self):
  32. return "raster"
  33. def get_new_instance(self, ident):
  34. """Return a new instance with the type of this class"""
  35. return raster_dataset(ident)
  36. def get_new_stds_instance(self, ident):
  37. """Return a new space time dataset instance in which maps are stored with the type of this class"""
  38. return space_time_raster_dataset(ident)
  39. def get_stds_register(self):
  40. """Return the space time dataset register table name in which stds are listed in which this map is registered"""
  41. return self.metadata.get_strds_register()
  42. def set_stds_register(self, name):
  43. """Set the space time dataset register table name in which stds are listed in which this map is registered"""
  44. self.metadata.set_strds_register(name)
  45. def get_timestamp_module_name(self):
  46. """Return the name of the C-module to set the time stamp in the file system"""
  47. return "r.timestamp"
  48. def spatial_overlapping(self, dataset):
  49. """Return True if the spatial extents 2d overlap"""
  50. return self.spatial_extent.overlapping_2d(dataset.spatial_extent)
  51. def spatial_relation(self, dataset):
  52. """Return the two dimensional spatial relation"""
  53. return self.spatial_extent.spatial_relation_2d(dataset.spatial_extent)
  54. def reset(self, ident):
  55. """Reset the internal structure and set the identifier"""
  56. self.ident = ident
  57. self.base = raster_base(ident=ident)
  58. self.absolute_time = raster_absolute_time(ident=ident)
  59. self.relative_time = raster_relative_time(ident=ident)
  60. self.spatial_extent = raster_spatial_extent(ident=ident)
  61. self.metadata = raster_metadata(ident=ident)
  62. def load(self):
  63. """Load all info from an existing raster map into the internal structure"""
  64. # Get the data from an existing raster map
  65. kvp = raster.raster_info(self.get_map_id())
  66. # Fill base information
  67. self.base.set_name(self.ident.split("@")[0])
  68. self.base.set_mapset(self.ident.split("@")[1])
  69. self.base.set_creator(str(getpass.getuser()))
  70. # Fill spatial extent
  71. self.set_spatial_extent(north=kvp["north"], south=kvp["south"], \
  72. east=kvp["east"], west=kvp["west"])
  73. # Fill metadata
  74. self.metadata.set_nsres(kvp["nsres"])
  75. self.metadata.set_ewres(kvp["ewres"])
  76. self.metadata.set_datatype(kvp["datatype"])
  77. self.metadata.set_min(kvp["min"])
  78. self.metadata.set_max(kvp["max"])
  79. rows = int((kvp["north"] - kvp["south"])/kvp["nsres"] + 0.5)
  80. cols = int((kvp["east"] - kvp["west"])/kvp["ewres"] + 0.5)
  81. ncells = cols * rows
  82. self.metadata.set_cols(cols)
  83. self.metadata.set_rows(rows)
  84. self.metadata.set_number_of_cells(ncells)
  85. ###############################################################################
  86. class raster3d_dataset(abstract_map_dataset):
  87. """Raster3d dataset class
  88. This class provides functions to select, update, insert or delete raster3d
  89. map information and valid time stamps into the SQL temporal database.
  90. """
  91. def __init__(self, ident):
  92. self.reset(ident)
  93. def get_type(self):
  94. return "raster3d"
  95. def get_new_instance(self, ident):
  96. """Return a new instance with the type of this class"""
  97. return raster3d_dataset(ident)
  98. def get_new_stds_instance(self, ident):
  99. """Return a new space time dataset instance in which maps are stored with the type of this class"""
  100. return space_time_raster3d_dataset(ident)
  101. def get_stds_register(self):
  102. """Return the space time dataset register table name in which stds are listed in which this map is registered"""
  103. return self.metadata.get_str3ds_register()
  104. def set_stds_register(self, name):
  105. """Set the space time dataset register table name in which stds are listed in which this map is registered"""
  106. self.metadata.set_str3ds_register(name)
  107. def get_timestamp_module_name(self):
  108. """Return the name of the C-module to set the time stamp in the file system"""
  109. return "r3.timestamp"
  110. def spatial_overlapping(self, dataset):
  111. """Return True if the spatial extents overlap"""
  112. if self.get_type() == dataset.get_type() or dataset.get_type() == "str3ds":
  113. return self.spatial_extent.overlapping(dataset.spatial_extent)
  114. else:
  115. return self.spatial_extent.overlapping_2d(dataset.spatial_extent)
  116. def spatial_relation(self, dataset):
  117. """Return the two or three dimensional spatial relation"""
  118. if self.get_type() == dataset.get_type() or dataset.get_type() == "str3ds":
  119. return self.spatial_extent.spatial_relation(dataset.spatial_extent)
  120. else:
  121. return self.spatial_extent.spatial_relation_2d(dataset.spatial_extent)
  122. def reset(self, ident):
  123. """Reset the internal structure and set the identifier"""
  124. self.ident = ident
  125. self.base = raster3d_base(ident=ident)
  126. self.absolute_time = raster3d_absolute_time(ident=ident)
  127. self.relative_time = raster3d_relative_time(ident=ident)
  128. self.spatial_extent = raster3d_spatial_extent(ident=ident)
  129. self.metadata = raster3d_metadata(ident=ident)
  130. def load(self):
  131. """Load all info from an existing raster3d map into the internal structure"""
  132. # Get the data from an existing raster map
  133. kvp = raster3d.raster3d_info(self.get_map_id())
  134. # Fill base information
  135. self.base.set_name(self.ident.split("@")[0])
  136. self.base.set_mapset(self.ident.split("@")[1])
  137. self.base.set_creator(str(getpass.getuser()))
  138. # Fill spatial extent
  139. self.set_spatial_extent(north=kvp["north"], south=kvp["south"], \
  140. east=kvp["east"], west=kvp["west"],\
  141. top=kvp["top"], bottom=kvp["bottom"])
  142. # Fill metadata
  143. self.metadata.set_nsres(kvp["nsres"])
  144. self.metadata.set_ewres(kvp["ewres"])
  145. self.metadata.set_tbres(kvp["tbres"])
  146. self.metadata.set_datatype(kvp["datatype"])
  147. self.metadata.set_min(kvp["min"])
  148. self.metadata.set_max(kvp["max"])
  149. rows = int((kvp["north"] - kvp["south"])/kvp["nsres"] + 0.5)
  150. cols = int((kvp["east"] - kvp["west"])/kvp["ewres"] + 0.5)
  151. depths = int((kvp["top"] - kvp["bottom"])/kvp["tbres"] + 0.5)
  152. ncells = cols * rows * depths
  153. self.metadata.set_cols(cols)
  154. self.metadata.set_rows(rows)
  155. self.metadata.set_depths(depths)
  156. self.metadata.set_number_of_cells(ncells)
  157. ###############################################################################
  158. class vector_dataset(abstract_map_dataset):
  159. """Vector dataset class
  160. This class provides functions to select, update, insert or delete vector
  161. map information and valid time stamps into the SQL temporal database.
  162. """
  163. def __init__(self, ident):
  164. self.reset(ident)
  165. def get_type(self):
  166. return "vector"
  167. def get_new_instance(self, ident):
  168. """Return a new instance with the type of this class"""
  169. return vector_dataset(ident)
  170. def get_new_stds_instance(self, ident):
  171. """Return a new space time dataset instance in which maps are stored with the type of this class"""
  172. return space_time_vector_dataset(ident)
  173. def get_stds_register(self):
  174. """Return the space time dataset register table name in which stds are listed in which this map is registered"""
  175. return self.metadata.get_stvds_register()
  176. def set_stds_register(self, name):
  177. """Set the space time dataset register table name in which stds are listed in which this map is registered"""
  178. self.metadata.set_stvds_register(name)
  179. def get_timestamp_module_name(self):
  180. """Return the name of the C-module to set the time stamp in the file system"""
  181. return "v.timestamp"
  182. def get_layer(self):
  183. """Return the layer"""
  184. return self.base.get_layer()
  185. def spatial_overlapping(self, dataset):
  186. """Return True if the spatial extents 2d overlap"""
  187. return self.spatial_extent.overlapping_2d(dataset.spatial_extent)
  188. def spatial_relation(self, dataset):
  189. """Return the two dimensional spatial relation"""
  190. return self.spatial_extent.spatial_relation_2d(dataset.spatial_extent)
  191. def reset(self, ident):
  192. """Reset the internal structure and set the identifier"""
  193. self.ident = ident
  194. self.base = vector_base(ident=ident)
  195. self.absolute_time = vector_absolute_time(ident=ident)
  196. self.relative_time = vector_relative_time(ident=ident)
  197. self.spatial_extent = vector_spatial_extent(ident=ident)
  198. self.metadata = vector_metadata(ident=ident)
  199. def load(self):
  200. """Load all info from an existing vector map into the internal structure"""
  201. # Get the data from an existing raster map
  202. kvp = vector.vector_info(self.get_map_id())
  203. # Fill base information
  204. if self.ident.find(":") >= 0:
  205. self.base.set_name(self.ident.split("@")[0].split(":")[0])
  206. self.base.set_layer(self.ident.split("@")[0].split(":")[1])
  207. else:
  208. self.base.set_name(self.ident.split("@")[0])
  209. self.base.set_mapset(self.ident.split("@")[1])
  210. self.base.set_creator(str(getpass.getuser()))
  211. # Fill spatial extent
  212. self.set_spatial_extent(north=kvp["north"], south=kvp["south"], \
  213. east=kvp["east"], west=kvp["west"],\
  214. top=kvp["top"], bottom=kvp["bottom"])
  215. # Fill metadata .. no metadata yet
  216. ###############################################################################
  217. class space_time_raster_dataset(abstract_space_time_dataset):
  218. """Space time raster dataset class
  219. """
  220. def __init__(self, ident):
  221. abstract_space_time_dataset.__init__(self, ident)
  222. def get_type(self):
  223. return "strds"
  224. def get_new_instance(self, ident):
  225. """Return a new instance with the type of this class"""
  226. return space_time_raster_dataset(ident)
  227. def get_new_map_instance(self, ident):
  228. """Return a new instance of a map dataset which is associated with the type of this class"""
  229. return raster_dataset(ident)
  230. def get_map_register(self):
  231. """Return the name of the map register table"""
  232. return self.metadata.get_raster_register()
  233. def set_map_register(self, name):
  234. """Set the name of the map register table"""
  235. self.metadata.set_raster_register(name)
  236. def spatial_overlapping(self, dataset):
  237. """Return True if the spatial extents 2d overlap"""
  238. return self.spatial_extent.overlapping_2d(dataset.spatial_extent)
  239. def spatial_relation(self, dataset):
  240. """Return the two dimensional spatial relation"""
  241. return self.spatial_extent.spatial_relation_2d(dataset.spatial_extent)
  242. def reset(self, ident):
  243. """Reset the internal structure and set the identifier"""
  244. self.ident = ident
  245. self.base = strds_base(ident=ident)
  246. if ident != None:
  247. self.base.set_name(self.ident.split("@")[0])
  248. self.base.set_mapset(self.ident.split("@")[1])
  249. self.base.set_creator(str(getpass.getuser()))
  250. self.absolute_time = strds_absolute_time(ident=ident)
  251. self.relative_time = strds_relative_time(ident=ident)
  252. self.spatial_extent = strds_spatial_extent(ident=ident)
  253. self.metadata = strds_metadata(ident=ident)
  254. ###############################################################################
  255. class space_time_raster3d_dataset(abstract_space_time_dataset):
  256. """Space time raster3d dataset class
  257. """
  258. def __init__(self, ident):
  259. abstract_space_time_dataset.__init__(self, ident)
  260. def get_type(self):
  261. return "str3ds"
  262. def get_new_instance(self, ident):
  263. """Return a new instance with the type of this class"""
  264. return space_time_raster3d_dataset(ident)
  265. def get_new_map_instance(self, ident):
  266. """Return a new instance of a map dataset which is associated with the type of this class"""
  267. return raster3d_dataset(ident)
  268. def get_map_register(self):
  269. """Return the name of the map register table"""
  270. return self.metadata.get_raster3d_register()
  271. def set_map_register(self, name):
  272. """Set the name of the map register table"""
  273. self.metadata.set_raster3d_register(name)
  274. def spatial_overlapping(self, dataset):
  275. """Return True if the spatial extents overlap"""
  276. if self.get_type() == dataset.get_type() or dataset.get_type() == "str3ds":
  277. return self.spatial_extent.overlapping(dataset.spatial_extent)
  278. else:
  279. return self.spatial_extent.overlapping_2d(dataset.spatial_extent)
  280. def spatial_relation(self, dataset):
  281. """Return the two or three dimensional spatial relation"""
  282. if self.get_type() == dataset.get_type() or dataset.get_type() == "str3ds":
  283. return self.spatial_extent.spatial_relation(dataset.spatial_extent)
  284. else:
  285. return self.spatial_extent.spatial_relation_2d(dataset.spatial_extent)
  286. def reset(self, ident):
  287. """Reset the internal structure and set the identifier"""
  288. self.ident = ident
  289. self.base = str3ds_base(ident=ident)
  290. if ident != None:
  291. self.base.set_name(self.ident.split("@")[0])
  292. self.base.set_mapset(self.ident.split("@")[1])
  293. self.base.set_creator(str(getpass.getuser()))
  294. self.absolute_time = str3ds_absolute_time(ident=ident)
  295. self.relative_time = str3ds_relative_time(ident=ident)
  296. self.spatial_extent = str3ds_spatial_extent(ident=ident)
  297. self.metadata = str3ds_metadata(ident=ident)
  298. ###############################################################################
  299. class space_time_vector_dataset(abstract_space_time_dataset):
  300. """Space time vector dataset class
  301. """
  302. def __init__(self, ident):
  303. abstract_space_time_dataset.__init__(self, ident)
  304. def get_type(self):
  305. return "stvds"
  306. def get_new_instance(self, ident):
  307. """Return a new instance with the type of this class"""
  308. return space_time_vector_dataset(ident)
  309. def get_new_map_instance(self, ident):
  310. """Return a new instance of a map dataset which is associated with the type of this class"""
  311. return vector_dataset(ident)
  312. def get_map_register(self):
  313. """Return the name of the map register table"""
  314. return self.metadata.get_vector_register()
  315. def set_map_register(self, name):
  316. """Set the name of the map register table"""
  317. self.metadata.set_vector_register(name)
  318. def spatial_overlapping(self, dataset):
  319. """Return True if the spatial extents 2d overlap"""
  320. return self.spatial_extent.overlapping_2d(dataset.spatial_extent)
  321. def spatial_relation(self, dataset):
  322. """Return the two dimensional spatial relation"""
  323. return self.spatial_extent.spatial_relation_2d(dataset.spatial_extent)
  324. def reset(self, ident):
  325. """Reset the internal structure and set the identifier"""
  326. self.ident = ident
  327. self.base = stvds_base(ident=ident)
  328. if ident != None:
  329. self.base.set_name(self.ident.split("@")[0])
  330. self.base.set_mapset(self.ident.split("@")[1])
  331. self.base.set_creator(str(getpass.getuser()))
  332. self.absolute_time = stvds_absolute_time(ident=ident)
  333. self.relative_time = stvds_relative_time(ident=ident)
  334. self.spatial_extent = stvds_spatial_extent(ident=ident)
  335. self.metadata = stvds_metadata(ident=ident)