/Atlas-Python/atlas/geomap/__init__.py

https://github.com/sajty/atlas-cpp
Python | 191 lines | 126 code | 25 blank | 40 comment | 42 complexity | 0bbfb5c1dc6954c1ef003c544caed113 MD5 | raw file
  1. #geographical map
  2. #Copyright 2002 by AIR-IX SUUNNITTELU/Ahiplan Oy
  3. #This program is free software; you can redistribute it and/or modify
  4. #it under the terms of the GNU General Public License as published by
  5. #the Free Software Foundation; either version 2 of the License, or
  6. #(at your option) any later version.
  7. #This program is distributed in the hope that it will be useful,
  8. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. #GNU General Public License for more details.
  11. #You should have received a copy of the GNU General Public License
  12. #along with this program; if not, write to the Free Software
  13. #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. from types import *
  15. from atlas.geomap.Vector3D import Vector3D
  16. from atlas.geomap.object import MapObject
  17. import atlas
  18. import atlas.analyse
  19. from copy import deepcopy
  20. from atlas.util.minmax import MinMaxVector
  21. class GeoMap:
  22. def __init__(self, objects):
  23. self.objects = {}
  24. self.all_objects = {}
  25. self.resolver = atlas.analyse.Resolver(self.all_objects)
  26. self.add_objects(objects.values())
  27. def is_geo_object(self, obj):
  28. return obj.objtype=="object" and obj.has_parent("root_geometry")
  29. def convert2geo(self, obj):
  30. if isinstance(obj, MapObject):
  31. return obj
  32. return MapObject(obj)
  33. def add_objects(self, objects, resolve_pointers=1):
  34. if not objects: return
  35. if resolve_pointers:
  36. lst = map(MapObject, objects)
  37. else:
  38. lst = objects
  39. for obj in lst:
  40. #if obj.objtype=="object" and hasattr(obj, "pos"):
  41. if self.is_geo_object(obj):
  42. self.objects[obj.id] = obj
  43. self.all_objects[obj.id] = obj
  44. if resolve_pointers:
  45. self.resolve_geo_pointers()
  46. self.resolve_position()
  47. def resolve_attributes(self, obj):
  48. unresolved = []
  49. for attr in ["loc", "polyline", "area", "volume"]:
  50. unresolved = unresolved + self.resolver.resolve_attribute(obj, attr)
  51. return unresolved
  52. def create_object(self, obj):
  53. #atlas.check_bug("add_object: %s:" % obj.id)
  54. print "GeoMap:",
  55. atlas.print_parents(obj)
  56. #print obj.__class__
  57. #if hasattr(obj, "contains"):
  58. # print "contains:", map(lambda o:"%s,%s" % (o.id,o.__class__), obj.contains)
  59. atlas.uri_type["loc"] = 1
  60. #atlas.uri_list_type["polyline"] = 1
  61. unresolved = self.resolve_attributes(obj)
  62. print "GeoMap?:", unresolved
  63. #atlas.check_bug("add_object before resolve:")
  64. resolved_objects = self.resolver.new_object(obj)
  65. print "GeoMap!:", map(lambda o:o.id, resolved_objects)
  66. #atlas.check_bug("add_object before add_objects:")
  67. self.add_objects(resolved_objects, resolve_pointers=0)
  68. #atlas.check_bug("add_object done:")
  69. #print self.resolver.pending
  70. #print self.resolver.depencies
  71. #print "objects:", self.objects.keys()
  72. #print "all_objects:", self.all_objects.keys()
  73. return unresolved
  74. def set_object(self, obj):
  75. self.resolve_attributes(obj)
  76. self.resolve_position()
  77. def delete_object(self, obj):
  78. if self.objects.has_key(obj.id):
  79. del self.objects[obj.id]
  80. if self.all_objects.has_key(obj.id):
  81. del self.all_objects[obj.id]
  82. self.resolve_position()
  83. def resolve_pointer(self, id):
  84. return atlas.resolve_pointer(self.objects, id)
  85. def resolve_pointer_list(self, lst):
  86. for i in range(len(lst)):
  87. lst[i] = self.resolve_pointer(lst[i])
  88. return lst
  89. def resolve_pointer_list2(self, lst2):
  90. for i in range(len(lst2)):
  91. self.resolve_pointer_list(lst2[i])
  92. def resolve_geo_pointers(self):
  93. for obj in self.objects.values():
  94. if hasattr(obj, "contains"):
  95. self.resolve_pointer_list(obj.contains)
  96. else:
  97. obj.contains = []
  98. if hasattr(obj, "polyline"):
  99. self.resolve_pointer_list(obj.polyline)
  100. if hasattr(obj, "area"):
  101. self.resolve_pointer_list2(obj.area)
  102. if hasattr(obj, "loc") and obj.loc:
  103. obj.loc = self.resolve_pointer(obj.loc)
  104. else:
  105. obj.loc = None
  106. def resolve_position(self):
  107. for obj in self.objects.values():
  108. if hasattr(obj, "pos"):
  109. if type(obj.pos)==ListType:
  110. obj.pos = Vector3D(obj.pos)
  111. else:
  112. obj.pos = Vector3D(0.0, 0.0, 0.0)
  113. for obj in self.objects.values():
  114. obj._pos = obj.get_xyz()
  115. for obj in self.objects.values():
  116. if hasattr(obj, "polyline"):
  117. obj._polyline = deepcopy(obj.polyline)
  118. resolve_list_position(obj, obj._polyline)
  119. if hasattr(obj, "area"):
  120. obj._area = deepcopy(obj.area)
  121. resolve_list_position2(obj, obj._area)
  122. def dimensions(self):
  123. ## x = MinMax()
  124. ## x.add(0.0); x.add(10.0)
  125. ## y = MinMax()
  126. ## y.add(0.0); y.add(10.0)
  127. limits = MinMaxVector()
  128. ## last_limits = deepcopy(limits)
  129. for obj in self.objects.values():
  130. index = None
  131. if obj.detailed_contents: continue
  132. try:
  133. if obj._area:
  134. for t in obj._area:
  135. for p in t:
  136. index = "area", t, p
  137. limits.add(p)
  138. ## if limits!=last_limits:
  139. ## print "area", obj.id, limits
  140. ## last_limits = deepcopy(limits)
  141. elif obj._polyline:
  142. for p in obj._polyline:
  143. index = "polyline", p
  144. limits.add(p)
  145. ## if limits!=last_limits:
  146. ## print "polyline", obj.id, limits
  147. ## last_limits = deepcopy(limits)
  148. else:
  149. index = "pos"
  150. limits.add(obj._pos)
  151. ## if limits!=last_limits:
  152. ## print "pos", obj.id, limits
  153. ## last_limits = deepcopy(limits)
  154. except ValueError:
  155. print "="*60
  156. print index
  157. print "-"*60
  158. print obj
  159. return limits.as_tuple()
  160. def resolve_list_position(obj, lst):
  161. for i in range(len(lst)):
  162. if type(lst[i])==ListType:
  163. lst[i] = Vector3D(lst[i]) + obj._pos
  164. def resolve_list_position2(obj, lst2):
  165. for i in range(len(lst2)):
  166. resolve_list_position(obj, lst2[i])