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

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

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

revision 1786 by bh, Wed Oct 8 10:39:11 2003 UTC revision 1933 by bh, Tue Nov 11 16:37:53 2003 UTC
# Line 14  __version__ = "$Revision$" Line 14  __version__ = "$Revision$"
14    
15  import os  import os
16  import os.path  import os.path
17    import weakref
18    
19  import Thuban  import Thuban
20  from Thuban import _  from Thuban import _
# Line 46  try: Line 47  try:
47  except ImportError:  except ImportError:
48      pass      pass
49    
50    projfile_cache = weakref.WeakValueDictionary()
51    
52    def clear_proj_file_cache():
53        """Clear the cache of ProjFile objects maintained by read_proj_file.
54    
55        This function is probably only useful for the test suite.
56        """
57        projfile_cache.clear()
58    
59  def read_proj_file(filename):  def read_proj_file(filename):
60      """Read a .proj file and return a ProjFile object and warnings      """Read a .proj file and return a ProjFile object and warnings
61    
62      The return value is a tuple with the ProjFile object and a list of      The return value is a tuple with the ProjFile object and a list of
63      strings with warnings messages that might have been generated by the      strings with warnings messages that might have been generated by the
64      proj ifle parser.      proj file parser.
65    
66        The objects returned cached so that reading the same file
67        (identified by its absolute name) several times yields the same
68        ProjFile object. The cache uses weak references so the objects will
69        be removed from the cache once the last reference an object in the
70        cache is removed.
71    
72      Raises IOError if the file cannot be opened, OSError if the file      Raises IOError if the file cannot be opened, OSError if the file
73      cannot be read and SAXParseException if the file is not valid XML.      cannot be read and SAXParseException if the file is not valid XML.
74      """      """
75      handler = ProjFileReader()      filename = os.path.abspath(filename)
76      handler.read(filename)      if filename in projfile_cache:
77      return handler.GetProjFile(), handler.GetWarnings()          return projfile_cache[filename], []
78        else:
79            handler = ProjFileReader()
80            handler.read(filename)
81            proj_file = handler.GetProjFile()
82            projfile_cache[filename] = proj_file
83            return proj_file, handler.GetWarnings()
84    
85  def write_proj_file(pf):  def write_proj_file(pf):
86      """Write a single .proj file      """Write a single .proj file
# Line 70  def write_proj_file(pf): Line 92  def write_proj_file(pf):
92      saver.write(pf.GetFilename())      saver.write(pf.GetFilename())
93    
94    
95  def get_system_proj_file():  # Constants for the get_system_proj_file function
96      """Return the standard projections and a list with warnings  DEFAULT_PROJ_FILE = "defaults.proj"
97    EPSG_PROJ_FILE = "epsg.proj"
98      The projections read from the default thuban projection file  
99      (usually in Resources/Projections/defaults.proj). The return value  def get_system_proj_file(filename):
100      is a tuple with the projections in a ProjFile object and a list of      """Return the projections from the indicated file and a list with warnings
101      strings with warning messages. The warnings list is usually empty  
102      but may contain messages about ignored errors.      The filename argument should be the name of a file in the directory
103        with Thuban's default projection files (Resources/Projections/). If
104        possible callers should not use hardwired string literal for the
105        name to avoid unnecessary duplication. Instead they should use one
106        of the predefined constants, currently DEFAULT_PROJ_FILE or
107        EPSG_PROJ_FILE.
108    
109        The return value is a tuple with the projections in a ProjFile
110        object and a list of strings with warning messages. The warnings
111        list is usually empty but may contain messages about ignored errors.
112    
113      If the file could could not be opened return an empty projection      If the file could could not be opened return an empty projection
114      file object set to store data in the default file.      file object set to store data in the indicated default file.
115      """      """
116      filename = os.path.join(projdir, "defaults.proj")      fullname = os.path.join(projdir, filename)
117      try:      try:
118          return read_proj_file(filename)          return read_proj_file(fullname)
119      except (OSError, IOError, SAXParseException), val:      except (OSError, IOError, SAXParseException), val:
120          msg = _('Could not read "%s": %s') % (filename, str(val))          msg = _('Could not read "%s": %s') % (fullname, str(val))
121          return ProjFile(filename), [msg]          return ProjFile(fullname), [msg]
122    
123  def get_user_proj_file():  def get_user_proj_file():
124      """Return the user's projections and a list with warnings      """Return the user's projections and a list with warnings
# Line 105  def get_user_proj_file(): Line 136  def get_user_proj_file():
136      filename = os.path.join(usrdir, "user.proj")      filename = os.path.join(usrdir, "user.proj")
137      try:      try:
138          return read_proj_file(filename)          return read_proj_file(filename)
139      except (OSError, IOError, SAXParseException):      except (OSError, IOError, SAXParseException), val:
140          msg = _('Could not read "%s": %s') % (filename, str(val))          msg = _('Could not read "%s": %s') % (filename, str(val))
141          return ProjFile(filename), [msg]          return ProjFile(filename), [msg]
142    
# Line 132  class ProjFileReader(XMLReader): Line 163  class ProjFileReader(XMLReader):
163          if name is None:          if name is None:
164              name = _("Unknown")              name = _("Unknown")
165          self.name = name          self.name = name
166            self.epsg = self.encode(attrs.get((None, 'epsg')))
167    
168      def end_projection(self, name, qname):      def end_projection(self, name, qname):
169          try:          try:
170              proj = Projection(self.params, self.name)              proj = Projection(self.params, self.name, epsg = self.epsg)
171          except IOError, val:          except IOError, val:
172              self.warnings.append(_('Error in projection "%s": %s')              self.warnings.append(_('Error in projection "%s": %s')
173                                   % (self.name, str(val)))                                   % (self.name, str(val)))
# Line 173  class ProjFileSaver(XMLWriter): Line 205  class ProjFileSaver(XMLWriter):
205          self.open_element("projectionlist")          self.open_element("projectionlist")
206    
207          for p in pf.GetProjections():          for p in pf.GetProjections():
208              self.open_element("projection", {"name": p.GetName()})              attrs = {"name": p.GetName()}
209                if p.EPSGCode():
210                    attrs["epsg"] = p.EPSGCode()
211                self.open_element("projection", attrs)
212    
213              for param in p.GetAllParameters():              for param in p.GetAllParameters():
214                  self.write_element("parameter", {"value": param})                  self.write_element("parameter", {"value": param})

Legend:
Removed from v.1786  
changed lines
  Added in v.1933

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26