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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 695 by jonathan, Wed Apr 16 16:39:36 2003 UTC revision 1797 by bh, Thu Oct 9 10:00:06 2003 UTC
# Line 1  Line 1 
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  #  #
# Line 7  Line 7 
7    
8  __version__ = "$Revision$"  __version__ = "$Revision$"
9    
10    from Thuban import _
11    
12    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):
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)          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          self.params = params
         self.name = name  
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
# Line 35  class Projection(BaseProjection): Line 52  class Projection(BaseProjection):
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."""
60          return self.name          return self.name
61    
62      def GetParameters(self):      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          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):      def __repr__(self):
94          return repr(self.params)          return self.name + ": " + repr(self.params)
95    
96  class ProjFile:  class ProjFile:
97    
98      def __init__(self, filename):      def __init__(self, filename):
99          self.projs = []          """Intialize the ProjFile.
100    
101            filename -- name of the file that this ProjFile represents.
102            """
103    
104          self.SetFileName(filename)          self.__projs = []
105    
106            self.SetFilename(filename)
107            
108      def Add(self, proj):      def Add(self, proj):
109          self.projs.append(proj)          """Add the given projection to the end of the file. If 'proj'
110            already exists, it is replaced in the same location as the
111            original projection.
112            """
113    
114            try:
115                #
116                # see if the projection already exists.
117                # this only works if Projection doesn't override __eq__
118                #
119                self.__projs[self.__projs.index(proj)] = proj
120            except ValueError:
121                self.__projs.append(proj)
122    
123        def Remove(self, proj):
124            """Remove the object proj from the projection file.
125    
126      def GetFileName(self):          Raises a ValueError is proj is not found.
127          return self.filename          """
128    
129            self.__projs.remove(proj)
130    
131      def SetFileName(self, filename):      def Replace(self, oldproj, newproj):
132          self.filename = filename          """Replace the object 'oldproj' with 'newproj'.
133            
134            Raises ValueError if oldproj is not in the file.
135            """
136    
137            #
138            # see if the projection already exists.
139            # this only works if Projection doesn't override __eq__
140            #
141            self.__projs[self.__projs.index(oldproj)] = newproj
142    
143        def GetFilename(self):
144            """Return the filename where the ProjFile was read or will be
145            written to.
146            """
147    
148            return self.__filename
149    
150        def SetFilename(self, filename):
151            """Set the filename where the ProjFile will be written to."""
152            self.__filename = filename
153    
154      def GetProjections(self):      def GetProjections(self):
155          return self.projs          """Return a list of the projections in the order they were read
156            from the file or will be written.
157    
158            This is not a deep copy list, so any modifications made to the
159            Projection objects will be written to the file.
160            """
161    
162            return self.__projs
163    

Legend:
Removed from v.695  
changed lines
  Added in v.1797

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26