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 |
# |
# |
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""" |
30 |
|
|
31 |
BaseProjection.__init__(self, params) |
BaseProjection.__init__(self, params) |
32 |
|
|
33 |
self.SetName(name) |
if name is None: |
34 |
self.SetAllParameters(params) |
self.name = _("Unknown") |
35 |
|
elif isinstance(name, StringTypes): |
36 |
|
self.name = name |
37 |
|
|
38 |
|
self.params = params |
39 |
|
|
40 |
def ForwardBBox(self, bbox): |
def ForwardBBox(self, bbox): |
41 |
"""Return the bounding box of the corners of the bounding box bbox |
"""Return the bounding box of the corners of the bounding box bbox |
52 |
xs.append(x); ys.append(y) |
xs.append(x); ys.append(y) |
53 |
x, y = self.Forward(urx, lly) |
x, y = self.Forward(urx, lly) |
54 |
xs.append(x); ys.append(y) |
xs.append(x); ys.append(y) |
55 |
|
|
56 |
return min(xs), min(ys), max(xs), max(ys) |
return min(xs), min(ys), max(xs), max(ys) |
57 |
|
|
58 |
def GetName(self): |
def GetName(self): |
59 |
"""Return the name of the projection.""" |
"""Return the name of the projection.""" |
60 |
return self.name |
return self.name |
61 |
|
|
|
def SetName(self, name): |
|
|
"""Set the name of the projection. |
|
|
|
|
|
If 'name' is None, the name will be set to 'Unknown' |
|
|
in the local language. |
|
|
""" |
|
|
|
|
|
if name is None: |
|
|
self.name = _("Unknown") |
|
|
elif isinstance(name, StringTypes): |
|
|
self.name = name |
|
|
|
|
62 |
def GetParameter(self, param): |
def GetParameter(self, param): |
63 |
"""Return the projection value for the given parameter. |
"""Return the projection value for the given parameter. |
64 |
|
|
65 |
If 'param' exists as a valid parameter then the returned |
If 'param' exists as a valid parameter return the associated |
66 |
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 |
67 |
exist an empty string is returned. |
e.g. the 'south' parameter for utm) then the value is the |
68 |
|
parameter name itself. |
69 |
|
|
70 |
|
If the parameter doesn't exist return an empty string. |
71 |
""" |
""" |
72 |
|
|
73 |
for pair in self.params: |
for pair in self.params: |
74 |
p, v = pair.split("=") |
if "=" in pair: |
75 |
|
p, v = pair.split("=") |
76 |
|
else: |
77 |
|
p = v = pair |
78 |
if p == param: |
if p == param: |
79 |
return v |
return v |
80 |
|
|
84 |
"""Return list of 'parameter=value' strings""" |
"""Return list of 'parameter=value' strings""" |
85 |
return self.params |
return self.params |
86 |
|
|
87 |
def SetAllParameters(self, params): |
def GetProjectedUnits(self): |
88 |
"""Set the projection's parameters to the given parameter list.""" |
if self.GetParameter("proj") == "latlong": |
89 |
self.params = params |
return PROJ_UNITS_DEGREES |
90 |
|
else: |
91 |
def SetProjection(self, proj): |
return PROJ_UNITS_METERS |
|
"""Set the projection properties to those of the given projection. |
|
|
|
|
|
This does not copy the object. |
|
|
""" |
|
|
|
|
|
self.SetName(proj.name) |
|
|
self.SetAllParameters(proj.params) |
|
92 |
|
|
93 |
def __repr__(self): |
def __repr__(self): |
94 |
return self.name + ": " + repr(self.params) |
return self.name + ": " + repr(self.params) |
106 |
self.SetFilename(filename) |
self.SetFilename(filename) |
107 |
|
|
108 |
def Add(self, proj): |
def Add(self, proj): |
109 |
"""Add the given projection to the end of the file. If 'proj' |
"""Add the projection to the end of the file.""" |
110 |
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) |
|
111 |
|
|
112 |
def Remove(self, proj): |
def Remove(self, proj): |
113 |
"""Remove the objection proj from the projection file. |
"""Remove the object proj from the projection file. |
114 |
|
|
115 |
Raises a ValueError is proj is not found. |
Raises a ValueError is proj is not found. |
116 |
""" |
""" |
117 |
|
|
118 |
self.__projs.remove(proj) |
self.__projs.remove(proj) |
119 |
|
|
120 |
|
def Replace(self, oldproj, newproj): |
121 |
|
"""Replace the object 'oldproj' with 'newproj'. |
122 |
|
|
123 |
|
Raises ValueError if oldproj is not in the file. |
124 |
|
""" |
125 |
|
|
126 |
|
# |
127 |
|
# see if the projection already exists. |
128 |
|
# this only works if Projection doesn't override __eq__ |
129 |
|
# |
130 |
|
self.__projs[self.__projs.index(oldproj)] = newproj |
131 |
|
|
132 |
def GetFilename(self): |
def GetFilename(self): |
133 |
"""Return the filename where the ProjFile was read or will be |
"""Return the filename where the ProjFile was read or will be |
134 |
written to. |
written to. |