1 |
# Copyright (c) 2001, 2002 by Intevation GmbH |
2 |
# 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 |
from base import TitledObject, Modifiable |
15 |
|
16 |
from label import LabelLayer |
17 |
|
18 |
|
19 |
|
20 |
class Map(TitledObject, Modifiable): |
21 |
|
22 |
"""Represent a map. A map is simply a list of layers. |
23 |
|
24 |
Map objects send the following message types: |
25 |
|
26 |
TITLE_CHANGED -- The title has changed. Parameter: the map. |
27 |
|
28 |
LAYERS_CHANGED -- Layers were added, removed or rearranged. |
29 |
Parameters: the map |
30 |
|
31 |
MAP_PROJECTION_CHANGED -- the map's projection has changed. |
32 |
Parameter: the map |
33 |
""" |
34 |
|
35 |
forwarded_channels = (LAYER_PROJECTION_CHANGED, |
36 |
LAYER_LEGEND_CHANGED, |
37 |
LAYER_VISIBILITY_CHANGED) |
38 |
|
39 |
def __init__(self, title, projection = None): |
40 |
"""Initialize the map.""" |
41 |
TitledObject.__init__(self, title) |
42 |
self.layers = [] |
43 |
self.label_layer = LabelLayer("Labels") |
44 |
self.label_layer.Subscribe(CHANGED, self.forward, LAYERS_CHANGED) |
45 |
self.projection = projection |
46 |
|
47 |
def Destroy(self): |
48 |
for layer in self.layers: |
49 |
layer.Destroy() |
50 |
self.label_layer.Unsubscribe(CHANGED, self.forward, LAYERS_CHANGED) |
51 |
Modifiable.Destroy(self) |
52 |
|
53 |
def AddLayer(self, layer): |
54 |
self.layers.append(layer) |
55 |
for channel in self.forwarded_channels: |
56 |
layer.Subscribe(channel, self.forward, channel) |
57 |
self.changed(LAYERS_CHANGED, self) |
58 |
|
59 |
def RemoveLayer(self, layer): |
60 |
for channel in self.forwarded_channels: |
61 |
layer.Unsubscribe(channel, self.forward, channel) |
62 |
self.layers.remove(layer) |
63 |
self.changed(LAYERS_CHANGED, self) |
64 |
layer.Destroy() |
65 |
|
66 |
def LabelLayer(self): |
67 |
"""Return the Map's label layer""" |
68 |
return self.label_layer |
69 |
|
70 |
def Layers(self): |
71 |
return self.layers |
72 |
|
73 |
def HasLayers(self): |
74 |
return len(self.layers) > 0 |
75 |
|
76 |
def RaiseLayer(self, layer): |
77 |
index = self.layers.index(layer) |
78 |
if index < len(self.layers) - 1: |
79 |
del self.layers[index] |
80 |
self.layers.insert(index + 1, layer) |
81 |
self.changed(LAYERS_CHANGED, self) |
82 |
|
83 |
def LowerLayer(self, layer): |
84 |
index = self.layers.index(layer) |
85 |
if index > 0: |
86 |
del self.layers[index] |
87 |
self.layers.insert(index - 1, layer) |
88 |
self.changed(LAYERS_CHANGED, self) |
89 |
|
90 |
def BoundingBox(self): |
91 |
"""Return the bounding box of the map in projected coordinates. |
92 |
|
93 |
Return None if there are no layers or no layer contains any shapes. |
94 |
""" |
95 |
if not self.layers: |
96 |
return None |
97 |
llx = [] |
98 |
lly = [] |
99 |
urx = [] |
100 |
ury = [] |
101 |
for layer in self.layers: |
102 |
if layer is self.label_layer: |
103 |
continue |
104 |
# the layer's bbox may be None if it doesn't have any layers |
105 |
bbox = layer.LatLongBoundingBox() |
106 |
if bbox is not None: |
107 |
left, bottom, right, top = bbox |
108 |
llx.append(left) |
109 |
lly.append(bottom) |
110 |
urx.append(right) |
111 |
ury.append(top) |
112 |
|
113 |
# check whether there were any empty layers. |
114 |
if llx: |
115 |
return (min(llx), min(lly), max(urx), max(ury)) |
116 |
else: |
117 |
return None |
118 |
|
119 |
def ProjectedBoundingBox(self): |
120 |
# This simply returns the rectangle given by the projected |
121 |
# corners of the non-projected bbox. |
122 |
bbox = self.BoundingBox() |
123 |
if bbox is not None and self.projection is not None: |
124 |
bbox = self.projection.ForwardBBox(bbox) |
125 |
return bbox |
126 |
|
127 |
def SetProjection(self, projection): |
128 |
self.projection = projection |
129 |
self.changed(MAP_PROJECTION_CHANGED, self) |
130 |
|
131 |
def forward(self, *args): |
132 |
"""Reissue events""" |
133 |
if len(args) > 1: |
134 |
args = (args[-1],) + args[:-1] |
135 |
apply(self.issue, args) |
136 |
|
137 |
def WasModified(self): |
138 |
"""Return true if the map or one of the layers was modified""" |
139 |
if self.modified: |
140 |
return 1 |
141 |
else: |
142 |
for layer in self.layers: |
143 |
if layer.WasModified(): |
144 |
return 1 |
145 |
return self.label_layer.WasModified() |
146 |
|
147 |
def UnsetModified(self): |
148 |
"""Unset the modified flag of the map and the layers""" |
149 |
Modifiable.UnsetModified(self) |
150 |
for layer in self.layers: |
151 |
layer.UnsetModified() |
152 |
self.label_layer.UnsetModified() |
153 |
|
154 |
def TreeInfo(self): |
155 |
items = [] |
156 |
if self.BoundingBox() != None: |
157 |
items.append("Extent (lat-lon): (%g, %g, %g, %g)" |
158 |
% self.BoundingBox()) |
159 |
if self.projection and len(self.projection.params) > 0: |
160 |
items.append("Extent (projected): (%g, %g, %g, %g)" |
161 |
% self.ProjectedBoundingBox()) |
162 |
items.append(("Projection", |
163 |
[str(param) |
164 |
for param in self.projection.params])) |
165 |
|
166 |
layers = self.layers[:] |
167 |
layers.reverse() |
168 |
items.extend(layers) |
169 |
|
170 |
return ("Map: %s" % self.title, items) |
171 |
|