spatial_topology_dataset_connector.py 13 KB

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