Changeset 2698


Ignore:
Timestamp:
Apr 11, 2006, 4:53:24 PM (18 years ago)
Author:
nick
Message:

updates to geospatial_data.py removed absolute from init
and updated export_points_file

Location:
inundation/geospatial_data
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/geospatial_data/geospatial_data.py

    r2643 r2698  
    2020                 geo_reference = None,
    2121                 default_attribute_name = None,
    22                  file_name = None,
    23                  absolute = True):
    24 
    25        
    26         """
     22                 file_name = None):#,
     23#                 absolute = True):
     24
     25       
     26        '''
    2727        Create instance from data points and associated attributes
    2828
     
    5050        have dimensions "points" etc.
    5151        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        '''
    5889       
    5990
     
    6192            self.file_name = None
    6293            self.check_data_points(data_points)
    63 
    6494            self.set_attributes(attributes)
    6595            self.set_geo_reference(geo_reference)
    6696            self.set_default_attribute_name(default_attribute_name)
    67             self.set_absolute(absolute)
     97#            self.set_absolute(absolute)
    6898       
    6999        else:
     
    156186                self.file_name = file_name
    157187                print 'file name from set', self.file_name
    158    
     188    '''   
    159189    def set_absolute(self, absolute = True):
    160190        if absolute is False:
     
    162192        else:
    163193            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    '''   
    177195    def get_geo_reference(self):
    178196        return self.geo_reference
     
    292310        return Geospatial_data(new_points,
    293311                               new_attributes,
    294                                new_geo_ref,
    295                                absolute = False)
     312                               new_geo_ref)
    296313
    297314
     
    415432#        return all_data
    416433   
    417     def new_export_points_file(self, ofile):
     434    def export_points_file(self, ofile, absolute=False):
     435       
    418436        """
    419437        write a points file, ofile, as a text (.xya) or binary (.pts) file
    420    
    421438        ofile is the file name, including the extension
    422    
    423439        The point_dict is defined at the top of this file.
    424440        """
     441   
    425442        #this was done for all keys in the mesh file.
    426443        #if not mesh_dict.has_key('points'):
    427444        #    mesh_dict['points'] = []
     445           
    428446        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),
    430452                                   self.get_all_attributes(),
    431453                                   self.get_geo_reference())
     454                                   
    432455        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),
    434461                                   self.get_all_attributes(),
    435462                                   self.get_geo_reference())
     
    732759
    733760
    734 def _new_write_pts_file(file_name, write_data_points,
     761def _write_pts_file(file_name, write_data_points,
    735762                                   write_attributes,
    736763                                   write_geo_reference = None):
     
    782809
    783810
    784 def _new_write_xya_file( file_name, write_data_points,
     811def _write_xya_file( file_name, write_data_points,
    785812                                    write_attributes,
    786813                                    write_geo_reference = None,
     
    9861013#    G_points_dict = geospatial_data2points_dictionary(G)
    9871014
    988     G.new_export_points_file(results_file)
     1015    G.export_points_file(results_file)
    9891016   
    9901017   
  • inundation/geospatial_data/test_geospatial_data.py

    r2643 r2698  
    313313        FN = 'test_points.xya'
    314314        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)
    317317
    318318        #Create object from file
     
    320320       
    321321        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))
    322351        assert allclose(G.get_attributes('att1'), attributes)
    323352        assert allclose(G.get_attributes('att2'), array(attributes) + 1)
     
    414443       
    415444        try:
    416 #            export_points_file("_???/yeah.xya",dict)
    417             G.new_export_points_file("_???/yeah.xya")
     445            G.export_points_file("_???/yeah.xya")
    418446           
    419447        except IOError:
     
    581609       
    582610        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)
    585613        dict2 = import_points_file(fileName)
    586614        #print "fileName",fileName
     
    597625
    598626    def test_export_xya_file2(self):
    599 #        dict = {}
    600627        att_dict = {}
    601628        pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    602629        att_dict['elevation'] = array([10.0, 0.0, 10.4])
    603630        att_dict['brightness'] = array([10.0, 0.0, 10.4])
    604 #        dict['attributelist'] = att_dict
    605631       
    606632        fileName = tempfile.mktemp(".xya")
    607633        G = Geospatial_data(pointlist, att_dict)
    608         G.new_export_points_file(fileName)
     634        G.export_points_file(fileName)
    609635        dict2 = import_points_file(fileName)
    610         #print "fileName",fileName
    611         os.remove(fileName)
    612         #print "dict2",dict2
     636        os.remove(fileName)
    613637       
    614638        assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     
    617641        assert allclose(dict2['attributelist']['brightness'], answer)
    618642
    619     def test_new_export_xya_file2(self):
    620         dict = {}
     643    def test_export_xya_file2(self):
    621644        att_dict = {}
    622645        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)
    632654        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]
    639663        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')
    640667
    641668    def test_new_export_pts_file(self):
     
    649676        G = Geospatial_data(pointlist, att_dict)
    650677       
    651         G.new_export_points_file(fileName)
     678        G.export_points_file(fileName)
    652679
    653680        dict2 = import_points_file(fileName)
     
    660687        assert allclose(dict2['attributelist']['brightness'], answer)
    661688
     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)
    662711
    663712    def test_loadpts(self):
     
    710759        fileName = tempfile.mktemp(".pts")
    711760       
    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)
    715764       
    716765        dict2 = import_points_file(fileName)
     
    867916        fileName2 = tempfile.mktemp(".pts")
    868917
    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)
    871920       
    872921        results_file = 'resulting_points.pts'
     
    876925        G = G1 + G2
    877926       
    878         G.new_export_points_file(results_file)
     927        G.export_points_file(results_file)
    879928#        add_points_files(fileName1, fileName2, results_file )
    880929       
Note: See TracChangeset for help on using the changeset viewer.