7 |
|
|
8 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
9 |
|
|
10 |
|
from Thuban import _ |
11 |
|
|
12 |
from types import StringTypes |
from types import StringTypes |
13 |
|
|
14 |
import Projection |
import Projection |
19 |
|
|
20 |
"""A proj4 projection object that remembers the parameters""" |
"""A proj4 projection object that remembers the parameters""" |
21 |
|
|
22 |
def __init__(self, params, name = "Unknown"): |
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 |
BaseProjection.__init__(self, params) |
BaseProjection.__init__(self, params) |
29 |
|
|
30 |
|
if name is None: |
31 |
|
self.name = _("Unknown") |
32 |
|
elif isinstance(name, StringTypes): |
33 |
|
self.name = name |
34 |
|
|
35 |
self.params = params |
self.params = params |
|
self.name = name |
|
36 |
|
|
37 |
def ForwardBBox(self, bbox): |
def ForwardBBox(self, bbox): |
38 |
"""Return the bounding box of the corners of the bounding box bbox |
"""Return the bounding box of the corners of the bounding box bbox |
49 |
xs.append(x); ys.append(y) |
xs.append(x); ys.append(y) |
50 |
x, y = self.Forward(urx, lly) |
x, y = self.Forward(urx, lly) |
51 |
xs.append(x); ys.append(y) |
xs.append(x); ys.append(y) |
52 |
|
|
53 |
return min(xs), min(ys), max(xs), max(ys) |
return min(xs), min(ys), max(xs), max(ys) |
54 |
|
|
55 |
def GetName(self): |
def GetName(self): |
56 |
|
"""Return the name of the projection.""" |
57 |
return self.name |
return self.name |
58 |
|
|
|
def SetName(self, name): |
|
|
if isinstance(name, StringTypes): |
|
|
self.name = name |
|
|
|
|
59 |
def GetParameter(self, param): |
def GetParameter(self, param): |
60 |
|
"""Return the projection value for the given parameter. |
61 |
|
|
62 |
|
If 'param' exists as a valid parameter then the returned |
63 |
|
value is a string with that value. If the parameter doesn't |
64 |
|
exist an empty string is returned. |
65 |
|
""" |
66 |
|
|
67 |
for pair in self.params: |
for pair in self.params: |
68 |
p, v = pair.split("=") |
p, v = pair.split("=") |
72 |
return "" |
return "" |
73 |
|
|
74 |
def GetAllParameters(self): |
def GetAllParameters(self): |
75 |
|
"""Return list of 'parameter=value' strings""" |
76 |
return self.params |
return self.params |
77 |
|
|
78 |
def __repr__(self): |
def __repr__(self): |
81 |
class ProjFile: |
class ProjFile: |
82 |
|
|
83 |
def __init__(self, filename): |
def __init__(self, filename): |
84 |
self.projs = {} |
"""Intialize the ProjFile. |
85 |
|
|
86 |
|
filename -- name of the file that this ProjFile represents. |
87 |
|
""" |
88 |
|
|
89 |
|
self.__projs = [] |
90 |
|
|
91 |
self.SetFileName(filename) |
self.SetFilename(filename) |
92 |
|
|
93 |
def Add(self, proj): |
def Add(self, proj): |
94 |
self.projs[proj] = 0 |
"""Add the given projection to the end of the file. If 'proj' |
95 |
|
already exists, it is replaced in the same location as the |
96 |
|
original projection. |
97 |
|
""" |
98 |
|
|
99 |
|
try: |
100 |
|
# |
101 |
|
# see if the projection already exists. |
102 |
|
# this only works if Projection doesn't override __eq__ |
103 |
|
# |
104 |
|
self.__projs[self.__projs.index(proj)] = proj |
105 |
|
except ValueError: |
106 |
|
self.__projs.append(proj) |
107 |
|
|
108 |
def Remove(self, proj): |
def Remove(self, proj): |
109 |
if self.projs.has_key(proj): |
"""Remove the object proj from the projection file. |
110 |
del self.projs[proj] |
|
111 |
|
Raises a ValueError is proj is not found. |
112 |
|
""" |
113 |
|
|
114 |
def GetFileName(self): |
self.__projs.remove(proj) |
|
return self.filename |
|
115 |
|
|
116 |
def SetFileName(self, filename): |
def Replace(self, oldproj, newproj): |
117 |
self.filename = filename |
"""Replace the object 'oldproj' with 'newproj'. |
118 |
|
|
119 |
|
Raises ValueError if oldproj is not in the file. |
120 |
|
""" |
121 |
|
|
122 |
|
# |
123 |
|
# see if the projection already exists. |
124 |
|
# this only works if Projection doesn't override __eq__ |
125 |
|
# |
126 |
|
self.__projs[self.__projs.index(oldproj)] = newproj |
127 |
|
|
128 |
|
def GetFilename(self): |
129 |
|
"""Return the filename where the ProjFile was read or will be |
130 |
|
written to. |
131 |
|
""" |
132 |
|
|
133 |
|
return self.__filename |
134 |
|
|
135 |
|
def SetFilename(self, filename): |
136 |
|
"""Set the filename where the ProjFile will be written to.""" |
137 |
|
self.__filename = filename |
138 |
|
|
139 |
def GetProjections(self): |
def GetProjections(self): |
140 |
return self.projs.keys() |
"""Return a list of the projections in the order they were read |
141 |
|
from the file or will be written. |
142 |
|
|
143 |
|
This is not a deep copy list, so any modifications made to the |
144 |
|
Projection objects will be written to the file. |
145 |
|
""" |
146 |
|
|
147 |
|
return self.__projs |
148 |
|
|