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