1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | |
---|
4 | import unittest |
---|
5 | from Numeric import zeros, array, allclose |
---|
6 | from math import sqrt, pi |
---|
7 | |
---|
8 | |
---|
9 | from geospatial_data import * |
---|
10 | |
---|
11 | class Test_Geospatial_data(unittest.TestCase): |
---|
12 | def setUp(self): |
---|
13 | pass |
---|
14 | |
---|
15 | def tearDown(self): |
---|
16 | pass |
---|
17 | |
---|
18 | |
---|
19 | def test_0(self): |
---|
20 | """Basic points |
---|
21 | """ |
---|
22 | from coordinate_transforms.geo_reference import Geo_reference |
---|
23 | |
---|
24 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
25 | G = Geospatial_data(points) |
---|
26 | |
---|
27 | assert allclose(G.data_points, [[1.0, 2.1], [3.0, 5.3]]) |
---|
28 | |
---|
29 | #Check defaults |
---|
30 | assert G.attributes is None |
---|
31 | |
---|
32 | assert G.geo_reference.zone == Geo_reference().zone |
---|
33 | assert G.geo_reference.xllcorner == Geo_reference().xllcorner |
---|
34 | assert G.geo_reference.yllcorner == Geo_reference().yllcorner |
---|
35 | |
---|
36 | |
---|
37 | def test_1(self): |
---|
38 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
39 | attributes = [2, 4] |
---|
40 | G = Geospatial_data(points, attributes) |
---|
41 | |
---|
42 | assert G.attributes.keys()[0] == 'attribute' |
---|
43 | assert allclose(G.attributes.values()[0], [2, 4]) |
---|
44 | |
---|
45 | |
---|
46 | def test_2(self): |
---|
47 | from coordinate_transforms.geo_reference import Geo_reference |
---|
48 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
49 | attributes = [2, 4] |
---|
50 | G = Geospatial_data(points, attributes, |
---|
51 | geo_reference = Geo_reference(56, 100, 200)) |
---|
52 | |
---|
53 | assert G.geo_reference.zone == 56 |
---|
54 | assert G.geo_reference.xllcorner == 100 |
---|
55 | assert G.geo_reference.yllcorner == 200 |
---|
56 | |
---|
57 | |
---|
58 | def test_get_attribute_values_1(self): |
---|
59 | from coordinate_transforms.geo_reference import Geo_reference |
---|
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 | |
---|
66 | P = G.get_data_points() |
---|
67 | assert allclose(P, [[1.0, 2.1], [3.0, 5.3]]) |
---|
68 | |
---|
69 | V = G.get_attribute_values() #Simply get them |
---|
70 | assert allclose(V, [2, 4]) |
---|
71 | |
---|
72 | V = G.get_attribute_values('attribute') #Get by name |
---|
73 | assert allclose(V, [2, 4]) |
---|
74 | |
---|
75 | |
---|
76 | def test_get_attribute_values_2(self): |
---|
77 | """Multiple attributes |
---|
78 | """ |
---|
79 | |
---|
80 | from coordinate_transforms.geo_reference import Geo_reference |
---|
81 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
82 | attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]} |
---|
83 | G = Geospatial_data(points, attributes, |
---|
84 | geo_reference = Geo_reference(56, 100, 200), |
---|
85 | default_attribute_name = 'a1') |
---|
86 | |
---|
87 | |
---|
88 | P = G.get_data_points() |
---|
89 | assert allclose(P, [[1.0, 2.1], [3.0, 5.3]]) |
---|
90 | |
---|
91 | V = G.get_attribute_values() #Get default attribute |
---|
92 | assert allclose(V, [2, 4]) |
---|
93 | |
---|
94 | V = G.get_attribute_values('a0') #Get by name |
---|
95 | assert allclose(V, [0, 0]) |
---|
96 | |
---|
97 | V = G.get_attribute_values('a1') #Get by name |
---|
98 | assert allclose(V, [2, 4]) |
---|
99 | |
---|
100 | V = G.get_attribute_values('a2') #Get by name |
---|
101 | assert allclose(V, [79.4, -7]) |
---|
102 | |
---|
103 | try: |
---|
104 | V = G.get_attribute_values('hdnoatedu') #Invalid |
---|
105 | except AssertionError: |
---|
106 | pass |
---|
107 | else: |
---|
108 | raise 'Should have raised exception' |
---|
109 | |
---|
110 | |
---|
111 | def test_conversions_to_points_dict(self): |
---|
112 | """test conversions to points_dict |
---|
113 | """ |
---|
114 | |
---|
115 | from coordinate_transforms.geo_reference import Geo_reference |
---|
116 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
117 | attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]} |
---|
118 | G = Geospatial_data(points, attributes, |
---|
119 | geo_reference = Geo_reference(56, 100, 200), |
---|
120 | default_attribute_name = 'a1') |
---|
121 | |
---|
122 | |
---|
123 | points_dict = geospatial_data2points_dictionary(G) |
---|
124 | |
---|
125 | assert points_dict.has_key('pointlist') |
---|
126 | assert points_dict.has_key('attributelist') |
---|
127 | assert points_dict.has_key('geo_reference') |
---|
128 | |
---|
129 | assert allclose( points_dict['pointlist'], points ) |
---|
130 | |
---|
131 | A = points_dict['attributelist'] |
---|
132 | assert A.has_key('a0') |
---|
133 | assert A.has_key('a1') |
---|
134 | assert A.has_key('a2') |
---|
135 | |
---|
136 | assert allclose( A['a0'], [0, 0] ) |
---|
137 | assert allclose( A['a1'], [2, 4] ) |
---|
138 | assert allclose( A['a2'], [79.4, -7] ) |
---|
139 | |
---|
140 | |
---|
141 | geo = points_dict['geo_reference'] |
---|
142 | assert geo is G.geo_reference |
---|
143 | |
---|
144 | |
---|
145 | def test_conversions_from_points_dict(self): |
---|
146 | """test conversions from points_dict |
---|
147 | """ |
---|
148 | |
---|
149 | from coordinate_transforms.geo_reference import Geo_reference |
---|
150 | |
---|
151 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
152 | attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]} |
---|
153 | |
---|
154 | points_dict = {} |
---|
155 | points_dict['pointlist'] = points |
---|
156 | points_dict['attributelist'] = attributes |
---|
157 | points_dict['geo_reference'] = Geo_reference(56, 100, 200) |
---|
158 | |
---|
159 | |
---|
160 | G = points_dictionary2geospatial_data(points_dict) |
---|
161 | |
---|
162 | P = G.get_data_points() |
---|
163 | assert allclose(P, [[1.0, 2.1], [3.0, 5.3]]) |
---|
164 | |
---|
165 | #V = G.get_attribute_values() #Get default attribute |
---|
166 | #assert allclose(V, [2, 4]) |
---|
167 | |
---|
168 | V = G.get_attribute_values('a0') #Get by name |
---|
169 | assert allclose(V, [0, 0]) |
---|
170 | |
---|
171 | V = G.get_attribute_values('a1') #Get by name |
---|
172 | assert allclose(V, [2, 4]) |
---|
173 | |
---|
174 | V = G.get_attribute_values('a2') #Get by name |
---|
175 | assert allclose(V, [79.4, -7]) |
---|
176 | |
---|
177 | |
---|
178 | |
---|
179 | |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | if __name__ == "__main__": |
---|
184 | suite = unittest.makeSuite(Test_Geospatial_data,'test') |
---|
185 | runner = unittest.TextTestRunner() |
---|
186 | runner.run(suite) |
---|
187 | |
---|
188 | |
---|