1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | |
---|
4 | import unittest |
---|
5 | import os |
---|
6 | from Numeric import zeros, array, allclose, concatenate |
---|
7 | from math import sqrt, pi |
---|
8 | import tempfile |
---|
9 | |
---|
10 | from geospatial_data import * |
---|
11 | from coordinate_transforms.geo_reference import Geo_reference, TitleError |
---|
12 | from coordinate_transforms.redfearn import degminsec2decimal_degrees |
---|
13 | |
---|
14 | |
---|
15 | class Test_Geospatial_data(unittest.TestCase): |
---|
16 | def setUp(self): |
---|
17 | pass |
---|
18 | |
---|
19 | def tearDown(self): |
---|
20 | pass |
---|
21 | |
---|
22 | |
---|
23 | def test_0(self): |
---|
24 | """Basic points |
---|
25 | """ |
---|
26 | from coordinate_transforms.geo_reference import Geo_reference |
---|
27 | |
---|
28 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
29 | G = Geospatial_data(points) |
---|
30 | |
---|
31 | assert allclose(G.data_points, [[1.0, 2.1], [3.0, 5.3]]) |
---|
32 | |
---|
33 | #Check defaults |
---|
34 | assert G.attributes is None |
---|
35 | |
---|
36 | assert G.geo_reference.zone == Geo_reference().zone |
---|
37 | assert G.geo_reference.xllcorner == Geo_reference().xllcorner |
---|
38 | assert G.geo_reference.yllcorner == Geo_reference().yllcorner |
---|
39 | |
---|
40 | |
---|
41 | def test_1(self): |
---|
42 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
43 | attributes = [2, 4] |
---|
44 | G = Geospatial_data(points, attributes) |
---|
45 | |
---|
46 | assert G.attributes.keys()[0] == 'attribute' |
---|
47 | assert allclose(G.attributes.values()[0], [2, 4]) |
---|
48 | |
---|
49 | |
---|
50 | def test_2(self): |
---|
51 | from coordinate_transforms.geo_reference import Geo_reference |
---|
52 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
53 | attributes = [2, 4] |
---|
54 | G = Geospatial_data(points, attributes, |
---|
55 | geo_reference=Geo_reference(56, 100, 200)) |
---|
56 | |
---|
57 | assert G.geo_reference.zone == 56 |
---|
58 | assert G.geo_reference.xllcorner == 100 |
---|
59 | assert G.geo_reference.yllcorner == 200 |
---|
60 | |
---|
61 | |
---|
62 | def test_get_attributes_1(self): |
---|
63 | from coordinate_transforms.geo_reference import Geo_reference |
---|
64 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
65 | attributes = [2, 4] |
---|
66 | G = Geospatial_data(points, attributes, |
---|
67 | geo_reference=Geo_reference(56, 100, 200)) |
---|
68 | |
---|
69 | |
---|
70 | P = G.get_data_points(absolute=False) |
---|
71 | assert allclose(P, [[1.0, 2.1], [3.0, 5.3]]) |
---|
72 | |
---|
73 | P = G.get_data_points(absolute=True) |
---|
74 | assert allclose(P, [[101.0, 202.1], [103.0, 205.3]]) |
---|
75 | |
---|
76 | V = G.get_attributes() #Simply get them |
---|
77 | assert allclose(V, [2, 4]) |
---|
78 | |
---|
79 | V = G.get_attributes('attribute') #Get by name |
---|
80 | assert allclose(V, [2, 4]) |
---|
81 | |
---|
82 | def test_get_attributes_2(self): |
---|
83 | """Multiple attributes |
---|
84 | """ |
---|
85 | |
---|
86 | from coordinate_transforms.geo_reference import Geo_reference |
---|
87 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
88 | attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]} |
---|
89 | G = Geospatial_data(points, attributes, |
---|
90 | geo_reference=Geo_reference(56, 100, 200), |
---|
91 | default_attribute_name='a1') |
---|
92 | |
---|
93 | |
---|
94 | P = G.get_data_points(absolute=False) |
---|
95 | assert allclose(P, [[1.0, 2.1], [3.0, 5.3]]) |
---|
96 | |
---|
97 | V = G.get_attributes() #Get default attribute |
---|
98 | assert allclose(V, [2, 4]) |
---|
99 | |
---|
100 | V = G.get_attributes('a0') #Get by name |
---|
101 | assert allclose(V, [0, 0]) |
---|
102 | |
---|
103 | V = G.get_attributes('a1') #Get by name |
---|
104 | assert allclose(V, [2, 4]) |
---|
105 | |
---|
106 | V = G.get_attributes('a2') #Get by name |
---|
107 | assert allclose(V, [79.4, -7]) |
---|
108 | |
---|
109 | try: |
---|
110 | V = G.get_attributes('hdnoatedu') #Invalid |
---|
111 | except AssertionError: |
---|
112 | pass |
---|
113 | else: |
---|
114 | raise 'Should have raised exception' |
---|
115 | |
---|
116 | def test_get_data_points(self): |
---|
117 | points_ab = [[12.5,34.7],[-4.5,-60.0]] |
---|
118 | x_p = -10 |
---|
119 | y_p = -40 |
---|
120 | geo_ref = Geo_reference(56, x_p, y_p) |
---|
121 | points_rel = geo_ref.change_points_geo_ref(points_ab) |
---|
122 | |
---|
123 | spatial = Geospatial_data(points_rel, geo_reference=geo_ref) |
---|
124 | |
---|
125 | results = spatial.get_data_points(absolute=False) |
---|
126 | |
---|
127 | assert allclose(results, points_rel) |
---|
128 | |
---|
129 | x_p = -1770 |
---|
130 | y_p = 4.01 |
---|
131 | geo_ref = Geo_reference(56, x_p, y_p) |
---|
132 | points_rel = geo_ref.change_points_geo_ref(points_ab) |
---|
133 | results = spatial.get_data_points \ |
---|
134 | ( geo_reference=geo_ref) |
---|
135 | |
---|
136 | assert allclose(results, points_rel) |
---|
137 | |
---|
138 | |
---|
139 | def test_set_geo_reference(self): |
---|
140 | points_ab = [[12.5,34.7],[-4.5,-60.0]] |
---|
141 | x_p = -10 |
---|
142 | y_p = -40 |
---|
143 | geo_ref = Geo_reference(56, x_p, y_p) |
---|
144 | points_rel = geo_ref.change_points_geo_ref(points_ab) |
---|
145 | spatial = Geospatial_data(points_rel) |
---|
146 | # since the geo_ref wasn't set |
---|
147 | assert not allclose( points_ab, spatial.get_data_points(absolute=True)) |
---|
148 | |
---|
149 | spatial = Geospatial_data(points_rel, geo_reference=geo_ref) |
---|
150 | assert allclose( points_ab, spatial.get_data_points(absolute=True)) |
---|
151 | |
---|
152 | x_p = 10 |
---|
153 | y_p = 400 |
---|
154 | new_geo_ref = Geo_reference(56, x_p, y_p) |
---|
155 | spatial.set_geo_reference(new_geo_ref) |
---|
156 | assert allclose( points_ab, spatial.get_data_points(absolute=True)) |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | def test_conversions_to_points_dict(self): |
---|
161 | """test conversions to points_dict |
---|
162 | """ |
---|
163 | |
---|
164 | from coordinate_transforms.geo_reference import Geo_reference |
---|
165 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
166 | attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]} |
---|
167 | G = Geospatial_data(points, attributes, |
---|
168 | geo_reference=Geo_reference(56, 100, 200), |
---|
169 | default_attribute_name='a1') |
---|
170 | |
---|
171 | |
---|
172 | points_dict = geospatial_data2points_dictionary(G) |
---|
173 | |
---|
174 | assert points_dict.has_key('pointlist') |
---|
175 | assert points_dict.has_key('attributelist') |
---|
176 | assert points_dict.has_key('geo_reference') |
---|
177 | |
---|
178 | assert allclose( points_dict['pointlist'], points ) |
---|
179 | |
---|
180 | A = points_dict['attributelist'] |
---|
181 | assert A.has_key('a0') |
---|
182 | assert A.has_key('a1') |
---|
183 | assert A.has_key('a2') |
---|
184 | |
---|
185 | assert allclose( A['a0'], [0, 0] ) |
---|
186 | assert allclose( A['a1'], [2, 4] ) |
---|
187 | assert allclose( A['a2'], [79.4, -7] ) |
---|
188 | |
---|
189 | |
---|
190 | geo = points_dict['geo_reference'] |
---|
191 | assert geo is G.geo_reference |
---|
192 | |
---|
193 | |
---|
194 | def test_conversions_from_points_dict(self): |
---|
195 | """test conversions from points_dict |
---|
196 | """ |
---|
197 | |
---|
198 | from coordinate_transforms.geo_reference import Geo_reference |
---|
199 | |
---|
200 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
201 | attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]} |
---|
202 | |
---|
203 | points_dict = {} |
---|
204 | points_dict['pointlist'] = points |
---|
205 | points_dict['attributelist'] = attributes |
---|
206 | points_dict['geo_reference'] = Geo_reference(56, 100, 200) |
---|
207 | |
---|
208 | |
---|
209 | G = points_dictionary2geospatial_data(points_dict) |
---|
210 | |
---|
211 | P = G.get_data_points(absolute=False) |
---|
212 | assert allclose(P, [[1.0, 2.1], [3.0, 5.3]]) |
---|
213 | |
---|
214 | #V = G.get_attribute_values() #Get default attribute |
---|
215 | #assert allclose(V, [2, 4]) |
---|
216 | |
---|
217 | V = G.get_attributes('a0') #Get by name |
---|
218 | assert allclose(V, [0, 0]) |
---|
219 | |
---|
220 | V = G.get_attributes('a1') #Get by name |
---|
221 | assert allclose(V, [2, 4]) |
---|
222 | |
---|
223 | V = G.get_attributes('a2') #Get by name |
---|
224 | assert allclose(V, [79.4, -7]) |
---|
225 | |
---|
226 | def test_add(self): |
---|
227 | """ test the addition of two geospatical objects |
---|
228 | no geo_reference see next test |
---|
229 | """ |
---|
230 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
231 | attributes = {'depth':[2, 4], 'elevation':[6.1, 5]} |
---|
232 | attributes1 = {'depth':[2, 4], 'elevation':[2.5, 1]} |
---|
233 | G1 = Geospatial_data(points, attributes) |
---|
234 | G2 = Geospatial_data(points, attributes1) |
---|
235 | |
---|
236 | # g3 = geospatial_data2points_dictionary(G1) |
---|
237 | # print 'g3=', g3 |
---|
238 | |
---|
239 | G = G1 + G2 |
---|
240 | |
---|
241 | assert G.attributes.has_key('depth') |
---|
242 | assert G.attributes.has_key('elevation') |
---|
243 | assert allclose(G.attributes['depth'], [2, 4, 2, 4]) |
---|
244 | assert allclose(G.attributes['elevation'], [6.1, 5, 2.5, 1]) |
---|
245 | assert allclose(G.get_data_points(), [[1.0, 2.1], [3.0, 5.3], |
---|
246 | [1.0, 2.1], [3.0, 5.3]]) |
---|
247 | |
---|
248 | def test_add_with_geo (self): |
---|
249 | """ |
---|
250 | Difference in Geo_reference resolved |
---|
251 | """ |
---|
252 | points1 = [[1.0, 2.1], [3.0, 5.3]] |
---|
253 | points2 = [[5.0, 6.1], [6.0, 3.3]] |
---|
254 | attributes1 = [2, 4] |
---|
255 | attributes2 = [5, 76] |
---|
256 | geo_ref1= Geo_reference(55, 1.0, 2.0) |
---|
257 | geo_ref2 = Geo_reference(zone=55, |
---|
258 | xllcorner=0.1, |
---|
259 | yllcorner=3.0, |
---|
260 | datum='wgs84', |
---|
261 | projection='UTM', |
---|
262 | units='m') |
---|
263 | |
---|
264 | G1 = Geospatial_data(points1, attributes1, geo_ref1) |
---|
265 | G2 = Geospatial_data(points2, attributes2, geo_ref2) |
---|
266 | |
---|
267 | #Check that absolute values are as expected |
---|
268 | P1 = G1.get_data_points(absolute=True) |
---|
269 | assert allclose(P1, [[2.0, 4.1], [4.0, 7.3]]) |
---|
270 | |
---|
271 | P2 = G2.get_data_points(absolute=True) |
---|
272 | assert allclose(P2, [[5.1, 9.1], [6.1, 6.3]]) |
---|
273 | |
---|
274 | G = G1 + G2 |
---|
275 | |
---|
276 | assert allclose(G.get_geo_reference().get_xllcorner(), 0.1) |
---|
277 | assert allclose(G.get_geo_reference().get_yllcorner(), 2.0) |
---|
278 | |
---|
279 | P = G.get_data_points(absolute=True) |
---|
280 | |
---|
281 | P_relative = G.get_data_points(absolute=False) |
---|
282 | |
---|
283 | assert allclose(P_relative, P - [0.1, 2.0]) |
---|
284 | |
---|
285 | assert allclose(P, concatenate( (P1,P2) )) |
---|
286 | assert allclose(P, [[2.0, 4.1], [4.0, 7.3], |
---|
287 | [5.1, 9.1], [6.1, 6.3]]) |
---|
288 | |
---|
289 | |
---|
290 | |
---|
291 | |
---|
292 | |
---|
293 | def test_add_with_geo_absolute (self): |
---|
294 | """ |
---|
295 | Difference in Geo_reference resolved |
---|
296 | """ |
---|
297 | points1 = array([[2.0, 4.1], [4.0, 7.3]]) |
---|
298 | points2 = array([[5.1, 9.1], [6.1, 6.3]]) |
---|
299 | attributes1 = [2, 4] |
---|
300 | attributes2 = [5, 76] |
---|
301 | geo_ref1= Geo_reference(55, 1.0, 2.0) |
---|
302 | geo_ref2 = Geo_reference(55, 2.0, 3.0) |
---|
303 | |
---|
304 | |
---|
305 | |
---|
306 | G1 = Geospatial_data(points1 - [geo_ref1.get_xllcorner(), geo_ref1.get_yllcorner()], |
---|
307 | attributes1, geo_ref1) |
---|
308 | |
---|
309 | G2 = Geospatial_data(points2 - [geo_ref2.get_xllcorner(), geo_ref2.get_yllcorner()], |
---|
310 | attributes2, geo_ref2) |
---|
311 | |
---|
312 | #Check that absolute values are as expected |
---|
313 | P1 = G1.get_data_points(absolute=True) |
---|
314 | assert allclose(P1, points1) |
---|
315 | |
---|
316 | P1 = G1.get_data_points(absolute=False) |
---|
317 | assert allclose(P1, points1 - [geo_ref1.get_xllcorner(), geo_ref1.get_yllcorner()]) |
---|
318 | |
---|
319 | P2 = G2.get_data_points(absolute=True) |
---|
320 | assert allclose(P2, points2) |
---|
321 | |
---|
322 | P2 = G2.get_data_points(absolute=False) |
---|
323 | assert allclose(P2, points2 - [geo_ref2.get_xllcorner(), geo_ref2.get_yllcorner()]) |
---|
324 | |
---|
325 | G = G1 + G2 |
---|
326 | |
---|
327 | assert allclose(G.get_geo_reference().get_xllcorner(), 1.0) |
---|
328 | assert allclose(G.get_geo_reference().get_yllcorner(), 2.0) |
---|
329 | |
---|
330 | P = G.get_data_points(absolute=True) |
---|
331 | |
---|
332 | P_relative = G.get_data_points(absolute=False) |
---|
333 | |
---|
334 | assert allclose(P_relative, [[1.0, 2.1], [3.0, 5.3], [4.1, 7.1], [5.1, 4.3]]) |
---|
335 | |
---|
336 | assert allclose(P, concatenate( (points1,points2) )) |
---|
337 | |
---|
338 | |
---|
339 | |
---|
340 | |
---|
341 | def test_create_from_xya_file(self): |
---|
342 | """Check that object can be created from a points file (.pts and .xya) |
---|
343 | """ |
---|
344 | |
---|
345 | points = [[1.0, 2.1], [3.0, 5.3], [5.0, 6.1], [6.0, 3.3]] |
---|
346 | attributes = [2, 4, 5, 76] |
---|
347 | ''' |
---|
348 | # Use old pointsdict format |
---|
349 | pointsdict = {'pointlist': points, |
---|
350 | 'attributelist': {'att1': attributes, |
---|
351 | 'att2': array(attributes) + 1}} |
---|
352 | ''' |
---|
353 | att_dict = {'att1': attributes, |
---|
354 | 'att2': array(attributes) +1} |
---|
355 | |
---|
356 | # Create points as an xya file |
---|
357 | FN = 'test_points.xya' |
---|
358 | G1 = Geospatial_data(points, att_dict) |
---|
359 | G1.export_points_file(FN) |
---|
360 | # G1.export_points_file(ofile) |
---|
361 | |
---|
362 | #Create object from file |
---|
363 | G = Geospatial_data(file_name = FN) |
---|
364 | |
---|
365 | assert allclose(G.get_data_points(), points) |
---|
366 | assert allclose(G.get_attributes('att1'), attributes) |
---|
367 | assert allclose(G.get_attributes('att2'), array(attributes) + 1) |
---|
368 | |
---|
369 | os.remove(FN) |
---|
370 | |
---|
371 | def test_create_from_xya_file1(self): |
---|
372 | """ |
---|
373 | Check that object can be created from an Absolute xya file |
---|
374 | """ |
---|
375 | |
---|
376 | points = [[1.0, 2.1], [3.0, 5.3], [5.0, 6.1], [6.0, 3.3]] |
---|
377 | attributes = [2, 4, 5, 76] |
---|
378 | |
---|
379 | att_dict = {'att1': attributes, |
---|
380 | 'att2': array(attributes) +1} |
---|
381 | |
---|
382 | geo_ref = Geo_reference(56, 10, 5) |
---|
383 | |
---|
384 | # Create points as an xya file |
---|
385 | FN = 'test_points.xya' |
---|
386 | G1 = Geospatial_data(points, att_dict, geo_ref) |
---|
387 | |
---|
388 | G1.export_points_file(FN, absolute=True) |
---|
389 | |
---|
390 | #Create object from file |
---|
391 | G = Geospatial_data(file_name = FN) |
---|
392 | |
---|
393 | assert allclose(G.get_data_points(absolute=True), |
---|
394 | G1.get_data_points(absolute=True)) |
---|
395 | assert allclose(G.get_attributes('att1'), attributes) |
---|
396 | assert allclose(G.get_attributes('att2'), array(attributes) + 1) |
---|
397 | |
---|
398 | os.remove(FN) |
---|
399 | |
---|
400 | def test_loadxya(self): |
---|
401 | """ |
---|
402 | comma delimited |
---|
403 | """ |
---|
404 | fileName = tempfile.mktemp(".xya") |
---|
405 | file = open(fileName,"w") |
---|
406 | file.write("elevation , speed \n\ |
---|
407 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
408 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
409 | 1.0, 0.0, 10.4, 40.0\n") |
---|
410 | file.close() |
---|
411 | results = Geospatial_data(fileName, delimiter=',') |
---|
412 | os.remove(fileName) |
---|
413 | # print 'data', results.get_data_points() |
---|
414 | assert allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
415 | assert allclose(results.get_attributes(attribute_name='elevation'), [10.0, 0.0, 10.4]) |
---|
416 | assert allclose(results.get_attributes(attribute_name='speed'), [0.0, 10.0, 40.0]) |
---|
417 | |
---|
418 | def test_loadxya2(self): |
---|
419 | """ |
---|
420 | space delimited |
---|
421 | """ |
---|
422 | import os |
---|
423 | |
---|
424 | fileName = tempfile.mktemp(".xya") |
---|
425 | file = open(fileName,"w") |
---|
426 | file.write(" elevation speed \n\ |
---|
427 | 1.0 0.0 10.0 0.0\n\ |
---|
428 | 0.0 1.0 0.0 10.0\n\ |
---|
429 | 1.0 0.0 10.4 40.0\n") |
---|
430 | file.close() |
---|
431 | |
---|
432 | results = Geospatial_data(fileName, delimiter=' ') |
---|
433 | |
---|
434 | os.remove(fileName) |
---|
435 | |
---|
436 | assert allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
437 | assert allclose(results.get_attributes(attribute_name='elevation'), [10.0, 0.0, 10.4]) |
---|
438 | assert allclose(results.get_attributes(attribute_name='speed'), [0.0, 10.0, 40.0]) |
---|
439 | |
---|
440 | def test_loadxya3(self): |
---|
441 | """ |
---|
442 | space delimited |
---|
443 | """ |
---|
444 | import os |
---|
445 | |
---|
446 | fileName = tempfile.mktemp(".xya") |
---|
447 | file = open(fileName,"w") |
---|
448 | file.write(" elevation speed \n\ |
---|
449 | 1.0 0.0 10.0 0.0\n\ |
---|
450 | 0.0 1.0 0.0 10.0\n\ |
---|
451 | 1.0 0.0 10.4 40.0\n\ |
---|
452 | #geocrap\n\ |
---|
453 | 56\n\ |
---|
454 | 56.6\n\ |
---|
455 | 3\n") |
---|
456 | file.close() |
---|
457 | |
---|
458 | results = Geospatial_data(fileName, delimiter=' ') |
---|
459 | |
---|
460 | os.remove(fileName) |
---|
461 | assert allclose(results.get_data_points(absolute=False), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
462 | assert allclose(results.get_attributes(attribute_name='elevation'), [10.0, 0.0, 10.4]) |
---|
463 | assert allclose(results.get_attributes(attribute_name='speed'), [0.0, 10.0, 40.0]) |
---|
464 | |
---|
465 | def BADtest_loadxya4(self): |
---|
466 | """ |
---|
467 | comma delimited |
---|
468 | """ |
---|
469 | fileName = tempfile.mktemp(".xya") |
---|
470 | file = open(fileName,"w") |
---|
471 | file.write("elevation , speed \n\ |
---|
472 | 1.0, 0.0, splat, 0.0\n\ |
---|
473 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
474 | 1.0, 0.0, 10.4, 40.0\n") |
---|
475 | file.close() |
---|
476 | results = Geospatial_data(fileName, delimiter=',') |
---|
477 | os.remove(fileName) |
---|
478 | # print 'data', results.get_data_points() |
---|
479 | assert allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
480 | assert allclose(results.get_attributes(attribute_name='elevation'), ["splat", 0.0, 10.4]) |
---|
481 | assert allclose(results.get_attributes(attribute_name='speed'), [0.0, 10.0, 40.0]) |
---|
482 | |
---|
483 | def test_read_write_points_file_bad2(self): |
---|
484 | att_dict = {} |
---|
485 | pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
486 | att_dict['elevation'] = array([10.0, 0.0, 10.4]) |
---|
487 | att_dict['brightness'] = array([10.0, 0.0, 10.4]) |
---|
488 | geo_reference=Geo_reference(56,1.9,1.9) |
---|
489 | |
---|
490 | G = Geospatial_data(pointlist, att_dict, geo_reference) |
---|
491 | |
---|
492 | try: |
---|
493 | G.export_points_file("_???/yeah.xya") |
---|
494 | |
---|
495 | except IOError: |
---|
496 | pass |
---|
497 | else: |
---|
498 | msg = 'bad points file extension did not raise error!' |
---|
499 | raise msg |
---|
500 | # self.failUnless(0 == 1, |
---|
501 | # 'bad points file extension did not raise error!') |
---|
502 | |
---|
503 | def test_loadxy_bad(self): |
---|
504 | import os |
---|
505 | |
---|
506 | fileName = tempfile.mktemp(".xya") |
---|
507 | file = open(fileName,"w") |
---|
508 | file.write(" elevation \n\ |
---|
509 | 1.0 0.0 10.0 0.0\n\ |
---|
510 | 0.0 1.0 0.0 10.0\n\ |
---|
511 | 1.0 0.0 10.4 40.0\n") |
---|
512 | file.close() |
---|
513 | #print fileName |
---|
514 | try: |
---|
515 | results = Geospatial_data(fileName, delimiter=' ') |
---|
516 | except IOError: |
---|
517 | pass |
---|
518 | else: |
---|
519 | msg = 'bad xya file did not raise error!' |
---|
520 | raise msg |
---|
521 | # self.failUnless(0 == 1, |
---|
522 | # 'bad xya file did not raise error!') |
---|
523 | os.remove(fileName) |
---|
524 | |
---|
525 | def test_loadxy_bad2(self): |
---|
526 | import os |
---|
527 | |
---|
528 | fileName = tempfile.mktemp(".xya") |
---|
529 | file = open(fileName,"w") |
---|
530 | file.write("elevation\n\ |
---|
531 | 1.0 0.0 10.0 \n\ |
---|
532 | 0.0 1.0\n\ |
---|
533 | 1.0 \n") |
---|
534 | file.close() |
---|
535 | #print fileName |
---|
536 | try: |
---|
537 | results = Geospatial_data(fileName, delimiter=' ') |
---|
538 | except IOError: |
---|
539 | pass |
---|
540 | else: |
---|
541 | msg = 'bad xya file did not raise error!' |
---|
542 | raise msg |
---|
543 | os.remove(fileName) |
---|
544 | |
---|
545 | def test_loadxy_bad3(self): |
---|
546 | """ |
---|
547 | specifying wrong delimiter |
---|
548 | """ |
---|
549 | import os |
---|
550 | |
---|
551 | fileName = tempfile.mktemp(".xya") |
---|
552 | file = open(fileName,"w") |
---|
553 | file.write(" elevation , speed \n\ |
---|
554 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
555 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
556 | 1.0, 0.0, 10.4, 40.0\n") |
---|
557 | file.close() |
---|
558 | try: |
---|
559 | results = Geospatial_data(fileName, delimiter=' ') |
---|
560 | except IOError: |
---|
561 | pass |
---|
562 | else: |
---|
563 | msg = 'bad xya file did not raise error!' |
---|
564 | raise msg |
---|
565 | os.remove(fileName) |
---|
566 | |
---|
567 | def test_loadxy_bad4(self): |
---|
568 | """ |
---|
569 | specifying wrong delimiter |
---|
570 | """ |
---|
571 | import os |
---|
572 | fileName = tempfile.mktemp(".xya") |
---|
573 | file = open(fileName,"w") |
---|
574 | file.write(" elevation speed \n\ |
---|
575 | 1.0 0.0 10.0 0.0\n\ |
---|
576 | 0.0 1.0 0.0 10.0\n\ |
---|
577 | 1.0 0.0 10.4 40.0\n\ |
---|
578 | #geocrap\n\ |
---|
579 | 56\n\ |
---|
580 | 56.6\n\ |
---|
581 | 3\n" |
---|
582 | ) |
---|
583 | file.close() |
---|
584 | try: |
---|
585 | results = Geospatial_data(fileName, delimiter=',') |
---|
586 | except IOError: |
---|
587 | pass |
---|
588 | else: |
---|
589 | msg = 'bad xya file did not raise error!' |
---|
590 | raise msg |
---|
591 | |
---|
592 | os.remove(fileName) |
---|
593 | |
---|
594 | def test_loadxy_bad5(self): |
---|
595 | """ |
---|
596 | specifying wrong delimiter |
---|
597 | """ |
---|
598 | import os |
---|
599 | |
---|
600 | fileName = tempfile.mktemp(".xya") |
---|
601 | file = open(fileName,"w") |
---|
602 | file.write(" elevation speed \n\ |
---|
603 | 1.0 0.0 10.0 0.0\n\ |
---|
604 | 0.0 1.0 0.0 10.0\n\ |
---|
605 | 1.0 0.0 10.4 40.0\n\ |
---|
606 | #geocrap\n\ |
---|
607 | crap") |
---|
608 | file.close() |
---|
609 | try: |
---|
610 | # dict = import_points_file(fileName,delimiter=' ') |
---|
611 | # results = Geospatial_data() |
---|
612 | results = Geospatial_data(fileName, delimiter=' ', verbose=True) |
---|
613 | # results.import_points_file(fileName, delimiter=' ') |
---|
614 | except IOError: |
---|
615 | pass |
---|
616 | else: |
---|
617 | msg = 'bad xya file did not raise error!' |
---|
618 | raise msg |
---|
619 | |
---|
620 | # self.failUnless(0 ==1, |
---|
621 | # 'bad xya file did not raise error!') |
---|
622 | os.remove(fileName) |
---|
623 | |
---|
624 | def test_loadxy_bad_no_file_xya(self): |
---|
625 | import os |
---|
626 | |
---|
627 | fileName = tempfile.mktemp(".xya") |
---|
628 | try: |
---|
629 | results = Geospatial_data(fileName, delimiter=' ') |
---|
630 | except IOError: |
---|
631 | pass |
---|
632 | else: |
---|
633 | msg = 'imaginary file did not raise error!' |
---|
634 | raise msg |
---|
635 | |
---|
636 | # except IOError: |
---|
637 | # pass |
---|
638 | # else: |
---|
639 | # self.failUnless(0 == 1, |
---|
640 | # 'imaginary file did not raise error!') |
---|
641 | |
---|
642 | |
---|
643 | ###################### .XYA ############################## |
---|
644 | |
---|
645 | def test_export_xya_file(self): |
---|
646 | # dict = {} |
---|
647 | att_dict = {} |
---|
648 | pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
649 | att_dict['elevation'] = array([10.0, 0.0, 10.4]) |
---|
650 | att_dict['brightness'] = array([10.0, 0.0, 10.4]) |
---|
651 | # dict['attributelist'] = att_dict |
---|
652 | geo_reference=Geo_reference(56,1.9,1.9) |
---|
653 | |
---|
654 | |
---|
655 | fileName = tempfile.mktemp(".xya") |
---|
656 | G = Geospatial_data(pointlist, att_dict, geo_reference) |
---|
657 | G.export_points_file(fileName, False) |
---|
658 | |
---|
659 | # dict2 = import_points_file(fileName) |
---|
660 | results = Geospatial_data(file_name = fileName) |
---|
661 | #print "fileName",fileName |
---|
662 | os.remove(fileName) |
---|
663 | |
---|
664 | assert allclose(results.get_data_points(absolute=False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
665 | assert allclose(results.get_attributes(attribute_name='elevation'), [10.0, 0.0, 10.4]) |
---|
666 | answer = [10.0, 0.0, 10.4] |
---|
667 | assert allclose(results.get_attributes(attribute_name='brightness'), answer) |
---|
668 | #print "dict2['geo_reference']",dict2['geo_reference'] |
---|
669 | self.failUnless(results.get_geo_reference() == geo_reference, |
---|
670 | 'test_writepts failed. Test geo_reference') |
---|
671 | |
---|
672 | def test_export_xya_file2(self): |
---|
673 | """test absolute xya file |
---|
674 | """ |
---|
675 | att_dict = {} |
---|
676 | pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
677 | att_dict['elevation'] = array([10.0, 0.0, 10.4]) |
---|
678 | att_dict['brightness'] = array([10.0, 0.0, 10.4]) |
---|
679 | |
---|
680 | fileName = tempfile.mktemp(".xya") |
---|
681 | G = Geospatial_data(pointlist, att_dict) |
---|
682 | G.export_points_file(fileName) |
---|
683 | results = Geospatial_data(file_name = fileName) |
---|
684 | # dict2 = import_points_file(fileName) |
---|
685 | os.remove(fileName) |
---|
686 | |
---|
687 | assert allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
688 | assert allclose(results.get_attributes('elevation'), [10.0, 0.0, 10.4]) |
---|
689 | answer = [10.0, 0.0, 10.4] |
---|
690 | assert allclose(results.get_attributes('brightness'), answer) |
---|
691 | |
---|
692 | def test_export_xya_file3(self): |
---|
693 | """test absolute xya file with geo_ref |
---|
694 | """ |
---|
695 | att_dict = {} |
---|
696 | pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
697 | att_dict['elevation'] = array([10.0, 0.0, 10.4]) |
---|
698 | att_dict['brightness'] = array([10.0, 0.0, 10.4]) |
---|
699 | geo_reference=Geo_reference(56,1.9,1.9) |
---|
700 | |
---|
701 | |
---|
702 | fileName = tempfile.mktemp(".xya") |
---|
703 | G = Geospatial_data(pointlist, att_dict, geo_reference) |
---|
704 | |
---|
705 | G.export_points_file(fileName, absolute=True) |
---|
706 | |
---|
707 | results = Geospatial_data(file_name = fileName) |
---|
708 | os.remove(fileName) |
---|
709 | |
---|
710 | assert allclose(results.get_data_points(), |
---|
711 | [[2.9, 1.9],[1.9, 2.9],[2.9, 1.9]]) |
---|
712 | assert allclose(results.get_attributes(attribute_name='elevation'), |
---|
713 | [10.0, 0.0, 10.4]) |
---|
714 | answer = [10.0, 0.0, 10.4] |
---|
715 | assert allclose(results.get_attributes(attribute_name='brightness'), answer) |
---|
716 | self.failUnless(results.get_geo_reference() == geo_reference, |
---|
717 | 'test_writepts failed. Test geo_reference') |
---|
718 | |
---|
719 | |
---|
720 | |
---|
721 | def test_new_export_pts_file(self): |
---|
722 | att_dict = {} |
---|
723 | pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
724 | att_dict['elevation'] = array([10.1, 0.0, 10.4]) |
---|
725 | att_dict['brightness'] = array([10.0, 1.0, 10.4]) |
---|
726 | |
---|
727 | fileName = tempfile.mktemp(".pts") |
---|
728 | |
---|
729 | G = Geospatial_data(pointlist, att_dict) |
---|
730 | |
---|
731 | G.export_points_file(fileName) |
---|
732 | |
---|
733 | results = Geospatial_data(file_name = fileName) |
---|
734 | |
---|
735 | os.remove(fileName) |
---|
736 | |
---|
737 | assert allclose(results.get_data_points(),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
738 | assert allclose(results.get_attributes(attribute_name='elevation'), [10.1, 0.0, 10.4]) |
---|
739 | answer = [10.0, 1.0, 10.4] |
---|
740 | assert allclose(results.get_attributes(attribute_name='brightness'), answer) |
---|
741 | |
---|
742 | def test_new_export_absolute_pts_file(self): |
---|
743 | att_dict = {} |
---|
744 | pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
745 | att_dict['elevation'] = array([10.1, 0.0, 10.4]) |
---|
746 | att_dict['brightness'] = array([10.0, 1.0, 10.4]) |
---|
747 | geo_ref = Geo_reference(50, 25, 55) |
---|
748 | |
---|
749 | fileName = tempfile.mktemp(".pts") |
---|
750 | |
---|
751 | G = Geospatial_data(pointlist, att_dict, geo_ref) |
---|
752 | |
---|
753 | G.export_points_file(fileName, absolute=True) |
---|
754 | |
---|
755 | results = Geospatial_data(file_name = fileName) |
---|
756 | |
---|
757 | os.remove(fileName) |
---|
758 | |
---|
759 | assert allclose(results.get_data_points(), G.get_data_points(True)) |
---|
760 | assert allclose(results.get_attributes(attribute_name='elevation'), [10.1, 0.0, 10.4]) |
---|
761 | answer = [10.0, 1.0, 10.4] |
---|
762 | assert allclose(results.get_attributes(attribute_name='brightness'), answer) |
---|
763 | |
---|
764 | def test_loadpts(self): |
---|
765 | |
---|
766 | from Scientific.IO.NetCDF import NetCDFFile |
---|
767 | |
---|
768 | fileName = tempfile.mktemp(".pts") |
---|
769 | # NetCDF file definition |
---|
770 | outfile = NetCDFFile(fileName, 'w') |
---|
771 | |
---|
772 | # dimension definitions |
---|
773 | outfile.createDimension('number_of_points', 3) |
---|
774 | outfile.createDimension('number_of_dimensions', 2) #This is 2d data |
---|
775 | |
---|
776 | # variable definitions |
---|
777 | outfile.createVariable('points', Float, ('number_of_points', |
---|
778 | 'number_of_dimensions')) |
---|
779 | outfile.createVariable('elevation', Float, ('number_of_points',)) |
---|
780 | |
---|
781 | # Get handles to the variables |
---|
782 | points = outfile.variables['points'] |
---|
783 | elevation = outfile.variables['elevation'] |
---|
784 | |
---|
785 | points[0, :] = [1.0,0.0] |
---|
786 | elevation[0] = 10.0 |
---|
787 | points[1, :] = [0.0,1.0] |
---|
788 | elevation[1] = 0.0 |
---|
789 | points[2, :] = [1.0,0.0] |
---|
790 | elevation[2] = 10.4 |
---|
791 | |
---|
792 | outfile.close() |
---|
793 | |
---|
794 | results = Geospatial_data(file_name = fileName) |
---|
795 | os.remove(fileName) |
---|
796 | answer = [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]] |
---|
797 | assert allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
798 | assert allclose(results.get_attributes(attribute_name='elevation'), [10.0, 0.0, 10.4]) |
---|
799 | |
---|
800 | def test_writepts(self): |
---|
801 | att_dict = {} |
---|
802 | pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
803 | att_dict['elevation'] = array([10.0, 0.0, 10.4]) |
---|
804 | att_dict['brightness'] = array([10.0, 0.0, 10.4]) |
---|
805 | geo_reference=Geo_reference(56,1.9,1.9) |
---|
806 | |
---|
807 | fileName = tempfile.mktemp(".pts") |
---|
808 | |
---|
809 | G = Geospatial_data(pointlist, att_dict, geo_reference) |
---|
810 | |
---|
811 | G.export_points_file(fileName, False) |
---|
812 | |
---|
813 | results = Geospatial_data(file_name = fileName) |
---|
814 | |
---|
815 | os.remove(fileName) |
---|
816 | |
---|
817 | assert allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
818 | assert allclose(results.get_attributes('elevation'), [10.0, 0.0, 10.4]) |
---|
819 | answer = [10.0, 0.0, 10.4] |
---|
820 | assert allclose(results.get_attributes('brightness'), answer) |
---|
821 | |
---|
822 | |
---|
823 | self.failUnless(geo_reference == geo_reference, |
---|
824 | 'test_writepts failed. Test geo_reference') |
---|
825 | |
---|
826 | ########################## BAD .PTS ########################## |
---|
827 | |
---|
828 | def test_load_bad_no_file_pts(self): |
---|
829 | import os |
---|
830 | import tempfile |
---|
831 | |
---|
832 | fileName = tempfile.mktemp(".pts") |
---|
833 | #print fileName |
---|
834 | try: |
---|
835 | results = Geospatial_data(file_name = fileName) |
---|
836 | # dict = import_points_file(fileName) |
---|
837 | except IOError: |
---|
838 | pass |
---|
839 | else: |
---|
840 | msg = 'imaginary file did not raise error!' |
---|
841 | raise msg |
---|
842 | # self.failUnless(0 == 1, |
---|
843 | # 'imaginary file did not raise error!') |
---|
844 | |
---|
845 | |
---|
846 | def test_create_from_pts_file(self): |
---|
847 | |
---|
848 | from Scientific.IO.NetCDF import NetCDFFile |
---|
849 | |
---|
850 | # fileName = tempfile.mktemp(".pts") |
---|
851 | FN = 'test_points.pts' |
---|
852 | # NetCDF file definition |
---|
853 | outfile = NetCDFFile(FN, 'w') |
---|
854 | |
---|
855 | # dimension definitions |
---|
856 | outfile.createDimension('number_of_points', 3) |
---|
857 | outfile.createDimension('number_of_dimensions', 2) #This is 2d data |
---|
858 | |
---|
859 | # variable definitions |
---|
860 | outfile.createVariable('points', Float, ('number_of_points', |
---|
861 | 'number_of_dimensions')) |
---|
862 | outfile.createVariable('elevation', Float, ('number_of_points',)) |
---|
863 | |
---|
864 | # Get handles to the variables |
---|
865 | points = outfile.variables['points'] |
---|
866 | elevation = outfile.variables['elevation'] |
---|
867 | |
---|
868 | points[0, :] = [1.0,0.0] |
---|
869 | elevation[0] = 10.0 |
---|
870 | points[1, :] = [0.0,1.0] |
---|
871 | elevation[1] = 0.0 |
---|
872 | points[2, :] = [1.0,0.0] |
---|
873 | elevation[2] = 10.4 |
---|
874 | |
---|
875 | outfile.close() |
---|
876 | |
---|
877 | G = Geospatial_data(file_name = FN) |
---|
878 | |
---|
879 | assert allclose(G.get_geo_reference().get_xllcorner(), 0.0) |
---|
880 | assert allclose(G.get_geo_reference().get_yllcorner(), 0.0) |
---|
881 | |
---|
882 | assert allclose(G.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
883 | assert allclose(G.get_attributes(), [10.0, 0.0, 10.4]) |
---|
884 | os.remove(FN) |
---|
885 | |
---|
886 | def test_create_from_pts_file_with_geo(self): |
---|
887 | """This test reveals if Geospatial data is correctly instantiated from a pts file. |
---|
888 | """ |
---|
889 | |
---|
890 | from Scientific.IO.NetCDF import NetCDFFile |
---|
891 | |
---|
892 | FN = 'test_points.pts' |
---|
893 | # NetCDF file definition |
---|
894 | outfile = NetCDFFile(FN, 'w') |
---|
895 | |
---|
896 | # Make up an arbitrary georef |
---|
897 | xll = 0.1 |
---|
898 | yll = 20 |
---|
899 | geo_reference=Geo_reference(56, xll, yll) |
---|
900 | geo_reference.write_NetCDF(outfile) |
---|
901 | |
---|
902 | # dimension definitions |
---|
903 | outfile.createDimension('number_of_points', 3) |
---|
904 | outfile.createDimension('number_of_dimensions', 2) #This is 2d data |
---|
905 | |
---|
906 | # variable definitions |
---|
907 | outfile.createVariable('points', Float, ('number_of_points', |
---|
908 | 'number_of_dimensions')) |
---|
909 | outfile.createVariable('elevation', Float, ('number_of_points',)) |
---|
910 | |
---|
911 | # Get handles to the variables |
---|
912 | points = outfile.variables['points'] |
---|
913 | elevation = outfile.variables['elevation'] |
---|
914 | |
---|
915 | points[0, :] = [1.0,0.0] |
---|
916 | elevation[0] = 10.0 |
---|
917 | points[1, :] = [0.0,1.0] |
---|
918 | elevation[1] = 0.0 |
---|
919 | points[2, :] = [1.0,0.0] |
---|
920 | elevation[2] = 10.4 |
---|
921 | |
---|
922 | outfile.close() |
---|
923 | |
---|
924 | G = Geospatial_data(file_name = FN) |
---|
925 | |
---|
926 | assert allclose(G.get_geo_reference().get_xllcorner(), xll) |
---|
927 | assert allclose(G.get_geo_reference().get_yllcorner(), yll) |
---|
928 | |
---|
929 | assert allclose(G.get_data_points(), [[1.0+xll, 0.0+yll], |
---|
930 | [0.0+xll, 1.0+yll], |
---|
931 | [1.0+xll, 0.0+yll]]) |
---|
932 | |
---|
933 | assert allclose(G.get_attributes(), [10.0, 0.0, 10.4]) |
---|
934 | os.remove(FN) |
---|
935 | |
---|
936 | |
---|
937 | def test_add_(self): |
---|
938 | '''adds an xya and pts files, reads the files and adds them |
---|
939 | checking results are correct |
---|
940 | ''' |
---|
941 | |
---|
942 | # create files |
---|
943 | att_dict1 = {} |
---|
944 | pointlist1 = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
945 | att_dict1['elevation'] = array([-10.0, 0.0, 10.4]) |
---|
946 | att_dict1['brightness'] = array([10.0, 0.0, 10.4]) |
---|
947 | geo_reference1 = Geo_reference(56, 2.0, 1.0) |
---|
948 | |
---|
949 | att_dict2 = {} |
---|
950 | pointlist2 = array([[2.0, 1.0],[1.0, 2.0],[2.0, 1.0]]) |
---|
951 | att_dict2['elevation'] = array([1.0, 15.0, 1.4]) |
---|
952 | att_dict2['brightness'] = array([14.0, 1.0, -12.4]) |
---|
953 | geo_reference2 = Geo_reference(56, 1.0, 2.0) |
---|
954 | |
---|
955 | G1 = Geospatial_data(pointlist1, att_dict1, geo_reference1) |
---|
956 | G2 = Geospatial_data(pointlist2, att_dict2, geo_reference2) |
---|
957 | |
---|
958 | fileName1 = tempfile.mktemp(".xya") |
---|
959 | fileName2 = tempfile.mktemp(".pts") |
---|
960 | |
---|
961 | #makes files |
---|
962 | G1.export_points_file(fileName1) |
---|
963 | G2.export_points_file(fileName2) |
---|
964 | |
---|
965 | # add files |
---|
966 | |
---|
967 | G3 = Geospatial_data(file_name = fileName1) |
---|
968 | G4 = Geospatial_data(file_name = fileName2) |
---|
969 | |
---|
970 | G = G3 + G4 |
---|
971 | |
---|
972 | |
---|
973 | #read results |
---|
974 | # print'res', G.get_data_points() |
---|
975 | # print'res1', G.get_data_points(False) |
---|
976 | assert allclose(G.get_data_points(), |
---|
977 | [[ 3.0, 1.0], [ 2.0, 2.0], |
---|
978 | [ 3.0, 1.0], [ 3.0, 3.0], |
---|
979 | [ 2.0, 4.0], [ 3.0, 3.0]]) |
---|
980 | |
---|
981 | assert allclose(G.get_attributes(attribute_name='elevation'), |
---|
982 | [-10.0, 0.0, 10.4, 1.0, 15.0, 1.4]) |
---|
983 | |
---|
984 | answer = [10.0, 0.0, 10.4, 14.0, 1.0, -12.4] |
---|
985 | assert allclose(G.get_attributes(attribute_name='brightness'), answer) |
---|
986 | |
---|
987 | self.failUnless(G.get_geo_reference() == geo_reference1, |
---|
988 | 'test_writepts failed. Test geo_reference') |
---|
989 | |
---|
990 | os.remove(fileName1) |
---|
991 | os.remove(fileName2) |
---|
992 | |
---|
993 | def test_ensure_absolute(self): |
---|
994 | points = [[2.0, 0.0],[1.0, 1.0], |
---|
995 | [2.0, 0.0],[2.0, 2.0], |
---|
996 | [1.0, 3.0],[2.0, 2.0]] |
---|
997 | new_points = ensure_absolute(points) |
---|
998 | |
---|
999 | assert allclose(new_points, points) |
---|
1000 | |
---|
1001 | points = array([[2.0, 0.0],[1.0, 1.0], |
---|
1002 | [2.0, 0.0],[2.0, 2.0], |
---|
1003 | [1.0, 3.0],[2.0, 2.0]]) |
---|
1004 | new_points = ensure_absolute(points) |
---|
1005 | |
---|
1006 | assert allclose(new_points, points) |
---|
1007 | |
---|
1008 | ab_points = array([[2.0, 0.0],[1.0, 1.0], |
---|
1009 | [2.0, 0.0],[2.0, 2.0], |
---|
1010 | [1.0, 3.0],[2.0, 2.0]]) |
---|
1011 | |
---|
1012 | mesh_origin = (56, 290000, 618000) #zone, easting, northing |
---|
1013 | |
---|
1014 | data_points = zeros((ab_points.shape), Float) |
---|
1015 | #Shift datapoints according to new origins |
---|
1016 | for k in range(len(ab_points)): |
---|
1017 | data_points[k][0] = ab_points[k][0] - mesh_origin[1] |
---|
1018 | data_points[k][1] = ab_points[k][1] - mesh_origin[2] |
---|
1019 | #print "data_points",data_points |
---|
1020 | new_points = ensure_absolute(data_points, |
---|
1021 | geo_reference=mesh_origin) |
---|
1022 | #print "new_points",new_points |
---|
1023 | #print "ab_points",ab_points |
---|
1024 | |
---|
1025 | assert allclose(new_points, ab_points) |
---|
1026 | |
---|
1027 | geo = Geo_reference(56,67,-56) |
---|
1028 | |
---|
1029 | data_points = geo.change_points_geo_ref(ab_points) |
---|
1030 | new_points = ensure_absolute(data_points, |
---|
1031 | geo_reference=geo) |
---|
1032 | #print "new_points",new_points |
---|
1033 | #print "ab_points",ab_points |
---|
1034 | |
---|
1035 | assert allclose(new_points, ab_points) |
---|
1036 | |
---|
1037 | |
---|
1038 | geo_reference = Geo_reference(56, 100, 200) |
---|
1039 | ab_points = [[1.0, 2.1], [3.0, 5.3]] |
---|
1040 | points = geo_reference.change_points_geo_ref(ab_points) |
---|
1041 | attributes = [2, 4] |
---|
1042 | #print "geo in points", points |
---|
1043 | G = Geospatial_data(points, attributes, |
---|
1044 | geo_reference=geo_reference) |
---|
1045 | |
---|
1046 | new_points = ensure_absolute(G) |
---|
1047 | #print "new_points",new_points |
---|
1048 | #print "ab_points",ab_points |
---|
1049 | |
---|
1050 | assert allclose(new_points, ab_points) |
---|
1051 | |
---|
1052 | |
---|
1053 | fileName = tempfile.mktemp(".xya") |
---|
1054 | file = open(fileName,"w") |
---|
1055 | file.write(" elevation speed \n\ |
---|
1056 | 1.0 0.0 10.0 0.0\n\ |
---|
1057 | 0.0 1.0 0.0 10.0\n\ |
---|
1058 | 1.0 0.0 10.4 40.0\n\ |
---|
1059 | #geocrap\n\ |
---|
1060 | 56\n\ |
---|
1061 | 10\n\ |
---|
1062 | 20\n") |
---|
1063 | file.close() |
---|
1064 | |
---|
1065 | ab_points = ensure_absolute(fileName) |
---|
1066 | actual = [[11, 20.0],[10.0, 21.0],[11.0, 20.0]] |
---|
1067 | assert allclose(ab_points, actual) |
---|
1068 | os.remove(fileName) |
---|
1069 | |
---|
1070 | |
---|
1071 | def test_ensure_geospatial(self): |
---|
1072 | points = [[2.0, 0.0],[1.0, 1.0], |
---|
1073 | [2.0, 0.0],[2.0, 2.0], |
---|
1074 | [1.0, 3.0],[2.0, 2.0]] |
---|
1075 | new_points = ensure_geospatial(points) |
---|
1076 | |
---|
1077 | assert allclose(new_points.get_data_points(absolute = True), points) |
---|
1078 | |
---|
1079 | points = array([[2.0, 0.0],[1.0, 1.0], |
---|
1080 | [2.0, 0.0],[2.0, 2.0], |
---|
1081 | [1.0, 3.0],[2.0, 2.0]]) |
---|
1082 | new_points = ensure_geospatial(points) |
---|
1083 | |
---|
1084 | assert allclose(new_points.get_data_points(absolute = True), points) |
---|
1085 | |
---|
1086 | ab_points = array([[2.0, 0.0],[1.0, 1.0], |
---|
1087 | [2.0, 0.0],[2.0, 2.0], |
---|
1088 | [1.0, 3.0],[2.0, 2.0]]) |
---|
1089 | |
---|
1090 | mesh_origin = (56, 290000, 618000) #zone, easting, northing |
---|
1091 | |
---|
1092 | data_points = zeros((ab_points.shape), Float) |
---|
1093 | #Shift datapoints according to new origins |
---|
1094 | for k in range(len(ab_points)): |
---|
1095 | data_points[k][0] = ab_points[k][0] - mesh_origin[1] |
---|
1096 | data_points[k][1] = ab_points[k][1] - mesh_origin[2] |
---|
1097 | #print "data_points",data_points |
---|
1098 | new_geospatial = ensure_geospatial(data_points, |
---|
1099 | geo_reference=mesh_origin) |
---|
1100 | new_points = new_geospatial.get_data_points(absolute=True) |
---|
1101 | #print "new_points",new_points |
---|
1102 | #print "ab_points",ab_points |
---|
1103 | |
---|
1104 | assert allclose(new_points, ab_points) |
---|
1105 | |
---|
1106 | geo = Geo_reference(56,67,-56) |
---|
1107 | |
---|
1108 | data_points = geo.change_points_geo_ref(ab_points) |
---|
1109 | new_geospatial = ensure_geospatial(data_points, |
---|
1110 | geo_reference=geo) |
---|
1111 | new_points = new_geospatial.get_data_points(absolute=True) |
---|
1112 | #print "new_points",new_points |
---|
1113 | #print "ab_points",ab_points |
---|
1114 | |
---|
1115 | assert allclose(new_points, ab_points) |
---|
1116 | |
---|
1117 | |
---|
1118 | geo_reference = Geo_reference(56, 100, 200) |
---|
1119 | ab_points = [[1.0, 2.1], [3.0, 5.3]] |
---|
1120 | points = geo_reference.change_points_geo_ref(ab_points) |
---|
1121 | attributes = [2, 4] |
---|
1122 | #print "geo in points", points |
---|
1123 | G = Geospatial_data(points, attributes, |
---|
1124 | geo_reference=geo_reference) |
---|
1125 | |
---|
1126 | new_geospatial = ensure_geospatial(G) |
---|
1127 | new_points = new_geospatial.get_data_points(absolute=True) |
---|
1128 | #print "new_points",new_points |
---|
1129 | #print "ab_points",ab_points |
---|
1130 | |
---|
1131 | assert allclose(new_points, ab_points) |
---|
1132 | |
---|
1133 | def test_isinstance(self): |
---|
1134 | |
---|
1135 | import os |
---|
1136 | |
---|
1137 | fileName = tempfile.mktemp(".xya") |
---|
1138 | file = open(fileName,"w") |
---|
1139 | file.write(" elevation speed \n\ |
---|
1140 | 1.0 0.0 10.0 0.0\n\ |
---|
1141 | 0.0 1.0 0.0 10.0\n\ |
---|
1142 | 1.0 0.0 10.4 40.0\n\ |
---|
1143 | #geocrap\n\ |
---|
1144 | 56\n\ |
---|
1145 | 56.6\n\ |
---|
1146 | 3\n") |
---|
1147 | file.close() |
---|
1148 | |
---|
1149 | results = Geospatial_data(fileName) |
---|
1150 | assert allclose(results.get_data_points(absolute=False), \ |
---|
1151 | [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
1152 | assert allclose(results.get_attributes(attribute_name='elevation'), \ |
---|
1153 | [10.0, 0.0, 10.4]) |
---|
1154 | assert allclose(results.get_attributes(attribute_name='speed'), \ |
---|
1155 | [0.0, 10.0, 40.0]) |
---|
1156 | |
---|
1157 | os.remove(fileName) |
---|
1158 | |
---|
1159 | def test_delimiter(self): |
---|
1160 | |
---|
1161 | try: |
---|
1162 | G = Geospatial_data(delimiter=',') |
---|
1163 | # results = Geospatial_data(file_name = fileName) |
---|
1164 | # dict = import_points_file(fileName) |
---|
1165 | except ValueError: |
---|
1166 | pass |
---|
1167 | else: |
---|
1168 | msg = 'Instance with No fileName but has a delimiter\ |
---|
1169 | did not raise error!' |
---|
1170 | raise msg |
---|
1171 | |
---|
1172 | def test_no_constructors(self): |
---|
1173 | |
---|
1174 | try: |
---|
1175 | G = Geospatial_data() |
---|
1176 | # results = Geospatial_data(file_name = fileName) |
---|
1177 | # dict = import_points_file(fileName) |
---|
1178 | except ValueError: |
---|
1179 | pass |
---|
1180 | else: |
---|
1181 | msg = 'Instance must have a filename or data points' |
---|
1182 | raise msg |
---|
1183 | |
---|
1184 | def test_check_geo_reference(self): |
---|
1185 | """ |
---|
1186 | checks geo reference details are OK. eg can be called '#geo reference' |
---|
1187 | if not throws a clear error message |
---|
1188 | """ |
---|
1189 | import os |
---|
1190 | fileName = tempfile.mktemp(".xya") |
---|
1191 | file = open(fileName,"w") |
---|
1192 | file.write(" elevation \n\ |
---|
1193 | 1.0 0.0 10.0\n\ |
---|
1194 | 0.0 1.0 0.0\n\ |
---|
1195 | 1.0 0.0 10.4\n\ |
---|
1196 | #ge oreference\n\ |
---|
1197 | 56\n\ |
---|
1198 | 1.1\n\ |
---|
1199 | 1.0\n") |
---|
1200 | |
---|
1201 | file.close() |
---|
1202 | results = Geospatial_data(fileName) |
---|
1203 | assert allclose(results.get_geo_reference().get_xllcorner(), 1.1) |
---|
1204 | assert allclose(results.get_geo_reference().get_yllcorner(), 1.0) |
---|
1205 | |
---|
1206 | os.remove(fileName) |
---|
1207 | |
---|
1208 | fileName = tempfile.mktemp(".xya") |
---|
1209 | file = open(fileName,"w") |
---|
1210 | file.write(" elevation \n\ |
---|
1211 | 1.0 0.0 10.0\n\ |
---|
1212 | 0.0 1.0 0.0\n\ |
---|
1213 | 1.0 0.0 10.4\n") |
---|
1214 | |
---|
1215 | file.close() |
---|
1216 | results = Geospatial_data(fileName) |
---|
1217 | |
---|
1218 | os.remove(fileName) |
---|
1219 | |
---|
1220 | def test_check_geo_reference1(self): |
---|
1221 | """ |
---|
1222 | checks geo reference details are OK. eg can be called '#geo reference' |
---|
1223 | if not throws a clear error message |
---|
1224 | """ |
---|
1225 | import os |
---|
1226 | fileName = tempfile.mktemp(".xya") |
---|
1227 | file = open(fileName,"w") |
---|
1228 | file.write(" elevation \n\ |
---|
1229 | 1.0 0.0 10.0\n\ |
---|
1230 | 0.0 1.0 0.0\n\ |
---|
1231 | 1.0 0.0 10.4\n\ |
---|
1232 | #geo t t\n\ |
---|
1233 | 56\n\ |
---|
1234 | 1.1\n" |
---|
1235 | ) |
---|
1236 | file.close() |
---|
1237 | |
---|
1238 | try: |
---|
1239 | results = Geospatial_data(fileName, delimiter = " ") |
---|
1240 | except IOError: |
---|
1241 | pass |
---|
1242 | else: |
---|
1243 | msg = 'Geo reference data format is incorrect' |
---|
1244 | raise msg |
---|
1245 | |
---|
1246 | |
---|
1247 | os.remove(fileName) |
---|
1248 | |
---|
1249 | |
---|
1250 | |
---|
1251 | def test_lat_long(self): |
---|
1252 | lat_gong = degminsec2decimal_degrees(-34,30,0.) |
---|
1253 | lon_gong = degminsec2decimal_degrees(150,55,0.) |
---|
1254 | |
---|
1255 | lat_2 = degminsec2decimal_degrees(-34,00,0.) |
---|
1256 | lon_2 = degminsec2decimal_degrees(150,00,0.) |
---|
1257 | |
---|
1258 | lats = [lat_gong, lat_2] |
---|
1259 | longs = [lon_gong, lon_2] |
---|
1260 | gsd = Geospatial_data(latitudes=lats, longitudes=longs) |
---|
1261 | |
---|
1262 | points = gsd.get_data_points(absolute=True) |
---|
1263 | |
---|
1264 | assert allclose(points[0][0], 308728.009) |
---|
1265 | assert allclose(points[0][1], 6180432.601) |
---|
1266 | assert allclose(points[1][0], 222908.705) |
---|
1267 | assert allclose(points[1][1], 6233785.284) |
---|
1268 | self.failUnless(gsd.get_geo_reference().get_zone() == 56, |
---|
1269 | 'Bad zone error!') |
---|
1270 | |
---|
1271 | try: |
---|
1272 | results = Geospatial_data(latitudes=lats) |
---|
1273 | except ValueError: |
---|
1274 | pass |
---|
1275 | else: |
---|
1276 | self.failUnless(0 ==1, 'Error not thrown error!') |
---|
1277 | try: |
---|
1278 | results = Geospatial_data(latitudes=lats) |
---|
1279 | except ValueError: |
---|
1280 | pass |
---|
1281 | else: |
---|
1282 | self.failUnless(0 ==1, 'Error not thrown error!') |
---|
1283 | try: |
---|
1284 | results = Geospatial_data(longitudes=lats) |
---|
1285 | except ValueError: |
---|
1286 | pass |
---|
1287 | else: |
---|
1288 | self.failUnless(0 ==1, 'Error not thrown error!') |
---|
1289 | try: |
---|
1290 | results = Geospatial_data(latitudes=lats, longitudes=longs, |
---|
1291 | geo_reference="p") |
---|
1292 | except ValueError: |
---|
1293 | pass |
---|
1294 | else: |
---|
1295 | self.failUnless(0 ==1, 'Error not thrown error!') |
---|
1296 | |
---|
1297 | try: |
---|
1298 | results = Geospatial_data(latitudes=lats, longitudes=longs, |
---|
1299 | data_points=12) |
---|
1300 | except ValueError: |
---|
1301 | pass |
---|
1302 | else: |
---|
1303 | self.failUnless(0 ==1, 'Error not thrown error!') |
---|
1304 | |
---|
1305 | def test_lat_long2(self): |
---|
1306 | lat_gong = degminsec2decimal_degrees(-34,30,0.) |
---|
1307 | lon_gong = degminsec2decimal_degrees(150,55,0.) |
---|
1308 | |
---|
1309 | lat_2 = degminsec2decimal_degrees(-34,00,0.) |
---|
1310 | lon_2 = degminsec2decimal_degrees(150,00,0.) |
---|
1311 | |
---|
1312 | points = [[lat_gong, lon_gong], [lat_2, lon_2]] |
---|
1313 | gsd = Geospatial_data(data_points=points, points_are_lats_longs=True) |
---|
1314 | |
---|
1315 | points = gsd.get_data_points(absolute=True) |
---|
1316 | |
---|
1317 | assert allclose(points[0][0], 308728.009) |
---|
1318 | assert allclose(points[0][1], 6180432.601) |
---|
1319 | assert allclose(points[1][0], 222908.705) |
---|
1320 | assert allclose(points[1][1], 6233785.284) |
---|
1321 | self.failUnless(gsd.get_geo_reference().get_zone() == 56, |
---|
1322 | 'Bad zone error!') |
---|
1323 | |
---|
1324 | try: |
---|
1325 | results = Geospatial_data(points_are_lats_longs=True) |
---|
1326 | except ValueError: |
---|
1327 | pass |
---|
1328 | else: |
---|
1329 | self.failUnless(0 ==1, 'Error not thrown error!') |
---|
1330 | |
---|
1331 | def test_len(self): |
---|
1332 | |
---|
1333 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
1334 | G = Geospatial_data(points) |
---|
1335 | self.failUnless(2 ==len(G), 'Len error!') |
---|
1336 | |
---|
1337 | points = [[1.0, 2.1]] |
---|
1338 | G = Geospatial_data(points) |
---|
1339 | self.failUnless(1 ==len(G), 'Len error!') |
---|
1340 | |
---|
1341 | points = [[1.0, 2.1], [3.0, 5.3], [3.0, 5.3], [3.0, 5.3]] |
---|
1342 | G = Geospatial_data(points) |
---|
1343 | self.failUnless(4 ==len(G), 'Len error!') |
---|
1344 | |
---|
1345 | if __name__ == "__main__": |
---|
1346 | |
---|
1347 | #suite = unittest.makeSuite(Test_Geospatial_data, 'test_ensure_geospatial') |
---|
1348 | suite = unittest.makeSuite(Test_Geospatial_data, 'test') |
---|
1349 | runner = unittest.TextTestRunner() |
---|
1350 | runner.run(suite) |
---|
1351 | |
---|
1352 | |
---|