1 |
bh |
178 |
# Copyright (c) 2001, 2002 by Intevation GmbH |
2 |
bh |
6 |
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
|
8 |
|
|
__version__ = "$Revision$" |
9 |
|
|
|
10 |
|
|
from messages import LAYERS_CHANGED, MAP_PROJECTION_CHANGED, \ |
11 |
|
|
CHANGED, LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \ |
12 |
|
|
LAYER_VISIBILITY_CHANGED |
13 |
|
|
|
14 |
jan |
374 |
from Thuban import _ |
15 |
|
|
|
16 |
bh |
6 |
from base import TitledObject, Modifiable |
17 |
|
|
|
18 |
|
|
from label import LabelLayer |
19 |
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
class Map(TitledObject, Modifiable): |
23 |
|
|
|
24 |
|
|
"""Represent a map. A map is simply a list of layers. |
25 |
|
|
|
26 |
|
|
Map objects send the following message types: |
27 |
|
|
|
28 |
|
|
TITLE_CHANGED -- The title has changed. Parameter: the map. |
29 |
|
|
|
30 |
|
|
LAYERS_CHANGED -- Layers were added, removed or rearranged. |
31 |
|
|
Parameters: the map |
32 |
|
|
|
33 |
|
|
MAP_PROJECTION_CHANGED -- the map's projection has changed. |
34 |
|
|
Parameter: the map |
35 |
|
|
""" |
36 |
|
|
|
37 |
bh |
319 |
forwarded_channels = (CHANGED, |
38 |
|
|
LAYER_PROJECTION_CHANGED, |
39 |
bh |
6 |
LAYER_LEGEND_CHANGED, |
40 |
|
|
LAYER_VISIBILITY_CHANGED) |
41 |
|
|
|
42 |
|
|
def __init__(self, title, projection = None): |
43 |
|
|
"""Initialize the map.""" |
44 |
|
|
TitledObject.__init__(self, title) |
45 |
bh |
319 |
Modifiable.__init__(self) |
46 |
bh |
6 |
self.layers = [] |
47 |
jan |
374 |
self.label_layer = LabelLayer(_("Labels")) |
48 |
bh |
6 |
self.label_layer.Subscribe(CHANGED, self.forward, LAYERS_CHANGED) |
49 |
|
|
self.projection = projection |
50 |
|
|
|
51 |
|
|
def Destroy(self): |
52 |
bh |
249 |
# call Modifiable.Destroy first since it will call |
53 |
|
|
# Publisher.Destroy which removes all subscriptions. Otherwise |
54 |
|
|
# clearing the layers results in messages to be sent which can |
55 |
|
|
# cause problems. |
56 |
|
|
Modifiable.Destroy(self) |
57 |
|
|
self.ClearLayers() |
58 |
bh |
63 |
self.label_layer.Unsubscribe(CHANGED, self.forward, LAYERS_CHANGED) |
59 |
bh |
249 |
self.label_layer.Destroy() |
60 |
bh |
6 |
|
61 |
|
|
def AddLayer(self, layer): |
62 |
bh |
249 |
"""Append layer to the map on top opf all.""" |
63 |
bh |
6 |
self.layers.append(layer) |
64 |
bh |
245 |
self.subscribe_layer_channels(layer) |
65 |
bh |
6 |
self.changed(LAYERS_CHANGED, self) |
66 |
|
|
|
67 |
|
|
def RemoveLayer(self, layer): |
68 |
bh |
249 |
"""Remove layer from the map.""" |
69 |
bh |
245 |
self.unsubscribe_layer_channels(layer) |
70 |
bh |
6 |
self.layers.remove(layer) |
71 |
|
|
self.changed(LAYERS_CHANGED, self) |
72 |
|
|
layer.Destroy() |
73 |
|
|
|
74 |
bh |
298 |
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 |
bh |
249 |
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 |
bh |
245 |
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 |
bh |
6 |
def LabelLayer(self): |
103 |
|
|
"""Return the Map's label layer""" |
104 |
|
|
return self.label_layer |
105 |
|
|
|
106 |
|
|
def Layers(self): |
107 |
bh |
249 |
"""Return the list of layers contained in the map. |
108 |
|
|
|
109 |
|
|
The list does not include the label layer""" |
110 |
bh |
6 |
return self.layers |
111 |
|
|
|
112 |
|
|
def HasLayers(self): |
113 |
bh |
249 |
"""Return true if the map has at least one shape layer""" |
114 |
bh |
6 |
return len(self.layers) > 0 |
115 |
|
|
|
116 |
|
|
def RaiseLayer(self, layer): |
117 |
bh |
249 |
"""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 |
bh |
6 |
index = self.layers.index(layer) |
123 |
|
|
if index < len(self.layers) - 1: |
124 |
|
|
del self.layers[index] |
125 |
|
|
self.layers.insert(index + 1, layer) |
126 |
bh |
249 |
self.changed(LAYERS_CHANGED, self) |
127 |
bh |
6 |
|
128 |
|
|
def LowerLayer(self, layer): |
129 |
bh |
249 |
"""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 |
bh |
6 |
index = self.layers.index(layer) |
135 |
|
|
if index > 0: |
136 |
|
|
del self.layers[index] |
137 |
|
|
self.layers.insert(index - 1, layer) |
138 |
bh |
249 |
self.changed(LAYERS_CHANGED, self) |
139 |
bh |
6 |
|
140 |
|
|
def BoundingBox(self): |
141 |
bh |
249 |
"""Return the bounding box of the map in Lat/Lon coordinates. |
142 |
bh |
178 |
|
143 |
|
|
Return None if there are no layers or no layer contains any shapes. |
144 |
|
|
""" |
145 |
bh |
6 |
if not self.layers: |
146 |
|
|
return None |
147 |
|
|
llx = [] |
148 |
|
|
lly = [] |
149 |
|
|
urx = [] |
150 |
|
|
ury = [] |
151 |
|
|
for layer in self.layers: |
152 |
|
|
if layer is self.label_layer: |
153 |
|
|
continue |
154 |
bh |
178 |
# the layer's bbox may be None if it doesn't have any layers |
155 |
|
|
bbox = layer.LatLongBoundingBox() |
156 |
|
|
if bbox is not None: |
157 |
|
|
left, bottom, right, top = bbox |
158 |
|
|
llx.append(left) |
159 |
|
|
lly.append(bottom) |
160 |
|
|
urx.append(right) |
161 |
|
|
ury.append(top) |
162 |
bh |
6 |
|
163 |
bh |
178 |
# 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 |
bh |
6 |
def ProjectedBoundingBox(self): |
170 |
bh |
249 |
"""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 |
bh |
6 |
# This simply returns the rectangle given by the projected |
175 |
|
|
# corners of the non-projected bbox. |
176 |
|
|
bbox = self.BoundingBox() |
177 |
|
|
if bbox is not None and self.projection is not None: |
178 |
|
|
bbox = self.projection.ForwardBBox(bbox) |
179 |
|
|
return bbox |
180 |
|
|
|
181 |
|
|
def SetProjection(self, projection): |
182 |
bh |
249 |
"""Set the projection of the map. |
183 |
|
|
|
184 |
|
|
Issue a MAP_PROJECTION_CHANGED message.""" |
185 |
bh |
6 |
self.projection = projection |
186 |
|
|
self.changed(MAP_PROJECTION_CHANGED, self) |
187 |
|
|
|
188 |
|
|
def forward(self, *args): |
189 |
|
|
"""Reissue events""" |
190 |
|
|
if len(args) > 1: |
191 |
|
|
args = (args[-1],) + args[:-1] |
192 |
|
|
apply(self.issue, args) |
193 |
|
|
|
194 |
|
|
def WasModified(self): |
195 |
|
|
"""Return true if the map or one of the layers was modified""" |
196 |
|
|
if self.modified: |
197 |
|
|
return 1 |
198 |
|
|
else: |
199 |
|
|
for layer in self.layers: |
200 |
|
|
if layer.WasModified(): |
201 |
|
|
return 1 |
202 |
|
|
return self.label_layer.WasModified() |
203 |
|
|
|
204 |
|
|
def UnsetModified(self): |
205 |
|
|
"""Unset the modified flag of the map and the layers""" |
206 |
|
|
Modifiable.UnsetModified(self) |
207 |
|
|
for layer in self.layers: |
208 |
|
|
layer.UnsetModified() |
209 |
|
|
self.label_layer.UnsetModified() |
210 |
|
|
|
211 |
bh |
217 |
def TreeInfo(self): |
212 |
|
|
items = [] |
213 |
|
|
if self.BoundingBox() != None: |
214 |
jan |
374 |
items.append(_("Extent (lat-lon): (%g, %g, %g, %g)") |
215 |
bh |
217 |
% self.BoundingBox()) |
216 |
bh |
231 |
if self.projection and len(self.projection.params) > 0: |
217 |
jan |
374 |
items.append(_("Extent (projected): (%g, %g, %g, %g)") |
218 |
bh |
231 |
% self.ProjectedBoundingBox()) |
219 |
jan |
374 |
items.append((_("Projection"), |
220 |
bh |
231 |
[str(param) |
221 |
|
|
for param in self.projection.params])) |
222 |
bh |
6 |
|
223 |
bh |
217 |
layers = self.layers[:] |
224 |
|
|
layers.reverse() |
225 |
|
|
items.extend(layers) |
226 |
|
|
|
227 |
jan |
374 |
return (_("Map: %s") % self.title, items) |
228 |
bh |
217 |
|