/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/Model/map.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/Thuban/Model/map.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 63 by bh, Fri Sep 14 12:20:02 2001 UTC revision 374 by jan, Mon Jan 27 14:20:02 2003 UTC
# Line 1  Line 1 
1  # Copyright (c) 2001 by Intevation GmbH  # Copyright (c) 2001, 2002 by Intevation GmbH
2  # Authors:  # Authors:
3  # Bernhard Herzog <[email protected]>  # Bernhard Herzog <[email protected]>
4  #  #
# Line 11  from messages import LAYERS_CHANGED, MAP Line 11  from messages import LAYERS_CHANGED, MAP
11       CHANGED, LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \       CHANGED, LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \
12       LAYER_VISIBILITY_CHANGED       LAYER_VISIBILITY_CHANGED
13    
14    from Thuban import _
15    
16  from base import TitledObject, Modifiable  from base import TitledObject, Modifiable
17    
18  from label import LabelLayer  from label import LabelLayer
# Line 32  class Map(TitledObject, Modifiable): Line 34  class Map(TitledObject, Modifiable):
34                          Parameter: the map                          Parameter: the map
35      """      """
36    
37      forwarded_channels = (LAYER_PROJECTION_CHANGED,      forwarded_channels = (CHANGED,
38                              LAYER_PROJECTION_CHANGED,
39                            LAYER_LEGEND_CHANGED,                            LAYER_LEGEND_CHANGED,
40                            LAYER_VISIBILITY_CHANGED)                            LAYER_VISIBILITY_CHANGED)
41    
42      def __init__(self, title, projection = None):      def __init__(self, title, projection = None):
43          """Initialize the map."""          """Initialize the map."""
44          TitledObject.__init__(self, title)          TitledObject.__init__(self, title)
45            Modifiable.__init__(self)
46          self.layers = []          self.layers = []
47          self.label_layer = LabelLayer("Labels")          self.label_layer = LabelLayer(_("Labels"))
48          self.label_layer.Subscribe(CHANGED, self.forward, LAYERS_CHANGED)          self.label_layer.Subscribe(CHANGED, self.forward, LAYERS_CHANGED)
49          self.projection = projection          self.projection = projection
50    
51      def Destroy(self):      def Destroy(self):
52          for layer in self.layers:          # call Modifiable.Destroy first since it will call
53              layer.Destroy()          # Publisher.Destroy which removes all subscriptions. Otherwise
54          self.label_layer.Unsubscribe(CHANGED, self.forward, LAYERS_CHANGED)          # clearing the layers results in messages to be sent which can
55            # cause problems.
56          Modifiable.Destroy(self)          Modifiable.Destroy(self)
57            self.ClearLayers()
58            self.label_layer.Unsubscribe(CHANGED, self.forward, LAYERS_CHANGED)
59            self.label_layer.Destroy()
60    
61      def AddLayer(self, layer):      def AddLayer(self, layer):
62            """Append layer to the map on top opf all."""
63          self.layers.append(layer)          self.layers.append(layer)
64          for channel in self.forwarded_channels:          self.subscribe_layer_channels(layer)
             layer.Subscribe(channel, self.forward, channel)  
