14 |
# $Id$ |
# $Id$ |
15 |
|
|
16 |
import unittest |
import unittest |
17 |
|
import os |
18 |
|
|
19 |
import support |
import support |
20 |
support.initthuban() |
support.initthuban() |
25 |
|
|
26 |
from test_save import sax_eventlist |
from test_save import sax_eventlist |
27 |
|
|
28 |
|
from xml.sax import SAXParseException |
29 |
|
|
30 |
|
|
31 |
class TestProjection(unittest.TestCase, support.FloatComparisonMixin): |
class TestProjection(unittest.TestCase, support.FloatComparisonMixin): |
32 |
|
|
51 |
3875381.8535437919, 252962.10480170773), |
3875381.8535437919, 252962.10480170773), |
52 |
epsilon = 1e-5) |
epsilon = 1e-5) |
53 |
|
|
54 |
|
self.assertEquals(proj.GetParameter("zone"), "26") |
55 |
|
self.assertEquals(proj.GetParameter("proj"), "utm") |
56 |
|
self.assertEquals(proj.GetParameter("ellps"), "clrk66") |
57 |
|
self.assertEquals(proj.GetParameter("hallo"), "") |
58 |
|
|
59 |
|
|
60 |
sample_projfile = '''\ |
sample_projfile = '''\ |
114 |
def compare_xml(self, xml1, xml2): |
def compare_xml(self, xml1, xml2): |
115 |
self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2)) |
self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2)) |
116 |
|
|
117 |
|
def test(self): |
118 |
|
"""Test ProjFile""" |
119 |
|
|
120 |
|
proj0 = Projection(["proj=tmerc", "ellps=clrk66"]) |
121 |
|
proj1 = Projection(["proj=utm", "ellps=clrk66"]) |
122 |
|
proj2 = Projection(["proj=lcc", "ellps=clrk66"]) |
123 |
|
|
124 |
|
eq = self.assertEquals |
125 |
|
|
126 |
|
# |
127 |
|
# __init__() |
128 |
|
# GetFilename() |
129 |
|
# SetFilename() |
130 |
|
# |
131 |
|
for name in ["", "hello_world"]: |
132 |
|
projFile = ProjFile(name) |
133 |
|
eq(projFile.GetFilename(), name) |
134 |
|
|
135 |
|
projFile.SetFilename("XXX") |
136 |
|
projFile.SetFilename(name) |
137 |
|
eq(projFile.GetFilename(), name) |
138 |
|
|
139 |
|
# initial number of projections should be 0 |
140 |
|
eq(len(projFile.GetProjections()), 0) |
141 |
|
|
142 |
|
# |
143 |
|
# Add() |
144 |
|
# Remove() |
145 |
|
# |
146 |
|
projFile.Add(proj0) |
147 |
|
eq(len(projFile.GetProjections()), 1) |
148 |
|
projFile.Remove(proj0) |
149 |
|
eq(len(projFile.GetProjections()), 0) |
150 |
|
|
151 |
|
# try to remove something that doesn't exist |
152 |
|
self.assertRaises(ValueError, projFile.Remove, proj0) |
153 |
|
|
154 |
|
projFile.Add(proj0) |
155 |
|
projFile.Add(proj1) |
156 |
|
projFile.Add(proj2) |
157 |
|
eq(len(projFile.GetProjections()), 3) |
158 |
|
|
159 |
|
# GetProjections() -- tests order |
160 |
|
projs = projFile.GetProjections() |
161 |
|
eq(projs[0], proj0) |
162 |
|
eq(projs[1], proj1) |
163 |
|
eq(projs[2], proj2) |
164 |
|
|
165 |
def testRead(self): |
def testRead(self): |
166 |
"""Test ReadProjFile""" |
"""Test ReadProjFile""" |
167 |
|
|
168 |
self.doTestRead(sample_projfile_data, sample_projfile) |
self.doTestRead(sample_projfile_data, sample_projfile) |
169 |
self.doTestRead(sample_projfile_data2, sample_projfile2) |
self.doTestRead(sample_projfile_data2, sample_projfile2) |
170 |
|
|
171 |
|
# |
172 |
|
# file doesn't exist |
173 |
|
# |
174 |
|
self.assertRaises(IOError, |
175 |
|
resource.ReadProjFile, self.temp_file_name("nonexistent.proj")) |
176 |
|
|
177 |
|
# |
178 |
|
# file isn't readable |
179 |
|
# |
180 |
|
filename = self.temp_file_name("projfile.proj") |
181 |
|
file = open(filename, "w") |
182 |
|
file.close() |
183 |
|
os.chmod(filename, 0200) # write-only |
184 |
|
self.assertRaises(IOError, resource.ReadProjFile, filename) |
185 |
|
os.chmod(filename, 0600) # read/write so we reuse the file |
186 |
|
|
187 |
|
# |
188 |
|
# file has invalid XML (or none at all) |
189 |
|
# |
190 |
|
filename = self.temp_file_name("projfile.proj") |
191 |
|
file = open(filename, "w") |
192 |
|
file.close() |
193 |
|
|
194 |
|
self.assertRaises(SAXParseException, resource.ReadProjFile, filename) |
195 |
|
|
196 |
def testWrite(self): |
def testWrite(self): |
197 |
"""Test WriteProjFile""" |
"""Test WriteProjFile""" |
198 |
|
|
225 |
|
|
226 |
eq = self.assertEquals |
eq = self.assertEquals |
227 |
|
|
228 |
eq(pf.GetFileName(), filename) |
eq(pf.GetFilename(), filename) |
229 |
|
|
230 |
for proj, d in zip(pf.GetProjections(), data): |
for proj, d in zip(pf.GetProjections(), data): |
231 |
eq(proj.GetName(), d[0]) |
eq(proj.GetName(), d[0]) |
232 |
for param in proj.GetParameters(): |
for param in proj.GetAllParameters(): |
233 |
self.assert_(param in d[1]) |
self.assert_(param in d[1]) |
234 |
|
|
235 |
|
|