/[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 1687 - (hide annotations)
Fri Aug 29 10:02:16 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: 8115 byte(s)
Add some missing parameters to projections. Proj complains about
them on windows but for some reason not on Linux.

* test/test_save.py (SaveSessionTest.testLayerProjection): Add
missing required projection parameters

* test/test_proj.py (TestProjFile.test): Add missing required
projection parameters

* test/test_load_0_8.py (TestLayerProjection.file_contents)
(TestLayerProjection.test): Add missing required projection
parameters and tests for them

* test/test_load.py (TestLayerProjection.file_contents)
(TestLayerProjection.test): Add missing required projection
parameters and tests for them

* test/test_layer.py (TestLayer.test_base_layer): Add missing
required projection parameters

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 bh 1687 proj2 = Projection(["proj=lcc", "ellps=clrk66",
136     "lat_1=0", "lat_2=20"])
137 jonathan 742
138     eq = self.assertEquals
139    
140 jonathan 761
141 jonathan 742 #
142     # __init__()
143     # GetFilename()
144     # SetFilename()
145     #
146     for name in ["", "hello_world"]:
147     projFile = ProjFile(name)
148     eq(projFile.GetFilename(), name)
149    
150     projFile.SetFilename("XXX")
151     projFile.SetFilename(name)
152     eq(projFile.GetFilename(), name)
153    
154     # initial number of projections should be 0
155     eq(len(projFile.GetProjections()), 0)
156    
157     #
158     # Add()
159     # Remove()
160     #
161     projFile.Add(proj0)
162     eq(len(projFile.GetProjections()), 1)
163     projFile.Remove(proj0)
164     eq(len(projFile.GetProjections()), 0)
165    
166     # try to remove something that doesn't exist
167 jonathan 761 self.assertRaises(ValueError, projFile.Remove, None)
168 jonathan 742 self.assertRaises(ValueError, projFile.Remove, proj0)
169    
170     projFile.Add(proj0)
171     projFile.Add(proj1)
172     projFile.Add(proj2)
173     eq(len(projFile.GetProjections()), 3)
174    
175     # GetProjections() -- tests order
176     projs = projFile.GetProjections()
177     eq(projs[0], proj0)
178     eq(projs[1], proj1)
179     eq(projs[2], proj2)
180    
181 jonathan 761 projFile.Remove(proj2)
182     projFile.Remove(proj1)
183 jonathan 752
184 jonathan 761 # Replace()
185     projFile.Replace(proj0, proj1)
186     projs = projFile.GetProjections()
187     eq(projs[0], proj1)
188    
189     # replace a non-existent projection
190     self.assertRaises(ValueError, projFile.Replace, None, proj2)
191     self.assertRaises(ValueError, projFile.Replace, proj0, proj2)
192    
193 jonathan 698 def testRead(self):
194 jonathan 1167 """Test read_proj_file"""
195 jonathan 698
196     self.doTestRead(sample_projfile_data, sample_projfile)
197     self.doTestRead(sample_projfile_data2, sample_projfile2)
198    
199 jonathan 719 #
200     # file doesn't exist
201     #
202     self.assertRaises(IOError,
203 jonathan 1167 resource.read_proj_file, self.temp_file_name("nonexistent.proj"))
204 jonathan 719
205     #
206     # file isn't readable
207     #
208     filename = self.temp_file_name("projfile.proj")
209     file = open(filename, "w")
210     file.close()
211     os.chmod(filename, 0200) # write-only
212 jonathan 1167 self.assertRaises(IOError, resource.read_proj_file, filename)
213 jonathan 719 os.chmod(filename, 0600) # read/write so we reuse the file
214    
215     #
216     # file has invalid XML (or none at all)
217     #
218     filename = self.temp_file_name("projfile.proj")
219     file = open(filename, "w")
220     file.close()
221    
222 jonathan 1167 self.assertRaises(SAXParseException, resource.read_proj_file, filename)
223 jonathan 719
224 jonathan 698 def testWrite(self):
225 jonathan 1167 """Test write_proj_file"""
226 jonathan 698
227     self.doTestWrite(sample_projfile_data, sample_projfile)
228     self.doTestWrite(sample_projfile_data2, sample_projfile2)
229    
230     def doTestWrite(self, data, expected):
231    
232     filename = self.temp_file_name("projfile.proj")
233    
234     pf = ProjFile(filename)
235     for proj in data:
236     pf.Add(Projection(proj[1], proj[0]))
237    
238 jonathan 1167 resource.write_proj_file(pf)
239 jonathan 698
240     file = open(filename)
241     written_contents = file.read()
242     file.close()
243     self.compare_xml(written_contents, expected)
244    
245     def doTestRead(self, data, input):
246    
247     filename = self.temp_file_name("projfile.proj")
248     file = open(filename, "w")
249     file.write(input)
250     file.close()
251    
252 jonathan 1167 pf = resource.read_proj_file(filename)
253 jonathan 698
254     eq = self.assertEquals
255    
256 jonathan 742 eq(pf.GetFilename(), filename)
257 jonathan 698
258     for proj, d in zip(pf.GetProjections(), data):
259     eq(proj.GetName(), d[0])
260 jonathan 714 for param in proj.GetAllParameters():
261 jonathan 698 self.assert_(param in d[1])
262    
263    
264 bh 333 if __name__ == "__main__":
265     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