Changeset 2698
- Timestamp:
- Apr 11, 2006, 4:53:24 PM (19 years ago)
- Location:
- inundation/geospatial_data
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/geospatial_data/geospatial_data.py
r2643 r2698 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 # absolute = True): 24 25 26 ''' 27 27 Create instance from data points and associated attributes 28 28 … … 50 50 have dimensions "points" etc. 51 51 xya file is a CVS file with lats(x), longs(y) and elevation(a). 52 first line must be attribute name eg elevation 53 54 absolute: Return data points that are absolute or relative to 55 to a xll and yll in a zone 56 57 """ 52 first line must be attribute name eg elevation 53 54 The format for a .xya file is: 55 1st line: [attribute names] 56 other lines: x y [attributes] 57 58 for example: 59 elevation, friction 60 0.6, 0.7, 4.9, 0.3 61 1.9, 2.8, 5, 0.3 62 2.7, 2.4, 5.2, 0.3 63 64 The first two columns are always implicitly assumed to be x, y coordinates. 65 Use the same delimiter for the attribute names and the data 66 67 An xya file can optionally end with 68 #geo reference 69 56 70 466600.0 71 8644444.0 72 73 When the 1st # is the zone, 74 2nd # the xllcorner and 75 3rd # the yllcorner 76 77 The format for a Points dictionary is: 78 79 ['pointlist'] a 2 column array describing points. 1st column x, 2nd column y. 80 ['attributelist'], a dictionary of 1D arrays, representing attribute values 81 at the point. The dictionary key is the attribute header. 82 ['geo_reference'] a Geo_refernece object. Use if the point information 83 is relative. This is optional. 84 eg 85 dic['pointlist'] = [[1.0,2.0],[3.0,5.0]] 86 dic['attributelist']['elevation'] = [[7.0,5.0] 87 88 ''' 58 89 59 90 … … 61 92 self.file_name = None 62 93 self.check_data_points(data_points) 63 64 94 self.set_attributes(attributes) 65 95 self.set_geo_reference(geo_reference) 66 96 self.set_default_attribute_name(default_attribute_name) 67 self.set_absolute(absolute)97 # self.set_absolute(absolute) 68 98 69 99 else: … … 156 186 self.file_name = file_name 157 187 print 'file name from set', self.file_name 158 188 ''' 159 189 def set_absolute(self, absolute = True): 160 190 if absolute is False: … … 162 192 else: 163 193 self.absolute = True 164 165 ''' 166 def set_self_from_file(self, file_name = None): 167 # check if file_name is a string and also that the file exists 168 if file_name is None: 169 self.file_name = None 170 171 else: 172 self.file_name = file_name 173 self = self.import_points_file(file_name) 174 return self 175 ''' 176 194 ''' 177 195 def get_geo_reference(self): 178 196 return self.geo_reference … … 292 310 return Geospatial_data(new_points, 293 311 new_attributes, 294 new_geo_ref, 295 absolute = False) 312 new_geo_ref) 296 313 297 314 … … 415 432 # return all_data 416 433 417 def new_export_points_file(self, ofile): 434 def export_points_file(self, ofile, absolute=False): 435 418 436 """ 419 437 write a points file, ofile, as a text (.xya) or binary (.pts) file 420 421 438 ofile is the file name, including the extension 422 423 439 The point_dict is defined at the top of this file. 424 440 """ 441 425 442 #this was done for all keys in the mesh file. 426 443 #if not mesh_dict.has_key('points'): 427 444 # mesh_dict['points'] = [] 445 428 446 if (ofile[-4:] == ".xya"): 429 _new_write_xya_file(ofile, self.get_data_points(self.absolute), 447 if absolute is True: 448 _write_xya_file(ofile, self.get_data_points(absolute), 449 self.get_all_attributes()) 450 else: 451 _write_xya_file(ofile, self.get_data_points(absolute), 430 452 self.get_all_attributes(), 431 453 self.get_geo_reference()) 454 432 455 elif (ofile[-4:] == ".pts"): 433 _new_write_pts_file(ofile, self.get_data_points(self.absolute), 456 if absolute is True: 457 _write_pts_file(ofile, self.get_data_points(absolute), 458 self.get_all_attributes()) 459 else: 460 _write_pts_file(ofile, self.get_data_points(absolute), 434 461 self.get_all_attributes(), 435 462 self.get_geo_reference()) … … 732 759 733 760 734 def _ new_write_pts_file(file_name, write_data_points,761 def _write_pts_file(file_name, write_data_points, 735 762 write_attributes, 736 763 write_geo_reference = None): … … 782 809 783 810 784 def _ new_write_xya_file( file_name, write_data_points,811 def _write_xya_file( file_name, write_data_points, 785 812 write_attributes, 786 813 write_geo_reference = None, … … 986 1013 # G_points_dict = geospatial_data2points_dictionary(G) 987 1014 988 G. new_export_points_file(results_file)1015 G.export_points_file(results_file) 989 1016 990 1017 -
inundation/geospatial_data/test_geospatial_data.py
r2643 r2698 313 313 FN = 'test_points.xya' 314 314 G1 = Geospatial_data(points, att_dict) 315 G1. new_export_points_file(FN)316 # G1. new_export_points_file(ofile)315 G1.export_points_file(FN) 316 # G1.export_points_file(ofile) 317 317 318 318 #Create object from file … … 320 320 321 321 assert allclose(G.get_data_points(), points) 322 assert allclose(G.get_attributes('att1'), attributes) 323 assert allclose(G.get_attributes('att2'), array(attributes) + 1) 324 325 os.remove(FN) 326 327 def test_create_from_xya_file1(self): 328 """ 329 Check that object can be created from an Absolute xya file 330 """ 331 332 points = [[1.0, 2.1], [3.0, 5.3], [5.0, 6.1], [6.0, 3.3]] 333 attributes = [2, 4, 5, 76] 334 335 att_dict = {'att1': attributes, 336 'att2': array(attributes) +1} 337 338 geo_ref = Geo_reference(56, 10, 5) 339 340 # Create points as an xya file 341 FN = 'test_points.xya' 342 G1 = Geospatial_data(points, att_dict, geo_ref) 343 344 G1.export_points_file(FN, absolute = True) 345 346 #Create object from file 347 G = Geospatial_data(file_name = FN) 348 349 assert allclose(G.get_data_points(absolute = True), 350 G1.get_data_points(absolute = True)) 322 351 assert allclose(G.get_attributes('att1'), attributes) 323 352 assert allclose(G.get_attributes('att2'), array(attributes) + 1) … … 414 443 415 444 try: 416 # export_points_file("_???/yeah.xya",dict) 417 G.new_export_points_file("_???/yeah.xya") 445 G.export_points_file("_???/yeah.xya") 418 446 419 447 except IOError: … … 581 609 582 610 fileName = tempfile.mktemp(".xya") 583 G = Geospatial_data( pointlist, att_dict, geo_reference, absolute = False)584 G. new_export_points_file(fileName)611 G = Geospatial_data(pointlist, att_dict, geo_reference) 612 G.export_points_file(fileName) 585 613 dict2 = import_points_file(fileName) 586 614 #print "fileName",fileName … … 597 625 598 626 def test_export_xya_file2(self): 599 # dict = {}600 627 att_dict = {} 601 628 pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 602 629 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 603 630 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 604 # dict['attributelist'] = att_dict605 631 606 632 fileName = tempfile.mktemp(".xya") 607 633 G = Geospatial_data(pointlist, att_dict) 608 G. new_export_points_file(fileName)634 G.export_points_file(fileName) 609 635 dict2 = import_points_file(fileName) 610 #print "fileName",fileName 611 os.remove(fileName) 612 #print "dict2",dict2 636 os.remove(fileName) 613 637 614 638 assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) … … 617 641 assert allclose(dict2['attributelist']['brightness'], answer) 618 642 619 def test_new_export_xya_file2(self): 620 dict = {} 643 def test_export_xya_file2(self): 621 644 att_dict = {} 622 645 pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 623 att_dict['elevation'] = array([10.1, 0.0, 10.4]) 624 att_dict['brightness'] = array([10.0, 1.0, 10.4]) 625 626 fileName = tempfile.mktemp(".xya") 627 628 G = Geospatial_data(pointlist, att_dict) 629 630 G.new_export_points_file(fileName) 631 646 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 647 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 648 geo_reference = Geo_reference(56,1.9,1.9) 649 650 651 fileName = tempfile.mktemp(".xya") 652 G = Geospatial_data(pointlist, att_dict, geo_reference) 653 G.export_points_file(fileName, absolute=True) 632 654 dict2 = import_points_file(fileName) 633 634 os.remove(fileName) 635 636 assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 637 assert allclose(dict2['attributelist']['elevation'], [10.1, 0.0, 10.4]) 638 answer = [10.0, 1.0, 10.4] 655 #print "fileName",fileName 656 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]) 662 answer = [10.0, 0.0, 10.4] 639 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') 640 667 641 668 def test_new_export_pts_file(self): … … 649 676 G = Geospatial_data(pointlist, att_dict) 650 677 651 G. new_export_points_file(fileName)678 G.export_points_file(fileName) 652 679 653 680 dict2 = import_points_file(fileName) … … 660 687 assert allclose(dict2['attributelist']['brightness'], answer) 661 688 689 def test_new_export_absolute_pts_file(self): 690 att_dict = {} 691 pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 692 att_dict['elevation'] = array([10.1, 0.0, 10.4]) 693 att_dict['brightness'] = array([10.0, 1.0, 10.4]) 694 geo_ref = Geo_reference(50, 25, 55) 695 696 fileName = tempfile.mktemp(".pts") 697 698 G = Geospatial_data(pointlist, att_dict, geo_ref) 699 700 G.export_points_file(fileName, absolute=True) 701 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]) 709 answer = [10.0, 1.0, 10.4] 710 assert allclose(dict2['attributelist']['brightness'], answer) 662 711 663 712 def test_loadpts(self): … … 710 759 fileName = tempfile.mktemp(".pts") 711 760 712 G = Geospatial_data(pointlist, att_dict, geo_reference , absolute = False)713 714 G. new_export_points_file(fileName)761 G = Geospatial_data(pointlist, att_dict, geo_reference) 762 763 G.export_points_file(fileName) 715 764 716 765 dict2 = import_points_file(fileName) … … 867 916 fileName2 = tempfile.mktemp(".pts") 868 917 869 # G1. new_export_points_file(fileName1)870 # G2. new_export_points_file(fileName2)918 # G1.export_points_file(fileName1) 919 # G2.export_points_file(fileName2) 871 920 872 921 results_file = 'resulting_points.pts' … … 876 925 G = G1 + G2 877 926 878 G. new_export_points_file(results_file)927 G.export_points_file(results_file) 879 928 # add_points_files(fileName1, fileName2, results_file ) 880 929
Note: See TracChangeset
for help on using the changeset viewer.