7 |
|
|
8 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
9 |
|
|
10 |
from messages import LAYERS_CHANGED, MAP_PROJECTION_CHANGED, \ |
from messages import MAP_LAYERS_CHANGED, MAP_PROJECTION_CHANGED, \ |
11 |
CHANGED, LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \ |
CHANGED, LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \ |
12 |
LAYER_VISIBILITY_CHANGED |
LAYER_VISIBILITY_CHANGED, LAYER_CHANGED, MAP_STACKING_CHANGED, \ |
13 |
|
MAP_LAYERS_ADDED, MAP_LAYERS_REMOVED |
14 |
|
|
15 |
|
from Thuban import _ |
16 |
|
|
17 |
from base import TitledObject, Modifiable |
from base import TitledObject, Modifiable |
18 |
|
|
19 |
from label import LabelLayer |
from label import LabelLayer |
20 |
|
|
21 |
|
|
|
|
|
22 |
class Map(TitledObject, Modifiable): |
class Map(TitledObject, Modifiable): |
23 |
|
|
24 |
"""Represent a map. A map is simply a list of layers. |
"""Represent a map. A map is simply a list of layers. |
27 |
|
|
28 |
TITLE_CHANGED -- The title has changed. Parameter: the map. |
TITLE_CHANGED -- The title has changed. Parameter: the map. |
29 |
|
|
30 |
LAYERS_CHANGED -- Layers were added, removed or rearranged. |
MAP_LAYERS_CHANGED -- Layers were added, removed or rearranged. |
31 |
Parameters: the map |
Parameters: the map |
32 |
|
|
33 |
MAP_PROJECTION_CHANGED -- the map's projection has changed. |
MAP_PROJECTION_CHANGED -- the map's projection has changed. |
37 |
forwarded_channels = (CHANGED, |
forwarded_channels = (CHANGED, |
38 |
LAYER_PROJECTION_CHANGED, |
LAYER_PROJECTION_CHANGED, |
39 |
LAYER_LEGEND_CHANGED, |
LAYER_LEGEND_CHANGED, |
40 |
|
LAYER_CHANGED, |
41 |
LAYER_VISIBILITY_CHANGED) |
LAYER_VISIBILITY_CHANGED) |
42 |
|
|
43 |
def __init__(self, title, projection = None): |
def __init__(self, title, projection = None): |
45 |
TitledObject.__init__(self, title) |
TitledObject.__init__(self, title) |
46 |
Modifiable.__init__(self) |
Modifiable.__init__(self) |
47 |
self.layers = [] |
self.layers = [] |
48 |
self.label_layer = LabelLayer("Labels") |
self.label_layer = LabelLayer(_("Labels")) |
49 |
self.label_layer.Subscribe(CHANGED, self.forward, LAYERS_CHANGED) |
self.label_layer.Subscribe(CHANGED, self.forward, MAP_LAYERS_CHANGED) |
50 |
self.projection = projection |
self.projection = projection |
51 |
|
|
52 |
def Destroy(self): |
def Destroy(self): |
56 |
# cause problems. |
# cause problems. |
57 |
Modifiable.Destroy(self) |
Modifiable.Destroy(self) |
58 |
self.ClearLayers() |
self.ClearLayers() |
59 |
self.label_layer.Unsubscribe(CHANGED, self.forward, LAYERS_CHANGED) |
self.label_layer.Unsubscribe(CHANGED, self.forward, MAP_LAYERS_CHANGED) |
60 |
self.label_layer.Destroy() |
self.label_layer.Destroy() |
61 |
|
|
62 |
def AddLayer(self, layer): |
def AddLayer(self, layer): |
63 |
"""Append layer to the map on top opf all.""" |
"""Append layer to the map on top of all.""" |
64 |
self.layers.append(layer) |
self.layers.append(layer) |
65 |
self.subscribe_layer_channels(layer) |
self.subscribe_layer_channels(layer) |
66 |
self.changed(LAYERS_CHANGED, self) |
self.changed(MAP_LAYERS_CHANGED, self) |
67 |
|
self.changed(MAP_LAYERS_ADDED, self) |
68 |
|
|
69 |
def RemoveLayer(self, layer): |
def RemoveLayer(self, layer): |
70 |
"""Remove layer from the map.""" |
"""Remove layer from the map.""" |
71 |
self.unsubscribe_layer_channels(layer) |
self.unsubscribe_layer_channels(layer) |
72 |
self.layers.remove(layer) |
self.layers.remove(layer) |
73 |
self.changed(LAYERS_CHANGED, self) |
self.changed(MAP_LAYERS_CHANGED, self) |
74 |
|
self.changed(MAP_LAYERS_REMOVED, self) |
75 |
layer.Destroy() |
layer.Destroy() |
76 |
|
|
77 |
def CanRemoveLayer(self, layer): |
def CanRemoveLayer(self, layer): |
90 |
layer.Destroy() |
layer.Destroy() |
91 |
del self.layers[:] |
del self.layers[:] |
92 |
self.label_layer.ClearLabels() |
self.label_layer.ClearLabels() |
93 |
self.changed(LAYERS_CHANGED, self) |
self.changed(MAP_LAYERS_CHANGED, self) |
94 |
|
self.changed(MAP_LAYERS_REMOVED, self) |
95 |
|
|
96 |
def subscribe_layer_channels(self, layer): |
def subscribe_layer_channels(self, layer): |
97 |
"""Subscribe to some of layer's channels.""" |
"""Subscribe to some of layer's channels.""" |
117 |
"""Return true if the map has at least one shape layer""" |
"""Return true if the map has at least one shape layer""" |
118 |
return len(self.layers) > 0 |
return len(self.layers) > 0 |
119 |
|
|
120 |
|
def MoveLayerToTop(self, layer): |
121 |
|
"""Put the layer on top of the layer stack. |
122 |
|
|
123 |
|
If the layer is already at the top do nothing. If the stacking |
124 |
|
order has been changed, issue a MAP_LAYERS_CHANGED message. |
125 |
|
""" |
126 |
|
index = self.layers.index(layer) |
127 |
|
if index < len(self.layers) - 1: |
128 |
|
del self.layers[index] |
129 |
|
self.layers.append(layer) |
130 |
|
self.changed(MAP_LAYERS_CHANGED, self) |
131 |
|
self.changed(MAP_STACKING_CHANGED, self) |
132 |
|
|
133 |
|
|
134 |
def RaiseLayer(self, layer): |
def RaiseLayer(self, layer): |
135 |
"""Swap the layer with the one above it. |
"""Swap the layer with the one above it. |
136 |
|
|
137 |
If the layer is already at the top do nothing. If the stacking |
If the layer is already at the top do nothing. If the stacking |
138 |
order has been changed, issue a LAYERS_CHANGED message. |
order has been changed, issue a MAP_LAYERS_CHANGED message. |
139 |
""" |
""" |
140 |
index = self.layers.index(layer) |
index = self.layers.index(layer) |
141 |
if index < len(self.layers) - 1: |
if index < len(self.layers) - 1: |
142 |
del self.layers[index] |
del self.layers[index] |
143 |
self.layers.insert(index + 1, layer) |
self.layers.insert(index + 1, layer) |
144 |
self.changed(LAYERS_CHANGED, self) |
self.changed(MAP_LAYERS_CHANGED, self) |
145 |
|
self.changed(MAP_STACKING_CHANGED, self) |
146 |
|
|
147 |
def LowerLayer(self, layer): |
def LowerLayer(self, layer): |
148 |
"""Swap the layer with the one below it. |
"""Swap the layer with the one below it. |
149 |
|
|
150 |
If the layer is already at the bottom do nothing. If the |
If the layer is already at the bottom do nothing. If the |
151 |
stacking order has been changed, issue a LAYERS_CHANGED message. |
stacking order has been changed, issue a MAP_LAYERS_CHANGED message. |
152 |
""" |
""" |
153 |
index = self.layers.index(layer) |
index = self.layers.index(layer) |
154 |
if index > 0: |
if index > 0: |
155 |
del self.layers[index] |
del self.layers[index] |
156 |
self.layers.insert(index - 1, layer) |
self.layers.insert(index - 1, layer) |
157 |
self.changed(LAYERS_CHANGED, self) |
self.changed(MAP_LAYERS_CHANGED, self) |
158 |
|
self.changed(MAP_STACKING_CHANGED, self) |
159 |
|
|
160 |
|
def MoveLayerToBottom(self, layer): |
161 |
|
"""Put the layer at the bottom of the stack. |
162 |
|
|
163 |
|
If the layer is already at the bottom do nothing. If the |
164 |
|
stacking order has been changed, issue a MAP_LAYERS_CHANGED message. |
165 |
|
""" |
166 |
|
index = self.layers.index(layer) |
167 |
|
if index > 0: |
168 |
|
del self.layers[index] |
169 |
|
self.layers.insert(0, layer) |
170 |
|
self.changed(MAP_LAYERS_CHANGED, self) |
171 |
|
self.changed(MAP_STACKING_CHANGED, self) |
172 |
|
|
173 |
def BoundingBox(self): |
def BoundingBox(self): |
174 |
"""Return the bounding box of the map in Lat/Lon coordinates. |
"""Return the bounding box of the map in Lat/Lon coordinates. |
184 |
for layer in self.layers: |
for layer in self.layers: |
185 |
if layer is self.label_layer: |
if layer is self.label_layer: |
186 |
continue |
continue |
187 |
# the layer's bbox may be None if it doesn't have any layers |
# the layer's bbox may be None if it doesn't have any shapes |
188 |
bbox = layer.LatLongBoundingBox() |
bbox = layer.LatLongBoundingBox() |
189 |
if bbox is not None: |
if bbox is not None: |
190 |
left, bottom, right, top = bbox |
left, bottom, right, top = bbox |
211 |
bbox = self.projection.ForwardBBox(bbox) |
bbox = self.projection.ForwardBBox(bbox) |
212 |
return bbox |
return bbox |
213 |
|
|
214 |
|
def GetProjection(self): |
215 |
|
return self.projection |
216 |
|
|
217 |
def SetProjection(self, projection): |
def SetProjection(self, projection): |
218 |
"""Set the projection of the map. |
"""Set the projection of the map. |
219 |
|
|
220 |
Issue a MAP_PROJECTION_CHANGED message.""" |
Issue a MAP_PROJECTION_CHANGED message.""" |
221 |
|
old_proj = self.projection |
222 |
self.projection = projection |
self.projection = projection |
223 |
self.changed(MAP_PROJECTION_CHANGED, self) |
self.changed(MAP_PROJECTION_CHANGED, self, old_proj) |
224 |
|
|
225 |
def forward(self, *args): |
def forward(self, *args): |
226 |
"""Reissue events""" |
"""Reissue events""" |
248 |
def TreeInfo(self): |
def TreeInfo(self): |
249 |
items = [] |
items = [] |
250 |
if self.BoundingBox() != None: |
if self.BoundingBox() != None: |
251 |
items.append("Extent (lat-lon): (%g, %g, %g, %g)" |
items.append(_("Extent (lat-lon): (%g, %g, %g, %g)") |
252 |
% self.BoundingBox()) |
% self.BoundingBox()) |
253 |
if self.projection and len(self.projection.params) > 0: |
if self.projection and len(self.projection.params) > 0: |
254 |
items.append("Extent (projected): (%g, %g, %g, %g)" |
items.append(_("Extent (projected): (%g, %g, %g, %g)") |
255 |
% self.ProjectedBoundingBox()) |
% self.ProjectedBoundingBox()) |
256 |
items.append(("Projection", |
items.append((_("Projection"), |
257 |
[str(param) |
[str(param) |
258 |
for param in self.projection.params])) |
for param in self.projection.params])) |
259 |
|
|
261 |
layers.reverse() |
layers.reverse() |
262 |
items.extend(layers) |
items.extend(layers) |
263 |
|
|
264 |
return ("Map: %s" % self.title, items) |
return (_("Map: %s") % self.title, items) |
265 |
|
|