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