/[thuban]/branches/WIP-pyshapelib-bramz/test/test_proj.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/test/test_proj.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1756 - (hide annotations)
Fri Sep 26 14:03:51 2003 UTC (21 years, 5 months ago) by bh
Original Path: trunk/thuban/test/test_proj.py
File MIME type: text/x-python
File size: 8271 byte(s)
(sample_projfile, sample_projfile2): Use
'projectionlist' as the name in the document type declaration just
as it is done now in the files thuban would write
(sample_projfile, sample_projfile_data): Fix spelling of
"Mercator"
(TestProjFile.doTestWrite): Validate the written and the expected
XML data
(TestProjFile): Derive from ValidationTest so that we can run xml
validation tests

1 bh 1259 # Copyright (c) 2002, 2003 by Intevation GmbH
2 bh 333 # 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     """
9     Test the Thuban-specific Projection class
10     """
11    
12     __version__ = "$Revision$"
13     # $Source$
14     # $Id$
15    
16     import unittest
17 jonathan 719 import os
18 bh 333
19 bh 1756 import xmlsupport
20 bh 333 import support
21     support.initthuban()
22    
23 bh 1684 from Thuban import _
24 jonathan 698 from Thuban.Model.proj import Projection, ProjFile
25 bh 333
26 jonathan 698 import Thuban.Model.resource as resource
27 bh 333
28 bh 1259 from xmlsupport import sax_eventlist
29 jonathan 698
30 jonathan 719 from xml.sax import SAXParseException
31 jonathan 698
32 jonathan 719
33 bh 333 class TestProjection(unittest.TestCase, support.FloatComparisonMixin):
34    
35     """Test cases for the Thuban-specific Projection class
36     """
37    
38     def test(self):
39     """Test Projection"""
40 jonathan 761 params = ["zone=26", "proj=utm", "ellps=clrk66"]
41     proj = Projection(params)
42     self.assertEquals(proj.params, params)
43 bh 333
44     # It's not clear whether this value is really the correct one
45     # but a test failure here probably still means a bug somewhere
46     self.assertFloatSeqEqual(proj.Forward(0, 0),
47     [3623101.8103431347, 0.0],
48     epsilon = 1e-5)
49     self.assertFloatSeqEqual(proj.Inverse(3623101.8103431347, 0.0),
50     [-0.00065775699878736467, 0])
51    
52     self.assertFloatSeqEqual(proj.ForwardBBox((0, 0, 2, 2)),
53     (3620891.3077618643, 0.0,
54     3875381.8535437919, 252962.10480170773),
55     epsilon = 1e-5)
56    
57 jonathan 761 # GetName()
58     self.assertEquals(proj.GetName(), _("Unknown"))
59    
60     # GetParameter()
61 jonathan 714 self.assertEquals(proj.GetParameter("zone"), "26")
62     self.assertEquals(proj.GetParameter("proj"), "utm")
63     self.assertEquals(proj.GetParameter("ellps"), "clrk66")
64     self.assertEquals(proj.GetParameter("hallo"), "")
65 bh 333
66 jonathan 761 # GetAllParameters()
67     self.assertEquals(proj.GetAllParameters(), params)
68 bh 333
69 jonathan 761 # GetName()
70     proj = Projection(params, "MyName")
71     self.assertEquals(proj.GetName(), "MyName")
72    
73    
74 jonathan 698 sample_projfile = '''\
75     <?xml version="1.0" encoding="UTF-8"?>
76 bh 1756 <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
77 jonathan 698 <projectionlist>
78 bh 1756 <projection name="Transverse Mercator">
79 jonathan 698 <parameter value="proj=tmerc"/>
80     <parameter value="ellps=clrk66"/>
81     <parameter value="lat_0=90w"/>
82     <parameter value="lon_0=90w"/>
83     <parameter value="k=1"/>
84     </projection>
85 bh 1756 <projection name="Transverse Mercator">
86 jonathan 698 <parameter value="proj=tmerc"/>
87     <parameter value="ellps=clrk66"/>
88     <parameter value="lat_0=30w"/>
89     <parameter value="lon_0=30w"/>
90     <parameter value="k=1"/>
91     </projection>
92 bh 1756 <projection name="Universal Transverse Mercator">
93 jonathan 698 <parameter value="proj=utm"/>
94     <parameter value="ellps=clrk66"/>
95     <parameter value="zone=1"/>
96     </projection>
97     </projectionlist>
98     '''
99    
100 bh 1756 sample_projfile_data = [("Transverse Mercator", ["proj=tmerc",
101 jonathan 698 "ellps=clrk66",
102     "lat_0=90w",
103     "lon_0=90w",
104     "k=1"]),
105 bh 1756 ("Transverse Mercator", ["proj=tmerc",
106 jonathan 698 "ellps=clrk66",
107     "lat_0=30w",
108     "lon_0=30w",
109     "k=1"]),
110 bh 1756 ("Universal Transverse Mercator", ["proj=utm",
111 jonathan 698 "ellps=clrk66",
112     "zone=1"])]
113    
114     sample_projfile2 = '''\
115     <?xml version="1.0" encoding="UTF-8"?>
116 bh 1756 <!DOCTYPE projectionlist SYSTEM "projfile.dtd">
117 jonathan 698 <projectionlist>
118     </projectionlist>
119     '''
120    
121     sample_projfile_data2 = []
122    
123 bh 1756 class TestProjFile(unittest.TestCase, support.FileTestMixin,
124     xmlsupport.ValidationTest):
125 jonathan 698
126     """Test cases for reading and writing projection files.
127     """
128    
129     def compare_xml(self, xml1, xml2):
130     self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2))
131    
132 jonathan 742 def test(self):
133     """Test ProjFile"""
134    
135     proj0 = Projection(["proj=tmerc", "ellps=clrk66"])
136     proj1 = Projection(["proj=utm", "ellps=clrk66"])
137 bh 1687 proj2 = Projection(["proj=lcc", "ellps=clrk66",
138     "lat_1=0", "lat_2=20"])
139 jonathan 742
140     eq = self.assertEquals
141    
142 jonathan 761
143 jonathan 742 #
144     # __init__()
145     # GetFilename()
146     # SetFilename()
147     #
148     for name in ["", "hello_world"]:
149     projFile = ProjFile(name)
150     eq(projFile.GetFilename(), name)
151    
152     projFile.SetFilename("XXX")
153     projFile.SetFilename(name)
154     eq(projFile.GetFilename(), name)
155    
156     # initial number of projections should be 0
157     eq(len(projFile.GetProjections()), 0)
158    
159     #
160     # Add()
161     # Remove()
162     #
163     projFile.Add(proj0)
164     eq(len(projFile.GetProjections()), 1)
165     projFile.Remove(proj0)
166     eq(len(projFile.GetProjections()), 0)
167    
168     # try to remove something that doesn't exist
169 jonathan 761 self.assertRaises(ValueError, projFile.Remove, None)
170 jonathan 742 self.assertRaises(ValueError, projFile.Remove, proj0)
171    
172     projFile.Add(proj0)
173     projFile.Add(proj1)
174     projFile.Add(proj2)
175     eq(len(projFile.GetProjections()), 3)
176    
177     # GetProjections() -- tests order
178     projs = projFile.GetProjections()
179     eq(projs[0], proj0)
180     eq(projs[1], proj1)
181     eq(projs[2], proj2)
182    
183 jonathan 761 projFile.Remove(proj2)
184     projFile.Remove(proj1)
185 jonathan 752
186 jonathan 761 # Replace()
187     projFile.Replace(proj0, proj1)
188     projs = projFile.GetProjections()
189     eq(projs[0], proj1)
190    
191     # replace a non-existent projection
192     self.assertRaises(ValueError, projFile.Replace, None, proj2)
193     self.assertRaises(ValueError, projFile.Replace, proj0, proj2)
194    
195 jonathan 698 def testRead(self):
196 jonathan 1167 """Test read_proj_file"""
197 jonathan 698
198     self.doTestRead(sample_projfile_data, sample_projfile)
199     self.doTestRead(sample_projfile_data2, sample_projfile2)
200    
201 jonathan 719 #
202     # file doesn't exist
203     #
204     self.assertRaises(IOError,
205 jonathan 1167 resource.read_proj_file, self.temp_file_name("nonexistent.proj"))
206 jonathan 719
207     #
208     # file isn't readable
209     #
210     filename = self.temp_file_name("projfile.proj")
211     file = open(filename, "w")
212     file.close()
213     os.chmod(filename, 0200) # write-only
214 jonathan 1167 self.assertRaises(IOError, resource.read_proj_file, filename)
215 jonathan 719 os.chmod(filename, 0600) # read/write so we reuse the file
216    
217     #
218     # file has invalid XML (or none at all)
219     #
220     filename = self.temp_file_name("projfile.proj")
221     file = open(filename, "w")
222     file.close()
223    
224 jonathan 1167 self.assertRaises(SAXParseException, resource.read_proj_file, filename)
225 jonathan 719
226 jonathan 698 def testWrite(self):
227 jonathan 1167 """Test write_proj_file"""
228 jonathan 698
229     self.doTestWrite(sample_projfile_data, sample_projfile)
230     self.doTestWrite(sample_projfile_data2, sample_projfile2)
231    
232     def doTestWrite(self, data, expected):
233    
234     filename = self.temp_file_name("projfile.proj")
235    
236     pf = ProjFile(filename)
237     for proj in data:
238     pf.Add(Projection(proj[1], proj[0]))
239    
240 jonathan 1167 resource.write_proj_file(pf)
241 jonathan 698
242     file = open(filename)
243     written_contents = file.read()
244     file.close()
245     self.compare_xml(written_contents, expected)
246 bh 1756 self.validate_data(written_contents)
247     self.validate_data(expected)
248 jonathan 698
249     def doTestRead(self, data, input):
250    
251     filename = self.temp_file_name("projfile.proj")
252     file = open(filename, "w")
253     file.write(input)
254     file.close()
255    
256 jonathan 1167 pf = resource.read_proj_file(filename)
257 jonathan 698
258     eq = self.assertEquals
259    
260 jonathan 742 eq(pf.GetFilename(), filename)
261 jonathan 698
262     for proj, d in zip(pf.GetProjections(), data):
263     eq(proj.GetName(), d[0])
264 jonathan 714 for param in proj.GetAllParameters():
265 jonathan 698 self.assert_(param in d[1])
266    
267    
268 bh 333 if __name__ == "__main__":
269     unittest.main()

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26