abstract_spatial_dataset.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. Usage:
  6. >>> import grass.temporal as tgis
  7. >>> tmr = tgis.AbstractSpatialDataset()
  8. (C) 2008-2011 by the GRASS Development Team
  9. This program is free software under the GNU General Public
  10. License (>=v2). Read the file COPYING that comes with GRASS
  11. for details.
  12. @author Soeren Gebbert
  13. """
  14. from abstract_dataset import *
  15. from datetime_math import *
  16. class AbstractTemporalDataset(AbstractDataset):
  17. """!This class implements a spatial topology access structure for an abstract dataset
  18. This object will be set up by spatial topology creation method provided by the
  19. SpatialTopologyBuilder.
  20. The following spatial relations with access methods are supported:
  21. - equivalent
  22. - overlap
  23. - in
  24. - contain
  25. - meet
  26. - cover
  27. - covered
  28. Usage:
  29. @code
  30. >>> import grass.temporal as tgis
  31. >>> tgis.init()
  32. >>> map = tgis.RasterDataset("a@P")
  33. >>> tmr = tgis.AbstractTemporalDataset()
  34. >>> tmr.append_equivalent(map)
  35. >>> tmr.append_overlap(map)
  36. >>> tmr.append_in(map)
  37. >>> tmr.append_contain(map)
  38. >>> tmr.append_meet(map)
  39. >>> tmr.append_cover(map)
  40. >>> tmr.append_covered(map)
  41. >>> tmr.print_topology_info()
  42. +-------------------- Spatial Topology --------------------------------------+
  43. | Equivalent: ................ a@P
  44. | Cover: ..................... a@P
  45. | Covered: ................... a@P
  46. | Overlap: ................... a@P
  47. | In: ........................ a@P
  48. | Contain: ................... a@P
  49. | Meet: ...................... a@P
  50. >>> tmr.print_topology_shell_info()
  51. equivalent=a@P
  52. overlap=a@P
  53. in=a@P
  54. contain=a@P
  55. meet=a@P
  56. cover=a@P
  57. covered=a@P
  58. @endcode
  59. """
  60. def __init__(self):
  61. AbstractDataset.__init__(self)
  62. self.reset_topology()
  63. def reset_topology(self):
  64. """!Reset any information about temporal topology"""
  65. self._topology = {}
  66. self._has_topology = False
  67. def get_number_of_relations(self):
  68. """! Return a dictionary in which the keys are the relation names and the value
  69. are the number of relations.
  70. The following relations are available:
  71. - equivalent
  72. - overlap
  73. - in
  74. - contain
  75. - meet
  76. - cover
  77. - covered
  78. To access topological information the spatial topology must be build first
  79. using the SpatialTopologyBuilder.
  80. @return the dictionary with relations as keys and number as values or None in case the topology wasn't build
  81. """
  82. if self._has_topology == False:
  83. return None
  84. relations = {}
  85. try:
  86. relations["equivalent"] = len(self._topology["EQUIVALENT"])
  87. except:
  88. relations["equivalent"] = 0
  89. try:
  90. relations["overlap"] = len(self._topology["OVERLAP"])
  91. except:
  92. relations["overlap"] = 0
  93. try:
  94. relations["in"] = len(self._topology["IN"])
  95. except:
  96. relations["in"] = 0
  97. try:
  98. relations["contain"] = len(self._topology["CONTAIN"])
  99. except:
  100. relations["contain"] = 0
  101. try:
  102. relations["meet"] = len(self._topology["MEET"])
  103. except:
  104. relations["meet"] = 0
  105. try:
  106. relations["cover"] = len(self._topology["COVER"])
  107. except:
  108. relations["cover"] = 0
  109. try:
  110. relations["covered"] = len(self._topology["COVERED"])
  111. except:
  112. relations["covered"] = 0
  113. return relations
  114. def set_topology_build_true(self):
  115. """!Same as name"""
  116. self._has_topology = True
  117. def set_topology_build_false(self):
  118. """!Same as name"""
  119. self._has_topology = False
  120. def is_topology_build(self):
  121. """!Check if the temporal topology was build"""
  122. return self._has_topology
  123. def append_equivalent(self, map):
  124. """!Append a map with equivalent spatial extent as this map
  125. @param map This object should be of type AbstractMapDataset
  126. or derived classes
  127. """
  128. if "EQUIVALENT" not in self._topology:
  129. self._topology["EQUIVALENT"] = []
  130. self._topology["EQUIVALENT"].append(map)
  131. def get_equivalent(self):
  132. """!Return a list of map objects with equivalent spatial extent as this map
  133. @return A list of map objects or None
  134. """
  135. if "EQUIVALENT" not in self._topology:
  136. return None
  137. return self._topology["EQUIVALENT"]
  138. def append_overlap(self, map):
  139. """!Append a map that this spatial overlap with this map
  140. @param map This object should be of type AbstractMapDataset
  141. or derived classes
  142. """
  143. if "OVERLAP" not in self._topology:
  144. self._topology["OVERLAP"] = []
  145. self._topology["OVERLAP"].append(map)
  146. def get_overlap(self):
  147. """!Return a list of map objects that this map spatial overlap with
  148. @return A list of map objects or None
  149. """
  150. if "OVERLAP" not in self._topology:
  151. return None
  152. return self._topology["OVERLAP"]
  153. def append_in(self, map):
  154. """!Append a map that this is spatial in this map
  155. @param map This object should be of type AbstractMapDataset
  156. or derived classes
  157. """
  158. if "IN" not in self._topology:
  159. self._topology["IN"] = []
  160. self._topology["IN"].append(map)
  161. def get_in(self):
  162. """!Return a list of map objects that are spatial in this map
  163. @return A list of map objects or None
  164. """
  165. if "IN" not in self._topology:
  166. return None
  167. return self._topology["IN"]
  168. def append_contain(self, map):
  169. """!Append a map that this map spatially contains
  170. @param map This object should be of type AbstractMapDataset
  171. or derived classes
  172. """
  173. if "CONTAIN" not in self._topology:
  174. self._topology["CONTAIN"] = []
  175. self._topology["CONTAIN"].append(map)
  176. def get_contain(self):
  177. """!Return a list of map objects that this map contains
  178. @return A list of map objects or None
  179. """
  180. if "CONTAIN" not in self._topology:
  181. return None
  182. return self._topology["CONTAIN"]
  183. def append_meet(self, map):
  184. """!Append a map that spatially meet with this map
  185. @param map This object should be of type AbstractMapDataset
  186. or derived classes
  187. """
  188. if "MEET" not in self._topology:
  189. self._topology["MEET"] = []
  190. self._topology["MEET"].append(map)
  191. def get_meet(self):
  192. """!Return a list of map objects that spatially meet with this map
  193. @return A list of map objects or None
  194. """
  195. if "MEET" not in self._topology:
  196. return None
  197. return self._topology["MEET"]
  198. def append_cover(self, map):
  199. """!Append a map that spatially cover this map
  200. @param map This object should be of type AbstractMapDataset
  201. or derived classes
  202. """
  203. if "COVER" not in self._topology:
  204. self._topology["COVER"] = []
  205. self._topology["COVER"].append(map)
  206. def get_cover(self):
  207. """!Return a list of map objects that spatially cover this map
  208. @return A list of map objects or None
  209. """
  210. if "COVER" not in self._topology:
  211. return None
  212. return self._topology["COVER"]
  213. def append_covered(self, map):
  214. """!Append a map that is spatially covered by this map
  215. @param map This object should be of type AbstractMapDataset
  216. or derived classes
  217. """
  218. if "COVERED" not in self._topology:
  219. self._topology["COVERED"] = []
  220. self._topology["COVERED"].append(map)
  221. def get_covered(self):
  222. """!Return a list of map objects that are spatially covered by this map
  223. @return A list of map objects or None
  224. """
  225. if "COVERED" not in self._topology:
  226. return None
  227. return self._topology["COVERED"]
  228. def _generate_map_list_string(self, map_list, line_wrap=True):
  229. count = 0
  230. string = ""
  231. for map_ in map_list:
  232. if line_wrap and count > 0 and count % 3 == 0:
  233. string += "\n | ............................ "
  234. count = 0
  235. if count == 0:
  236. string += map_.get_id()
  237. else:
  238. string += ",%s" % map_.get_id()
  239. count += 1
  240. return string
  241. # Set the properties
  242. equivalent = property(fget=get_equivalent,
  243. fset=append_equivalent)
  244. cover = property(fget=get_cover,
  245. fset=append_cover)
  246. covered = property(fget=get_covered,
  247. fset=append_covered)
  248. overlap = property(fget=get_overlap,
  249. fset=append_overlap)
  250. in = property(fget=get_in,
  251. fset=append_in)
  252. contain = property(fget=get_contain,
  253. fset=append_contain)
  254. meet = property(fget=get_meet,
  255. fset=append_meet)
  256. def print_topology_info(self):
  257. """!Print information about this class in human readable style"""
  258. print " +-------------------- Spatial Topology --------------------------------------+"
  259. # 0123456789012345678901234567890
  260. if self.equivalent is not None:
  261. print " | Equivalent: ................ " + \
  262. self._generate_map_list_string(self.equivalent)
  263. if self.cover is not None:
  264. print " | Cover: ..................... " + \
  265. self._generate_map_list_string(self.cover)
  266. if self.covered is not None:
  267. print " | Covered: ................... " + \
  268. self._generate_map_list_string(self.covered)
  269. if self.overlap is not None:
  270. print " | Overlap: ................... " + \
  271. self._generate_map_list_string(self.overlap)
  272. if self.in is not None:
  273. print " | In: ........................ " + \
  274. self._generate_map_list_string(self.in)
  275. if self.contain is not None:
  276. print " | Contain: ................... " + \
  277. self._generate_map_list_string(self.contain)
  278. if self.meet is not None:
  279. print " | Meet: ...................... " + \
  280. self._generate_map_list_string(self.meet)
  281. def print_topology_shell_info(self):
  282. """!Print information about this class in shell style"""
  283. if self.next() is not None:
  284. if self.equivalent is not None:
  285. print "equivalent=" + self._generate_map_list_string(self.equivalent, False)
  286. if self.cover is not None:
  287. print "cover=" + self._generate_map_list_string(
  288. self.cover, False)
  289. if self.covered is not None:
  290. print "covered=" + \
  291. self._generate_map_list_string(self.covered, False)
  292. if self.overlap is not None:
  293. print "overlap=" + \
  294. self._generate_map_list_string(self.overlap)
  295. if self.in is not None:
  296. print "in=" + \
  297. self._generate_map_list_string(self.in)
  298. if self.contain is not None:
  299. print "contain=" + \
  300. self._generate_map_list_string(self.contain)
  301. if self.meet is not None:
  302. print "meet=" + \
  303. self._generate_map_list_string(self.meet)
  304. ###############################################################################
  305. if __name__ == "__main__":
  306. import doctest
  307. doctest.testmod()