spatial_topology_dataset_connector.py 12 KB

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