Changeset 3735
- Timestamp:
- Oct 10, 2006, 4:54:34 PM (18 years ago)
- Location:
- anuga_core/source/anuga/geospatial_data
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/geospatial_data/geospatial_data.py
r3723 r3735 511 511 if (file_name[-4:] == ".xya"): 512 512 if absolute is True: 513 _write_xya_file(file_name, self.get_data_points(absolute=True), 514 self.get_all_attributes()) 513 _write_xya_file(file_name, 514 self.get_data_points(absolute=True), 515 self.get_all_attributes()) 515 516 else: 516 _write_xya_file(file_name, self.get_data_points(absolute=False), 517 self.get_all_attributes(), 518 self.get_geo_reference()) 517 _write_xya_file(file_name, 518 self.get_data_points(absolute=False), 519 self.get_all_attributes(), 520 self.get_geo_reference()) 519 521 520 522 elif (file_name[-4:] == ".pts"): 521 523 if absolute is True: 522 _write_pts_file(file_name, self.get_data_points(absolute), 523 self.get_all_attributes()) 524 _write_pts_file(file_name, 525 self.get_data_points(absolute), 526 self.get_all_attributes()) 524 527 else: 525 _write_pts_file(file_name, self.get_data_points(absolute), 526 self.get_all_attributes(), 527 self.get_geo_reference()) 528 _write_pts_file(file_name, 529 self.get_data_points(absolute), 530 self.get_all_attributes(), 531 self.get_geo_reference()) 528 532 else: 529 533 msg = 'Unknown file type %s ' %file_name … … 632 636 return pointlist, att_dict, geo_reference 633 637 634 def _write_pts_file(file_name, write_data_points, 635 write_attributes, 636 write_geo_reference = None): 638 def _write_pts_file(file_name, 639 write_data_points, 640 write_attributes=None, 641 write_geo_reference=None): 637 642 """ 638 643 Write .pts NetCDF file … … 659 664 outfile.institution = 'Geoscience Australia' 660 665 outfile.description = 'NetCDF format for compact and portable storage ' +\ 661 'of spatial point data'666 'of spatial point data' 662 667 663 668 # dimension definitions … … 673 678 outfile.variables['points'][:] = write_data_points #.astype(Float32) 674 679 675 for key in write_attributes.keys(): 676 outfile.createVariable(key, Float, ('number_of_points',)) 677 outfile.variables[key][:] = write_attributes[key] #.astype(Float32) 680 if write_attributes is not None: 681 for key in write_attributes.keys(): 682 outfile.createVariable(key, Float, ('number_of_points',)) 683 outfile.variables[key][:] = write_attributes[key] #.astype(Float32) 678 684 679 685 if write_geo_reference is not None: … … 684 690 685 691 686 def _write_xya_file( file_name, write_data_points, 687 write_attributes, 688 write_geo_reference = None, 689 delimiter = ','): 692 def _write_xya_file(file_name, 693 write_data_points, 694 write_attributes=None, 695 write_geo_reference=None, 696 delimiter = ','): 690 697 """ 691 698 export a file, file_name, with the xya format … … 697 704 fd = open(file_name,'w') 698 705 titlelist = "" 699 for title in pointattributes.keys(): 700 titlelist = titlelist + title + delimiter 701 titlelist = titlelist[0:-len(delimiter)] # remove the last delimiter 706 if pointattributes is not None: 707 for title in pointattributes.keys(): 708 titlelist = titlelist + title + delimiter 709 titlelist = titlelist[0:-len(delimiter)] # remove the last delimiter 702 710 fd.write(titlelist+"\n") 711 703 712 #<vertex #> <x> <y> [attributes] 704 for i,vert in enumerate( points): 705 706 attlist = "," 707 for att in pointattributes.keys(): 708 attlist = attlist + str(pointattributes[att][i])+ delimiter 709 attlist = attlist[0:-len(delimiter)] # remove the last delimiter 710 attlist.strip() 711 fd.write( str(vert[0]) + delimiter 712 + str(vert[1]) 713 + attlist + "\n") 713 for i, vert in enumerate( points): 714 715 716 if pointattributes is not None: 717 attlist = "," 718 for att in pointattributes.keys(): 719 attlist = attlist + str(pointattributes[att][i])+ delimiter 720 attlist = attlist[0:-len(delimiter)] # remove the last delimiter 721 attlist.strip() 722 else: 723 attlist = '' 724 725 fd.write(str(vert[0]) + delimiter + 726 str(vert[1]) + attlist + "\n") 714 727 715 728 if write_geo_reference is not None: -
anuga_core/source/anuga/geospatial_data/test_geospatial_data.py
r3733 r3735 476 476 477 477 478 def no_test_clip1_inside_outside(self):478 def test_clip1_inside_outside(self): 479 479 """test_clip1_inside_outside(self): 480 480 … … 509 509 FN = 'test_combine.pts' 510 510 G.export_points_file(FN) 511 512 513 # Read it back in 514 G3 = Geospatial_data(FN) 515 516 517 # Check result 518 assert allclose(G3.get_data_points(), new_points) 519 520 511 521 os.remove(FN) 512 522 … … 972 982 973 983 def test_writepts(self): 984 """test_writepts: Test that storage of x,y,attributes works 985 """ 974 986 att_dict = {} 975 987 pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) … … 977 989 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 978 990 geo_reference=Geo_reference(56,1.9,1.9) 979 991 992 # Test pts format 980 993 fileName = tempfile.mktemp(".pts") 981 982 994 G = Geospatial_data(pointlist, att_dict, geo_reference) 983 984 995 G.export_points_file(fileName, False) 985 986 results = Geospatial_data(file_name = fileName) 987 996 results = Geospatial_data(file_name=fileName) 988 997 os.remove(fileName) 989 998 … … 992 1001 answer = [10.0, 0.0, 10.4] 993 1002 assert allclose(results.get_attributes('brightness'), answer) 994 995 996 1003 self.failUnless(geo_reference == geo_reference, 997 1004 'test_writepts failed. Test geo_reference') 1005 1006 # Test xya format 1007 fileName = tempfile.mktemp(".xya") 1008 G = Geospatial_data(pointlist, att_dict, geo_reference) 1009 G.export_points_file(fileName, False) 1010 results = Geospatial_data(file_name=fileName) 1011 os.remove(fileName) 1012 assert allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 1013 assert allclose(results.get_attributes('elevation'), [10.0, 0.0, 10.4]) 1014 answer = [10.0, 0.0, 10.4] 1015 assert allclose(results.get_attributes('brightness'), answer) 1016 self.failUnless(geo_reference == geo_reference, 1017 'test_writepts failed. Test geo_reference') 1018 1019 def test_writepts_no_attributes(self): 1020 """test_writepts_no_attributes: Test that storage of x,y alone works 1021 """ 1022 att_dict = {} 1023 pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 1024 geo_reference=Geo_reference(56,1.9,1.9) 1025 1026 # Test pts format 1027 fileName = tempfile.mktemp(".pts") 1028 G = Geospatial_data(pointlist, None, geo_reference) 1029 G.export_points_file(fileName, False) 1030 results = Geospatial_data(file_name=fileName) 1031 os.remove(fileName) 1032 1033 assert allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 1034 self.failUnless(geo_reference == geo_reference, 1035 'test_writepts failed. Test geo_reference') 1036 1037 # Test xya format 1038 fileName = tempfile.mktemp(".xya") 1039 G = Geospatial_data(pointlist, None, geo_reference) 1040 G.export_points_file(fileName, False) 1041 results = Geospatial_data(file_name=fileName) 1042 os.remove(fileName) 1043 assert allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 1044 self.failUnless(geo_reference == geo_reference, 1045 'test_writepts failed. Test geo_reference') 1046 1047 998 1048 999 1049 ########################## BAD .PTS ##########################
Note: See TracChangeset
for help on using the changeset viewer.