Changeset 2928
- Timestamp:
- May 19, 2006, 5:29:13 PM (19 years ago)
- Location:
- inundation/geospatial_data
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/geospatial_data/geospatial_data.py
r2890 r2928 20 20 geo_reference = None, 21 21 default_attribute_name = None, 22 file_name = None):#, 23 # absolute = True): 24 25 26 ''' 22 file_name = None, 23 delimiter = None):#, 24 # verbose = False): 25 26 27 """ 27 28 Create instance from data points and associated attributes 28 29 29 30 30 data_points: x,y coordinates in meters. Type must be either a … … 85 85 dic['pointlist'] = [[1.0,2.0],[3.0,5.0]] 86 86 dic['attributelist']['elevation'] = [[7.0,5.0] 87 88 delimiter: 89 90 verbose: 87 91 88 '''92 """ 89 93 90 94 if isinstance(data_points, basestring): … … 93 97 94 98 if file_name is None: 99 if delimiter is not None: 100 msg = 'No file specified yet a delimiter is provided!' 101 raise ValueError, msg 95 102 self.file_name = None 96 103 self.check_data_points(data_points) … … 98 105 self.set_geo_reference(geo_reference) 99 106 self.set_default_attribute_name(default_attribute_name) 100 # self.set_absolute(absolute) 101 107 108 102 109 else: 103 110 if access(file_name, F_OK) == 0 : 104 111 msg = 'File %s does not exist or is not accessible' %file_name 105 raise msg112 raise IOError, msg 106 113 else: 107 114 data = {} … … 109 116 # if file name then all provided info will be removed! 110 117 self.file_name = file_name 111 self.new_import_points_file(file_name) 118 self.delimiter = delimiter 119 self.import_points_file(file_name, delimiter) 112 120 113 # print'data: point in init',self.data_points114 # print'attrib: point in init',self.attributes115 # print'geo_ref: point in init',self.geo_reference116 121 self.check_data_points(self.data_points) 117 122 self.set_attributes(self.attributes) … … 126 131 if data_points is None: 127 132 self.data_points = None 128 # FIXME: should throw an error if bad file name and no data! 129 print 'There is no data or files to read for data!!' 130 msg = 'file name %s does not exist in data set and the ' %self.file_name 131 # some assert 133 msg = 'There is no data or file provided!' 134 raise ValueError, msg 132 135 133 136 else: … … 191 194 self.file_name = file_name 192 195 print 'file name from set', self.file_name 193 ''' 194 def set_absolute(self, absolute = True): 195 if absolute is False: 196 self.absolute = False 197 else: 198 self.absolute = True 199 ''' 196 200 197 def get_geo_reference(self): 201 198 return self.geo_reference … … 245 242 def __add__(self, other): 246 243 """ 247 returns the addition of 2 geospatical objects,244 Returns the addition of 2 geospatical objects, 248 245 objects are concatencated to the end of each other 249 246 … … 253 250 Always return relative points! 254 251 """ 255 252 256 253 # find objects zone and checks if the same 257 254 geo_ref1 = self.get_geo_reference() … … 288 285 #Now both point sets are relative to new_geo_ref and zones have been reconciled 289 286 290 291 287 # Concatenate points 292 288 new_points = concatenate((new_relative_points1, 293 289 new_relative_points2), 294 290 axis = 0) 295 # print 'new points:', new_points296 297 291 298 292 # Concatenate attributes … … 310 304 raise msg 311 305 312 313 306 # Instantiate new data object and return 314 307 return Geospatial_data(new_points, 315 308 new_attributes, 316 309 new_geo_ref) 317 318 319 '''320 def xxx__add__(self, other):321 """322 returns the addition of 2 geospatical objects,323 objects are concatencated to the end of each other324 325 NOTE: does not add if objects contain different326 attributes327 """328 329 # sets xll and yll as the smallest from self and other330 if self.geo_reference.xllcorner <= other.geo_reference.xllcorner:331 xll = self.geo_reference.xllcorner332 else:333 xll = other.geo_reference.xllcorner334 335 if self.geo_reference.yllcorner <= other.geo_reference.yllcorner:336 yll = self.geo_reference.yllcorner337 else:338 yll = other.geo_reference.yllcorner339 340 # find objects zone and checks if the same341 geo_ref1 = self.get_geo_reference()342 zone1 = geo_ref1.get_zone()343 344 geo_ref2 = other.get_geo_reference()345 zone2 = geo_ref2.get_zone()346 347 if zone1 == zone2:348 a_absolute_points = self.get_data_points(absolute=True)349 b_absolute_points = other.get_data_points(absolute=True)350 351 # subtract xll and yll from self's and other's352 # absolute data points and concatenate353 c_points = concatenate((a_absolute_points-[xll, yll],354 b_absolute_points-[xll, yll]),355 axis = 0)356 357 new_attributes = {}358 for x in self.attributes.keys():359 if other.attributes.has_key(x):360 361 a_attrib = self.attributes[x]362 b_attrib = other.attributes[x]363 new_attributes[x] = concatenate((a_attrib, b_attrib))364 365 else:366 msg = 'Both geospatial_data objects must have the same \n'367 msg += 'attributes to allow addition.'368 raise msg369 370 geo_ref = Geo_reference(zone1, xll, yll)371 return Geospatial_data(c_points, new_attributes, geo_ref)372 373 else:374 msg = 'Both geospatial_data objects must be in the same \375 ZONE to allow addition. I got zone %d and %d' %(zone1, zone2)376 raise msg377 378 '''379 310 380 311 ### 381 312 # IMPORT/EXPORT POINTS FILES 382 313 ### 383 def new_import_points_file(self, ofile, delimiter = None, verbose = False): 314 315 def import_points_file(self, ofile, delimiter = None, verbose = False): 384 316 """ load an .xya or .pts file 385 317 Note: will throw an IOError if it can't load the file. … … 393 325 try: 394 326 fd = open(ofile) 395 # all_data = _read_xya_file(fd, ',') 396 data_points, attributes, geo_reference = _new_read_xya_file(fd, ',') 327 data_points, attributes, geo_reference = _read_xya_file(fd, ',') 397 328 except SyntaxError: 398 329 fd.close() 399 330 fd = open(ofile) 400 # all_data = _read_xya_file(fd, ' ') 401 data_points, attributes, geo_reference = _new_read_xya_file(fd, ' ') 331 data_points, attributes, geo_reference = _read_xya_file(fd, ' ') 402 332 else: 403 333 fd = open(ofile) 404 # all_data = _read_xya_file(fd, delimiter) 405 data_points, attributes, geo_reference = _new_read_xya_file(fd, delimiter) 334 data_points, attributes, geo_reference = _read_xya_file(fd, delimiter) 406 335 fd.close() 407 336 except (IndexError,ValueError,SyntaxError): … … 416 345 elif ofile[-4:]== ".pts": 417 346 try: 418 # print 'hi from import_points_file' 419 # all_data = _read_pts_file(ofile, verbose) 420 data_points, attributes, geo_reference = _new_read_pts_file(ofile, verbose) 421 # print 'hi1 from import_points_file', all_data 347 data_points, attributes, geo_reference = _read_pts_file(ofile, verbose) 422 348 except IOError, e: 423 349 msg = 'Could not open file %s ' %ofile … … 472 398 473 399 474 def import_points_file( ofile, delimiter = None, verbose = False): 475 """ load an .xya or .pts file 476 Note: will throw an IOError if it can't load the file. 477 Catch these! 478 """ 479 480 all_data = {} 481 if ofile[-4:]== ".xya": 482 try: 483 if delimiter == None: 484 try: 485 fd = open(ofile) 486 all_data = _read_xya_file(fd, ',') 487 except SyntaxError: 488 fd.close() 489 fd = open(ofile) 490 all_data = _read_xya_file(fd, ' ') 491 else: 492 fd = open(ofile) 493 all_data = _read_xya_file(fd, delimiter) 494 fd.close() 495 except (IndexError,ValueError,SyntaxError): 496 fd.close() 497 msg = 'Could not open file %s ' %ofile 498 raise IOError, msg 499 except IOError, e: 500 # Catch this to add an error message 501 msg = 'Could not open file or incorrect file format %s:%s' %(ofile, e) 502 raise IOError, msg 503 504 elif ofile[-4:]== ".pts": 505 try: 506 # print 'hi from import_points_file' 507 all_data = _read_pts_file(ofile, verbose) 508 # print 'hi1 from import_points_file', all_data 509 except IOError, e: 510 msg = 'Could not open file %s ' %ofile 511 raise IOError, msg 512 else: 513 msg = 'Extension %s is unknown' %ofile[-4:] 514 raise IOError, msg 515 516 return all_data 517 ''' 518 def export_points_file(ofile, point_dict): 519 """ 520 write a points file, ofile, as a text (.xya) or binary (.pts) file 521 522 ofile is the file name, including the extension 523 524 The point_dict is defined at the top of this file. 525 """ 526 #this was done for all keys in the mesh file. 527 #if not mesh_dict.has_key('points'): 528 # mesh_dict['points'] = [] 529 if (ofile[-4:] == ".xya"): 530 _write_xya_file(ofile, point_dict) 531 elif (ofile[-4:] == ".pts"): 532 _write_pts_file(ofile, point_dict) 533 else: 534 msg = 'Unknown file type %s ' %ofile 535 raise IOError, msg 536 ''' 537 538 539 def _new_read_pts_file(file_name, verbose = False): 400 401 402 def _read_pts_file(file_name, verbose = False): 540 403 """Read .pts NetCDF file 541 404 … … 545 408 dic['attributelist']['elevation'] = [[7.0,5.0] 546 409 """ 547 #FIXME: (DSG) This format has issues. 548 # There can't be an attribute called points 549 # consider format change 550 551 # print 'hi for read points file' 410 552 411 from Scientific.IO.NetCDF import NetCDFFile 553 412 … … 596 455 597 456 598 def _ new_read_xya_file( fd, delimiter):457 def _read_xya_file( fd, delimiter): 599 458 # print 'hello from read xya data' 600 459 points = [] … … 653 512 654 513 655 def _ read_pts_file(file_name, verbose = False):514 def _old_read_pts_file(file_name, verbose = False): 656 515 """Read .pts NetCDF file 657 516 … … 709 568 710 569 711 def _read_xya_file( fd, delimiter): 712 # print 'hello from read xya data' 570 def _old_read_xya_file( fd, delimiter): 713 571 points = [] 714 572 pointattributes = [] … … 768 626 write_attributes, 769 627 write_geo_reference = None): 770 """Write .pts NetCDF file 771 628 """ 629 Write .pts NetCDF file 630 631 NOTE: Below might not be valid ask Duncan : NB 5/2006 632 772 633 WARNING: This function mangles the point_atts data structure 773 """ 774 #FIXME: (DSG)This format has issues. 634 #F??ME: (DSG)This format has issues. 775 635 # There can't be an attribute called points 776 636 # consider format change 777 637 # method changed by NB not sure if above statement is correct 778 779 '''should create new test for this638 639 should create new test for this 780 640 legal_keys = ['pointlist', 'attributelist', 'geo_reference'] 781 641 for key in point_atts.keys(): 782 642 msg = 'Key %s is illegal. Valid keys are %s' %(key, legal_keys) 783 643 assert key in legal_keys, msg 784 '''644 """ 785 645 from Scientific.IO.NetCDF import NetCDFFile 786 646 # NetCDF file definition … … 843 703 + str(vert[1]) 844 704 + attlist + "\n") 845 ''' 846 # geo_reference info 847 if xya_dict.has_key('geo_reference') and \ 848 not xya_dict['geo_reference'] is None: 849 xya_dict['geo_reference'].write_ASCII(fd) 850 ''' 705 851 706 if write_geo_reference is not None: 852 707 write_geo_reference.write_ASCII(fd) 853 708 fd.close() 854 ''' 855 def _write_pts_file(file_name, point_atts): 856 """Write .pts NetCDF file 857 858 WARNING: This function mangles the point_atts data structure 859 """ 860 #FIXME: (DSG)This format has issues. 861 # There can't be an attribute called points 862 # consider format change 863 864 865 legal_keys = ['pointlist', 'attributelist', 'geo_reference'] 866 for key in point_atts.keys(): 867 msg = 'Key %s is illegal. Valid keys are %s' %(key, legal_keys) 868 assert key in legal_keys, msg 869 870 from Scientific.IO.NetCDF import NetCDFFile 871 _point_atts2array(point_atts) 872 # NetCDF file definition 873 outfile = NetCDFFile(file_name, 'w') 874 875 #Create new file 876 outfile.institution = 'Geoscience Australia' 877 outfile.description = 'NetCDF format for compact and portable storage ' +\ 878 'of spatial point data' 879 880 # dimension definitions 881 shape = point_atts['pointlist'].shape[0] 882 outfile.createDimension('number_of_points', shape) 883 outfile.createDimension('number_of_dimensions', 2) #This is 2d data 884 885 # variable definition 886 outfile.createVariable('points', Float, ('number_of_points', 887 'number_of_dimensions')) 888 889 #create variables 890 outfile.variables['points'][:] = point_atts['pointlist'] #.astype(Float32) 891 for key in point_atts['attributelist'].keys(): 892 outfile.createVariable(key, Float, ('number_of_points',)) 893 outfile.variables[key][:] = point_atts['attributelist'][key] #.astype(Float32) 894 895 if point_atts.has_key('geo_reference') and not point_atts['geo_reference'] == None: 896 point_atts['geo_reference'].write_NetCDF(outfile) 897 898 outfile.close() 899 900 901 902 def _write_xya_file( file_name, xya_dict, delimiter = ','): 903 """ 904 export a file, ofile, with the xya format 905 906 """ 907 points = xya_dict['pointlist'] 908 pointattributes = xya_dict['attributelist'] 909 910 fd = open(file_name,'w') 911 912 titlelist = "" 913 for title in pointattributes.keys(): 914 titlelist = titlelist + title + delimiter 915 titlelist = titlelist[0:-len(delimiter)] # remove the last delimiter 916 fd.write(titlelist+"\n") 917 #<vertex #> <x> <y> [attributes] 918 for i,vert in enumerate( points): 919 920 attlist = "," 921 for att in pointattributes.keys(): 922 attlist = attlist + str(pointattributes[att][i])+ delimiter 923 attlist = attlist[0:-len(delimiter)] # remove the last delimiter 924 attlist.strip() 925 fd.write( str(vert[0]) + delimiter 926 + str(vert[1]) 927 + attlist + "\n") 928 929 # geo_reference info 930 if xya_dict.has_key('geo_reference') and \ 931 not xya_dict['geo_reference'] is None: 932 xya_dict['geo_reference'].write_ASCII(fd) 933 fd.close() 934 ''' 709 935 710 936 711 … … 1002 777 1003 778 def add_points_files(add_file1, add_file2, results_file): 1004 '''adds the points and attruibutes of 2 pts or xya files and779 """ adds the points and attruibutes of 2 pts or xya files and 1005 780 writes it to a pts file 1006 781 1007 782 NOTE will add the function to check and remove points from one set 1008 783 that are shared. This will require some work and maybe __subtract__ function 1009 '''784 """ 1010 785 1011 786 G1 = Geospatial_data(file_name = add_file1) -
inundation/geospatial_data/test_geospatial_data.py
r2890 r2928 365 365 1.0, 0.0, 10.4, 40.0\n") 366 366 file.close() 367 #print fileName 368 dict = import_points_file(fileName,delimiter = ',') 369 dict = import_points_file(fileName,delimiter = ',') 370 os.remove(fileName) 371 assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 372 assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) 373 assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0]) 367 results = Geospatial_data(fileName, delimiter = ',') 368 os.remove(fileName) 369 # print 'data', results.get_data_points() 370 assert allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 371 assert allclose(results.get_attributes(attribute_name = 'elevation'), [10.0, 0.0, 10.4]) 372 assert allclose(results.get_attributes(attribute_name = 'speed'), [0.0, 10.0, 40.0]) 374 373 375 374 def test_loadxya2(self): … … 386 385 1.0 0.0 10.4 40.0\n") 387 386 file.close() 388 #print fileName 389 # dict = import_points_file(fileName,delimiter = ' ') 390 dict = import_points_file(fileName,delimiter = ' ') 391 os.remove(fileName) 392 assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 393 assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) 394 assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0]) 387 388 results = Geospatial_data(fileName, delimiter = ' ') 389 390 os.remove(fileName) 391 392 assert allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 393 assert allclose(results.get_attributes(attribute_name = 'elevation'), [10.0, 0.0, 10.4]) 394 assert allclose(results.get_attributes(attribute_name = 'speed'), [0.0, 10.0, 40.0]) 395 395 396 396 def test_loadxya3(self): … … 411 411 3\n") 412 412 file.close() 413 #print fileName 414 dict = import_points_file(fileName,delimiter = ' ') 415 os.remove(fileName) 416 assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 417 assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) 418 assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0]) 419 420 def test_loadxy_bad_no_file_xya(self): 421 import os 422 423 fileName = tempfile.mktemp(".xya") 424 #print fileName 425 try: 426 dict = import_points_file(fileName,delimiter = ' ') 427 except IOError: 428 pass 429 else: 430 self.failUnless(0 ==1, 431 'imaginary file did not raise error!') 432 413 414 results = Geospatial_data(fileName, delimiter = ' ') 415 416 os.remove(fileName) 417 assert allclose(results.get_data_points(absolute = False), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 418 assert allclose(results.get_attributes(attribute_name = 'elevation'), [10.0, 0.0, 10.4]) 419 assert allclose(results.get_attributes(attribute_name = 'speed'), [0.0, 10.0, 40.0]) 420 433 421 def test_read_write_points_file_bad2(self): 434 # dict = {}435 422 att_dict = {} 436 423 pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 437 424 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 438 425 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 439 # dict['attributelist'] = att_dict440 426 geo_reference = Geo_reference(56,1.9,1.9) 441 427 … … 448 434 pass 449 435 else: 450 self.failUnless(0 == 1, 451 'bad points file extension did not raise error!') 436 msg = 'bad points file extension did not raise error!' 437 raise msg 438 # self.failUnless(0 == 1, 439 # 'bad points file extension did not raise error!') 452 440 453 441 def test_loadxy_bad(self): … … 463 451 #print fileName 464 452 try: 465 dict = import_points_file(fileName,delimiter = ' ')453 results = Geospatial_data(fileName, delimiter = ' ') 466 454 except IOError: 467 455 pass 468 456 else: 469 self.failUnless(0 ==1, 470 'bad xya file did not raise error!') 457 msg = 'bad xya file did not raise error!' 458 raise msg 459 # self.failUnless(0 == 1, 460 # 'bad xya file did not raise error!') 471 461 os.remove(fileName) 472 462 … … 483 473 #print fileName 484 474 try: 485 dict = import_points_file(fileName,delimiter = ' ')475 results = Geospatial_data(fileName, delimiter = ' ') 486 476 except IOError: 487 477 pass 488 478 else: 489 self.failUnless(0 ==1,490 'bad xya file did not raise error!')479 msg = 'bad xya file did not raise error!' 480 raise msg 491 481 os.remove(fileName) 492 482 … … 505 495 file.close() 506 496 try: 507 dict = import_points_file(fileName,delimiter = ' ')497 results = Geospatial_data(fileName, delimiter = ' ') 508 498 except IOError: 509 499 pass 510 500 else: 511 self.failUnless(0 ==1,512 'bad xya file did not raise error!')501 msg = 'bad xya file did not raise error!' 502 raise msg 513 503 os.remove(fileName) 514 504 515 def test_loadxy_bad4(self):516 """517 specifying wrong delimiter518 """519 import os520 521 fileName = tempfile.mktemp(".xya")522 file = open(fileName,"w")523 file.write(" elevation speed \n\524 1.0 0.0 10.0 0.0\n\525 0.0 1.0 0.0 10.0\n\526 1.0 0.0 10.4 40.0\n\527 yeah")528 file.close()529 try:530 dict = import_points_file(fileName,delimiter = ' ')531 except IOError:532 pass533 else:534 self.failUnless(0 ==1,535 'bad xya file did not raise error!')536 os.remove(fileName)537 538 505 def test_loadxy_bad4(self): 539 506 """ … … 551 518 file.close() 552 519 try: 553 dict = import_points_file(fileName,delimiter = ' ') 520 # dict = import_points_file(fileName,delimiter = ' ') 521 # results = Geospatial_data() 522 results = Geospatial_data(fileName, delimiter = ',') 523 # results.import_points_file(fileName, delimiter = ' ') 554 524 except IOError: 555 525 pass 556 526 else: 557 self.failUnless(0 ==1, 558 'bad xya file did not raise error!') 527 msg = 'bad xya file did not raise error!' 528 raise msg 529 530 # self.failUnless(0 ==1, 531 # 'bad xya file did not raise error!') 559 532 os.remove(fileName) 560 533 … … 575 548 file.close() 576 549 try: 577 dict = import_points_file(fileName,delimiter = ' ') 550 # dict = import_points_file(fileName,delimiter = ' ') 551 # results = Geospatial_data() 552 results = Geospatial_data(fileName, delimiter = ' ') 553 # results.import_points_file(fileName, delimiter = ' ') 578 554 except IOError: 579 555 pass 580 556 else: 581 self.failUnless(0 ==1, 582 'bad xya file did not raise error!') 557 msg = 'bad xya file did not raise error!' 558 raise msg 559 560 # self.failUnless(0 ==1, 561 # 'bad xya file did not raise error!') 583 562 os.remove(fileName) 584 563 … … 587 566 588 567 fileName = tempfile.mktemp(".xya") 589 #print fileName590 568 try: 591 dict = import_points_file(fileName,delimiter = ' ')569 results = Geospatial_data(fileName, delimiter = ' ') 592 570 except IOError: 593 571 pass 594 572 else: 595 self.failUnless(0 ==1, 596 'imaginary file did not raise error!') 573 msg = 'imaginary file did not raise error!' 574 raise msg 575 576 # except IOError: 577 # pass 578 # else: 579 # self.failUnless(0 == 1, 580 # 'imaginary file did not raise error!') 581 597 582 598 583 ###################### .XYA ############################## … … 611 596 G = Geospatial_data(pointlist, att_dict, geo_reference) 612 597 G.export_points_file(fileName) 613 dict2 = import_points_file(fileName) 598 599 # dict2 = import_points_file(fileName) 600 results = Geospatial_data(file_name = fileName) 614 601 #print "fileName",fileName 615 602 os.remove(fileName) 616 #print "dict2",dict2 617 618 assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 619 assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4]) 603 604 assert allclose(results.get_data_points(absolute=False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 605 assert allclose(results.get_attributes(attribute_name = 'elevation'), [10.0, 0.0, 10.4]) 620 606 answer = [10.0, 0.0, 10.4] 621 assert allclose( dict2['attributelist']['brightness'], answer)607 assert allclose(results.get_attributes(attribute_name = 'brightness'), answer) 622 608 #print "dict2['geo_reference']",dict2['geo_reference'] 623 self.failUnless( geo_reference == dict2['geo_reference'],609 self.failUnless(results.get_geo_reference() == geo_reference, 624 610 'test_writepts failed. Test geo_reference') 625 611 626 612 def test_export_xya_file2(self): 613 """test absolute xya file 614 """ 627 615 att_dict = {} 628 616 pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) … … 633 621 G = Geospatial_data(pointlist, att_dict) 634 622 G.export_points_file(fileName) 635 dict2 = import_points_file(fileName) 636 os.remove(fileName) 637 638 assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 639 assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4]) 623 results = Geospatial_data(file_name = fileName) 624 # dict2 = import_points_file(fileName) 625 os.remove(fileName) 626 627 assert allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 628 assert allclose(results.get_attributes('elevation'), [10.0, 0.0, 10.4]) 640 629 answer = [10.0, 0.0, 10.4] 641 assert allclose(dict2['attributelist']['brightness'], answer) 642 643 def test_export_xya_file2(self): 630 assert allclose(results.get_attributes('brightness'), answer) 631 632 def test_export_xya_file3(self): 633 """test absolute xya file with geo_ref 634 """ 644 635 att_dict = {} 645 636 pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) … … 651 642 fileName = tempfile.mktemp(".xya") 652 643 G = Geospatial_data(pointlist, att_dict, geo_reference) 644 653 645 G.export_points_file(fileName, absolute=True) 654 dict2 = import_points_file(fileName)655 #print "fileName",fileName656 os.remove(fileName) 657 #print "dict2",dict2 658 659 # assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])660 assert allclose( dict2['pointlist'],[[2.9, 1.9],[1.9, 2.9],[2.9, 1.9]])661 assert allclose(dict2['attributelist']['elevation'],[10.0, 0.0, 10.4])646 647 results = Geospatial_data(file_name = fileName) 648 os.remove(fileName) 649 650 assert allclose(results.get_data_points(), 651 [[2.9, 1.9],[1.9, 2.9],[2.9, 1.9]]) 652 assert allclose(results.get_attributes(attribute_name = 'elevation'), 653 [10.0, 0.0, 10.4]) 662 654 answer = [10.0, 0.0, 10.4] 663 assert allclose(dict2['attributelist']['brightness'], answer) 664 # print "dict2['geo_reference']",dict2['geo_reference'] 665 self.failUnless(None == dict2['geo_reference'], 666 'test_writepts failed. Test geo_reference') 667 655 assert allclose(results.get_attributes(attribute_name = 'brightness'), answer) 656 self.failUnless(results.get_geo_reference() == geo_reference, 657 'test_writepts failed. Test geo_reference') 658 659 660 668 661 def test_new_export_pts_file(self): 669 662 att_dict = {} … … 678 671 G.export_points_file(fileName) 679 672 680 dict2 = import_points_file(fileName)681 682 os.remove(fileName) 683 684 assert allclose( dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])685 assert allclose( dict2['attributelist']['elevation'], [10.1, 0.0, 10.4])673 results = Geospatial_data(file_name = fileName) 674 675 os.remove(fileName) 676 677 assert allclose(results.get_data_points(),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 678 assert allclose(results.get_attributes(attribute_name = 'elevation'), [10.1, 0.0, 10.4]) 686 679 answer = [10.0, 1.0, 10.4] 687 assert allclose( dict2['attributelist']['brightness'], answer)680 assert allclose(results.get_attributes(attribute_name = 'brightness'), answer) 688 681 689 682 def test_new_export_absolute_pts_file(self): … … 700 693 G.export_points_file(fileName, absolute=True) 701 694 702 dict2 = import_points_file(fileName) 703 704 os.remove(fileName) 705 706 # assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 707 assert allclose(dict2['pointlist'],G.get_data_points(True)) 708 assert allclose(dict2['attributelist']['elevation'], [10.1, 0.0, 10.4]) 695 results = Geospatial_data(file_name = fileName) 696 697 os.remove(fileName) 698 699 assert allclose(results.get_data_points(), G.get_data_points(True)) 700 assert allclose(results.get_attributes(attribute_name = 'elevation'), [10.1, 0.0, 10.4]) 709 701 answer = [10.0, 1.0, 10.4] 710 assert allclose( dict2['attributelist']['brightness'], answer)702 assert allclose(results.get_attributes(attribute_name = 'brightness'), answer) 711 703 712 704 def test_loadpts(self): … … 740 732 outfile.close() 741 733 742 dict = import_points_file(fileName)734 results = Geospatial_data(file_name = fileName) 743 735 os.remove(fileName) 744 736 answer = [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]] 745 #print "dict['pointlist']",dict['pointlist'] 746 #print "answer",answer 747 assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 748 assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) 737 assert allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 738 assert allclose(results.get_attributes(attribute_name = 'elevation'), [10.0, 0.0, 10.4]) 749 739 750 740 def test_writepts(self): 751 # dict = {}752 741 att_dict = {} 753 742 pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 754 743 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 755 744 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 756 # dict['attributelist'] = att_dict757 745 geo_reference = Geo_reference(56,1.9,1.9) 758 746 … … 763 751 G.export_points_file(fileName) 764 752 765 dict2 = import_points_file(fileName)766 767 os.remove(fileName) 768 769 assert allclose( dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])770 assert allclose( dict2['attributelist']['elevation'], [10.0, 0.0, 10.4])753 results = Geospatial_data(file_name = fileName) 754 755 os.remove(fileName) 756 757 assert allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 758 assert allclose(results.get_attributes('elevation'), [10.0, 0.0, 10.4]) 771 759 answer = [10.0, 0.0, 10.4] 772 assert allclose(dict2['attributelist']['brightness'], answer) 773 774 #print "dict['geo_reference'] ",dict['geo_reference'] 775 #print "dict2['geo_reference']",dict2['geo_reference'] 776 777 self.failUnless(geo_reference == dict2['geo_reference'], 760 assert allclose(results.get_attributes('brightness'), answer) 761 762 763 self.failUnless(geo_reference == geo_reference, 778 764 'test_writepts failed. Test geo_reference') 779 765 … … 787 773 #print fileName 788 774 try: 789 dict = import_points_file(fileName) 775 results = Geospatial_data(file_name = fileName) 776 # dict = import_points_file(fileName) 790 777 except IOError: 791 778 pass 792 779 else: 793 self.failUnless(0 ==1, 794 'imaginary file did not raise error!') 780 msg = 'imaginary file did not raise error!' 781 raise msg 782 # self.failUnless(0 == 1, 783 # 'imaginary file did not raise error!') 795 784 796 785 … … 916 905 fileName2 = tempfile.mktemp(".pts") 917 906 918 # G1.export_points_file(fileName1) 919 # G2.export_points_file(fileName2) 920 921 results_file = 'resulting_points.pts' 907 #makes files 908 G1.export_points_file(fileName1) 909 G2.export_points_file(fileName2) 922 910 923 911 # add files 924 912 925 G = G1 + G2 926 927 G.export_points_file(results_file) 928 # add_points_files(fileName1, fileName2, results_file ) 913 G3 = Geospatial_data(file_name = fileName1) 914 G4 = Geospatial_data(file_name = fileName2) 915 916 G = G3 + G4 917 929 918 930 919 #read results 931 results_dict = import_points_file(results_file) 932 # print 'results points:', results_dict['pointlist'] 933 # print 'results geo_ref:', results_dict['geo_reference'] 934 935 assert allclose(results_dict['pointlist'], 920 assert allclose(G.get_data_points(absolute = False), 936 921 [[2.0, 0.0],[1.0, 1.0], 937 922 [2.0, 0.0],[2.0, 2.0], 938 923 [1.0, 3.0],[2.0, 2.0]]) 939 assert allclose( results_dict['attributelist']['elevation'],924 assert allclose(G.get_attributes(attribute_name = 'elevation'), 940 925 [-10.0, 0.0, 10.4, 1.0, 15.0, 1.4]) 941 926 942 927 answer = [10.0, 0.0, 10.4, 14.0, 1.0, -12.4] 943 assert allclose( results_dict['attributelist']['brightness'], answer)944 945 self.failUnless( results_dict['geo_reference']== geo_reference1,928 assert allclose(G.get_attributes(attribute_name = 'brightness'), answer) 929 930 self.failUnless(G.get_geo_reference() == geo_reference1, 946 931 'test_writepts failed. Test geo_reference') 947 932 948 # os.remove(fileName1) 949 # os.remove(fileName2) 950 os.remove(results_file) 951 952 933 os.remove(fileName1) 934 os.remove(fileName2) 935 953 936 def test_ensure_absolute(self): 954 937 points = [[2.0, 0.0],[1.0, 1.0], … … 1010 993 assert allclose(new_points, ab_points) 1011 994 995 def test_isinstance(self): 996 997 import os 998 999 fileName = tempfile.mktemp(".xya") 1000 file = open(fileName,"w") 1001 file.write(" elevation speed \n\ 1002 1.0 0.0 10.0 0.0\n\ 1003 0.0 1.0 0.0 10.0\n\ 1004 1.0 0.0 10.4 40.0\n\ 1005 #geocrap\n\ 1006 56\n\ 1007 56.6\n\ 1008 3\n") 1009 file.close() 1010 1011 results = Geospatial_data(fileName) 1012 1013 assert allclose(results.get_data_points(absolute = False), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 1014 assert allclose(results.get_attributes(attribute_name = 'elevation'), [10.0, 0.0, 10.4]) 1015 assert allclose(results.get_attributes(attribute_name = 'speed'), [0.0, 10.0, 40.0]) 1016 1017 os.remove(fileName) 1018 1019 def test_delimiter(self): 1020 1021 try: 1022 G = Geospatial_data(delimiter = ',') 1023 # results = Geospatial_data(file_name = fileName) 1024 # dict = import_points_file(fileName) 1025 except ValueError: 1026 pass 1027 else: 1028 msg = 'Instance with No fileName but has a delimiter\ 1029 did not raise error!' 1030 raise msg 1031 1032 def test_no_constructors(self): 1033 1034 try: 1035 G = Geospatial_data() 1036 # results = Geospatial_data(file_name = fileName) 1037 # dict = import_points_file(fileName) 1038 except ValueError: 1039 pass 1040 else: 1041 msg = 'Instance must have a filename or data points' 1042 raise msg 1043 1044 1045 1012 1046 if __name__ == "__main__": 1013 1047 #suite = unittest.makeSuite(Test_Geospatial_data, 'test_ensure_absolute')
Note: See TracChangeset
for help on using the changeset viewer.