1 |
# 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 |
from types import StringTypes |
11 |
|
12 |
import Projection |
13 |
BaseProjection = Projection.Projection |
14 |
del Projection |
15 |
|
16 |
class Projection(BaseProjection): |
17 |
|
18 |
"""A proj4 projection object that remembers the parameters""" |
19 |
|
20 |
def __init__(self, params, name = "Unknown"): |
21 |
BaseProjection.__init__(self, params) |
22 |
self.params = params |
23 |
self.name = name |
24 |
|
25 |
def ForwardBBox(self, bbox): |
26 |
"""Return the bounding box of the corners of the bounding box bbox |
27 |
""" |
28 |
# This is not really the correct way to determine the bbox of a |
29 |
# projected shape, but for now it works well enough |
30 |
llx, lly, urx, ury = bbox |
31 |
xs = []; ys = [] |
32 |
x, y = self.Forward(llx, lly) |
33 |
xs.append(x); ys.append(y) |
34 |
x, y = self.Forward(llx, ury) |
35 |
xs.append(x); ys.append(y) |
36 |
x, y = self.Forward(urx, ury) |
37 |
xs.append(x); ys.append(y) |
38 |
x, y = self.Forward(urx, lly) |
39 |
xs.append(x); ys.append(y) |
40 |
return min(xs), min(ys), max(xs), max(ys) |
41 |
|
42 |
def GetName(self): |
43 |
"""Return the name of the projection.""" |
44 |
return self.name |
45 |
|
46 |
def SetName(self, name): |
47 |
"""Set the name of the projection.""" |
48 |
if isinstance(name, StringTypes): |
49 |
self.name = name |
50 |
|
51 |
def GetParameter(self, param): |
52 |
"""Return the projection value for the given parameter. |
53 |
|
54 |
If 'param' exists as a valid parameter then the returned |
55 |
value is a string with that value. If the parameter doesn't |
56 |
exist an empty string is returned. |
57 |
""" |
58 |
|
59 |
for pair in self.params: |
60 |
p, v = pair.split("=") |
61 |
if p == param: |
62 |
return v |
63 |
|
64 |
return "" |
65 |
|
66 |
def GetAllParameters(self): |
67 |
"""Return list of 'parameter=value' strings""" |
68 |
return self.params |
69 |
|
70 |
def __repr__(self): |
71 |
return self.name + ": " + repr(self.params) |
72 |
|
73 |
class ProjFile: |
74 |
|
75 |
def __init__(self, filename): |
76 |
"""Intialize the ProjFile. |
77 |
|
78 |
filename -- name of the file that this ProjFile represents. |
79 |
""" |
80 |
|
81 |
self.__projs = [] |
82 |
|
83 |
self.SetFilename(filename) |
84 |
|
85 |
def Add(self, proj): |
86 |
"""Add the given projection to the end of the file. If 'proj' |
87 |
already exists, it is replaced in the same location as the |
88 |
original projection. |
89 |
""" |
90 |
|
91 |
try: |
92 |
# |
93 |
# see if the projection already exists. |
94 |
# this only works if Projection doesn't override __eq__ |
95 |
# |
96 |
self.__projs[self.__projs.index(proj)] = proj |
97 |
except ValueError: |
98 |
self.__projs.append(proj) |
99 |
|
100 |
def Remove(self, proj): |
101 |
"""Remove the objection proj from the projection file. |
102 |
|
103 |
Raises a ValueError is proj is not found. |
104 |
""" |
105 |
|
106 |
self.__projs.remove(proj) |
107 |
|
108 |
def GetFilename(self): |
109 |
"""Return the filename where the ProjFile was read or will be |
110 |
written to. |
111 |
""" |
112 |
|
113 |
return self.__filename |
114 |
|
115 |
def SetFilename(self, filename): |
116 |
"""Set the filename where the ProjFile will be written to.""" |
117 |
self.__filename = filename |
118 |
|
119 |
def GetProjections(self): |
120 |
"""Return a list of the projections in the order they were read |
121 |
from the file or will be written. |
122 |
|
123 |
This is not a deep copy list, so any modifications made to the |
124 |
Projection objects will be written to the file. |
125 |
""" |
126 |
|
127 |
return self.__projs |
128 |
|