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 |
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 projected coordinates. |
"""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. |
Return None if there are no layers or no layer contains any shapes. |
144 |
""" |
""" |
167 |
return None |
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() |
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 |
|
|
211 |
def TreeInfo(self): |
def TreeInfo(self): |
212 |
items = [] |
items = [] |
213 |
if self.BoundingBox() != None: |
if self.BoundingBox() != None: |
214 |
items.append("Extent (lat-lon): (%g, %g, %g, %g)" |
items.append(_("Extent (lat-lon): (%g, %g, %g, %g)") |
215 |
% self.BoundingBox()) |
% self.BoundingBox()) |
216 |
if self.projection and len(self.projection.params) > 0: |
if self.projection and len(self.projection.params) > 0: |
217 |
items.append("Extent (projected): (%g, %g, %g, %g)" |
items.append(_("Extent (projected): (%g, %g, %g, %g)") |
218 |
% self.ProjectedBoundingBox()) |
% self.ProjectedBoundingBox()) |
219 |
items.append(("Projection", |
items.append((_("Projection"), |
220 |
[str(param) for param in self.projection.params])) |
[str(param) |
221 |
|
for param in self.projection.params])) |
222 |
|
|
223 |
layers = self.layers[:] |
layers = self.layers[:] |
224 |
layers.reverse() |
layers.reverse() |
225 |
items.extend(layers) |
items.extend(layers) |
226 |
|
|
227 |
return ("Map: %s" % self.title, items) |
return (_("Map: %s") % self.title, items) |
228 |
|
|