1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | #TEST |
---|
4 | |
---|
5 | import unittest |
---|
6 | from math import sqrt |
---|
7 | |
---|
8 | |
---|
9 | from least_squares import * |
---|
10 | from Numeric import allclose, array, transpose |
---|
11 | |
---|
12 | from coordinate_transforms.geo_reference import Geo_reference |
---|
13 | from combine_pts import * |
---|
14 | from load_mesh.loadASCII import import_points_file |
---|
15 | |
---|
16 | class Test_combine_pts(unittest.TestCase): |
---|
17 | |
---|
18 | def setUp(self): |
---|
19 | pass |
---|
20 | |
---|
21 | def tearDown(self): |
---|
22 | pass |
---|
23 | |
---|
24 | |
---|
25 | def test_combine_rectangular_points_files(self): |
---|
26 | from load_mesh.loadASCII import export_points_file |
---|
27 | import tempfile |
---|
28 | import os |
---|
29 | |
---|
30 | |
---|
31 | # create a fine .pts file |
---|
32 | fine_dict = {} |
---|
33 | fine_dict['pointlist']=[[0,1],[0,4],[4,4],[4,1]] |
---|
34 | att_dict = {} |
---|
35 | att_dict['elevation'] = [10,40,80,50] |
---|
36 | att_dict['sonic'] = [1,4,8,5] |
---|
37 | fine_dict['attributelist'] = att_dict |
---|
38 | fine_dict['geo_reference'] = Geo_reference(56,2.0,1.0) |
---|
39 | |
---|
40 | fine_file = tempfile.mktemp(".pts") |
---|
41 | export_points_file(fine_file, fine_dict) |
---|
42 | |
---|
43 | |
---|
44 | # create a coarse .pts file |
---|
45 | coarse_dict = {} |
---|
46 | coarse_dict['pointlist']=[[0,1],[2,1],[0,-2]] |
---|
47 | att_dict = {} |
---|
48 | att_dict['elevation'] = [10,30,-20] |
---|
49 | att_dict['sonic'] = [1,3,-2] |
---|
50 | coarse_dict['attributelist'] = att_dict |
---|
51 | coarse_dict['geo_reference'] = Geo_reference(56,4.0,3.0) |
---|
52 | |
---|
53 | coarse_file = tempfile.mktemp(".pts") |
---|
54 | export_points_file(coarse_file, coarse_dict) |
---|
55 | |
---|
56 | out_file = tempfile.mktemp(".pts") |
---|
57 | |
---|
58 | combine_rectangular_points_files(fine_file,coarse_file,out_file) |
---|
59 | |
---|
60 | #clean up |
---|
61 | os.remove(fine_file) |
---|
62 | os.remove(coarse_file) |
---|
63 | |
---|
64 | results = import_points_file(out_file, |
---|
65 | delimiter = ',') |
---|
66 | answer = [[2.0, 0.0], |
---|
67 | [0.0, 1.0], |
---|
68 | [0.0, 4.0], |
---|
69 | [4.0, 4.0], |
---|
70 | [4.0, 1.0]] |
---|
71 | #print "results",results |
---|
72 | #print "answer",answer |
---|
73 | |
---|
74 | self.failUnless(len(results['pointlist']) == len(answer), |
---|
75 | 'final number of points wrong. failed.') |
---|
76 | |
---|
77 | assert allclose(results['pointlist'], answer) |
---|
78 | assert allclose(results['attributelist']['sonic'], [ -2., |
---|
79 | 1., 4., |
---|
80 | 8., 5.]) |
---|
81 | assert allclose(results['attributelist']['elevation'],[ -20., |
---|
82 | 10., 40., |
---|
83 | 80., 50.]) |
---|
84 | |
---|
85 | self.failUnless(results['geo_reference'] == fine_dict['geo_reference'], |
---|
86 | ' failed.') |
---|
87 | #clean up |
---|
88 | os.remove(out_file) |
---|
89 | |
---|
90 | def test_combine_rectangular_points_filesII(self): |
---|
91 | from load_mesh.loadASCII import export_points_file |
---|
92 | import tempfile |
---|
93 | import os |
---|
94 | |
---|
95 | # create a fine .pts file |
---|
96 | fine_dict = {} |
---|
97 | fine_dict['pointlist']=[[0,1],[0,4],[4,4],[4,1],[3,1],[2,2],[1,3],[3,4]] |
---|
98 | att_dict = {} |
---|
99 | fine_dict['attributelist'] = att_dict |
---|
100 | fine_dict['geo_reference'] = Geo_reference(56,2.0,1.0) |
---|
101 | |
---|
102 | fine_file = tempfile.mktemp(".pts") |
---|
103 | export_points_file(fine_file, fine_dict) |
---|
104 | |
---|
105 | |
---|
106 | # create a coarse .pts file |
---|
107 | coarse_dict = {} |
---|
108 | coarse_dict['pointlist']=[[0,1],[0,0],[0.5,0.5],[1,1], |
---|
109 | [1.5,1.5],[2,1],[0,-2],[100,10], |
---|
110 | [-20,4],[-50,5],[60,70]] |
---|
111 | att_dict = {} |
---|
112 | coarse_dict['attributelist'] = att_dict |
---|
113 | coarse_dict['geo_reference'] = Geo_reference(56,4.0,3.0) |
---|
114 | |
---|
115 | coarse_file = tempfile.mktemp(".pts") |
---|
116 | export_points_file(coarse_file, coarse_dict) |
---|
117 | |
---|
118 | out_file = tempfile.mktemp(".pts") |
---|
119 | |
---|
120 | combine_rectangular_points_files(fine_file,coarse_file,out_file) |
---|
121 | |
---|
122 | #clean up |
---|
123 | os.remove(fine_file) |
---|
124 | os.remove(coarse_file) |
---|
125 | |
---|
126 | results = import_points_file(out_file, |
---|
127 | delimiter = ',') |
---|
128 | answer = [[2.0, 0.0], |
---|
129 | [102.,12.], |
---|
130 | [-18.,6.], |
---|
131 | [-48.,7.], |
---|
132 | [62.,72.], |
---|
133 | [0.0, 1.0], |
---|
134 | [0.0, 4.0], |
---|
135 | [4.0, 4.0], |
---|
136 | [4.0, 1.0], |
---|
137 | [3.,1.], |
---|
138 | [2.,2.], |
---|
139 | [1.,3.], |
---|
140 | [3.,4.]] |
---|
141 | #print "results",results['pointlist'] |
---|
142 | #print "answer",answer |
---|
143 | #print "len(results['pointlist']",len(results['pointlist']) |
---|
144 | #print "len(answer)",len(answer) |
---|
145 | |
---|
146 | self.failUnless(len(results['pointlist']) == len(answer), |
---|
147 | 'final number of points wrong. failed.') |
---|
148 | assert allclose(results['pointlist'], answer) |
---|
149 | |
---|
150 | self.failUnless(results['geo_reference'] == fine_dict['geo_reference'], |
---|
151 | ' failed.') |
---|
152 | #clean up |
---|
153 | os.remove(out_file) |
---|
154 | |
---|
155 | def test_combine_rectangular_points_files_errors(self): |
---|
156 | from load_mesh.loadASCII import export_points_file |
---|
157 | import tempfile |
---|
158 | import os |
---|
159 | |
---|
160 | # create a fine .pts file |
---|
161 | fine_dict = {} |
---|
162 | fine_dict['pointlist']=[[0,1],[0,4],[4,4],[4,1]] |
---|
163 | att_dict = {} |
---|
164 | att_dict['elevation'] = [1,4,8,5] |
---|
165 | att_dict['pneumonic'] = [1,4,8,5] |
---|
166 | fine_dict['attributelist'] = att_dict |
---|
167 | fine_dict['geo_reference'] = Geo_reference(56,2.0,1.0) |
---|
168 | |
---|
169 | fine_file = tempfile.mktemp(".pts") |
---|
170 | export_points_file(fine_file, fine_dict) |
---|
171 | |
---|
172 | |
---|
173 | # create a coarse .pts file |
---|
174 | coarse_dict = {} |
---|
175 | coarse_dict['pointlist']=[[0,1],[2,1],[0,-2]] |
---|
176 | att_dict = {} |
---|
177 | att_dict['elevation'] = [1,3,-2] |
---|
178 | att_dict['sonic'] = [1,3,-2] |
---|
179 | coarse_dict['attributelist'] = att_dict |
---|
180 | coarse_dict['geo_reference'] = Geo_reference(56,4.0,3.0) |
---|
181 | |
---|
182 | coarse_file = tempfile.mktemp(".pts") |
---|
183 | export_points_file(coarse_file, coarse_dict) |
---|
184 | |
---|
185 | out_file = tempfile.mktemp(".pts") |
---|
186 | try: |
---|
187 | combine_rectangular_points_files(fine_file,coarse_file,out_file) |
---|
188 | except AttributeError: |
---|
189 | pass |
---|
190 | else: |
---|
191 | self.failUnless(0 ==1, |
---|
192 | 'bad pts files did not raise error!') |
---|
193 | #clean up |
---|
194 | os.remove(fine_file) |
---|
195 | os.remove(coarse_file) |
---|
196 | |
---|
197 | def test_reduce_points_to_mesh_extent(self): |
---|
198 | from load_mesh.loadASCII import export_points_file, export_mesh_file |
---|
199 | import tempfile |
---|
200 | import os |
---|
201 | x_origin = -45435345. |
---|
202 | y_origin = 433432432. |
---|
203 | # create a fine .pts file |
---|
204 | fine_dict = {} |
---|
205 | fine_dict['pointlist']=[[1.,1.], |
---|
206 | [1.,7.], |
---|
207 | [7.,1.], |
---|
208 | [11.,11.], |
---|
209 | [7.,8.], |
---|
210 | [8.,8.], |
---|
211 | [9.,8.]] |
---|
212 | att_dict = {} |
---|
213 | att_dict['elevation'] = [10,40,80,50,78,78,45] |
---|
214 | att_dict['sonic'] = [1,4,8,5,56,34,213] |
---|
215 | fine_dict['attributelist'] = att_dict |
---|
216 | fine_dict['geo_reference'] = Geo_reference(56,x_origin,y_origin) |
---|
217 | |
---|
218 | points_file = tempfile.mktemp(".pts") |
---|
219 | export_points_file(points_file, fine_dict) |
---|
220 | |
---|
221 | |
---|
222 | # create a coarse .pts file |
---|
223 | mesh = {} |
---|
224 | mesh['vertices']=[[0,0], |
---|
225 | [0,3], |
---|
226 | [3,3], |
---|
227 | [3,0], |
---|
228 | [1,2] |
---|
229 | ] |
---|
230 | mesh['vertex_attributes']=[[], |
---|
231 | [], |
---|
232 | [], |
---|
233 | [], |
---|
234 | [] |
---|
235 | ] |
---|
236 | mesh['geo_reference'] = Geo_reference(56,x_origin+7.,y_origin+7.) |
---|
237 | |
---|
238 | mesh_file = tempfile.mktemp(".tsh") |
---|
239 | export_mesh_file(mesh_file, mesh) |
---|
240 | |
---|
241 | out_file = tempfile.mktemp(".pts") |
---|
242 | |
---|
243 | reduce_points_to_mesh_extent(points_file,mesh_file,out_file) |
---|
244 | |
---|
245 | #clean up |
---|
246 | #os.remove(fine_file) |
---|
247 | #os.remove(coarse_file) |
---|
248 | |
---|
249 | results = import_points_file(out_file, |
---|
250 | delimiter = ',') |
---|
251 | answer = [ |
---|
252 | [7.,8.], |
---|
253 | [8.,8.], |
---|
254 | [9.,8.]] |
---|
255 | #print "results",results['pointlist'] |
---|
256 | #print "answer",answer |
---|
257 | |
---|
258 | self.failUnless(len(results['pointlist']) == len(answer), |
---|
259 | 'final number of points wrong. failed.') |
---|
260 | assert allclose(results['pointlist'],answer) |
---|
261 | |
---|
262 | answer = [78., 78.,45.] |
---|
263 | |
---|
264 | self.failUnless(len(results['attributelist']['elevation']) == len(answer), |
---|
265 | 'final number of points wrong. failed.') |
---|
266 | assert allclose(results['attributelist']['elevation'], answer) |
---|
267 | |
---|
268 | #clean up |
---|
269 | os.remove(out_file) |
---|
270 | |
---|
271 | #FIXME do test for add points files |
---|
272 | |
---|
273 | #------------------------------------------------------------- |
---|
274 | if __name__ == "__main__": |
---|
275 | suite = unittest.makeSuite(Test_combine_pts,'test') |
---|
276 | #suite = unittest.makeSuite(Test_combine_pts,'test_reduce_points_to_mesh_extent') |
---|
277 | runner = unittest.TextTestRunner(verbosity=1) |
---|
278 | runner.run(suite) |
---|
279 | |
---|
280 | |
---|
281 | |
---|
282 | |
---|
283 | |
---|