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 |
# |
# |
9 |
|
|
10 |
from types import StringTypes |
from types import StringTypes |
11 |
|
|
12 |
|
from Thuban import _ |
13 |
|
from Thuban.Lib.connector import Publisher |
14 |
|
|
15 |
import Projection |
import Projection |
16 |
BaseProjection = Projection.Projection |
BaseProjection = Projection.Projection |
17 |
del Projection |
del Projection |
18 |
|
|
19 |
|
from messages import PROJECTION_ADDED, PROJECTION_REPLACED, PROJECTION_REMOVED |
20 |
|
|
21 |
|
PROJ_UNITS_METERS = 1 |
22 |
|
PROJ_UNITS_DEGREES = 2 |
23 |
|
|
24 |
class Projection(BaseProjection): |
class Projection(BaseProjection): |
25 |
|
|
26 |
"""A proj4 projection object that remembers the parameters""" |
"""A proj4 projection object that remembers the parameters""" |
27 |
|
|
28 |
def __init__(self, params, name = "Unknown"): |
def __init__(self, params, name = None, epsg = None): |
29 |
|
"""Initialize the Projection |
30 |
|
|
31 |
|
Parameters: |
32 |
|
|
33 |
|
params -- a list of 'parameter=value' strings |
34 |
|
|
35 |
|
name -- (optional) The name of the projectin. If None or omitted |
36 |
|
it defaults to 'Unknown' in the local language. |
37 |
|
|
38 |
|
epsg -- (optional) The EPSG code as a string. |
39 |
|
""" |
40 |
BaseProjection.__init__(self, params) |
BaseProjection.__init__(self, params) |
41 |
|
|
42 |
|
if name is None: |
43 |
|
self.name = _("Unknown") |
44 |
|
elif isinstance(name, StringTypes): |
45 |
|
self.name = name |
46 |
|
|
47 |
|
self.epsg = epsg |
48 |
self.params = params |
self.params = params |
|
self.name = name |
|
49 |
|
|
50 |
def ForwardBBox(self, bbox): |
def _transform_bbox(self, trafo, bbox): |
|
"""Return the bounding box of the corners of the bounding box bbox |
|
|
""" |
|
51 |
# This is not really the correct way to determine the bbox of a |
# This is not really the correct way to determine the bbox of a |
52 |
# projected shape, but for now it works well enough |
# projected bbox, but for now it works well enough |
53 |
llx, lly, urx, ury = bbox |
llx, lly, urx, ury = bbox |
54 |
xs = []; ys = [] |
xs = []; ys = [] |
55 |
x, y = self.Forward(llx, lly) |
for x, y in ((llx, lly), (llx, ury), (urx, lly), (urx, ury)): |
56 |
xs.append(x); ys.append(y) |
x, y = trafo(x, y) |
57 |
x, y = self.Forward(llx, ury) |
xs.append(x); ys.append(y) |
|
xs.append(x); ys.append(y) |
|
|
x, y = self.Forward(urx, ury) |
|
|
xs.append(x); ys.append(y) |
|
|
x, y = self.Forward(urx, lly) |
|
|
xs.append(x); ys.append(y) |
|
58 |
return min(xs), min(ys), max(xs), max(ys) |
return min(xs), min(ys), max(xs), max(ys) |
59 |
|
|
60 |
|
def ForwardBBox(self, bbox): |
61 |
|
"""Return the bounding box of the corners of the bounding box bbox |
62 |
|
""" |
63 |
|
return self._transform_bbox(self.Forward, bbox) |
64 |
|
|
65 |
|
def InverseBBox(self, bbox): |
66 |
|
return self._transform_bbox(self.Inverse, bbox) |
67 |
|
|
68 |
def GetName(self): |
def GetName(self): |
69 |
|
"""Return the name of the projection.""" |
70 |
return self.name |
return self.name |
71 |
|
|
72 |
def SetName(self, name): |
def Label(self): |
73 |
if isinstance(name, StringTypes): |
if self.epsg: |
74 |
self.name = name |
return "EPSG % 5s %s" % (self.epsg, self.name) |
75 |
|
return self.name |
76 |
|
|
77 |
|
def EPSGCode(self): |
78 |
|
"""Return the EPSG code as a string or None if there is none""" |
79 |
|
return self.epsg |
80 |
|
|
81 |
def GetParameter(self, param): |
def GetParameter(self, param): |
82 |
|
"""Return the projection value for the given parameter. |
83 |
|
|
84 |
|
If 'param' exists as a valid parameter return the associated |
85 |
|
value as a string. If the parameter does not have a value (like |
86 |
|
e.g. the 'south' parameter for utm) then the value is the |
87 |
|
parameter name itself. |
88 |
|
|
89 |
|
If the parameter doesn't exist return an empty string. |
90 |
|
""" |
91 |
|
|
92 |
for pair in self.params: |
for pair in self.params: |
93 |
p, v = pair.split("=") |
if "=" in pair: |
94 |
|
p, v = pair.split("=") |
95 |
|
else: |
96 |
|
p = v = pair |
97 |
if p == param: |
if p == param: |
98 |
return v |
return v |
99 |
|
|
100 |
return "" |
return "" |
101 |
|
|
102 |
def GetAllParameters(self): |
def GetAllParameters(self): |
103 |
|
"""Return list of 'parameter=value' strings""" |
104 |
return self.params |
return self.params |
105 |
|
|
106 |
|
def GetProjectedUnits(self): |
107 |
|
if self.GetParameter("proj") in [ 'latlong', 'longlat' ]: |
108 |
|
return PROJ_UNITS_DEGREES |
109 |
|
else: |
110 |
|
return PROJ_UNITS_METERS |
111 |
|
|
112 |
def __repr__(self): |
def __repr__(self): |
113 |
return self.name + ": " + repr(self.params) |
return self.name + ": " + repr(self.params) |
114 |
|
|
115 |
class ProjFile: |
|
116 |
|
class ProjFile(Publisher): |
117 |
|
|
118 |
def __init__(self, filename): |
def __init__(self, filename): |
119 |
self.projs = {} |
"""Intialize the ProjFile. |
120 |
|
|
121 |
|
filename -- name of the file that this ProjFile represents. |
122 |
|
""" |
123 |
|
|
124 |
|
self.__projs = [] |
125 |
|
|
126 |
|
self.SetFilename(filename) |
127 |
|
|
|
self.SetFileName(filename) |
|
|
|
|
128 |
def Add(self, proj): |
def Add(self, proj): |
129 |
self.projs[proj] = 0 |
"""Add the projection to the end of the file.""" |
130 |
|
self.__projs.append(proj) |
131 |
|
self.issue(PROJECTION_ADDED, proj) |
132 |
|
|
133 |
def Remove(self, proj): |
def Remove(self, proj): |
134 |
if self.projs.has_key(proj): |
"""Remove the object proj from the projection file. |
|
del self.projs[proj] |
|
135 |
|
|
136 |
def GetFileName(self): |
Raises a ValueError is proj is not found. |
137 |
return self.filename |
""" |
138 |
|
self.__projs.remove(proj) |
139 |
|
self.issue(PROJECTION_REMOVED, proj) |
140 |
|
|
141 |
def SetFileName(self, filename): |
def Replace(self, oldproj, newproj): |
142 |
self.filename = filename |
"""Replace the object 'oldproj' with 'newproj'. |
143 |
|
|
144 |
|
Raises ValueError if oldproj is not in the file. |
145 |
|
""" |
146 |
|
self.__projs[self.__projs.index(oldproj)] = newproj |
147 |
|
self.issue(PROJECTION_REPLACED, oldproj, newproj) |
148 |
|
|
149 |
|
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 |
def GetProjections(self): |
def GetProjections(self): |
161 |
return self.projs.keys() |
"""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 |
|
|