1 |
# Copyright (c) 2001 by Intevation GmbH |
# Copyright (c) 2001, 2003 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
# |
# |
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 |
15 |
BaseProjection = Projection.Projection |
BaseProjection = Projection.Projection |
16 |
del Projection |
del Projection |
17 |
|
|
18 |
|
PROJ_UNITS_METERS = 1 |
19 |
|
PROJ_UNITS_DEGREES = 2 |
20 |
|
|
21 |
class Projection(BaseProjection): |
class Projection(BaseProjection): |
22 |
|
|
23 |
"""A proj4 projection object that remembers the parameters""" |
"""A proj4 projection object that remembers the parameters""" |
24 |
|
|
25 |
def __init__(self, params, name = "Unknown"): |
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 |
|
""" |
37 |
BaseProjection.__init__(self, params) |
BaseProjection.__init__(self, params) |
38 |
|
|
39 |
|
if name is None: |
40 |
|
self.name = _("Unknown") |
41 |
|
elif isinstance(name, StringTypes): |
42 |
|
self.name = name |
43 |
|
|
44 |
|
self.epsg = epsg |
45 |
self.params = params |
self.params = params |
|
self.name = name |
|
46 |
|
|
47 |
def ForwardBBox(self, bbox): |
def ForwardBBox(self, bbox): |
48 |
"""Return the bounding box of the corners of the bounding box bbox |
"""Return the bounding box of the corners of the bounding box bbox |
59 |
xs.append(x); ys.append(y) |
xs.append(x); ys.append(y) |
60 |
x, y = self.Forward(urx, lly) |
x, y = self.Forward(urx, lly) |
61 |
xs.append(x); ys.append(y) |
xs.append(x); ys.append(y) |
62 |
|
|
63 |
return min(xs), min(ys), max(xs), max(ys) |
return min(xs), min(ys), max(xs), max(ys) |
64 |
|
|
65 |
def GetName(self): |
def GetName(self): |
66 |
"""Return the name of the projection.""" |
"""Return the name of the projection.""" |
67 |
return self.name |
return self.name |
68 |
|
|
69 |
def SetName(self, name): |
def Label(self): |
70 |
"""Set the name of the projection.""" |
if self.epsg: |
71 |
if isinstance(name, StringTypes): |
return "EPSG % 5s %s" % (self.epsg, self.name) |
72 |
self.name = name |
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 |
def GetParameter(self, param): |
def GetParameter(self, param): |
79 |
"""Return the projection value for the given parameter. |
"""Return the projection value for the given parameter. |
80 |
|
|
81 |
If 'param' exists as a valid parameter then the returned |
If 'param' exists as a valid parameter return the associated |
82 |
value is a string with that value. If the parameter doesn't |
value as a string. If the parameter does not have a value (like |
83 |
exist an empty string is returned. |
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 |
""" |
""" |
88 |
|
|
89 |
for pair in self.params: |
for pair in self.params: |
90 |
p, v = pair.split("=") |
if "=" in pair: |
91 |
|
p, v = pair.split("=") |
92 |
|
else: |
93 |
|
p = v = pair |
94 |
if p == param: |
if p == param: |
95 |
return v |
return v |
96 |
|
|
100 |
"""Return list of 'parameter=value' strings""" |
"""Return list of 'parameter=value' strings""" |
101 |
return self.params |
return self.params |
102 |
|
|
103 |
|
def GetProjectedUnits(self): |
104 |
|
if self.GetParameter("proj") == "latlong": |
105 |
|
return PROJ_UNITS_DEGREES |
106 |
|
else: |
107 |
|
return PROJ_UNITS_METERS |
108 |
|
|
109 |
def __repr__(self): |
def __repr__(self): |
110 |
return self.name + ": " + repr(self.params) |
return self.name + ": " + repr(self.params) |
111 |
|
|
112 |
|
|
113 |
class ProjFile: |
class ProjFile: |
114 |
|
|
115 |
def __init__(self, filename): |
def __init__(self, filename): |
123 |
self.SetFilename(filename) |
self.SetFilename(filename) |
124 |
|
|
125 |
def Add(self, proj): |
def Add(self, proj): |
126 |
"""Add the given projection to the end of the file. If 'proj' |
"""Add the projection to the end of the file.""" |
127 |
already exists, it is replaced in the same location as the |
self.__projs.append(proj) |
|
original projection. |
|
|
""" |
|
|
|
|
|
try: |
|
|
# |
|
|
# see if the projection already exists. |
|
|
# this only works if Projection doesn't override __eq__ |
|
|
# |
|
|
self.__projs[self.__projs.index(proj)] = proj |
|
|
except ValueError: |
|
|
self.__projs.append(proj) |
|
128 |
|
|
129 |
def Remove(self, proj): |
def Remove(self, proj): |
130 |
"""Remove the objection proj from the projection file. |
"""Remove the object proj from the projection file. |
131 |
|
|
132 |
Raises a ValueError is proj is not found. |
Raises a ValueError is proj is not found. |
133 |
""" |
""" |
134 |
|
|
135 |
self.__projs.remove(proj) |
self.__projs.remove(proj) |
136 |
|
|
137 |
|
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 |
def GetFilename(self): |
def GetFilename(self): |
150 |
"""Return the filename where the ProjFile was read or will be |
"""Return the filename where the ProjFile was read or will be |
151 |
written to. |
written to. |