1 |
bh |
6 |
# 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 |
jonathan |
745 |
from Thuban import _ |
11 |
|
|
|
12 |
jonathan |
726 |
from types import StringTypes |
13 |
|
|
|
14 |
bh |
6 |
import Projection |
15 |
|
|
BaseProjection = Projection.Projection |
16 |
|
|
del Projection |
17 |
|
|
|
18 |
|
|
class Projection(BaseProjection): |
19 |
|
|
|
20 |
|
|
"""A proj4 projection object that remembers the parameters""" |
21 |
|
|
|
22 |
jonathan |
745 |
def __init__(self, params, name = None): |
23 |
|
|
"""Initialize the Projection with a list of |
24 |
|
|
'parameter=value' strings and an optional name. If 'name' is |
25 |
|
|
None, the name will be set to 'Unknown' in the local language. |
26 |
|
|
""" |
27 |
|
|
|
28 |
bh |
6 |
BaseProjection.__init__(self, params) |
29 |
jonathan |
745 |
|
30 |
jonathan |
749 |
self.SetName(name) |
31 |
|
|
self.SetAllParameters(params) |
32 |
jonathan |
745 |
|
33 |
bh |
6 |
def ForwardBBox(self, bbox): |
34 |
|
|
"""Return the bounding box of the corners of the bounding box bbox |
35 |
|
|
""" |
36 |
|
|
# This is not really the correct way to determine the bbox of a |
37 |
|
|
# projected shape, but for now it works well enough |
38 |
|
|
llx, lly, urx, ury = bbox |
39 |
|
|
xs = []; ys = [] |
40 |
|
|
x, y = self.Forward(llx, lly) |
41 |
|
|
xs.append(x); ys.append(y) |
42 |
|
|
x, y = self.Forward(llx, ury) |
43 |
|
|
xs.append(x); ys.append(y) |
44 |
|
|
x, y = self.Forward(urx, ury) |
45 |
|
|
xs.append(x); ys.append(y) |
46 |
|
|
x, y = self.Forward(urx, lly) |
47 |
|
|
xs.append(x); ys.append(y) |
48 |
|
|
return min(xs), min(ys), max(xs), max(ys) |
49 |
jonathan |
695 |
|
50 |
|
|
def GetName(self): |
51 |
jonathan |
738 |
"""Return the name of the projection.""" |
52 |
jonathan |
695 |
return self.name |
53 |
|
|
|
54 |
jonathan |
726 |
def SetName(self, name): |
55 |
jonathan |
749 |
"""Set the name of the projection. |
56 |
|
|
|
57 |
|
|
If 'name' is None, the name will be set to 'Unknown' |
58 |
|
|
in the local language. |
59 |
|
|
""" |
60 |
|
|
|
61 |
|
|
if name is None: |
62 |
|
|
self.name = _("Unknown") |
63 |
|
|
elif isinstance(name, StringTypes): |
64 |
jonathan |
726 |
self.name = name |
65 |
|
|
|
66 |
jonathan |
708 |
def GetParameter(self, param): |
67 |
jonathan |
738 |
"""Return the projection value for the given parameter. |
68 |
jonathan |
708 |
|
69 |
jonathan |
738 |
If 'param' exists as a valid parameter then the returned |
70 |
|
|
value is a string with that value. If the parameter doesn't |
71 |
|
|
exist an empty string is returned. |
72 |
|
|
""" |
73 |
|
|
|
74 |
jonathan |
708 |
for pair in self.params: |
75 |
|
|
p, v = pair.split("=") |
76 |
|
|
if p == param: |
77 |
|
|
return v |
78 |
|
|
|
79 |
|
|
return "" |
80 |
|
|
|
81 |
|
|
def GetAllParameters(self): |
82 |
jonathan |
738 |
"""Return list of 'parameter=value' strings""" |
83 |
jonathan |
695 |
return self.params |
84 |
|
|
|
85 |
jonathan |
749 |
def SetAllParameters(self, params): |
86 |
|
|
"""Set the projection's parameters to the given parameter list.""" |
87 |
|
|
self.params = params |
88 |
|
|
|
89 |
|
|
def SetProjection(self, proj): |
90 |
|
|
"""Set the projection properties to those of the given projection. |
91 |
|
|
|
92 |
|
|
This does not copy the object. |
93 |
|
|
""" |
94 |
|
|
|
95 |
|
|
self.SetName(proj.name) |
96 |
|
|
self.SetAllParameters(proj.params) |
97 |
|
|
|
98 |
jonathan |
695 |
def __repr__(self): |
99 |
jonathan |
726 |
return self.name + ": " + repr(self.params) |
100 |
jonathan |
695 |
|
101 |
|
|
class ProjFile: |
102 |
|
|
|
103 |
|
|
def __init__(self, filename): |
104 |
jonathan |
738 |
"""Intialize the ProjFile. |
105 |
jonathan |
695 |
|
106 |
jonathan |
738 |
filename -- name of the file that this ProjFile represents. |
107 |
|
|
""" |
108 |
|
|
|
109 |
|
|
self.__projs = [] |
110 |
|
|
|
111 |
|
|
self.SetFilename(filename) |
112 |
jonathan |
695 |
|
113 |
|
|
def Add(self, proj): |
114 |
jonathan |
738 |
"""Add the given projection to the end of the file. If 'proj' |
115 |
|
|
already exists, it is replaced in the same location as the |
116 |
|
|
original projection. |
117 |
|
|
""" |
118 |
jonathan |
695 |
|
119 |
jonathan |
738 |
try: |
120 |
|
|
# |
121 |
|
|
# see if the projection already exists. |
122 |
|
|
# this only works if Projection doesn't override __eq__ |
123 |
|
|
# |
124 |
|
|
self.__projs[self.__projs.index(proj)] = proj |
125 |
|
|
except ValueError: |
126 |
|
|
self.__projs.append(proj) |
127 |
|
|
|
128 |
jonathan |
726 |
def Remove(self, proj): |
129 |
jonathan |
738 |
"""Remove the objection proj from the projection file. |
130 |
jonathan |
726 |
|
131 |
jonathan |
738 |
Raises a ValueError is proj is not found. |
132 |
|
|
""" |
133 |
jonathan |
695 |
|
134 |
jonathan |
738 |
self.__projs.remove(proj) |
135 |
jonathan |
695 |
|
136 |
jonathan |
738 |
def GetFilename(self): |
137 |
|
|
"""Return the filename where the ProjFile was read or will be |
138 |
|
|
written to. |
139 |
|
|
""" |
140 |
|
|
|
141 |
|
|
return self.__filename |
142 |
|
|
|
143 |
|
|
def SetFilename(self, filename): |
144 |
|
|
"""Set the filename where the ProjFile will be written to.""" |
145 |
|
|
self.__filename = filename |
146 |
|
|
|
147 |
jonathan |
695 |
def GetProjections(self): |
148 |
jonathan |
738 |
"""Return a list of the projections in the order they were read |
149 |
|
|
from the file or will be written. |
150 |
|
|
|
151 |
|
|
This is not a deep copy list, so any modifications made to the |
152 |
|
|
Projection objects will be written to the file. |
153 |
|
|
""" |
154 |
|
|
|
155 |
|
|
return self.__projs |
156 |
|
|
|