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 |
|
|
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 |
bh |
245 |
self.unsubscribe_layer_channels(layer) |
50 |
bh |
6 |
layer.Destroy() |
51 |
bh |
63 |
self.label_layer.Unsubscribe(CHANGED, self.forward, LAYERS_CHANGED) |
52 |
bh |
6 |
Modifiable.Destroy(self) |
53 |
|
|
|
54 |
|
|
def AddLayer(self, layer): |
55 |
|
|
self.layers.append(layer) |
56 |
bh |
245 |
self.subscribe_layer_channels(layer) |
57 |
bh |
6 |
self.changed(LAYERS_CHANGED, self) |
58 |
|
|
|
59 |
|
|
def RemoveLayer(self, layer): |
60 |
bh |
245 |
self.unsubscribe_layer_channels(layer) |
61 |
bh |
6 |
self.layers.remove(layer) |
62 |
|
|
self.changed(LAYERS_CHANGED, self) |
63 |
|
|
layer.Destroy() |
64 |
|
|
|
65 |
bh |
245 |
def subscribe_layer_channels(self, layer): |
66 |
|
|
"""Subscribe to some of layer's channels.""" |
67 |
|
|
for channel in self.forwarded_channels: |
68 |
|
|
layer.Subscribe(channel, self.forward, channel) |
69 |
|
|
|
70 |
|
|
def unsubscribe_layer_channels(self, layer): |
71 |
|
|
"""Unsubscribe to some of layer's channels.""" |
72 |
|
|
for channel in self.forwarded_channels: |
73 |
|
|
layer.Unsubscribe(channel, self.forward, channel) |
74 |
|
|
|
75 |
bh |
6 |
def LabelLayer(self): |
76 |
|
|
"""Return the Map's label layer""" |
77 |
|
|
return self.label_layer |
78 |
|
|
|
79 |
|
|
def Layers(self): |
80 |
|
|
return self.layers |
81 |
|
|
|
82 |
|
|
def HasLayers(self): |
83 |
|
|
return len(self.layers) > 0 |
84 |
|
|
|
85 |
|
|
def RaiseLayer(self, layer): |
86 |
|
|
index = self.layers.index(layer) |
87 |
|
|
if index < len(self.layers) - 1: |
88 |
|
|
del self.layers[index] |
89 |
|
|
self.layers.insert(index + 1, layer) |
90 |
|
|
self.changed(LAYERS_CHANGED, self) |
91 |
|
|
|
92 |
|
|
def LowerLayer(self, layer): |
93 |
|
|
index = self.layers.index(layer) |
94 |
|
|
if index > 0: |
95 |
|
|
del self.layers[index] |
96 |
|
|
self.layers.insert(index - 1, layer) |
97 |
|
|
self.changed(LAYERS_CHANGED, self) |
98 |
|
|
|
99 |
|
|
def BoundingBox(self): |
100 |
bh |
178 |
"""Return the bounding box of the map in projected coordinates. |
101 |
|
|
|
102 |
|
|
Return None if there are no layers or no layer contains any shapes. |
103 |
|
|
""" |
104 |
bh |
6 |
if not self.layers: |
105 |
|
|
return None |
106 |
|
|
llx = [] |
107 |
|
|
lly = [] |
108 |
|
|
urx = [] |
109 |
|
|
ury = [] |
110 |
|
|
for layer in self.layers: |
111 |
|
|
if layer is self.label_layer: |
112 |
|
|
continue |
113 |
bh |
178 |
# the layer's bbox may be None if it doesn't have any layers |
114 |
|
|
bbox = layer.LatLongBoundingBox() |
115 |
|
|
if bbox is not None: |
116 |
|
|
left, bottom, right, top = bbox |
117 |
|
|
llx.append(left) |
118 |
|
|
lly.append(bottom) |
119 |
|
|
urx.append(right) |
120 |
|
|
ury.append(top) |
121 |
bh |
6 |
|
122 |
bh |
178 |
# check whether there were any empty layers. |
123 |
|
|
if llx: |
124 |
|
|
return (min(llx), min(lly), max(urx), max(ury)) |
125 |
|
|
else: |
126 |
|
|
return None |
127 |
|
|
|
128 |
bh |
6 |
def ProjectedBoundingBox(self): |
129 |
|
|
# This simply returns the rectangle given by the projected |
130 |
|
|
# corners of the non-projected bbox. |
131 |
|
|
bbox = self.BoundingBox() |
132 |
|
|
if bbox is not None and self.projection is not None: |
133 |
|
|
bbox = self.projection.ForwardBBox(bbox) |
134 |
|
|
return bbox |
135 |
|
|
|
136 |
|
|
def SetProjection(self, projection): |
137 |
|
|
self.projection = projection |
138 |
|
|
self.changed(MAP_PROJECTION_CHANGED, self) |
139 |
|
|
|
140 |
|
|
def forward(self, *args): |
141 |
|
|
"""Reissue events""" |
142 |
|
|
if len(args) > 1: |
143 |
|
|
args = (args[-1],) + args[:-1] |
144 |
|
|
apply(self.issue, args) |
145 |
|
|
|
146 |
|
|
def WasModified(self): |
147 |
|
|
"""Return true if the map or one of the layers was modified""" |
148 |
|
|
if self.modified: |
149 |
|
|
return 1 |
150 |
|
|
else: |
151 |
|
|
for layer in self.layers: |
152 |
|
|
if layer.WasModified(): |
153 |
|
|
return 1 |
154 |
|
|
return self.label_layer.WasModified() |
155 |
|
|
|
156 |
|
|
def UnsetModified(self): |
157 |
|
|
"""Unset the modified flag of the map and the layers""" |
158 |
|
|
Modifiable.UnsetModified(self) |
159 |
|
|
for layer in self.layers: |
160 |
|
|
layer.UnsetModified() |
161 |
|
|
self.label_layer.UnsetModified() |
162 |
|
|
|
163 |
bh |
217 |
def TreeInfo(self): |
164 |
|
|
items = [] |
165 |
|
|
if self.BoundingBox() != None: |
166 |
|
|
items.append("Extent (lat-lon): (%g, %g, %g, %g)" |
167 |
|
|
% self.BoundingBox()) |
168 |
bh |
231 |
if self.projection and len(self.projection.params) > 0: |
169 |
|
|
items.append("Extent (projected): (%g, %g, %g, %g)" |
170 |
|
|
% self.ProjectedBoundingBox()) |
171 |
|
|
items.append(("Projection", |
172 |
|
|
[str(param) |
173 |
|
|
for param in self.projection.params])) |
174 |
bh |
6 |
|
175 |
bh |
217 |
layers = self.layers[:] |
176 |
|
|
layers.reverse() |
177 |
|
|
items.extend(layers) |
178 |
|
|
|
179 |
|
|
return ("Map: %s" % self.title, items) |
180 |
|
|
|