/[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 1684 - (hide annotations)
Thu Aug 28 18:55:01 2003 UTC (21 years, 6 months ago) by bh
Original Path: trunk/thuban/test/test_proj.py
File MIME type: text/x-python
File size: 8064 byte(s)
Import things from Thuban after calling
initthuban

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