65          self.changed(LAYERS_CHANGED, self)          self.changed(LAYERS_CHANGED, self)
66    
67      def RemoveLayer(self, layer):      def RemoveLayer(self, layer):
68          for channel in self.forwarded_channels:          """Remove layer from the map."""
69              layer.Unsubscribe(channel, self.forward, channel)          self.unsubscribe_layer_channels(layer)
70          self.layers.remove(layer)          self.layers.remove(layer)
71          self.changed(LAYERS_CHANGED, self)          self.changed(LAYERS_CHANGED, self)
72          layer.Destroy()          layer.Destroy()
73    
74        def CanRemoveLayer(self, layer):
75            """Return true if the layer can be deleted.
76    
77            The default implementation always returns 1. Derived classes
78            should override this method if they have e.g. special layers
79            that the user should not be able to remove.
80            """
81            return 1
82    
83        def ClearLayers(self):
84            """Delete all layers."""
85            for layer in self.layers:
86                self.unsubscribe_layer_channels(layer)
87                layer.Destroy()
88            del self.layers[:]
89            self.label_layer.ClearLabels()
90            self.changed(LAYERS_CHANGED, self)
91    
92        def subscribe_layer_channels(self, layer):
93            """Subscribe to some of layer's channels."""
94            for channel in self.forwarded_channels:
95                layer.Subscribe(channel, self.forward, channel)
96    
97        def unsubscribe_layer_channels(self, layer):
98            """Unsubscribe to some of layer's channels."""
99            for channel in self.forwarded_channels:
100                layer.Unsubscribe(channel, self.forward, channel)
101    
102      def LabelLayer(self):      def LabelLayer(self):
103          """Return the Map's label layer"""          """Return the Map's label layer"""
104          return self.label_layer          return self.label_layer
105    
106      def Layers(self):      def Layers(self):
107            """Return the list of layers contained in the map.
108    
109            The list does not include the label layer"""
110          return self.layers          return self.layers
111    
112      def HasLayers(self):      def HasLayers(self):
113            """Return true if the map has at least one shape layer"""
114          return len(self.layers) > 0          return len(self.layers) > 0
115    
116      def RaiseLayer(self, layer):      def RaiseLayer(self, layer):
117            """Swap the layer with the one above it.
118    
119            If the layer is already at the top do nothing. If the stacking
120            order has been changed, issue a LAYERS_CHANGED message.
121            """
122          index = self.layers.index(layer)          index = self.layers.index(layer)
123          if index < len(self.layers) - 1:          if index < len(self.layers) - 1:
124              del self.layers[index]              del self.layers[index]
125              self.layers.insert(index + 1, layer)              self.layers.insert(index + 1, layer)
126          self.changed(LAYERS_CHANGED, self)              self.changed(LAYERS_CHANGED, self)
127    
128      def LowerLayer(self, layer):      def LowerLayer(self, layer):
129            """Swap the layer with the one below it.
130    
131            If the layer is already at the bottom do nothing. If the
132            stacking order has been changed, issue a LAYERS_CHANGED message.
133            """
134          index = self.layers.index(layer)          index = self.layers.index(layer)
135          if index > 0:          if index > 0:
136              del self.layers[index]              del self.layers[index]
137              self.layers.insert(index - 1, layer)              self.layers.insert(index - 1, layer)
138          self.changed(LAYERS_CHANGED, self)              self.changed(LAYERS_CHANGED, self)
139    
140      def BoundingBox(self):      def BoundingBox(self):
141            """Return the bounding box of the map in Lat/Lon coordinates.
142    
143            Return None if there are no layers or no layer contains any shapes.
144            """
145          if not self.layers:          if not self.layers:
146              return None              return None
147          llx = []          llx = []
# Line 97  class Map(TitledObject, Modifiable): Line 151  class Map(TitledObject, Modifiable):
151          for layer in self.layers:          for layer in self.layers:
152              if layer is self.label_layer:              if layer is self.label_layer:
153                  continue                  continue
154              left, bottom, right, top = layer.LatLongBoundingBox()              # the layer's bbox may be None if it doesn't have any layers
155              llx.append(left)              bbox = layer.LatLongBoundingBox()
156              lly.append(bottom)              if bbox is not None:
157              urx.append(right)                  left, bottom, right, top = bbox
158              ury.append(top)                  llx.append(left)
159          return (min(llx), min(lly), max(urx), max(ury))                  lly.append(bottom)
160                    urx.append(right)
161                    ury.append(top)
162    
163            # check whether there were any empty layers.
164            if llx:
165                return (min(llx), min(lly), max(urx), max(ury))
166            else:
167                return None
168    
169      def ProjectedBoundingBox(self):      def ProjectedBoundingBox(self):
170            """Return the bounding box of the map in projected coordinates.
171    
172            Return None if there are no layers or no layer contains any shapes.
173            """
174          # This simply returns the rectangle given by the projected          # This simply returns the rectangle given by the projected
175          # corners of the non-projected bbox.          # corners of the non-projected bbox.
176          bbox = self.BoundingBox()          bbox = self.BoundingBox()
# Line 113  class Map(TitledObject, Modifiable): Line 179  class Map(TitledObject, Modifiable):
179          return bbox          return bbox
180    
181      def SetProjection(self, projection):      def SetProjection(self, projection):
182            """Set the projection of the map.
183    
184            Issue a MAP_PROJECTION_CHANGED message."""
185          self.projection = projection          self.projection = projection
186          self.changed(MAP_PROJECTION_CHANGED, self)          self.changed(MAP_PROJECTION_CHANGED, self)
187    
# Line 139  class Map(TitledObject, Modifiable): Line 208  class Map(TitledObject, Modifiable):
208              layer.UnsetModified()              layer.UnsetModified()
209          self.label_layer.UnsetModified()          self.label_layer.UnsetModified()
210    
211        def TreeInfo(self):
212            items = []
213            if self.BoundingBox() != None:
214                items.append(_("Extent (lat-lon): (%g, %g, %g, %g)")
215                             % self.BoundingBox())
216                if self.projection and len(self.projection.params) > 0:
217                    items.append(_("Extent (projected): (%g, %g, %g, %g)")
218                                 % self.ProjectedBoundingBox())
219                    items.append((_("Projection"),
220                                  [str(param)
221                                   for param in self.projection.params]))
222    
223            layers = self.layers[:]
224            layers.reverse()
225            items.extend(layers)
226    
227            return (_("Map: %s") % self.title, items)
228    

Legend:
Removed from v.63  
changed lines
  Added in v.374

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26