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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1230 - (hide annotations)
Wed Jun 18 14:46:43 2003 UTC (21 years, 8 months ago) by jonathan
Original Path: trunk/thuban/Thuban/Model/resource.py
File MIME type: text/x-python
File size: 4726 byte(s)
First check if we can import
        the gdalwarp Thuban extension before checking for gdal.
        Also added some comments.

1 jonathan 696 # Copyright (c) 2003 by Intevation GmbH
2     # Authors:
3     # Jonathan Coles <[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     import os
9 frank 1132 import os.path
10    
11 jonathan 696 import Thuban
12 jonathan 875 from Thuban import _
13    
14 frank 1149 from Thuban.Lib.fileutil import get_application_dir
15    
16 jonathan 1161 from Thuban.Model.xmlreader import XMLReader
17     from Thuban.Model.xmlwriter import XMLWriter
18 jonathan 696 from Thuban.Model.proj import Projection, ProjFile
19 jonathan 727 from xml.sax import SAXParseException
20 jonathan 696
21     projdir = \
22     os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Projections")
23    
24    
25     PROJ_EXT = ".proj"
26    
27 jonathan 1161 has_gdal_support = lambda: False
28     try:
29 jonathan 1230 # first try to see if our extension module is there. if it
30     # wasn't even compiled, we obviously don't have gdal support.
31     import gdalwarp
32    
33     # now try to import the python extension. if this doesn't
34     # exist then we can't do anything.
35 jonathan 1161 import gdal
36 jonathan 1230
37 jonathan 1161 """Return True if the gdal library is available."""
38     has_gdal_support = lambda: True
39     except ImportError:
40     pass
41    
42     def read_proj_file(filename):
43 jonathan 718 """Read a single .proj file and return a ProjFile object.
44    
45     Raises IOError if the file cannot be opened.
46     Raises OSError if the file cannot be read.
47     Raises SAXParseException if the file is not valid XML.
48     """
49 jonathan 696
50 jonathan 718 handler = ProjFileReader()
51     handler.read(filename)
52     return handler.GetProjFile()
53 jonathan 696
54 jonathan 1161 def write_proj_file(pf):
55 jonathan 727 """Write a single .proj file
56    
57     Raises IOError if the file cannot be written.
58     """
59    
60 jonathan 696 saver = ProjFileSaver(pf)
61 jonathan 739 saver.write(pf.GetFilename())
62 jonathan 696
63 jonathan 1161 def get_proj_files(dir):
64 jonathan 696 """Read all .proj files in the given directory and
65     return a list of ProjFile objects.
66     """
67    
68     list = []
69     try:
70 jonathan 718 dirlist = os.listdir(dir)
71 jonathan 696 except OSError:
72 jonathan 718 pass # if we can't get a directory listing just return []
73     else:
74     for file in filter(lambda s: s.endswith(PROJ_EXT), dirlist):
75     try:
76     filename = os.path.join(dir, file)
77 jonathan 1179 list.append(read_proj_file(filename))
78 jonathan 718 except (OSError, IOError, SAXParseException):
79     pass # just move onto the next file
80 jonathan 696
81     return list
82    
83 jonathan 1161 def get_system_proj_files():
84 jonathan 696 """Return a list of ProjFile objects from files that are
85     supplied by Thuban.
86 jonathan 727
87     If no files could not be opened return a list with one
88     empty projection file set to store data in the default file.
89 jonathan 696 """
90 jonathan 727 filename = os.path.join(projdir, "defaults.proj")
91     try:
92 jonathan 1179 return [read_proj_file(filename)]
93 jonathan 727 except (OSError, IOError, SAXParseException):
94     return [ProjFile(filename)]
95 jonathan 696
96 jonathan 1161 def get_user_proj_files():
97 jonathan 727 """Return a list of ProjFile objects from files that are user-defined.
98 jonathan 696
99 jonathan 727 If no files could not be opened return a list with one
100     empty projection file set to store data in the default file.
101     """
102    
103 jonathan 1189 usrdir = get_application_dir()
104 jonathan 727 filename = os.path.join(usrdir, "user.proj")
105     try:
106 jonathan 1179 return [read_proj_file(filename)]
107 jonathan 727 except (OSError, IOError, SAXParseException):
108     return [ProjFile(filename)]
109    
110 jonathan 709 class ProjFileReader(XMLReader):
111 jonathan 696
112 jonathan 709 def __init__(self):
113     XMLReader.__init__(self)
114     self.__pf = ProjFile("")
115    
116     XMLReader.AddDispatchers(self,
117     {'projection': ("start_projection", "end_projection"),
118     'parameter': ("start_parameter", None)})
119    
120     def read(self, file_or_filename):
121     XMLReader.read(self, file_or_filename)
122    
123 jonathan 739 self.__pf.SetFilename(XMLReader.GetFilename(self))
124 jonathan 696
125     def start_projection(self, name, qname, attrs):
126     self.params = []
127 jonathan 875 self.name = self.encode(attrs.get((None, 'name'), _("Unknown")))
128 jonathan 696
129     def end_projection(self, name, qname):
130 jonathan 709 self.__pf.Add(Projection(self.params, self.name))
131 jonathan 696
132     def start_parameter(self, name, qname, attrs):
133     s = attrs.get((None, 'value'))
134     s = str(s) # we can't handle unicode in proj
135     self.params.append(s)
136    
137     def GetProjFile(self):
138 jonathan 709 return self.__pf
139 jonathan 696
140 jonathan 709 class ProjFileSaver(XMLWriter):
141 jonathan 696
142     def __init__(self, pf):
143 jonathan 709 XMLWriter.__init__(self)
144     self.__pf = pf
145 jonathan 696
146     def write(self, file_or_filename):
147 jonathan 709 XMLWriter.write(self, file_or_filename)
148 jonathan 696
149 jonathan 875 self.write_header("projfile", "projfile.dtd")
150 jonathan 709 self.write_projfile(self.__pf)
151 jonathan 696 self.close()
152    
153     def write_projfile(self, pf):
154    
155     self.open_element("projectionlist")
156    
157     for p in pf.GetProjections():
158     self.open_element("projection", {"name": p.GetName()})
159    
160 jonathan 709 for param in p.GetAllParameters():
161 jonathan 696 self.write_element("parameter", {"value": param})
162    
163     self.close_element("projection")
164    
165     self.close_element("projectionlist")
166    
167    

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26