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