1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import unittest |
---|
4 | import os |
---|
5 | import tempfile |
---|
6 | from math import sqrt, pi |
---|
7 | |
---|
8 | import numpy as num |
---|
9 | |
---|
10 | from anuga.geospatial_data.geospatial_data import * |
---|
11 | from anuga.coordinate_transforms.geo_reference import Geo_reference, TitleError |
---|
12 | from anuga.coordinate_transforms.redfearn import degminsec2decimal_degrees |
---|
13 | from anuga.utilities.anuga_exceptions import ANUGAError |
---|
14 | from anuga.utilities.system_tools import get_host_name |
---|
15 | from anuga.config import netcdf_mode_r, netcdf_mode_w, netcdf_mode_a |
---|
16 | from anuga.config import netcdf_float |
---|
17 | |
---|
18 | |
---|
19 | class Test_Geospatial_data(unittest.TestCase): |
---|
20 | def setUp(self): |
---|
21 | pass |
---|
22 | |
---|
23 | def tearDown(self): |
---|
24 | pass |
---|
25 | |
---|
26 | def test_0(self): |
---|
27 | #Basic points |
---|
28 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
29 | |
---|
30 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
31 | G = Geospatial_data(points) |
---|
32 | assert num.allclose(G.data_points, [[1.0, 2.1], [3.0, 5.3]]) |
---|
33 | |
---|
34 | # Check __repr__ |
---|
35 | # FIXME (Ole): Is this really machine independent? |
---|
36 | rep = `G` |
---|
37 | ref = '[[ 1. 2.1]\n [ 3. 5.3]]' |
---|
38 | msg = 'Representation %s is not equal to %s' % (rep, ref) |
---|
39 | assert rep == ref, msg |
---|
40 | |
---|
41 | #Check getter |
---|
42 | assert num.allclose(G.get_data_points(), [[1.0, 2.1], [3.0, 5.3]]) |
---|
43 | |
---|
44 | #Check defaults |
---|
45 | assert G.attributes is None |
---|
46 | assert G.geo_reference.zone == Geo_reference().zone |
---|
47 | assert G.geo_reference.xllcorner == Geo_reference().xllcorner |
---|
48 | assert G.geo_reference.yllcorner == Geo_reference().yllcorner |
---|
49 | |
---|
50 | def test_1(self): |
---|
51 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
52 | attributes = [2, 4] |
---|
53 | G = Geospatial_data(points, attributes) |
---|
54 | assert G.attributes.keys()[0] == DEFAULT_ATTRIBUTE |
---|
55 | assert num.allclose(G.attributes.values()[0], [2, 4]) |
---|
56 | |
---|
57 | def test_2(self): |
---|
58 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
59 | |
---|
60 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
61 | attributes = [2, 4] |
---|
62 | G = Geospatial_data(points, attributes, |
---|
63 | geo_reference=Geo_reference(56, 100, 200)) |
---|
64 | |
---|
65 | assert G.geo_reference.zone == 56 |
---|
66 | assert G.geo_reference.xllcorner == 100 |
---|
67 | assert G.geo_reference.yllcorner == 200 |
---|
68 | |
---|
69 | def test_get_attributes_1(self): |
---|
70 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
71 | |
---|
72 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
73 | attributes = [2, 4] |
---|
74 | G = Geospatial_data(points, attributes, |
---|
75 | geo_reference=Geo_reference(56, 100, 200)) |
---|
76 | |
---|
77 | P = G.get_data_points(absolute=False) |
---|
78 | assert num.allclose(P, [[1.0, 2.1], [3.0, 5.3]]) |
---|
79 | |
---|
80 | P = G.get_data_points(absolute=True) |
---|
81 | assert num.allclose(P, [[101.0, 202.1], [103.0, 205.3]]) |
---|
82 | |
---|
83 | V = G.get_attributes() #Simply get them |
---|
84 | assert num.allclose(V, [2, 4]) |
---|
85 | |
---|
86 | V = G.get_attributes(DEFAULT_ATTRIBUTE) #Get by name |
---|
87 | assert num.allclose(V, [2, 4]) |
---|
88 | |
---|
89 | def test_get_attributes_2(self): |
---|
90 | #Multiple attributes |
---|
91 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
92 | |
---|
93 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
94 | attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]} |
---|
95 | G = Geospatial_data(points, attributes, |
---|
96 | geo_reference=Geo_reference(56, 100, 200), |
---|
97 | default_attribute_name='a1') |
---|
98 | |
---|
99 | P = G.get_data_points(absolute=False) |
---|
100 | assert num.allclose(P, [[1.0, 2.1], [3.0, 5.3]]) |
---|
101 | |
---|
102 | V = G.get_attributes() #Get default attribute |
---|
103 | assert num.allclose(V, [2, 4]) |
---|
104 | |
---|
105 | V = G.get_attributes('a0') #Get by name |
---|
106 | assert num.allclose(V, [0, 0]) |
---|
107 | |
---|
108 | V = G.get_attributes('a1') #Get by name |
---|
109 | assert num.allclose(V, [2, 4]) |
---|
110 | |
---|
111 | V = G.get_attributes('a2') #Get by name |
---|
112 | assert num.allclose(V, [79.4, -7]) |
---|
113 | |
---|
114 | try: |
---|
115 | V = G.get_attributes('hdnoatedu') #Invalid |
---|
116 | except AssertionError: |
---|
117 | pass |
---|
118 | else: |
---|
119 | raise Exception, 'Should have raised exception' |
---|
120 | |
---|
121 | def test_get_data_points(self): |
---|
122 | points_ab = [[12.5, 34.7], [-4.5, -60.0]] |
---|
123 | x_p = -10 |
---|
124 | y_p = -40 |
---|
125 | geo_ref = Geo_reference(56, x_p, y_p) |
---|
126 | points_rel = geo_ref.change_points_geo_ref(points_ab) |
---|
127 | |
---|
128 | spatial = Geospatial_data(points_rel, geo_reference=geo_ref) |
---|
129 | results = spatial.get_data_points(absolute=False) |
---|
130 | assert num.allclose(results, points_rel) |
---|
131 | |
---|
132 | x_p = -1770 |
---|
133 | y_p = 4.01 |
---|
134 | geo_ref = Geo_reference(56, x_p, y_p) |
---|
135 | points_rel = geo_ref.change_points_geo_ref(points_ab) |
---|
136 | results = spatial.get_data_points(geo_reference=geo_ref) |
---|
137 | |
---|
138 | assert num.allclose(results, points_rel) |
---|
139 | |
---|
140 | def test_get_data_points_lat_long(self): |
---|
141 | # lat long [-30.], [130] |
---|
142 | # Zone: 52 |
---|
143 | # Easting: 596450.153 Northing: 6680793.777 |
---|
144 | # lat long [-32.], [131] |
---|
145 | # Zone: 52 |
---|
146 | # Easting: 688927.638 Northing: 6457816.509 |
---|
147 | |
---|
148 | points_Lat_long = [[-30., 130], [-32, 131]] |
---|
149 | |
---|
150 | spatial = Geospatial_data(latitudes=[-30, -32.], longitudes=[130, 131]) |
---|
151 | results = spatial.get_data_points(as_lat_long=True) |
---|
152 | assert num.allclose(results, points_Lat_long) |
---|
153 | |
---|
154 | def test_get_data_points_lat_longII(self): |
---|
155 | # x,y North,east long,lat |
---|
156 | boundary_polygon = [[ 250000, 7630000]] |
---|
157 | zone = 50 |
---|
158 | |
---|
159 | geo_reference = Geo_reference(zone=zone) |
---|
160 | geo = Geospatial_data(boundary_polygon ,geo_reference=geo_reference) |
---|
161 | seg_lat_long = geo.get_data_points(as_lat_long=True) |
---|
162 | lat_result = degminsec2decimal_degrees(-21, 24, 54) |
---|
163 | long_result = degminsec2decimal_degrees(114, 35, 17.89) |
---|
164 | assert num.allclose(seg_lat_long[0][0], lat_result) #lat |
---|
165 | assert num.allclose(seg_lat_long[0][1], long_result) #long |
---|
166 | |
---|
167 | def test_get_data_points_lat_longIII(self): |
---|
168 | # x,y North,east long,lat |
---|
169 | # for northern hemisphere |
---|
170 | boundary_polygon = [[419944.8, 918642.4]] |
---|
171 | zone = 47 |
---|
172 | |
---|
173 | geo_reference = Geo_reference(zone=zone) |
---|
174 | geo = Geospatial_data(boundary_polygon, geo_reference=geo_reference) |
---|
175 | seg_lat_long = geo.get_data_points(as_lat_long=True, |
---|
176 | isSouthHemisphere=False) |
---|
177 | lat_result = degminsec2decimal_degrees(8.31, 0, 0) |
---|
178 | long_result = degminsec2decimal_degrees(98.273, 0, 0) |
---|
179 | assert num.allclose(seg_lat_long[0][0], lat_result)#lat |
---|
180 | assert num.allclose(seg_lat_long[0][1], long_result)#long |
---|
181 | |
---|
182 | def test_set_geo_reference(self): |
---|
183 | '''test_set_georeference |
---|
184 | |
---|
185 | Test that georeference can be changed without changing the |
---|
186 | absolute values. |
---|
187 | ''' |
---|
188 | |
---|
189 | points_ab = [[12.5, 34.7], [-4.5, -60.0]] |
---|
190 | x_p = -10 |
---|
191 | y_p = -40 |
---|
192 | geo_ref = Geo_reference(56, x_p, y_p) |
---|
193 | points_rel = geo_ref.change_points_geo_ref(points_ab) |
---|
194 | |
---|
195 | # Create without geo_ref properly set |
---|
196 | G = Geospatial_data(points_rel) |
---|
197 | assert not num.allclose(points_ab, G.get_data_points(absolute=True)) |
---|
198 | |
---|
199 | # Create the way it should be |
---|
200 | G = Geospatial_data(points_rel, geo_reference=geo_ref) |
---|
201 | assert num.allclose(points_ab, G.get_data_points(absolute=True)) |
---|
202 | |
---|
203 | # Change georeference and check that absolute values are unchanged. |
---|
204 | x_p = 10 |
---|
205 | y_p = 400 |
---|
206 | new_geo_ref = Geo_reference(56, x_p, y_p) |
---|
207 | G.set_geo_reference(new_geo_ref) |
---|
208 | msg = ('points_ab=\n%s\nbut G.get_data_points(absolute=True)=\n%s' |
---|
209 | % (str(points_ab), str(G.get_data_points(absolute=True)))) |
---|
210 | assert num.allclose(points_ab, G.get_data_points(absolute=True)), msg |
---|
211 | |
---|
212 | def test_conversions_to_points_dict(self): |
---|
213 | #test conversions to points_dict |
---|
214 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
215 | |
---|
216 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
217 | attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]} |
---|
218 | G = Geospatial_data(points, attributes, |
---|
219 | geo_reference=Geo_reference(56, 100, 200), |
---|
220 | default_attribute_name='a1') |
---|
221 | |
---|
222 | points_dict = geospatial_data2points_dictionary(G) |
---|
223 | |
---|
224 | assert points_dict.has_key('pointlist') |
---|
225 | assert points_dict.has_key('attributelist') |
---|
226 | assert points_dict.has_key('geo_reference') |
---|
227 | |
---|
228 | assert num.allclose( points_dict['pointlist'], points ) |
---|
229 | |
---|
230 | A = points_dict['attributelist'] |
---|
231 | assert A.has_key('a0') |
---|
232 | assert A.has_key('a1') |
---|
233 | assert A.has_key('a2') |
---|
234 | |
---|
235 | assert num.allclose( A['a0'], [0, 0] ) |
---|
236 | assert num.allclose( A['a1'], [2, 4] ) |
---|
237 | assert num.allclose( A['a2'], [79.4, -7] ) |
---|
238 | |
---|
239 | geo = points_dict['geo_reference'] |
---|
240 | assert geo is G.geo_reference |
---|
241 | |
---|
242 | def test_conversions_from_points_dict(self): |
---|
243 | '''test conversions from points_dict''' |
---|
244 | |
---|
245 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
246 | |
---|
247 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
248 | attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]} |
---|
249 | |
---|
250 | points_dict = {} |
---|
251 | points_dict['pointlist'] = points |
---|
252 | points_dict['attributelist'] = attributes |
---|
253 | points_dict['geo_reference'] = Geo_reference(56, 100, 200) |
---|
254 | |
---|
255 | G = points_dictionary2geospatial_data(points_dict) |
---|
256 | P = G.get_data_points(absolute=False) |
---|
257 | assert num.allclose(P, [[1.0, 2.1], [3.0, 5.3]]) |
---|
258 | |
---|
259 | V = G.get_attributes('a0') #Get by name |
---|
260 | assert num.allclose(V, [0, 0]) |
---|
261 | |
---|
262 | V = G.get_attributes('a1') #Get by name |
---|
263 | assert num.allclose(V, [2, 4]) |
---|
264 | |
---|
265 | V = G.get_attributes('a2') #Get by name |
---|
266 | assert num.allclose(V, [79.4, -7]) |
---|
267 | |
---|
268 | def test_add(self): |
---|
269 | '''test the addition of two geospatical objects |
---|
270 | no geo_reference see next test |
---|
271 | ''' |
---|
272 | |
---|
273 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
274 | attributes = {'depth': [2, 4], 'elevation': [6.1, 5]} |
---|
275 | attributes1 = {'depth': [2, 4], 'elevation': [2.5, 1]} |
---|
276 | G1 = Geospatial_data(points, attributes) |
---|
277 | G2 = Geospatial_data(points, attributes1) |
---|
278 | |
---|
279 | G = G1 + G2 |
---|
280 | |
---|
281 | assert G.attributes.has_key('depth') |
---|
282 | assert G.attributes.has_key('elevation') |
---|
283 | assert num.allclose(G.attributes['depth'], [2, 4, 2, 4]) |
---|
284 | assert num.allclose(G.attributes['elevation'], [6.1, 5, 2.5, 1]) |
---|
285 | assert num.allclose(G.get_data_points(), [[1.0, 2.1], [3.0, 5.3], |
---|
286 | [1.0, 2.1], [3.0, 5.3]]) |
---|
287 | |
---|
288 | def test_addII(self): |
---|
289 | '''test the addition of two geospatical objects |
---|
290 | no geo_reference see next test |
---|
291 | ''' |
---|
292 | |
---|
293 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
294 | attributes = {'depth': [2, 4]} |
---|
295 | G1 = Geospatial_data(points, attributes) |
---|
296 | |
---|
297 | points = [[5.0, 2.1], [3.0, 50.3]] |
---|
298 | attributes = {'depth': [200, 400]} |
---|
299 | G2 = Geospatial_data(points, attributes) |
---|
300 | |
---|
301 | G = G1 + G2 |
---|
302 | |
---|
303 | assert G.attributes.has_key('depth') |
---|
304 | assert G.attributes.keys(), ['depth'] |
---|
305 | assert num.allclose(G.attributes['depth'], [2, 4, 200, 400]) |
---|
306 | assert num.allclose(G.get_data_points(), [[1.0, 2.1], [3.0, 5.3], |
---|
307 | [5.0, 2.1], [3.0, 50.3]]) |
---|
308 | |
---|
309 | def test_add_with_geo (self): |
---|
310 | '''Difference in Geo_reference resolved''' |
---|
311 | |
---|
312 | points1 = [[1.0, 2.1], [3.0, 5.3]] |
---|
313 | points2 = [[5.0, 6.1], [6.0, 3.3]] |
---|
314 | attributes1 = [2, 4] |
---|
315 | attributes2 = [5, 76] |
---|
316 | geo_ref1= Geo_reference(55, 1.0, 2.0) |
---|
317 | geo_ref2 = Geo_reference(zone=55, |
---|
318 | xllcorner=0.1, |
---|
319 | yllcorner=3.0, |
---|
320 | datum='wgs84', |
---|
321 | projection='UTM', |
---|
322 | units='m') |
---|
323 | |
---|
324 | G1 = Geospatial_data(points1, attributes1, geo_ref1) |
---|
325 | G2 = Geospatial_data(points2, attributes2, geo_ref2) |
---|
326 | |
---|
327 | #Check that absolute values are as expected |
---|
328 | P1 = G1.get_data_points(absolute=True) |
---|
329 | assert num.allclose(P1, [[2.0, 4.1], [4.0, 7.3]]) |
---|
330 | |
---|
331 | P2 = G2.get_data_points(absolute=True) |
---|
332 | assert num.allclose(P2, [[5.1, 9.1], [6.1, 6.3]]) |
---|
333 | |
---|
334 | G = G1 + G2 |
---|
335 | |
---|
336 | # Check absoluteness |
---|
337 | assert num.allclose(G.get_geo_reference().get_xllcorner(), 0.0) |
---|
338 | assert num.allclose(G.get_geo_reference().get_yllcorner(), 0.0) |
---|
339 | |
---|
340 | P = G.get_data_points(absolute=True) |
---|
341 | |
---|
342 | assert num.allclose(P, num.concatenate((P1,P2), axis=0)) #??default# |
---|
343 | msg = ('P=\n%s\nshould be close to\n' |
---|
344 | '[[2.0, 4.1], [4.0, 7.3],\n' |
---|
345 | ' [5.1, 9.1], [6.1, 6.3]]' |
---|
346 | % str(P)) |
---|
347 | assert num.allclose(P, [[2.0, 4.1], [4.0, 7.3], |
---|
348 | [5.1, 9.1], [6.1, 6.3]]), msg |
---|
349 | |
---|
350 | def test_add_with_geo_absolute (self): |
---|
351 | '''Difference in Geo_reference resolved''' |
---|
352 | |
---|
353 | points1 = num.array([[2.0, 4.1], [4.0, 7.3]]) |
---|
354 | points2 = num.array([[5.1, 9.1], [6.1, 6.3]]) |
---|
355 | attributes1 = [2, 4] |
---|
356 | attributes2 = [5, 76] |
---|
357 | geo_ref1= Geo_reference(55, 1.0, 2.0) |
---|
358 | geo_ref2 = Geo_reference(55, 2.0, 3.0) |
---|
359 | |
---|
360 | G1 = Geospatial_data(points1 - [geo_ref1.get_xllcorner(), |
---|
361 | geo_ref1.get_yllcorner()], |
---|
362 | attributes1, geo_ref1) |
---|
363 | |
---|
364 | G2 = Geospatial_data(points2 - [geo_ref2.get_xllcorner(), |
---|
365 | geo_ref2.get_yllcorner()], |
---|
366 | attributes2, geo_ref2) |
---|
367 | |
---|
368 | #Check that absolute values are as expected |
---|
369 | P1 = G1.get_data_points(absolute=True) |
---|
370 | assert num.allclose(P1, points1) |
---|
371 | |
---|
372 | P1 = G1.get_data_points(absolute=False) |
---|
373 | msg = ('P1=\n%s\nbut points1 - <...>=\n%s' |
---|
374 | % (str(P1), |
---|
375 | str(points1 - [geo_ref1.get_xllcorner(), |
---|
376 | geo_ref1.get_yllcorner()]))) |
---|
377 | assert num.allclose(P1, points1 - [geo_ref1.get_xllcorner(), |
---|
378 | geo_ref1.get_yllcorner()]), msg |
---|
379 | |
---|
380 | P2 = G2.get_data_points(absolute=True) |
---|
381 | assert num.allclose(P2, points2) |
---|
382 | |
---|
383 | P2 = G2.get_data_points(absolute=False) |
---|
384 | assert num.allclose(P2, points2 - [geo_ref2.get_xllcorner(), |
---|
385 | geo_ref2.get_yllcorner()]) |
---|
386 | |
---|
387 | G = G1 + G2 |
---|
388 | P = G.get_data_points(absolute=True) |
---|
389 | |
---|
390 | assert num.allclose(P, num.concatenate((points1, points2), axis=0)) #??default# |
---|
391 | |
---|
392 | def test_add_with_None(self): |
---|
393 | '''test that None can be added to a geospatical objects''' |
---|
394 | |
---|
395 | points1 = num.array([[2.0, 4.1], [4.0, 7.3]]) |
---|
396 | points2 = num.array([[5.1, 9.1], [6.1, 6.3]]) |
---|
397 | |
---|
398 | geo_ref1= Geo_reference(55, 1.0, 2.0) |
---|
399 | geo_ref2 = Geo_reference(zone=55, |
---|
400 | xllcorner=0.1, |
---|
401 | yllcorner=3.0, |
---|
402 | datum='wgs84', |
---|
403 | projection='UTM', |
---|
404 | units='m') |
---|
405 | |
---|
406 | attributes1 = {'depth': [2, 4.7], 'elevation': [6.1, 5]} |
---|
407 | attributes2 = {'depth': [-2.3, 4], 'elevation': [2.5, 1]} |
---|
408 | |
---|
409 | G1 = Geospatial_data(points1, attributes1, geo_ref1) |
---|
410 | assert num.allclose(G1.get_geo_reference().get_xllcorner(), 1.0) |
---|
411 | assert num.allclose(G1.get_geo_reference().get_yllcorner(), 2.0) |
---|
412 | assert G1.attributes.has_key('depth') |
---|
413 | assert G1.attributes.has_key('elevation') |
---|
414 | assert num.allclose(G1.attributes['depth'], [2, 4.7]) |
---|
415 | assert num.allclose(G1.attributes['elevation'], [6.1, 5]) |
---|
416 | |
---|
417 | G2 = Geospatial_data(points2, attributes2, geo_ref2) |
---|
418 | assert num.allclose(G2.get_geo_reference().get_xllcorner(), 0.1) |
---|
419 | assert num.allclose(G2.get_geo_reference().get_yllcorner(), 3.0) |
---|
420 | assert G2.attributes.has_key('depth') |
---|
421 | assert G2.attributes.has_key('elevation') |
---|
422 | assert num.allclose(G2.attributes['depth'], [-2.3, 4]) |
---|
423 | assert num.allclose(G2.attributes['elevation'], [2.5, 1]) |
---|
424 | |
---|
425 | #Check that absolute values are as expected |
---|
426 | P1 = G1.get_data_points(absolute=True) |
---|
427 | assert num.allclose(P1, [[3.0, 6.1], [5.0, 9.3]]) |
---|
428 | |
---|
429 | P2 = G2.get_data_points(absolute=True) |
---|
430 | assert num.allclose(P2, [[5.2, 12.1], [6.2, 9.3]]) |
---|
431 | |
---|
432 | # Normal add |
---|
433 | G = G1 + None |
---|
434 | assert G.attributes.has_key('depth') |
---|
435 | assert G.attributes.has_key('elevation') |
---|
436 | assert num.allclose(G.attributes['depth'], [2, 4.7]) |
---|
437 | assert num.allclose(G.attributes['elevation'], [6.1, 5]) |
---|
438 | |
---|
439 | # Points are now absolute. |
---|
440 | assert num.allclose(G.get_geo_reference().get_xllcorner(), 0.0) |
---|
441 | assert num.allclose(G.get_geo_reference().get_yllcorner(), 0.0) |
---|
442 | P = G.get_data_points(absolute=True) |
---|
443 | msg = 'P=\n%s' % str(P) |
---|
444 | assert num.allclose(P, [[3.0, 6.1], [5.0, 9.3]]), msg |
---|
445 | |
---|
446 | G = G2 + None |
---|
447 | assert G.attributes.has_key('depth') |
---|
448 | assert G.attributes.has_key('elevation') |
---|
449 | assert num.allclose(G.attributes['depth'], [-2.3, 4]) |
---|
450 | assert num.allclose(G.attributes['elevation'], [2.5, 1]) |
---|
451 | assert num.allclose(G.get_geo_reference().get_xllcorner(), 0.0) |
---|
452 | assert num.allclose(G.get_geo_reference().get_yllcorner(), 0.0) |
---|
453 | |
---|
454 | P = G.get_data_points(absolute=True) |
---|
455 | assert num.allclose(P, [[5.2, 12.1], [6.2, 9.3]]) |
---|
456 | |
---|
457 | # Reverse add |
---|
458 | G = None + G1 |
---|
459 | assert G.attributes.has_key('depth') |
---|
460 | assert G.attributes.has_key('elevation') |
---|
461 | assert num.allclose(G.attributes['depth'], [2, 4.7]) |
---|
462 | assert num.allclose(G.attributes['elevation'], [6.1, 5]) |
---|
463 | |
---|
464 | # Points are now absolute. |
---|
465 | assert num.allclose(G.get_geo_reference().get_xllcorner(), 0.0) |
---|
466 | assert num.allclose(G.get_geo_reference().get_yllcorner(), 0.0) |
---|
467 | |
---|
468 | P = G.get_data_points(absolute=True) |
---|
469 | assert num.allclose(P, [[3.0, 6.1], [5.0, 9.3]]) |
---|
470 | |
---|
471 | G = None + G2 |
---|
472 | assert G.attributes.has_key('depth') |
---|
473 | assert G.attributes.has_key('elevation') |
---|
474 | assert num.allclose(G.attributes['depth'], [-2.3, 4]) |
---|
475 | assert num.allclose(G.attributes['elevation'], [2.5, 1]) |
---|
476 | |
---|
477 | assert num.allclose(G.get_geo_reference().get_xllcorner(), 0.0) |
---|
478 | assert num.allclose(G.get_geo_reference().get_yllcorner(), 0.0) |
---|
479 | |
---|
480 | P = G.get_data_points(absolute=True) |
---|
481 | assert num.allclose(P, [[5.2, 12.1], [6.2, 9.3]]) |
---|
482 | |
---|
483 | def test_clip0(self): |
---|
484 | '''test_clip0(self): |
---|
485 | |
---|
486 | Test that point sets can be clipped by a polygon |
---|
487 | ''' |
---|
488 | |
---|
489 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
490 | |
---|
491 | points = [[-1, 4], [0.2, 0.5], [1.0, 2.1], [0.4, 0.3], [3.0, 5.3], |
---|
492 | [0, 0], [2.4, 3.3]] |
---|
493 | G = Geospatial_data(points) |
---|
494 | |
---|
495 | # First try the unit square |
---|
496 | U = [[0,0], [1,0], [1,1], [0,1]] |
---|
497 | assert num.allclose(G.clip(U).get_data_points(), |
---|
498 | [[0.2, 0.5], [0.4, 0.3], [0, 0]]) |
---|
499 | |
---|
500 | # Then a more complex polygon |
---|
501 | polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] |
---|
502 | points = [[0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], |
---|
503 | [0.5, 1.5], [0.5, -0.5]] |
---|
504 | G = Geospatial_data(points) |
---|
505 | |
---|
506 | assert num.allclose(G.clip(polygon).get_data_points(), |
---|
507 | [[0.5, 0.5], [1, -0.5], [1.5, 0]]) |
---|
508 | |
---|
509 | def test_clip0_with_attributes(self): |
---|
510 | '''test_clip0_with_attributes(self): |
---|
511 | |
---|
512 | Test that point sets with attributes can be clipped by a polygon |
---|
513 | ''' |
---|
514 | |
---|
515 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
516 | |
---|
517 | points = [[-1, 4], [0.2, 0.5], [1.0, 2.1], [0.4, 0.3], [3.0, 5.3], |
---|
518 | [0, 0], [2.4, 3.3]] |
---|
519 | |
---|
520 | attributes = [2, -4, 5, 76, -2, 0.1, 3] |
---|
521 | att_dict = {'att1': attributes, |
---|
522 | 'att2': num.array(attributes)+1} |
---|
523 | |
---|
524 | G = Geospatial_data(points, att_dict) |
---|
525 | |
---|
526 | # First try the unit square |
---|
527 | U = [[0,0], [1,0], [1,1], [0,1]] |
---|
528 | assert num.allclose(G.clip(U).get_data_points(), |
---|
529 | [[0.2, 0.5], [0.4, 0.3], [0, 0]]) |
---|
530 | assert num.allclose(G.clip(U).get_attributes('att1'), [-4, 76, 0.1]) |
---|
531 | assert num.allclose(G.clip(U).get_attributes('att2'), [-3, 77, 1.1]) |
---|
532 | |
---|
533 | # Then a more complex polygon |
---|
534 | polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] |
---|
535 | points = [[0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], |
---|
536 | [0.5, 1.5], [0.5, -0.5]] |
---|
537 | |
---|
538 | # This time just one attribute |
---|
539 | attributes = [2, -4, 5, 76, -2, 0.1] |
---|
540 | G = Geospatial_data(points, attributes) |
---|
541 | assert num.allclose(G.clip(polygon).get_data_points(), |
---|
542 | [[0.5, 0.5], [1, -0.5], [1.5, 0]]) |
---|
543 | assert num.allclose(G.clip(polygon).get_attributes(), [-4, 5, 76]) |
---|
544 | |
---|
545 | def test_clip1(self): |
---|
546 | '''test_clip1(self): |
---|
547 | |
---|
548 | Test that point sets can be clipped by a polygon given as |
---|
549 | another Geospatial dataset |
---|
550 | ''' |
---|
551 | |
---|
552 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
553 | |
---|
554 | points = [[-1, 4], [0.2, 0.5], [1.0, 2.1], [0.4, 0.3], [3.0, 5.3], |
---|
555 | [0, 0], [2.4, 3.3]] |
---|
556 | attributes = [2, -4, 5, 76, -2, 0.1, 3] |
---|
557 | att_dict = {'att1': attributes, |
---|
558 | 'att2': num.array(attributes)+1} |
---|
559 | G = Geospatial_data(points, att_dict) |
---|
560 | |
---|
561 | # First try the unit square |
---|
562 | U = Geospatial_data([[0,0], [1,0], [1,1], [0,1]]) |
---|
563 | assert num.allclose(G.clip(U).get_data_points(), |
---|
564 | [[0.2, 0.5], [0.4, 0.3], [0, 0]]) |
---|
565 | assert num.allclose(G.clip(U).get_attributes('att1'), [-4, 76, 0.1]) |
---|
566 | assert num.allclose(G.clip(U).get_attributes('att2'), [-3, 77, 1.1]) |
---|
567 | |
---|
568 | # Then a more complex polygon |
---|
569 | points = [[0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], |
---|
570 | [0.5, 1.5], [0.5, -0.5]] |
---|
571 | attributes = [2, -4, 5, 76, -2, 0.1] |
---|
572 | G = Geospatial_data(points, attributes) |
---|
573 | polygon = Geospatial_data([[0,0], [1,0], [0.5,-1], [2, -1], |
---|
574 | [2,1], [0,1]]) |
---|
575 | |
---|
576 | assert num.allclose(G.clip(polygon).get_data_points(), |
---|
577 | [[0.5, 0.5], [1, -0.5], [1.5, 0]]) |
---|
578 | assert num.allclose(G.clip(polygon).get_attributes(), [-4, 5, 76]) |
---|
579 | |
---|
580 | def test_clip0_outside(self): |
---|
581 | '''test_clip0_outside(self): |
---|
582 | |
---|
583 | Test that point sets can be clipped outside of a polygon |
---|
584 | ''' |
---|
585 | |
---|
586 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
587 | |
---|
588 | points = [[-1, 4], [0.2, 0.5], [1.0, 2.1], [0.4, 0.3], [3.0, 5.3], |
---|
589 | [0, 0], [2.4, 3.3]] |
---|
590 | attributes = [2, -4, 5, 76, -2, 0.1, 3] |
---|
591 | G = Geospatial_data(points, attributes) |
---|
592 | |
---|
593 | # First try the unit square |
---|
594 | U = [[0,0], [1,0], [1,1], [0,1]] |
---|
595 | assert num.allclose(G.clip_outside(U).get_data_points(), |
---|
596 | [[-1, 4], [1.0, 2.1], [3.0, 5.3], [2.4, 3.3]]) |
---|
597 | assert num.allclose(G.clip_outside(U).get_attributes(), [2, 5, -2, 3]) |
---|
598 | |
---|
599 | # Then a more complex polygon |
---|
600 | polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] |
---|
601 | points = [[0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], |
---|
602 | [0.5, 1.5], [0.5, -0.5]] |
---|
603 | attributes = [2, -4, 5, 76, -2, 0.1] |
---|
604 | G = Geospatial_data(points, attributes) |
---|
605 | assert num.allclose(G.clip_outside(polygon).get_data_points(), |
---|
606 | [[0.5, 1.4], [0.5, 1.5], [0.5, -0.5]]) |
---|
607 | assert num.allclose(G.clip_outside(polygon).get_attributes(), |
---|
608 | [2, -2, 0.1]) |
---|
609 | |
---|
610 | def test_clip1_outside(self): |
---|
611 | '''test_clip1_outside(self): |
---|
612 | |
---|
613 | Test that point sets can be clipped outside of a polygon given as |
---|
614 | another Geospatial dataset |
---|
615 | ''' |
---|
616 | |
---|
617 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
618 | |
---|
619 | points = [[-1, 4], [0.2, 0.5], [1.0, 2.1], [0.4, 0.3], [3.0, 5.3], |
---|
620 | [0, 0], [2.4, 3.3]] |
---|
621 | attributes = [2, -4, 5, 76, -2, 0.1, 3] |
---|
622 | G = Geospatial_data(points, attributes) |
---|
623 | |
---|
624 | # First try the unit square |
---|
625 | U = Geospatial_data([[0,0], [1,0], [1,1], [0,1]]) |
---|
626 | assert num.allclose(G.clip_outside(U).get_data_points(), |
---|
627 | [[-1, 4], [1.0, 2.1], [3.0, 5.3], [2.4, 3.3]]) |
---|
628 | assert num.allclose(G.clip(U).get_attributes(), [-4, 76, 0.1]) |
---|
629 | |
---|
630 | # Then a more complex polygon |
---|
631 | points = [[0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], |
---|
632 | [0.5, 1.5], [0.5, -0.5]] |
---|
633 | attributes = [2, -4, 5, 76, -2, 0.1] |
---|
634 | G = Geospatial_data(points, attributes) |
---|
635 | polygon = Geospatial_data([[0, 0], [1, 0], [0.5, -1], [2, -1], |
---|
636 | [2, 1], [0, 1]]) |
---|
637 | assert num.allclose(G.clip_outside(polygon).get_data_points(), |
---|
638 | [[0.5, 1.4], [0.5, 1.5], [0.5, -0.5]]) |
---|
639 | assert num.allclose(G.clip_outside(polygon).get_attributes(), |
---|
640 | [2, -2, 0.1]) |
---|
641 | |
---|
642 | def test_clip1_inside_outside(self): |
---|
643 | '''test_clip1_inside_outside(self): |
---|
644 | |
---|
645 | Test that point sets can be clipped outside of a polygon given as |
---|
646 | another Geospatial dataset |
---|
647 | ''' |
---|
648 | |
---|
649 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
650 | |
---|
651 | points = [[-1, 4], [0.2, 0.5], [1.0, 2.1], [0.4, 0.3], [3.0, 5.3], |
---|
652 | [0, 0], [2.4, 3.3]] |
---|
653 | attributes = [2, -4, 5, 76, -2, 0.1, 3] |
---|
654 | G = Geospatial_data(points, attributes) |
---|
655 | |
---|
656 | # First try the unit square |
---|
657 | U = Geospatial_data([[0,0], [1,0], [1,1], [0,1]]) |
---|
658 | G1 = G.clip(U) |
---|
659 | assert num.allclose(G1.get_data_points(), |
---|
660 | [[0.2, 0.5], [0.4, 0.3], [0, 0]]) |
---|
661 | assert num.allclose(G.clip(U).get_attributes(), [-4, 76, 0.1]) |
---|
662 | G2 = G.clip_outside(U) |
---|
663 | assert num.allclose(G2.get_data_points(),[[-1, 4], [1.0, 2.1], |
---|
664 | [3.0, 5.3], [2.4, 3.3]]) |
---|
665 | assert num.allclose(G.clip_outside(U).get_attributes(), [2, 5, -2, 3]) |
---|
666 | |
---|
667 | # New ordering |
---|
668 | new_points = [[0.2, 0.5], [0.4, 0.3], [0, 0], [-1, 4], |
---|
669 | [1.0, 2.1], [3.0, 5.3], [2.4, 3.3]] |
---|
670 | new_attributes = [-4, 76, 0.1, 2, 5, -2, 3] |
---|
671 | |
---|
672 | assert num.allclose((G1+G2).get_data_points(), new_points) |
---|
673 | assert num.allclose((G1+G2).get_attributes(), new_attributes) |
---|
674 | |
---|
675 | G = G1+G2 |
---|
676 | FN = 'test_combine.pts' |
---|
677 | G.export_points_file(FN) |
---|
678 | |
---|
679 | # Read it back in |
---|
680 | G3 = Geospatial_data(FN) |
---|
681 | |
---|
682 | # Check result |
---|
683 | assert num.allclose(G3.get_data_points(), new_points) |
---|
684 | assert num.allclose(G3.get_attributes(), new_attributes) |
---|
685 | |
---|
686 | os.remove(FN) |
---|
687 | |
---|
688 | def test_load_csv(self): |
---|
689 | fileName = tempfile.mktemp('.csv') |
---|
690 | file = open(fileName,'w') |
---|
691 | file.write('x,y,elevation speed \n\ |
---|
692 | 1.0 0.0 10.0 0.0\n\ |
---|
693 | 0.0 1.0 0.0 10.0\n\ |
---|
694 | 1.0 0.0 10.4 40.0\n') |
---|
695 | file.close() |
---|
696 | |
---|
697 | results = Geospatial_data(fileName) |
---|
698 | os.remove(fileName) |
---|
699 | assert num.allclose(results.get_data_points(), |
---|
700 | [[1.0, 0.0],[0.0, 1.0], [1.0, 0.0]]) |
---|
701 | assert num.allclose(results.get_attributes(attribute_name='elevation'), |
---|
702 | [10.0, 0.0, 10.4]) |
---|
703 | assert num.allclose(results.get_attributes(attribute_name='speed'), |
---|
704 | [0.0, 10.0, 40.0]) |
---|
705 | |
---|
706 | ################################################################################ |
---|
707 | # Test CSV files |
---|
708 | ################################################################################ |
---|
709 | |
---|
710 | def test_load_csv_lat_long_bad_blocking(self): |
---|
711 | '''test_load_csv_lat_long_bad_blocking(self): |
---|
712 | Different zones in Geo references |
---|
713 | ''' |
---|
714 | |
---|
715 | fileName = tempfile.mktemp('.csv') |
---|
716 | file = open(fileName, 'w') |
---|
717 | file.write('Lati,LONG,z \n\ |
---|
718 | -25.0,180.0,452.688000\n\ |
---|
719 | -34,150.0,459.126000\n') |
---|
720 | file.close() |
---|
721 | |
---|
722 | results = Geospatial_data(fileName, max_read_lines=1, |
---|
723 | load_file_now=False) |
---|
724 | |
---|
725 | try: |
---|
726 | for i in results: |
---|
727 | pass |
---|
728 | except ANUGAError: |
---|
729 | pass |
---|
730 | else: |
---|
731 | msg = 'Different zones in Geo references not caught.' |
---|
732 | raise Exception, msg |
---|
733 | |
---|
734 | os.remove(fileName) |
---|
735 | |
---|
736 | def test_load_csv(self): |
---|
737 | fileName = tempfile.mktemp('.txt') |
---|
738 | file = open(fileName, 'w') |
---|
739 | file.write(' x,y, elevation , speed \n\ |
---|
740 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
741 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
742 | 1.0, 0.0 ,10.4, 40.0\n') |
---|
743 | file.close() |
---|
744 | |
---|
745 | results = Geospatial_data(fileName, max_read_lines=2) |
---|
746 | |
---|
747 | assert num.allclose(results.get_data_points(), |
---|
748 | [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
749 | assert num.allclose(results.get_attributes(attribute_name='elevation'), |
---|
750 | [10.0, 0.0, 10.4]) |
---|
751 | assert num.allclose(results.get_attributes(attribute_name='speed'), |
---|
752 | [0.0, 10.0, 40.0]) |
---|
753 | |
---|
754 | # Blocking |
---|
755 | geo_list = [] |
---|
756 | for i in results: |
---|
757 | geo_list.append(i) |
---|
758 | |
---|
759 | assert num.allclose(geo_list[0].get_data_points(), |
---|
760 | [[1.0, 0.0],[0.0, 1.0]]) |
---|
761 | assert num.allclose(geo_list[0].get_attributes(attribute_name='elevation'), |
---|
762 | [10.0, 0.0]) |
---|
763 | assert num.allclose(geo_list[1].get_data_points(), |
---|
764 | [[1.0, 0.0]]) |
---|
765 | assert num.allclose(geo_list[1].get_attributes(attribute_name='elevation'), |
---|
766 | [10.4]) |
---|
767 | |
---|
768 | os.remove(fileName) |
---|
769 | |
---|
770 | def test_load_csv_bad(self): |
---|
771 | '''test_load_csv_bad(self): |
---|
772 | header column, body column missmatch |
---|
773 | (Format error) |
---|
774 | ''' |
---|
775 | |
---|
776 | fileName = tempfile.mktemp('.txt') |
---|
777 | file = open(fileName, 'w') |
---|
778 | file.write(' elevation , speed \n\ |
---|
779 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
780 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
781 | 1.0, 0.0 ,10.4, 40.0\n') |
---|
782 | file.close() |
---|
783 | |
---|
784 | results = Geospatial_data(fileName, max_read_lines=2, |
---|
785 | load_file_now=False) |
---|
786 | |
---|
787 | # Blocking |
---|
788 | geo_list = [] |
---|
789 | try: |
---|
790 | for i in results: |
---|
791 | geo_list.append(i) |
---|
792 | except SyntaxError: |
---|
793 | pass |
---|
794 | else: |
---|
795 | msg = 'Bad file did not raise error!' |
---|
796 | raise Exception, msg |
---|
797 | os.remove(fileName) |
---|
798 | |
---|
799 | def test_load_csv_badII(self): |
---|
800 | '''test_load_csv_bad(self): |
---|
801 | header column, body column missmatch |
---|
802 | (Format error) |
---|
803 | ''' |
---|
804 | |
---|
805 | fileName = tempfile.mktemp('.txt') |
---|
806 | file = open(fileName, 'w') |
---|
807 | file.write(' x,y,elevation , speed \n\ |
---|
808 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
809 | 0.0, 1.0, 0.0, 10\n\ |
---|
810 | 1.0, 0.0 ,10.4 yeah\n') |
---|
811 | file.close() |
---|
812 | |
---|
813 | results = Geospatial_data(fileName, max_read_lines=2, |
---|
814 | load_file_now=False) |
---|
815 | |
---|
816 | # Blocking |
---|
817 | geo_list = [] |
---|
818 | try: |
---|
819 | for i in results: |
---|
820 | geo_list.append(i) |
---|
821 | except SyntaxError: |
---|
822 | pass |
---|
823 | else: |
---|
824 | msg = 'Bad file did not raise error!' |
---|
825 | raise Exception, msg |
---|
826 | os.remove(fileName) |
---|
827 | |
---|
828 | def test_load_csv_badIII(self): |
---|
829 | '''test_load_csv_bad(self): |
---|
830 | space delimited |
---|
831 | ''' |
---|
832 | |
---|
833 | fileName = tempfile.mktemp('.txt') |
---|
834 | file = open(fileName, 'w') |
---|
835 | file.write(' x y elevation speed \n\ |
---|
836 | 1. 0.0 10.0 0.0\n\ |
---|
837 | 0.0 1.0 0.0 10.0\n\ |
---|
838 | 1.0 0.0 10.4 40.0\n') |
---|
839 | file.close() |
---|
840 | |
---|
841 | try: |
---|
842 | results = Geospatial_data(fileName, max_read_lines=2, |
---|
843 | load_file_now=True) |
---|
844 | except SyntaxError: |
---|
845 | pass |
---|
846 | else: |
---|
847 | msg = 'Bad file did not raise error!' |
---|
848 | raise Exception, msg |
---|
849 | os.remove(fileName) |
---|
850 | |
---|
851 | def test_load_csv_badIV(self): |
---|
852 | ''' test_load_csv_bad(self): |
---|
853 | header column, body column missmatch |
---|
854 | (Format error) |
---|
855 | ''' |
---|
856 | |
---|
857 | fileName = tempfile.mktemp('.txt') |
---|
858 | file = open(fileName, 'w') |
---|
859 | file.write(' x,y,elevation , speed \n\ |
---|
860 | 1.0, 0.0, 10.0, wow\n\ |
---|
861 | 0.0, 1.0, 0.0, ha\n\ |
---|
862 | 1.0, 0.0 ,10.4, yeah\n') |
---|
863 | file.close() |
---|
864 | |
---|
865 | results = Geospatial_data(fileName, max_read_lines=2, |
---|
866 | load_file_now=False) |
---|
867 | |
---|
868 | # Blocking |
---|
869 | geo_list = [] |
---|
870 | try: |
---|
871 | for i in results: |
---|
872 | geo_list.append(i) |
---|
873 | except SyntaxError: |
---|
874 | pass |
---|
875 | else: |
---|
876 | msg = 'Bad file did not raise error!' |
---|
877 | raise Exception, msg |
---|
878 | os.remove(fileName) |
---|
879 | |
---|
880 | def test_load_pts_blocking(self): |
---|
881 | #This is pts! |
---|
882 | fileName = tempfile.mktemp('.txt') |
---|
883 | file = open(fileName, 'w') |
---|
884 | file.write(' x,y, elevation , speed \n\ |
---|
885 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
886 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
887 | 1.0, 0.0 ,10.4, 40.0\n') |
---|
888 | file.close() |
---|
889 | |
---|
890 | pts_file = tempfile.mktemp('.pts') |
---|
891 | |
---|
892 | convert = Geospatial_data(fileName) |
---|
893 | convert.export_points_file(pts_file) |
---|
894 | results = Geospatial_data(pts_file, max_read_lines=2) |
---|
895 | |
---|
896 | assert num.allclose(results.get_data_points(), |
---|
897 | [[1.0, 0.0],[0.0, 1.0], [1.0, 0.0]]) |
---|
898 | assert num.allclose(results.get_attributes(attribute_name='elevation'), |
---|
899 | [10.0, 0.0, 10.4]) |
---|
900 | assert num.allclose(results.get_attributes(attribute_name='speed'), |
---|
901 | [0.0, 10.0, 40.0]) |
---|
902 | |
---|
903 | # Blocking |
---|
904 | geo_list = [] |
---|
905 | for i in results: |
---|
906 | geo_list.append(i) |
---|
907 | assert num.allclose(geo_list[0].get_data_points(), |
---|
908 | [[1.0, 0.0],[0.0, 1.0]]) |
---|
909 | assert num.allclose(geo_list[0].get_attributes(attribute_name='elevation'), |
---|
910 | [10.0, 0.0]) |
---|
911 | assert num.allclose(geo_list[1].get_data_points(), |
---|
912 | [[1.0, 0.0]]) |
---|
913 | assert num.allclose(geo_list[1].get_attributes(attribute_name='elevation'), |
---|
914 | [10.4]) |
---|
915 | |
---|
916 | os.remove(fileName) |
---|
917 | os.remove(pts_file) |
---|
918 | |
---|
919 | def verbose_test_load_pts_blocking(self): |
---|
920 | fileName = tempfile.mktemp('.txt') |
---|
921 | file = open(fileName, 'w') |
---|
922 | file.write(' x,y, elevation , speed \n\ |
---|
923 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
924 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
925 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
926 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
927 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
928 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
929 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
930 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
931 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
932 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
933 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
934 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
935 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
936 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
937 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
938 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
939 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
940 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
941 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
942 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
943 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
944 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
945 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
946 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
947 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
948 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
949 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
950 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
951 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
952 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
953 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
954 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
955 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
956 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
957 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
958 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
959 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
960 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
961 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
962 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
963 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
964 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
965 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
966 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
967 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
968 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
969 | 1.0, 0.0 ,10.4, 40.0\n') |
---|
970 | file.close() |
---|
971 | |
---|
972 | pts_file = tempfile.mktemp('.pts') |
---|
973 | convert = Geospatial_data(fileName) |
---|
974 | convert.export_points_file(pts_file) |
---|
975 | results = Geospatial_data(pts_file, max_read_lines=2, verbose=True) |
---|
976 | |
---|
977 | # Blocking |
---|
978 | geo_list = [] |
---|
979 | for i in results: |
---|
980 | geo_list.append(i) |
---|
981 | assert num.allclose(geo_list[0].get_data_points(), |
---|
982 | [[1.0, 0.0], [0.0, 1.0]]) |
---|
983 | assert num.allclose(geo_list[0].get_attributes(attribute_name='elevation'), |
---|
984 | [10.0, 0.0]) |
---|
985 | assert num.allclose(geo_list[1].get_data_points(), |
---|
986 | [[1.0, 0.0],[0.0, 1.0] ]) |
---|
987 | assert num.allclose(geo_list[1].get_attributes(attribute_name='elevation'), |
---|
988 | [10.0, 0.0]) |
---|
989 | |
---|
990 | os.remove(fileName) |
---|
991 | os.remove(pts_file) |
---|
992 | |
---|
993 | def test_new_export_pts_file(self): |
---|
994 | att_dict = {} |
---|
995 | pointlist = num.array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) |
---|
996 | att_dict['elevation'] = num.array([10.1, 0.0, 10.4]) |
---|
997 | att_dict['brightness'] = num.array([10.0, 1.0, 10.4]) |
---|
998 | |
---|
999 | fileName = tempfile.mktemp('.pts') |
---|
1000 | G = Geospatial_data(pointlist, att_dict) |
---|
1001 | G.export_points_file(fileName) |
---|
1002 | results = Geospatial_data(file_name = fileName) |
---|
1003 | os.remove(fileName) |
---|
1004 | |
---|
1005 | assert num.allclose(results.get_data_points(), |
---|
1006 | [[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]]) |
---|
1007 | assert num.allclose(results.get_attributes(attribute_name='elevation'), |
---|
1008 | [10.1, 0.0, 10.4]) |
---|
1009 | answer = [10.0, 1.0, 10.4] |
---|
1010 | assert num.allclose(results.get_attributes(attribute_name='brightness'), |
---|
1011 | answer) |
---|
1012 | |
---|
1013 | def test_new_export_absolute_pts_file(self): |
---|
1014 | att_dict = {} |
---|
1015 | pointlist = num.array([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]]) |
---|
1016 | att_dict['elevation'] = num.array([10.1, 0.0, 10.4]) |
---|
1017 | att_dict['brightness'] = num.array([10.0, 1.0, 10.4]) |
---|
1018 | geo_ref = Geo_reference(50, 25, 55) |
---|
1019 | |
---|
1020 | fileName = tempfile.mktemp('.pts') |
---|
1021 | G = Geospatial_data(pointlist, att_dict, geo_ref) |
---|
1022 | G.export_points_file(fileName, absolute=True) |
---|
1023 | results = Geospatial_data(file_name = fileName) |
---|
1024 | os.remove(fileName) |
---|
1025 | |
---|
1026 | msg = ('results.get_data_points()=\n%s\nbut G.get_data_points(True)=\n%s' |
---|
1027 | % (str(results.get_data_points()), str(G.get_data_points(True)))) |
---|
1028 | assert num.allclose(results.get_data_points(), |
---|
1029 | G.get_data_points(True)), msg |
---|
1030 | msg = ("results.get_attributes(attribute_name='elevation')=%s" |
---|
1031 | % str(results.get_attributes(attribute_name='elevation'))) |
---|
1032 | assert num.allclose(results.get_attributes(attribute_name='elevation'), |
---|
1033 | [10.1, 0.0, 10.4]), msg |
---|
1034 | answer = [10.0, 1.0, 10.4] |
---|
1035 | msg = ("results.get_attributes(attribute_name='brightness')=%s, " |
---|
1036 | 'answer=%s' % |
---|
1037 | (str(results.get_attributes(attribute_name='brightness')), |
---|
1038 | str(answer))) |
---|
1039 | assert num.allclose(results.get_attributes(attribute_name='brightness'), |
---|
1040 | answer), msg |
---|
1041 | |
---|
1042 | def test_loadpts(self): |
---|
1043 | from Scientific.IO.NetCDF import NetCDFFile |
---|
1044 | |
---|
1045 | fileName = tempfile.mktemp('.pts') |
---|
1046 | # NetCDF file definition |
---|
1047 | outfile = NetCDFFile(fileName, netcdf_mode_w) |
---|
1048 | |
---|
1049 | # dimension definitions |
---|
1050 | outfile.createDimension('number_of_points', 3) |
---|
1051 | outfile.createDimension('number_of_dimensions', 2) # This is 2d data |
---|
1052 | |
---|
1053 | # variable definitions |
---|
1054 | outfile.createVariable('points', netcdf_float, ('number_of_points', |
---|
1055 | 'number_of_dimensions')) |
---|
1056 | outfile.createVariable('elevation', netcdf_float, ('number_of_points',)) |
---|
1057 | |
---|
1058 | # Get handles to the variables |
---|
1059 | points = outfile.variables['points'] |
---|
1060 | elevation = outfile.variables['elevation'] |
---|
1061 | |
---|
1062 | points[0, :] = [1.0,0.0] |
---|
1063 | elevation[0] = 10.0 |
---|
1064 | points[1, :] = [0.0,1.0] |
---|
1065 | elevation[1] = 0.0 |
---|
1066 | points[2, :] = [1.0,0.0] |
---|
1067 | elevation[2] = 10.4 |
---|
1068 | |
---|
1069 | outfile.close() |
---|
1070 | |
---|
1071 | results = Geospatial_data(file_name = fileName) |
---|
1072 | os.remove(fileName) |
---|
1073 | |
---|
1074 | answer = [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]] |
---|
1075 | assert num.allclose(results.get_data_points(), |
---|
1076 | [[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]]) |
---|
1077 | assert num.allclose(results.get_attributes(attribute_name='elevation'), |
---|
1078 | [10.0, 0.0, 10.4]) |
---|
1079 | |
---|
1080 | def test_writepts(self): |
---|
1081 | '''Test that storage of x,y,attributes works''' |
---|
1082 | |
---|
1083 | att_dict = {} |
---|
1084 | pointlist = num.array([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]]) |
---|
1085 | att_dict['elevation'] = num.array([10.0, 0.0, 10.4]) |
---|
1086 | att_dict['brightness'] = num.array([10.0, 0.0, 10.4]) |
---|
1087 | geo_reference=Geo_reference(56, 1.9, 1.9) |
---|
1088 | |
---|
1089 | # Test pts format |
---|
1090 | fileName = tempfile.mktemp('.pts') |
---|
1091 | G = Geospatial_data(pointlist, att_dict, geo_reference) |
---|
1092 | G.export_points_file(fileName, False) |
---|
1093 | results = Geospatial_data(file_name=fileName) |
---|
1094 | os.remove(fileName) |
---|
1095 | |
---|
1096 | assert num.allclose(results.get_data_points(False), |
---|
1097 | [[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]]) |
---|
1098 | assert num.allclose(results.get_attributes('elevation'), |
---|
1099 | [10.0, 0.0, 10.4]) |
---|
1100 | answer = [10.0, 0.0, 10.4] |
---|
1101 | assert num.allclose(results.get_attributes('brightness'), answer) |
---|
1102 | self.failUnless(geo_reference == geo_reference, |
---|
1103 | 'test_writepts failed. Test geo_reference') |
---|
1104 | |
---|
1105 | def test_write_csv_attributes(self): |
---|
1106 | '''Test that storage of x,y,attributes works''' |
---|
1107 | |
---|
1108 | att_dict = {} |
---|
1109 | pointlist = num.array([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]]) |
---|
1110 | att_dict['elevation'] = num.array([10.0, 0.0, 10.4]) |
---|
1111 | att_dict['brightness'] = num.array([10.0, 0.0, 10.4]) |
---|
1112 | geo_reference=Geo_reference(56, 0, 0) |
---|
1113 | |
---|
1114 | # Test txt format |
---|
1115 | fileName = tempfile.mktemp('.txt') |
---|
1116 | G = Geospatial_data(pointlist, att_dict, geo_reference) |
---|
1117 | G.export_points_file(fileName) |
---|
1118 | results = Geospatial_data(file_name=fileName) |
---|
1119 | os.remove(fileName) |
---|
1120 | |
---|
1121 | assert num.allclose(results.get_data_points(False), |
---|
1122 | [[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]]) |
---|
1123 | assert num.allclose(results.get_attributes('elevation'), |
---|
1124 | [10.0, 0.0, 10.4]) |
---|
1125 | answer = [10.0, 0.0, 10.4] |
---|
1126 | assert num.allclose(results.get_attributes('brightness'), answer) |
---|
1127 | |
---|
1128 | def test_write_csv_attributes_lat_long(self): |
---|
1129 | '''Test that storage of x,y,attributes works''' |
---|
1130 | |
---|
1131 | att_dict = {} |
---|
1132 | pointlist = num.array([[-21.5, 114.5], [-21.6, 114.5], [-21.7, 114.5]]) |
---|
1133 | att_dict['elevation'] = num.array([10.0, 0.0, 10.4]) |
---|
1134 | att_dict['brightness'] = num.array([10.0, 0.0, 10.4]) |
---|
1135 | |
---|
1136 | # Test txt format |
---|
1137 | fileName = tempfile.mktemp('.txt') |
---|
1138 | G = Geospatial_data(pointlist, att_dict, points_are_lats_longs=True) |
---|
1139 | G.export_points_file(fileName, as_lat_long=True) |
---|
1140 | results = Geospatial_data(file_name=fileName) |
---|
1141 | os.remove(fileName) |
---|
1142 | |
---|
1143 | assert num.allclose(results.get_data_points(False, as_lat_long=True), |
---|
1144 | pointlist) |
---|
1145 | assert num.allclose(results.get_attributes('elevation'), |
---|
1146 | [10.0, 0.0, 10.4]) |
---|
1147 | answer = [10.0, 0.0, 10.4] |
---|
1148 | assert num.allclose(results.get_attributes('brightness'), answer) |
---|
1149 | |
---|
1150 | def test_writepts_no_attributes(self): |
---|
1151 | '''Test that storage of x,y alone works''' |
---|
1152 | |
---|
1153 | att_dict = {} |
---|
1154 | pointlist = num.array([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]]) |
---|
1155 | geo_reference=Geo_reference(56, 1.9, 1.9) |
---|
1156 | |
---|
1157 | # Test pts format |
---|
1158 | fileName = tempfile.mktemp('.pts') |
---|
1159 | G = Geospatial_data(pointlist, None, geo_reference) |
---|
1160 | G.export_points_file(fileName, False) |
---|
1161 | results = Geospatial_data(file_name=fileName) |
---|
1162 | os.remove(fileName) |
---|
1163 | |
---|
1164 | assert num.allclose(results.get_data_points(False), |
---|
1165 | [[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]]) |
---|
1166 | self.failUnless(geo_reference == geo_reference, |
---|
1167 | 'test_writepts failed. Test geo_reference') |
---|
1168 | |
---|
1169 | def test_write_csv_no_attributes(self): |
---|
1170 | '''Test that storage of x,y alone works''' |
---|
1171 | |
---|
1172 | att_dict = {} |
---|
1173 | pointlist = num.array([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]]) |
---|
1174 | geo_reference=Geo_reference(56,0,0) |
---|
1175 | |
---|
1176 | # Test format |
---|
1177 | fileName = tempfile.mktemp('.txt') |
---|
1178 | G = Geospatial_data(pointlist, None, geo_reference) |
---|
1179 | G.export_points_file(fileName) |
---|
1180 | results = Geospatial_data(file_name=fileName) |
---|
1181 | os.remove(fileName) |
---|
1182 | |
---|
1183 | assert num.allclose(results.get_data_points(False), |
---|
1184 | [[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]]) |
---|
1185 | |
---|
1186 | ################################################################################ |
---|
1187 | # Check bad PTS files. |
---|
1188 | ################################################################################ |
---|
1189 | |
---|
1190 | def test_load_bad_no_file_pts(self): |
---|
1191 | fileName = tempfile.mktemp('.pts') |
---|
1192 | try: |
---|
1193 | results = Geospatial_data(file_name=fileName) |
---|
1194 | except IOError: |
---|
1195 | pass |
---|
1196 | else: |
---|
1197 | msg = 'imaginary file did not raise error!' |
---|
1198 | raise Exception, msg |
---|
1199 | |
---|
1200 | def test_create_from_pts_file(self): |
---|
1201 | from Scientific.IO.NetCDF import NetCDFFile |
---|
1202 | |
---|
1203 | # NetCDF file definition |
---|
1204 | FN = 'test_points.pts' |
---|
1205 | outfile = NetCDFFile(FN, netcdf_mode_w) |
---|
1206 | |
---|
1207 | # dimension definitions |
---|
1208 | outfile.createDimension('number_of_points', 3) |
---|
1209 | outfile.createDimension('number_of_dimensions', 2) # This is 2d data |
---|
1210 | |
---|
1211 | # variable definitions |
---|
1212 | outfile.createVariable('points', netcdf_float, ('number_of_points', |
---|
1213 | 'number_of_dimensions')) |
---|
1214 | outfile.createVariable('elevation', netcdf_float, ('number_of_points',)) |
---|
1215 | |
---|
1216 | # Get handles to the variables |
---|
1217 | points = outfile.variables['points'] |
---|
1218 | elevation = outfile.variables['elevation'] |
---|
1219 | |
---|
1220 | points[0, :] = [1.0,0.0] |
---|
1221 | elevation[0] = 10.0 |
---|
1222 | points[1, :] = [0.0,1.0] |
---|
1223 | elevation[1] = 0.0 |
---|
1224 | points[2, :] = [1.0,0.0] |
---|
1225 | elevation[2] = 10.4 |
---|
1226 | |
---|
1227 | outfile.close() |
---|
1228 | |
---|
1229 | G = Geospatial_data(file_name = FN) |
---|
1230 | |
---|
1231 | assert num.allclose(G.get_geo_reference().get_xllcorner(), 0.0) |
---|
1232 | assert num.allclose(G.get_geo_reference().get_yllcorner(), 0.0) |
---|
1233 | assert num.allclose(G.get_data_points(), |
---|
1234 | [[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]]) |
---|
1235 | assert num.allclose(G.get_attributes(), [10.0, 0.0, 10.4]) |
---|
1236 | os.remove(FN) |
---|
1237 | |
---|
1238 | def test_create_from_pts_file_with_geo(self): |
---|
1239 | '''Test if Geospatial data is correctly instantiated from a pts file.''' |
---|
1240 | |
---|
1241 | from Scientific.IO.NetCDF import NetCDFFile |
---|
1242 | |
---|
1243 | # NetCDF file definition |
---|
1244 | FN = 'test_points.pts' |
---|
1245 | outfile = NetCDFFile(FN, netcdf_mode_w) |
---|
1246 | |
---|
1247 | # Make up an arbitrary georef |
---|
1248 | xll = 0.1 |
---|
1249 | yll = 20 |
---|
1250 | geo_reference=Geo_reference(56, xll, yll) |
---|
1251 | geo_reference.write_NetCDF(outfile) |
---|
1252 | |
---|
1253 | # dimension definitions |
---|
1254 | outfile.createDimension('number_of_points', 3) |
---|
1255 | outfile.createDimension('number_of_dimensions', 2) # This is 2d data |
---|
1256 | |
---|
1257 | # variable definitions |
---|
1258 | outfile.createVariable('points', netcdf_float, ('number_of_points', |
---|
1259 | 'number_of_dimensions')) |
---|
1260 | outfile.createVariable('elevation', netcdf_float, ('number_of_points',)) |
---|
1261 | |
---|
1262 | # Get handles to the variables |
---|
1263 | points = outfile.variables['points'] |
---|
1264 | elevation = outfile.variables['elevation'] |
---|
1265 | |
---|
1266 | points[0, :] = [1.0,0.0] |
---|
1267 | elevation[0] = 10.0 |
---|
1268 | points[1, :] = [0.0,1.0] |
---|
1269 | elevation[1] = 0.0 |
---|
1270 | points[2, :] = [1.0,0.0] |
---|
1271 | elevation[2] = 10.4 |
---|
1272 | |
---|
1273 | outfile.close() |
---|
1274 | |
---|
1275 | G = Geospatial_data(file_name = FN) |
---|
1276 | |
---|
1277 | assert num.allclose(G.get_geo_reference().get_xllcorner(), xll) |
---|
1278 | assert num.allclose(G.get_geo_reference().get_yllcorner(), yll) |
---|
1279 | assert num.allclose(G.get_data_points(), [[1.0+xll, 0.0+yll], |
---|
1280 | [0.0+xll, 1.0+yll], |
---|
1281 | [1.0+xll, 0.0+yll]]) |
---|
1282 | assert num.allclose(G.get_attributes(), [10.0, 0.0, 10.4]) |
---|
1283 | |
---|
1284 | os.remove(FN) |
---|
1285 | |
---|
1286 | def test_add_(self): |
---|
1287 | '''test_add_(self): |
---|
1288 | adds an txt and pts files, reads the files and adds them |
---|
1289 | checking results are correct |
---|
1290 | ''' |
---|
1291 | |
---|
1292 | # create files |
---|
1293 | att_dict1 = {} |
---|
1294 | pointlist1 = num.array([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]]) |
---|
1295 | att_dict1['elevation'] = num.array([-10.0, 0.0, 10.4]) |
---|
1296 | att_dict1['brightness'] = num.array([10.0, 0.0, 10.4]) |
---|
1297 | geo_reference1 = Geo_reference(56, 2.0, 1.0) |
---|
1298 | |
---|
1299 | att_dict2 = {} |
---|
1300 | pointlist2 = num.array([[2.0, 1.0], [1.0, 2.0], [2.0, 1.0]]) |
---|
1301 | att_dict2['elevation'] = num.array([1.0, 15.0, 1.4]) |
---|
1302 | att_dict2['brightness'] = num.array([14.0, 1.0, -12.4]) |
---|
1303 | geo_reference2 = Geo_reference(56, 1.0, 2.0) |
---|
1304 | |
---|
1305 | G1 = Geospatial_data(pointlist1, att_dict1, geo_reference1) |
---|
1306 | G2 = Geospatial_data(pointlist2, att_dict2, geo_reference2) |
---|
1307 | |
---|
1308 | fileName1 = tempfile.mktemp('.txt') |
---|
1309 | fileName2 = tempfile.mktemp('.pts') |
---|
1310 | |
---|
1311 | # makes files |
---|
1312 | G1.export_points_file(fileName1) |
---|
1313 | G2.export_points_file(fileName2) |
---|
1314 | |
---|
1315 | # add files |
---|
1316 | G3 = Geospatial_data(file_name=fileName1) |
---|
1317 | G4 = Geospatial_data(file_name=fileName2) |
---|
1318 | G = G3 + G4 |
---|
1319 | |
---|
1320 | #read results |
---|
1321 | assert num.allclose(G.get_data_points(), |
---|
1322 | [[ 3.0, 1.0], [ 2.0, 2.0], |
---|
1323 | [ 3.0, 1.0], [ 3.0, 3.0], |
---|
1324 | [ 2.0, 4.0], [ 3.0, 3.0]]) |
---|
1325 | assert num.allclose(G.get_attributes(attribute_name='elevation'), |
---|
1326 | [-10.0, 0.0, 10.4, 1.0, 15.0, 1.4]) |
---|
1327 | answer = [10.0, 0.0, 10.4, 14.0, 1.0, -12.4] |
---|
1328 | assert num.allclose(G.get_attributes(attribute_name='brightness'), |
---|
1329 | answer) |
---|
1330 | self.failUnless(G.get_geo_reference() == geo_reference1, |
---|
1331 | 'test_writepts failed. Test geo_reference') |
---|
1332 | |
---|
1333 | os.remove(fileName1) |
---|
1334 | os.remove(fileName2) |
---|
1335 | |
---|
1336 | def test_ensure_absolute(self): |
---|
1337 | points = [[2.0, 0.0], [1.0, 1.0], |
---|
1338 | [2.0, 0.0], [2.0, 2.0], |
---|
1339 | [1.0, 3.0], [2.0, 2.0]] |
---|
1340 | new_points = ensure_absolute(points) |
---|
1341 | |
---|
1342 | assert num.allclose(new_points, points) |
---|
1343 | |
---|
1344 | points = num.array([[2.0, 0.0], [1.0, 1.0], |
---|
1345 | [2.0, 0.0], [2.0, 2.0], |
---|
1346 | [1.0, 3.0], [2.0, 2.0]]) |
---|
1347 | new_points = ensure_absolute(points) |
---|
1348 | |
---|
1349 | assert num.allclose(new_points, points) |
---|
1350 | |
---|
1351 | ab_points = num.array([[2.0, 0.0], [1.0, 1.0], |
---|
1352 | [2.0, 0.0], [2.0, 2.0], |
---|
1353 | [1.0, 3.0], [2.0, 2.0]]) |
---|
1354 | |
---|
1355 | mesh_origin = (56, 290000, 618000) # zone, easting, northing |
---|
1356 | data_points = num.zeros((ab_points.shape), num.float) |
---|
1357 | |
---|
1358 | #Shift datapoints according to new origins |
---|
1359 | for k in range(len(ab_points)): |
---|
1360 | data_points[k][0] = ab_points[k][0] - mesh_origin[1] |
---|
1361 | data_points[k][1] = ab_points[k][1] - mesh_origin[2] |
---|
1362 | new_points = ensure_absolute(data_points, geo_reference=mesh_origin) |
---|
1363 | |
---|
1364 | assert num.allclose(new_points, ab_points) |
---|
1365 | |
---|
1366 | geo = Geo_reference(56,67,-56) |
---|
1367 | data_points = geo.change_points_geo_ref(ab_points) |
---|
1368 | new_points = ensure_absolute(data_points, geo_reference=geo) |
---|
1369 | |
---|
1370 | assert num.allclose(new_points, ab_points) |
---|
1371 | |
---|
1372 | geo_reference = Geo_reference(56, 100, 200) |
---|
1373 | ab_points = [[1.0, 2.1], [3.0, 5.3]] |
---|
1374 | points = geo_reference.change_points_geo_ref(ab_points) |
---|
1375 | attributes = [2, 4] |
---|
1376 | G = Geospatial_data(points, attributes, geo_reference=geo_reference) |
---|
1377 | new_points = ensure_absolute(G) |
---|
1378 | |
---|
1379 | assert num.allclose(new_points, ab_points) |
---|
1380 | |
---|
1381 | def test_ensure_geospatial(self): |
---|
1382 | points = [[2.0, 0.0], [1.0, 1.0], |
---|
1383 | [2.0, 0.0], [2.0, 2.0], |
---|
1384 | [1.0, 3.0], [2.0, 2.0]] |
---|
1385 | new_points = ensure_geospatial(points) |
---|
1386 | assert num.allclose(new_points.get_data_points(absolute=True), points) |
---|
1387 | |
---|
1388 | points = num.array([[2.0, 0.0], [1.0, 1.0], |
---|
1389 | [2.0, 0.0], [2.0, 2.0], |
---|
1390 | [1.0, 3.0], [2.0, 2.0]]) |
---|
1391 | new_points = ensure_geospatial(points) |
---|
1392 | assert num.allclose(new_points.get_data_points(absolute=True), points) |
---|
1393 | |
---|
1394 | ab_points = num.array([[2.0, 0.0],[1.0, 1.0], |
---|
1395 | [2.0, 0.0],[2.0, 2.0], |
---|
1396 | [1.0, 3.0],[2.0, 2.0]]) |
---|
1397 | mesh_origin = (56, 290000, 618000) # zone, easting, northing |
---|
1398 | data_points = num.zeros((ab_points.shape), num.float) |
---|
1399 | |
---|
1400 | #Shift datapoints according to new origins |
---|
1401 | for k in range(len(ab_points)): |
---|
1402 | data_points[k][0] = ab_points[k][0] - mesh_origin[1] |
---|
1403 | data_points[k][1] = ab_points[k][1] - mesh_origin[2] |
---|
1404 | new_geospatial = ensure_geospatial(data_points, |
---|
1405 | geo_reference=mesh_origin) |
---|
1406 | new_points = new_geospatial.get_data_points(absolute=True) |
---|
1407 | assert num.allclose(new_points, ab_points) |
---|
1408 | |
---|
1409 | geo = Geo_reference(56, 67, -56) |
---|
1410 | data_points = geo.change_points_geo_ref(ab_points) |
---|
1411 | new_geospatial = ensure_geospatial(data_points, geo_reference=geo) |
---|
1412 | new_points = new_geospatial.get_data_points(absolute=True) |
---|
1413 | msg = ('new_points=\n%s\nab_points=\n%s' |
---|
1414 | % (str(new_points), str(ab_points))) |
---|
1415 | assert num.allclose(new_points, ab_points), msg |
---|
1416 | |
---|
1417 | geo_reference = Geo_reference(56, 100, 200) |
---|
1418 | ab_points = [[1.0, 2.1], [3.0, 5.3]] |
---|
1419 | points = geo_reference.change_points_geo_ref(ab_points) |
---|
1420 | attributes = [2, 4] |
---|
1421 | G = Geospatial_data(points, attributes, geo_reference=geo_reference) |
---|
1422 | new_geospatial = ensure_geospatial(G) |
---|
1423 | new_points = new_geospatial.get_data_points(absolute=True) |
---|
1424 | assert num.allclose(new_points, ab_points) |
---|
1425 | |
---|
1426 | def test_isinstance(self): |
---|
1427 | fileName = tempfile.mktemp('.csv') |
---|
1428 | file = open(fileName, 'w') |
---|
1429 | file.write('x,y, elevation , speed \n\ |
---|
1430 | 1.0, 0.0, 10.0, 0.0\n\ |
---|
1431 | 0.0, 1.0, 0.0, 10.0\n\ |
---|
1432 | 1.0, 0.0, 10.4, 40.0\n') |
---|
1433 | file.close() |
---|
1434 | |
---|
1435 | results = Geospatial_data(fileName) |
---|
1436 | assert num.allclose(results.get_data_points(absolute=True), |
---|
1437 | [[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]]) |
---|
1438 | assert num.allclose(results.get_attributes(attribute_name='elevation'), |
---|
1439 | [10.0, 0.0, 10.4]) |
---|
1440 | assert num.allclose(results.get_attributes(attribute_name='speed'), |
---|
1441 | [0.0, 10.0, 40.0]) |
---|
1442 | |
---|
1443 | os.remove(fileName) |
---|
1444 | |
---|
1445 | def test_no_constructors(self): |
---|
1446 | try: |
---|
1447 | G = Geospatial_data() |
---|
1448 | except ValueError: |
---|
1449 | pass |
---|
1450 | else: |
---|
1451 | msg = 'Instance must have a filename or data points' |
---|
1452 | raise Exception, msg |
---|
1453 | |
---|
1454 | def test_load_csv_lat_long(self): |
---|
1455 | '''comma delimited''' |
---|
1456 | |
---|
1457 | fileName = tempfile.mktemp('.csv') |
---|
1458 | file = open(fileName, 'w') |
---|
1459 | file.write('long,lat, elevation, yeah \n\ |
---|
1460 | 150.916666667,-34.50,452.688000, 10\n\ |
---|
1461 | 150.0,-34,459.126000, 10\n') |
---|
1462 | file.close() |
---|
1463 | |
---|
1464 | results = Geospatial_data(fileName) |
---|
1465 | os.remove(fileName) |
---|
1466 | points = results.get_data_points() |
---|
1467 | |
---|
1468 | assert num.allclose(points[0][0], 308728.009) |
---|
1469 | assert num.allclose(points[0][1], 6180432.601) |
---|
1470 | assert num.allclose(points[1][0], 222908.705) |
---|
1471 | assert num.allclose(points[1][1], 6233785.284) |
---|
1472 | |
---|
1473 | def test_load_csv_lat_longII(self): |
---|
1474 | '''comma delimited''' |
---|
1475 | |
---|
1476 | fileName = tempfile.mktemp('.csv') |
---|
1477 | file = open(fileName, 'w') |
---|
1478 | file.write('Lati,LONG,z \n\ |
---|
1479 | -34.50,150.916666667,452.688000\n\ |
---|
1480 | -34,150.0,459.126000\n') |
---|
1481 | file.close() |
---|
1482 | |
---|
1483 | results = Geospatial_data(fileName) |
---|
1484 | os.remove(fileName) |
---|
1485 | points = results.get_data_points() |
---|
1486 | |
---|
1487 | assert num.allclose(points[0][0], 308728.009) |
---|
1488 | assert num.allclose(points[0][1], 6180432.601) |
---|
1489 | assert num.allclose(points[1][0], 222908.705) |
---|
1490 | assert num.allclose(points[1][1], 6233785.284) |
---|
1491 | |
---|
1492 | def test_load_csv_lat_long_bad(self): |
---|
1493 | '''comma delimited''' |
---|
1494 | |
---|
1495 | fileName = tempfile.mktemp('.csv') |
---|
1496 | file = open(fileName, 'w') |
---|
1497 | file.write('Lati,LONG,z \n\ |
---|
1498 | -25.0,180.0,452.688000\n\ |
---|
1499 | -34,150.0,459.126000\n') |
---|
1500 | file.close() |
---|
1501 | |
---|
1502 | try: |
---|
1503 | results = Geospatial_data(fileName) |
---|
1504 | except ANUGAError: |
---|
1505 | pass |
---|
1506 | else: |
---|
1507 | msg = 'Different zones in Geo references not caught.' |
---|
1508 | raise Exception, msg |
---|
1509 | |
---|
1510 | os.remove(fileName) |
---|
1511 | |
---|
1512 | def test_lat_long(self): |
---|
1513 | lat_gong = degminsec2decimal_degrees(-34, 30, 0.) |
---|
1514 | lon_gong = degminsec2decimal_degrees(150, 55, 0.) |
---|
1515 | |
---|
1516 | lat_2 = degminsec2decimal_degrees(-34, 00, 0.) |
---|
1517 | lon_2 = degminsec2decimal_degrees(150, 00, 0.) |
---|
1518 | |
---|
1519 | lats = [lat_gong, lat_2] |
---|
1520 | longs = [lon_gong, lon_2] |
---|
1521 | gsd = Geospatial_data(latitudes=lats, longitudes=longs) |
---|
1522 | |
---|
1523 | points = gsd.get_data_points(absolute=True) |
---|
1524 | |
---|
1525 | assert num.allclose(points[0][0], 308728.009) |
---|
1526 | assert num.allclose(points[0][1], 6180432.601) |
---|
1527 | assert num.allclose(points[1][0], 222908.705) |
---|
1528 | assert num.allclose(points[1][1], 6233785.284) |
---|
1529 | self.failUnless(gsd.get_geo_reference().get_zone() == 56, |
---|
1530 | 'Bad zone error!') |
---|
1531 | |
---|
1532 | try: |
---|
1533 | results = Geospatial_data(latitudes=lats) |
---|
1534 | except ValueError: |
---|
1535 | pass |
---|
1536 | else: |
---|
1537 | self.fail('Error not thrown error!') |
---|
1538 | try: |
---|
1539 | results = Geospatial_data(latitudes=lats) |
---|
1540 | except ValueError: |
---|
1541 | pass |
---|
1542 | else: |
---|
1543 | self.fail('Error not thrown error!') |
---|
1544 | try: |
---|
1545 | results = Geospatial_data(longitudes=lats) |
---|
1546 | except ValueError: |
---|
1547 | pass |
---|
1548 | else: |
---|
1549 | self.fail('Error not thrown error!') |
---|
1550 | try: |
---|
1551 | results = Geospatial_data(latitudes=lats, longitudes=longs, |
---|
1552 | geo_reference="p") |
---|
1553 | except ValueError: |
---|
1554 | pass |
---|
1555 | else: |
---|
1556 | self.fail('Error not thrown error!') |
---|
1557 | |
---|
1558 | try: |
---|
1559 | results = Geospatial_data(latitudes=lats, longitudes=longs, |
---|
1560 | data_points=12) |
---|
1561 | except ValueError: |
---|
1562 | pass |
---|
1563 | else: |
---|
1564 | self.fail('Error not thrown error!') |
---|
1565 | |
---|
1566 | def test_lat_long2(self): |
---|
1567 | lat_gong = degminsec2decimal_degrees(-34, 30, 0.) |
---|
1568 | lon_gong = degminsec2decimal_degrees(150, 55, 0.) |
---|
1569 | |
---|
1570 | lat_2 = degminsec2decimal_degrees(-34, 00, 0.) |
---|
1571 | lon_2 = degminsec2decimal_degrees(150, 00, 0.) |
---|
1572 | |
---|
1573 | points = [[lat_gong, lon_gong], [lat_2, lon_2]] |
---|
1574 | gsd = Geospatial_data(data_points=points, points_are_lats_longs=True) |
---|
1575 | |
---|
1576 | points = gsd.get_data_points(absolute=True) |
---|
1577 | |
---|
1578 | assert num.allclose(points[0][0], 308728.009) |
---|
1579 | assert num.allclose(points[0][1], 6180432.601) |
---|
1580 | assert num.allclose(points[1][0], 222908.705) |
---|
1581 | assert num.allclose(points[1][1], 6233785.284) |
---|
1582 | self.failUnless(gsd.get_geo_reference().get_zone() == 56, |
---|
1583 | 'Bad zone error!') |
---|
1584 | |
---|
1585 | try: |
---|
1586 | results = Geospatial_data(points_are_lats_longs=True) |
---|
1587 | except ValueError: |
---|
1588 | pass |
---|
1589 | else: |
---|
1590 | self.fail('Error not thrown error!') |
---|
1591 | |
---|
1592 | def test_write_urs_file(self): |
---|
1593 | lat_gong = degminsec2decimal_degrees(-34, 00, 0) |
---|
1594 | lon_gong = degminsec2decimal_degrees(150, 30, 0.) |
---|
1595 | |
---|
1596 | lat_2 = degminsec2decimal_degrees(-34, 00, 1) |
---|
1597 | lon_2 = degminsec2decimal_degrees(150, 00, 0.) |
---|
1598 | p1 = (lat_gong, lon_gong) |
---|
1599 | p2 = (lat_2, lon_2) |
---|
1600 | points = frozenset([p1, p2, p1]) |
---|
1601 | gsd = Geospatial_data(data_points=list(points), |
---|
1602 | points_are_lats_longs=True) |
---|
1603 | |
---|
1604 | fn = tempfile.mktemp('.urs') |
---|
1605 | gsd.export_points_file(fn) |
---|
1606 | handle = open(fn) |
---|
1607 | lines = handle.readlines() |
---|
1608 | assert lines[0], '2' |
---|
1609 | assert lines[1], '-34.0002778 150.0 0' |
---|
1610 | assert lines[2], '-34.0 150.5 1' |
---|
1611 | handle.close() |
---|
1612 | os.remove(fn) |
---|
1613 | |
---|
1614 | def test_lat_long_set(self): |
---|
1615 | lat_gong = degminsec2decimal_degrees(-34, 30, 0.) |
---|
1616 | lon_gong = degminsec2decimal_degrees(150, 55, 0.) |
---|
1617 | |
---|
1618 | lat_2 = degminsec2decimal_degrees(-34, 00, 0.) |
---|
1619 | lon_2 = degminsec2decimal_degrees(150, 00, 0.) |
---|
1620 | p1 = (lat_gong, lon_gong) |
---|
1621 | p2 = (lat_2, lon_2) |
---|
1622 | points = frozenset([p1, p2, p1]) |
---|
1623 | gsd = Geospatial_data(data_points=list(points), |
---|
1624 | points_are_lats_longs=True) |
---|
1625 | |
---|
1626 | points = gsd.get_data_points(absolute=True) |
---|
1627 | #Note the order is unknown, due to using sets |
---|
1628 | # and it changes from windows to linux |
---|
1629 | try: |
---|
1630 | assert num.allclose(points[1][0], 308728.009) |
---|
1631 | assert num.allclose(points[1][1], 6180432.601) |
---|
1632 | assert num.allclose(points[0][0], 222908.705) |
---|
1633 | assert num.allclose(points[0][1], 6233785.284) |
---|
1634 | except AssertionError: |
---|
1635 | assert num.allclose(points[0][0], 308728.009) |
---|
1636 | assert num.allclose(points[0][1], 6180432.601) |
---|
1637 | assert num.allclose(points[1][0], 222908.705) |
---|
1638 | assert num.allclose(points[1][1], 6233785.284) |
---|
1639 | |
---|
1640 | self.failUnless(gsd.get_geo_reference().get_zone() == 56, |
---|
1641 | 'Bad zone error!') |
---|
1642 | points = gsd.get_data_points(as_lat_long=True) |
---|
1643 | try: |
---|
1644 | assert num.allclose(points[0][0], -34) |
---|
1645 | assert num.allclose(points[0][1], 150) |
---|
1646 | except AssertionError: |
---|
1647 | assert num.allclose(points[1][0], -34) |
---|
1648 | assert num.allclose(points[1][1], 150) |
---|
1649 | |
---|
1650 | def test_len(self): |
---|
1651 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
1652 | G = Geospatial_data(points) |
---|
1653 | self.failUnless(2 == len(G), 'Len error!') |
---|
1654 | |
---|
1655 | points = [[1.0, 2.1]] |
---|
1656 | G = Geospatial_data(points) |
---|
1657 | self.failUnless(1 == len(G), 'Len error!') |
---|
1658 | |
---|
1659 | points = [[1.0, 2.1], [3.0, 5.3], [3.0, 5.3], [3.0, 5.3]] |
---|
1660 | G = Geospatial_data(points) |
---|
1661 | self.failUnless(4 == len(G), 'Len error!') |
---|
1662 | |
---|
1663 | def test_split(self): |
---|
1664 | """test if the results from spilt are disjoin sets""" |
---|
1665 | |
---|
1666 | # below is a workaround until randint works on cyclones compute nodes |
---|
1667 | if get_host_name()[8:9] != '0': |
---|
1668 | points = [[1.0, 1.0], [1.0, 2.0],[1.0, 3.0], [1.0, 4.0], [1.0, 5.0], |
---|
1669 | [2.0, 1.0], [2.0, 2.0],[2.0, 3.0], [2.0, 4.0], [2.0, 5.0], |
---|
1670 | [3.0, 1.0], [3.0, 2.0],[3.0, 3.0], [3.0, 4.0], [3.0, 5.0], |
---|
1671 | [4.0, 1.0], [4.0, 2.0],[4.0, 3.0], [4.0, 4.0], [4.0, 5.0], |
---|
1672 | [5.0, 1.0], [5.0, 2.0],[5.0, 3.0], [5.0, 4.0], [5.0, 5.0]] |
---|
1673 | attributes = {'depth': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
---|
1674 | 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, |
---|
1675 | 21, 22, 23, 24, 25], |
---|
1676 | 'speed': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
---|
1677 | 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, |
---|
1678 | 21, 22, 23, 24, 25]} |
---|
1679 | G = Geospatial_data(points, attributes) |
---|
1680 | |
---|
1681 | factor = 0.21 |
---|
1682 | |
---|
1683 | # will return G1 with 10% of points and G2 with 90% |
---|
1684 | G1, G2 = G.split(factor, 100) |
---|
1685 | assert num.allclose(len(G), len(G1)+len(G2)) |
---|
1686 | assert num.allclose(round(len(G)*factor), len(G1)) |
---|
1687 | |
---|
1688 | P = G1.get_data_points(absolute=False) |
---|
1689 | expected = [[5.,4.], [4.,1.], [2.,4.], [2.,3.], [1.,4.]] |
---|
1690 | # expected = [[5.0, 4.0], [4.0, 3.0], [4.0, 2.0], |
---|
1691 | # [3.0, 1.0], [2.0, 3.0]] |
---|
1692 | msg = 'Expected %s, but\nP=%s' % (str(expected), str(P)) |
---|
1693 | assert num.allclose(P, expected), msg |
---|
1694 | |
---|
1695 | A = G1.get_attributes() |
---|
1696 | expected = [24, 16, 9, 8, 4] |
---|
1697 | msg = 'expected=%s, but A=%s' % (str(expected), str(A)) |
---|
1698 | assert num.allclose(A, expected), msg |
---|
1699 | |
---|
1700 | def test_split1(self): |
---|
1701 | """test if the results from split are disjoint sets""" |
---|
1702 | |
---|
1703 | # below is a workaround until randint works on cyclones compute nodes |
---|
1704 | if get_host_name()[8:9] != '0': |
---|
1705 | from numpy.random import randint, seed |
---|
1706 | |
---|
1707 | seed((100, 100)) |
---|
1708 | a_points = randint(0, 999999, (10,2)) |
---|
1709 | points = a_points.tolist() |
---|
1710 | |
---|
1711 | G = Geospatial_data(points) |
---|
1712 | |
---|
1713 | factor = 0.1 |
---|
1714 | |
---|
1715 | # will return G1 with 10% of points and G2 with 90% |
---|
1716 | G1, G2 = G.split(factor, 100) |
---|
1717 | assert num.allclose(len(G), len(G1)+len(G2)) |
---|
1718 | assert num.allclose(round(len(G)*factor), len(G1)) |
---|
1719 | |
---|
1720 | P = G1.get_data_points(absolute=False) |
---|
1721 | expected = [[425835., 137518.]] |
---|
1722 | msg = 'expected=%s, but\nP=%s' % (str(expected), str(P)) |
---|
1723 | assert num.allclose(P, expected), msg |
---|
1724 | |
---|
1725 | def test_find_optimal_smoothing_parameter(self): |
---|
1726 | """ |
---|
1727 | Creates a elevation file representing a hill (sort of) and runs |
---|
1728 | find_optimal_smoothing_parameter for 3 different alphas, |
---|
1729 | |
---|
1730 | NOTE the random number seed is provided to control the results |
---|
1731 | """ |
---|
1732 | |
---|
1733 | from cmath import cos |
---|
1734 | |
---|
1735 | # below is a workaround until randint works on cyclones compute nodes |
---|
1736 | if get_host_name()[8:9]!='0': |
---|
1737 | filename = tempfile.mktemp('.csv') |
---|
1738 | file = open(filename, 'w') |
---|
1739 | file.write('x,y,elevation \n') |
---|
1740 | |
---|
1741 | for i in range(-5, 6): |
---|
1742 | for j in range(-5, 6): |
---|
1743 | # this equation makes a surface like a circle ripple |
---|
1744 | z = abs(cos(((i*i) + (j*j))*.1)*2) |
---|
1745 | file.write("%s, %s, %s\n" % (i, j, z)) |
---|
1746 | |
---|
1747 | file.close() |
---|
1748 | |
---|
1749 | value, alpha = find_optimal_smoothing_parameter(data_file=filename, |
---|
1750 | alpha_list=[0.0001, 0.01, 1], |
---|
1751 | mesh_file=None, |
---|
1752 | mesh_resolution=3, |
---|
1753 | north_boundary=5, |
---|
1754 | south_boundary=-5, |
---|
1755 | east_boundary=5, |
---|
1756 | west_boundary=-5, |
---|
1757 | plot_name=None, |
---|
1758 | seed_num=100000, |
---|
1759 | verbose=False) |
---|
1760 | |
---|
1761 | os.remove(filename) |
---|
1762 | |
---|
1763 | # print value, alpha |
---|
1764 | assert(alpha == 0.01) |
---|
1765 | |
---|
1766 | def test_find_optimal_smoothing_parameter1(self): |
---|
1767 | """ |
---|
1768 | Creates a elevation file representing a hill (sort of) and |
---|
1769 | Then creates a mesh file and passes the mesh file and the elevation |
---|
1770 | file to find_optimal_smoothing_parameter for 3 different alphas, |
---|
1771 | |
---|
1772 | NOTE the random number seed is provided to control the results |
---|
1773 | """ |
---|
1774 | |
---|
1775 | # below is a workaround until randint works on cyclones compute nodes |
---|
1776 | if get_host_name()[8:9]!='0': |
---|
1777 | from cmath import cos |
---|
1778 | from anuga.pmesh.mesh_interface import create_mesh_from_regions |
---|
1779 | |
---|
1780 | filename = tempfile.mktemp('.csv') |
---|
1781 | file = open(filename, 'w') |
---|
1782 | file.write('x,y,elevation \n') |
---|
1783 | |
---|
1784 | for i in range(-5 ,6): |
---|
1785 | for j in range(-5, 6): |
---|
1786 | # this equation makes a surface like a circle ripple |
---|
1787 | z = abs(cos(((i*i) + (j*j))*.1)*2) |
---|
1788 | file.write('%s, %s, %s\n' % (i, j, z)) |
---|
1789 | |
---|
1790 | file.close() |
---|
1791 | |
---|
1792 | poly=[[5,5], [5,-5], [-5,-5], [-5,5]] |
---|
1793 | internal_poly=[[[[1,1], [1,-1], [-1,-1], [-1,1]], .5]] |
---|
1794 | mesh_filename= tempfile.mktemp('.msh') |
---|
1795 | |
---|
1796 | create_mesh_from_regions(poly, |
---|
1797 | boundary_tags={'back': [2], |
---|
1798 | 'side': [1,3], |
---|
1799 | 'ocean': [0]}, |
---|
1800 | maximum_triangle_area=3, |
---|
1801 | interior_regions=internal_poly, |
---|
1802 | filename=mesh_filename, |
---|
1803 | use_cache=False, |
---|
1804 | verbose=False) |
---|
1805 | |
---|
1806 | value, alpha = find_optimal_smoothing_parameter(data_file=filename, |
---|
1807 | alpha_list=[0.0001, 0.01, 1], |
---|
1808 | mesh_file=mesh_filename, |
---|
1809 | plot_name=None, |
---|
1810 | seed_num=174, |
---|
1811 | verbose=False) |
---|
1812 | |
---|
1813 | os.remove(filename) |
---|
1814 | os.remove(mesh_filename) |
---|
1815 | |
---|
1816 | msg = 'alpha=%s' % str(alpha) |
---|
1817 | # 0.01 was expected with Numeric.RandomArray RNG |
---|
1818 | assert alpha==1.0, msg |
---|
1819 | |
---|
1820 | def test_find_optimal_smoothing_parameter2(self): |
---|
1821 | '''Tests requirement that mesh file must exist or IOError is thrown |
---|
1822 | |
---|
1823 | NOTE the random number seed is provided to control the results |
---|
1824 | ''' |
---|
1825 | |
---|
1826 | from cmath import cos |
---|
1827 | from anuga.pmesh.mesh_interface import create_mesh_from_regions |
---|
1828 | |
---|
1829 | filename = tempfile.mktemp('.csv') |
---|
1830 | mesh_filename= tempfile.mktemp('.msh') |
---|
1831 | |
---|
1832 | try: |
---|
1833 | value, alpha = find_optimal_smoothing_parameter(data_file=filename, |
---|
1834 | alpha_list=[0.0001, 0.01, 1], |
---|
1835 | mesh_file=mesh_filename, |
---|
1836 | plot_name=None, |
---|
1837 | seed_num=174, |
---|
1838 | verbose=False) |
---|
1839 | except IOError: |
---|
1840 | pass |
---|
1841 | else: |
---|
1842 | self.fail('Error not thrown error!') |
---|
1843 | |
---|
1844 | ################################################################################ |
---|
1845 | |
---|
1846 | if __name__ == "__main__": |
---|
1847 | suite = unittest.makeSuite(Test_Geospatial_data, 'test') |
---|
1848 | runner = unittest.TextTestRunner() #verbosity=2) |
---|
1849 | runner.run(suite) |
---|
1850 | |
---|
1851 | |
---|