/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/Model/proj.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/Thuban/Model/proj.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1802 - (show annotations)
Thu Oct 9 15:18:35 2003 UTC (21 years, 5 months ago) by bh
Original Path: trunk/thuban/Thuban/Model/proj.py
File MIME type: text/x-python
File size: 4261 byte(s)
(ProjFile.Add): Do not check whether the
projection is already in the list. This is *a lot* faster when
loading files with hundreds of projections since it saves a linear
search. OTOH this will allow adding the same projection to the
user.proj file multiple times in the projection dialog but we'll
deal with that later

1 # Copyright (c) 2001, 2003 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 Thuban import _
11
12 from types import StringTypes
13
14 import Projection
15 BaseProjection = Projection.Projection
16 del Projection
17
18 PROJ_UNITS_METERS = 1
19 PROJ_UNITS_DEGREES = 2
20
21 class Projection(BaseProjection):
22
23 """A proj4 projection object that remembers the parameters"""
24
25 def __init__(self, params, name = None):
26 """Initialize the Projection with a list of
27 'parameter=value' strings and an optional name. If 'name' is
28 None, the name will be set to 'Unknown' in the local language.
29 """
30
31 BaseProjection.__init__(self, params)
32
33 if name is None:
34 self.name = _("Unknown")
35 elif isinstance(name, StringTypes):
36 self.name = name
37
38 self.params = params
39
40 def ForwardBBox(self, bbox):
41 """Return the bounding box of the corners of the bounding box bbox
42 """
43 # This is not really the correct way to determine the bbox of a
44 # projected shape, but for now it works well enough
45 llx, lly, urx, ury = bbox
46 xs = []; ys = []
47 x, y = self.Forward(llx, lly)
48 xs.append(x); ys.append(y)
49 x, y = self.Forward(llx, ury)
50 xs.append(x); ys.append(y)
51 x, y = self.Forward(urx, ury)
52 xs.append(x); ys.append(y)
53 x, y = self.Forward(urx, lly)
54 xs.append(x); ys.append(y)
55
56 return min(xs), min(ys), max(xs), max(ys)
57
58 def GetName(self):
59 """Return the name of the projection."""
60 return self.name
61
62 def GetParameter(self, param):
63 """Return the projection value for the given parameter.
64
65 If 'param' exists as a valid parameter return the associated
66 value as a string. If the parameter does not have a value (like
67 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:
74 if "=" in pair:
75 p, v = pair.split("=")
76 else:
77 p = v = pair
78 if p == param:
79 return v
80
81 return ""
82
83 def GetAllParameters(self):
84 """Return list of 'parameter=value' strings"""
85 return self.params
86
87 def GetProjectedUnits(self):
88 if self.GetParameter("proj") == "latlong":
89 return PROJ_UNITS_DEGREES
90 else:
91 return PROJ_UNITS_METERS
92
93 def __repr__(self):
94 return self.name + ": " + repr(self.params)
95
96 class ProjFile:
97
98 def __init__(self, filename):
99 """Intialize the ProjFile.
100
101 filename -- name of the file that this ProjFile represents.
102 """
103
104 self.__projs = []
105
106 self.SetFilename(filename)
107
108 def Add(self, proj):
109 """Add the projection to the end of the file."""
110 self.__projs.append(proj)
111
112 def Remove(self, proj):
113 """Remove the object proj from the projection file.
114
115 Raises a ValueError is proj is not found.
116 """
117
118 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):
133 """Return the filename where the ProjFile was read or will be
134 written to.
135 """
136
137 return self.__filename
138
139 def SetFilename(self, filename):
140 """Set the filename where the ProjFile will be written to."""
141 self.__filename = filename
142
143 def GetProjections(self):
144 """Return a list of the projections in the order they were read
145 from the file or will be written.
146
147 This is not a deep copy list, so any modifications made to the
148 Projection objects will be written to the file.
149 """
150
151 return self.__projs
152

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26