Changeset 4154


Ignore:
Timestamp:
Jan 9, 2007, 4:15:45 PM (17 years ago)
Author:
duncan
Message:

added export of .txt files

Location:
anuga_core/source/anuga/geospatial_data
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/geospatial_data/geospatial_data.py

    r4150 r4154  
    608608               
    609609        elif (file_name[-4:] == ".txt"):
    610             _write_xya_file(file_name,
     610            msg = "ERROR: trying to write a .txt file with relative data."
     611            assert absolute, msg
     612            _write_csv_file(file_name,
    611613                            self.get_data_points(absolute=True),
    612614                            self.get_all_attributes())
     
    10091011
    10101012
     1013def _write_csv_file(file_name,
     1014                    write_data_points,
     1015                    write_attributes=None,
     1016                    delimiter=','):
     1017    """
     1018    export a file, file_name, with the xya format
     1019   
     1020    """
     1021    points = write_data_points
     1022    pointattributes = write_attributes
     1023   
     1024    fd = open(file_name,'w')
     1025    titlelist = "x" + delimiter + "y"  + delimiter
     1026    if pointattributes is not None:   
     1027        for title in pointattributes.keys():
     1028            titlelist = titlelist + title + delimiter
     1029        titlelist = titlelist[0:-len(delimiter)] # remove the last delimiter
     1030    fd.write(titlelist+"\n")
     1031   
     1032    #<vertex #> <x> <y> [attributes]
     1033    for i, vert in enumerate( points):
     1034
     1035
     1036        if pointattributes is not None:           
     1037            attlist = ","
     1038            for att in pointattributes.keys():
     1039                attlist = attlist + str(pointattributes[att][i])+ delimiter
     1040            attlist = attlist[0:-len(delimiter)] # remove the last delimiter
     1041            attlist.strip()
     1042        else:
     1043            attlist = ''
     1044
     1045        fd.write(str(vert[0]) + delimiter +
     1046                 str(vert[1]) + attlist + "\n")
     1047
     1048    fd.close()
    10111049   
    10121050def _point_atts2array(point_atts):
  • anuga_core/source/anuga/geospatial_data/test_geospatial_data.py

    r4150 r4154  
    11691169                         'test_writepts failed. Test geo_reference')
    11701170
     1171    def test_write_csv_attributes(self):
     1172        #test_write : Test that storage of x,y,attributes works
     1173       
     1174        att_dict = {}
     1175        pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     1176        att_dict['elevation'] = array([10.0, 0.0, 10.4])
     1177        att_dict['brightness'] = array([10.0, 0.0, 10.4])
     1178        geo_reference=Geo_reference(56,0,0)
     1179        # Test xya format
     1180        fileName = tempfile.mktemp(".txt")
     1181        G = Geospatial_data(pointlist, att_dict, geo_reference)
     1182        G.export_points_file(fileName)
     1183        #print "fileName", fileName
     1184        results = Geospatial_data(file_name=fileName)
     1185        os.remove(fileName)
     1186        assert allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     1187        assert allclose(results.get_attributes('elevation'), [10.0, 0.0, 10.4])
     1188        answer = [10.0, 0.0, 10.4]
     1189        assert allclose(results.get_attributes('brightness'), answer)
     1190       
     1191       
    11711192    def test_writepts_no_attributes(self):
    11721193
     
    12041225                         'test_writepts failed. Test geo_reference')
    12051226
    1206        
     1227       
     1228    def test_write_csv_no_attributes(self):
     1229        #test_write xya _no_attributes: Test that storage of x,y alone works
     1230       
     1231        att_dict = {}
     1232        pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     1233        geo_reference=Geo_reference(56,0,0)
     1234        # Test xya format
     1235        fileName = tempfile.mktemp(".txt")
     1236        G = Geospatial_data(pointlist, None, geo_reference)
     1237        G.export_points_file(fileName)
     1238        results = Geospatial_data(file_name=fileName)
     1239        os.remove(fileName)
     1240        assert allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     1241
     1242         
    12071243       
    12081244 ########################## BAD .PTS ##########################         
Note: See TracChangeset for help on using the changeset viewer.