1 |
# Copyright (c) 2001 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 |
if not self.layers: |
92 |
return None |
93 |
llx = [] |
94 |
lly = [] |
95 |
urx = [] |
96 |
ury = [] |
97 |
for layer in self.layers: |
98 |
if layer is self.label_layer: |
99 |
continue |
100 |
left, bottom, right, top = layer.LatLongBoundingBox() |
101 |
llx.append(left) |
102 |
lly.append(bottom) |
103 |
urx.append(right) |
104 |
ury.append(top) |
105 |
return (min(llx), min(lly), max(urx), max(ury)) |
106 |
|
107 |
def ProjectedBoundingBox(self): |
108 |
# This simply returns the rectangle given by the projected |
109 |
# corners of the non-projected bbox. |
110 |
bbox = self.BoundingBox() |
111 |
if bbox is not None and self.projection is not None: |
112 |
bbox = self.projection.ForwardBBox(bbox) |
113 |
return bbox |
114 |
|
115 |
def SetProjection(self, projection): |
116 |
self.projection = projection |
117 |
self.changed(MAP_PROJECTION_CHANGED, self) |
118 |
|
119 |
def forward(self, *args): |
120 |
"""Reissue events""" |
121 |
if len(args) > 1: |
122 |
args = (args[-1],) + args[:-1] |
123 |
apply(self.issue, args) |
124 |
|
125 |
def WasModified(self): |
126 |
"""Return true if the map or one of the layers was modified""" |
127 |
if self.modified: |
128 |
return 1 |
129 |
else: |
130 |
for layer in self.layers: |
131 |
if layer.WasModified(): |
132 |
return 1 |
133 |
return self.label_layer.WasModified() |
134 |
|
135 |
def UnsetModified(self): |
136 |
"""Unset the modified flag of the map and the layers""" |
137 |
Modifiable.UnsetModified(self) |
138 |
for layer in self.layers: |
139 |
layer.UnsetModified() |
140 |
self.label_layer.UnsetModified() |
141 |
|
142 |
|