1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | |
---|
4 | import unittest |
---|
5 | from Numeric import zeros, array, allclose, concatenate |
---|
6 | from math import sqrt, pi |
---|
7 | |
---|
8 | from geospatial_data import * |
---|
9 | |
---|
10 | from coordinate_transforms.geo_reference import Geo_reference |
---|
11 | |
---|
12 | class Test_Geospatial_data(unittest.TestCase): |
---|
13 | def setUp(self): |
---|
14 | pass |
---|
15 | |
---|
16 | def tearDown(self): |
---|
17 | pass |
---|
18 | |
---|
19 | |
---|
20 | def test_0(self): |
---|
21 | """Basic points |
---|
22 | """ |
---|
23 | from coordinate_transforms.geo_reference import Geo_reference |
---|
24 | |
---|
25 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
26 | G = Geospatial_data(points) |
---|
27 | |
---|
28 | assert allclose(G.data_points, [[1.0, 2.1], [3.0, 5.3]]) |
---|
29 | |
---|
30 | #Check defaults |
---|
31 | assert G.attributes is None |
---|
32 | |
---|
33 | assert G.geo_reference.zone == Geo_reference().zone |
---|
34 | assert G.geo_reference.xllcorner == Geo_reference().xllcorner |
---|
35 | assert G.geo_reference.yllcorner == Geo_reference().yllcorner |
---|
36 | |
---|
37 | |
---|
38 | def test_1(self): |
---|
39 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
40 | attributes = [2, 4] |
---|
41 | G = Geospatial_data(points, attributes) |
---|
42 | |
---|
43 | assert G.attributes.keys()[0] == 'attribute' |
---|
44 | assert allclose(G.attributes.values()[0], [2, 4]) |
---|
45 | |
---|
46 | |
---|
47 | def test_2(self): |
---|
48 | from coordinate_transforms.geo_reference import Geo_reference |
---|
49 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
50 | attributes = [2, 4] |
---|
51 | G = Geospatial_data(points, attributes, |
---|
52 | geo_reference = Geo_reference(56, 100, 200)) |
---|
53 | |
---|
54 | assert G.geo_reference.zone == 56 |
---|
55 | assert G.geo_reference.xllcorner == 100 |
---|
56 | assert G.geo_reference.yllcorner == 200 |
---|
57 | |
---|
58 | |
---|
59 | def test_get_attributes_1(self): |
---|
60 | from coordinate_transforms.geo_reference import Geo_reference |
---|
61 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
62 | attributes = [2, 4] |
---|
63 | G = Geospatial_data(points, attributes, |
---|
64 | geo_reference = Geo_reference(56, 100, 200)) |
---|
65 | |
---|
66 | |
---|
67 | P = G.get_data_points(absolute = False) |
---|
68 | assert allclose(P, [[1.0, 2.1], [3.0, 5.3]]) |
---|
69 | |
---|
70 | P = G.get_data_points(absolute = True) |
---|
71 | assert allclose(P, [[101.0, 202.1], [103.0, 205.3]]) |
---|
72 | |
---|
73 | V = G.get_attributes() #Simply get them |
---|
74 | assert allclose(V, [2, 4]) |
---|
75 | |
---|
76 | V = G.get_attributes('attribute') #Get by name |
---|
77 | assert allclose(V, [2, 4]) |
---|
78 | |
---|
79 | def test_get_attributes_2(self): |
---|
80 | """Multiple attributes |
---|
81 | """ |
---|
82 | |
---|
83 | from coordinate_transforms.geo_reference import Geo_reference |
---|
84 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
85 | attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]} |
---|
86 | G = Geospatial_data(points, attributes, |
---|
87 | geo_reference = Geo_reference(56, 100, 200), |
---|
88 | default_attribute_name = 'a1') |
---|
89 | |
---|
90 | |
---|
91 | P = G.get_data_points(absolute = False) |
---|
92 | assert allclose(P, [[1.0, 2.1], [3.0, 5.3]]) |
---|
93 | |
---|
94 | V = G.get_attributes() #Get default attribute |
---|
95 | assert allclose(V, [2, 4]) |
---|
96 | |
---|
97 | V = G.get_attributes('a0') #Get by name |
---|
98 | assert allclose(V, [0, 0]) |
---|
99 | |
---|
100 | V = G.get_attributes('a1') #Get by name |
---|
101 | assert allclose(V, [2, 4]) |
---|
102 | |
---|
103 | V = G.get_attributes('a2') #Get by name |
---|
104 | assert allclose(V, [79.4, -7]) |
---|
105 | |
---|
106 | try: |
---|
107 | V = G.get_attributes('hdnoatedu') #Invalid |
---|
108 | except AssertionError: |
---|
109 | pass |
---|
110 | else: |
---|
111 | raise 'Should have raised exception' |
---|
112 | |
---|
113 | |
---|
114 | def test_conversions_to_points_dict(self): |
---|
115 | """test conversions to points_dict |
---|
116 | """ |
---|
117 | |
---|
118 | from coordinate_transforms.geo_reference import Geo_reference |
---|
119 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
120 | attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]} |
---|
121 | G = Geospatial_data(points, attributes, |
---|
122 | geo_reference = Geo_reference(56, 100, 200), |
---|
123 | default_attribute_name = 'a1') |
---|
124 | |
---|
125 | |
---|
126 | points_dict = geospatial_data2points_dictionary(G) |
---|
127 | |
---|
128 | assert points_dict.has_key('pointlist') |
---|
129 | assert points_dict.has_key('attributelist') |
---|
130 | assert points_dict.has_key('geo_reference') |
---|
131 | |
---|
132 | assert allclose( points_dict['pointlist'], points ) |
---|
133 | |
---|
134 | A = points_dict['attributelist'] |
---|
135 | assert A.has_key('a0') |
---|
136 | assert A.has_key('a1') |
---|
137 | assert A.has_key('a2') |
---|
138 | |
---|
139 | assert allclose( A['a0'], [0, 0] ) |
---|
140 | assert allclose( A['a1'], [2, 4] ) |
---|
141 | assert allclose( A['a2'], [79.4, -7] ) |
---|
142 | |
---|
143 | |
---|
144 | geo = points_dict['geo_reference'] |
---|
145 | assert geo is G.geo_reference |
---|
146 | |
---|
147 | |
---|
148 | def test_conversions_from_points_dict(self): |
---|
149 | """test conversions from points_dict |
---|
150 | """ |
---|
151 | |
---|
152 | from coordinate_transforms.geo_reference import Geo_reference |
---|
153 | |
---|
154 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
155 | attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]} |
---|
156 | |
---|
157 | points_dict = {} |
---|
158 | points_dict['pointlist'] = points |
---|
159 | points_dict['attributelist'] = attributes |
---|
160 | points_dict['geo_reference'] = Geo_reference(56, 100, 200) |
---|
161 | |
---|
162 | |
---|
163 | G = points_dictionary2geospatial_data(points_dict) |
---|
164 | |
---|
165 | P = G.get_data_points(absolute = False) |
---|
166 | assert allclose(P, [[1.0, 2.1], [3.0, 5.3]]) |
---|
167 | |
---|
168 | #V = G.get_attribute_values() #Get default attribute |
---|
169 | #assert allclose(V, [2, 4]) |
---|
170 | |
---|
171 | V = G.get_attributes('a0') #Get by name |
---|
172 | assert allclose(V, [0, 0]) |
---|
173 | |
---|
174 | V = G.get_attributes('a1') #Get by name |
---|
175 | assert allclose(V, [2, 4]) |
---|
176 | |
---|
177 | V = G.get_attributes('a2') #Get by name |
---|
178 | assert allclose(V, [79.4, -7]) |
---|
179 | |
---|
180 | def test_add(self): |
---|
181 | """ test the addition of two geospatical objects |
---|
182 | no geo_reference see next test |
---|
183 | """ |
---|
184 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
185 | attributes = {'depth':[2, 4], 'elevation':[6.1, 5]} |
---|
186 | attributes1 = {'depth':[2, 4], 'elevation':[2.5, 1]} |
---|
187 | G1 = Geospatial_data(points, attributes) |
---|
188 | G2 = Geospatial_data(points, attributes1) |
---|
189 | |
---|
190 | # print 'G1 depth=', G1.get_attributes('depth') |
---|
191 | # print 'G1 attrib keys=', G1.attributes.keys() |
---|
192 | g3 = geospatial_data2points_dictionary(G1) |
---|
193 | # print 'g3=', g3 |
---|
194 | |
---|
195 | G = G1 + G2 |
---|
196 | |
---|
197 | # g = geospatial_data2points_dictionary(G) |
---|
198 | # print 'G=', g |
---|
199 | # print 'G points =', G.get_data_points() |
---|
200 | # print 'G attrib keys =', G.attributes.keys() |
---|
201 | # print 'G test =', G.get_attributes('elevation') |
---|
202 | assert G.attributes.has_key('depth') |
---|
203 | assert G.attributes.has_key('elevation') |
---|
204 | assert allclose(G.attributes['depth'], [2, 4, 2, 4]) |
---|
205 | assert allclose(G.attributes['elevation'], [6.1, 5, 2.5, 1]) |
---|
206 | assert allclose(G.get_data_points(), [[1.0, 2.1], [3.0, 5.3], |
---|
207 | [1.0, 2.1], [3.0, 5.3]]) |
---|
208 | |
---|
209 | def xtest_add1 (self): |
---|
210 | """ |
---|
211 | difference in Geo_reference resolved |
---|
212 | """ |
---|
213 | points = [[1.0, 2.1], [3.0, 5.3]] |
---|
214 | points1 = [[5.0, 6.1], [6.0, 3.3]] |
---|
215 | attributes = [2, 4] |
---|
216 | attributes1 = [5, 76] |
---|
217 | geo_ref = Geo_reference(55, 1.0, 2.0) |
---|
218 | geo_ref1 = Geo_reference(zone = 55, xllcorner = 0.1, |
---|
219 | yllcorner = 3.0, datum = 'wgs84', |
---|
220 | projection = 'UTM', units = 'm') |
---|
221 | |
---|
222 | G1 = Geospatial_data(points, attributes, geo_ref) |
---|
223 | G2 = Geospatial_data(points1, attributes1, geo_ref1) |
---|
224 | |
---|
225 | G = G1 + G2 |
---|
226 | |
---|
227 | assert allclose(G.get_geo_reference().get_xllcorner(), 0.1) |
---|
228 | assert allclose(G.get_geo_reference().get_yllcorner(), 2.0) |
---|
229 | assert allclose(G.get_data_points(), [[2.0, 4.1], [4.0, 7.3], [5.1, 9.3], [6.1, 6.3]]) |
---|
230 | |
---|
231 | |
---|
232 | |
---|
233 | if __name__ == "__main__": |
---|
234 | suite = unittest.makeSuite(Test_Geospatial_data,'test') |
---|
235 | runner = unittest.TextTestRunner() |
---|
236 | runner.run(suite) |
---|
237 | |
---|
238 | |
---|