Changeset 2928


Ignore:
Timestamp:
May 19, 2006, 5:29:13 PM (19 years ago)
Author:
nick
Message:

geospatial_data object updated so not to use dictionaries

Location:
inundation/geospatial_data
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/geospatial_data/geospatial_data.py

    r2890 r2928  
    2020                 geo_reference = None,
    2121                 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        """
    2728        Create instance from data points and associated attributes
    28 
    2929
    3030        data_points: x,y coordinates in meters. Type must be either a
     
    8585            dic['pointlist'] = [[1.0,2.0],[3.0,5.0]]
    8686            dic['attributelist']['elevation'] = [[7.0,5.0]
     87               
     88        delimiter:
     89           
     90        verbose:
    8791         
    88         '''
     92        """
    8993
    9094        if isinstance(data_points, basestring):
     
    9397
    9498        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
    95102            self.file_name = None
    96103            self.check_data_points(data_points)
     
    98105            self.set_geo_reference(geo_reference)
    99106            self.set_default_attribute_name(default_attribute_name)
    100 #            self.set_absolute(absolute)
    101         
     107
     108       
    102109        else:
    103110            if access(file_name, F_OK) == 0 :
    104111                msg = 'File %s does not exist or is not accessible' %file_name
    105                 raise msg
     112                raise IOError, msg
    106113            else:
    107114                data = {}   
     
    109116                # if file name then all provided info will be removed!
    110117                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)
    112120               
    113 #                print'data: point in init',self.data_points
    114 #                print'attrib: point in init',self.attributes
    115 #                print'geo_ref: point in init',self.geo_reference
    116121                self.check_data_points(self.data_points)
    117122                self.set_attributes(self.attributes)
     
    126131        if data_points is None:
    127132            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
    132135           
    133136        else:
     
    191194                self.file_name = file_name
    192195                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   
    200197    def get_geo_reference(self):
    201198        return self.geo_reference
     
    245242    def __add__(self, other):
    246243        """
    247         returns the addition of 2 geospatical objects,
     244        Returns the addition of 2 geospatical objects,
    248245        objects are concatencated to the end of each other
    249246           
     
    253250        Always return relative points!
    254251        """
    255        
     252
    256253        # find objects zone and checks if the same
    257254        geo_ref1 = self.get_geo_reference()
     
    288285        #Now both point sets are relative to new_geo_ref and zones have been reconciled
    289286
    290 
    291287        # Concatenate points
    292288        new_points = concatenate((new_relative_points1,
    293289                                  new_relative_points2),
    294290                                  axis = 0)
    295  #       print 'new points:', new_points
    296 
    297291     
    298292        # Concatenate attributes
     
    310304                raise msg
    311305
    312        
    313306        # Instantiate new data object and return   
    314307        return Geospatial_data(new_points,
    315308                               new_attributes,
    316309                               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 other
    324            
    325         NOTE: does not add if objects contain different
    326         attributes 
    327         """
    328 
    329         # sets xll and yll as the smallest from self and other
    330         if self.geo_reference.xllcorner <= other.geo_reference.xllcorner:
    331             xll = self.geo_reference.xllcorner
    332         else:
    333             xll = other.geo_reference.xllcorner
    334 
    335         if self.geo_reference.yllcorner <= other.geo_reference.yllcorner:
    336             yll = self.geo_reference.yllcorner
    337         else:
    338             yll = other.geo_reference.yllcorner
    339 
    340         # find objects zone and checks if the same
    341         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's
    352             # absolute data points and concatenate
    353             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 msg
    369                
    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 msg
    377 
    378     '''   
    379310   
    380311    ###
    381312    #  IMPORT/EXPORT POINTS FILES
    382313    ###
    383     def new_import_points_file(self, ofile, delimiter = None, verbose = False):
     314
     315    def import_points_file(self, ofile, delimiter = None, verbose = False):
    384316        """ load an .xya or .pts file
    385317        Note: will throw an IOError if it can't load the file.
     
    393325                    try:
    394326                        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, ',')
    397328                    except SyntaxError:
    398329                        fd.close()
    399330                        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, ' ')
    402332                else:
    403333                    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)
    406335                fd.close()
    407336            except (IndexError,ValueError,SyntaxError):
     
    416345        elif ofile[-4:]== ".pts":
    417346            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)
    422348            except IOError, e:   
    423349                msg = 'Could not open file %s ' %ofile
     
    472398   
    473399
    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
     402def _read_pts_file(file_name, verbose = False):
    540403    """Read .pts NetCDF file
    541404   
     
    545408    dic['attributelist']['elevation'] = [[7.0,5.0]
    546409    """   
    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
    552411    from Scientific.IO.NetCDF import NetCDFFile
    553412   
     
    596455
    597456
    598 def _new_read_xya_file( fd, delimiter):
     457def _read_xya_file( fd, delimiter):
    599458#    print 'hello from read xya data'
    600459    points = []
     
    653512
    654513
    655 def _read_pts_file(file_name, verbose = False):
     514def _old_read_pts_file(file_name, verbose = False):
    656515    """Read .pts NetCDF file
    657516   
     
    709568
    710569
    711 def _read_xya_file( fd, delimiter):
    712 #    print 'hello from read xya data'
     570def _old_read_xya_file( fd, delimiter):
    713571    points = []
    714572    pointattributes = []
     
    768626                                   write_attributes,
    769627                                   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   
    772633    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.
    775635    # There can't be an attribute called points
    776636    # consider format change
    777637    # method changed by NB not sure if above statement is correct
    778 
    779     ''' should create new test for this
     638   
     639    should create new test for this
    780640    legal_keys = ['pointlist', 'attributelist', 'geo_reference']
    781641    for key in point_atts.keys():
    782642        msg = 'Key %s is illegal. Valid keys are %s' %(key, legal_keys)
    783643        assert key in legal_keys, msg
    784     '''   
     644    """   
    785645    from Scientific.IO.NetCDF import NetCDFFile
    786646    # NetCDF file definition
     
    843703                  + str(vert[1])
    844704                  + 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
    851706    if  write_geo_reference is not None:
    852707        write_geo_reference.write_ASCII(fd)
    853708    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
    935710
    936711   
     
    1002777           
    1003778def add_points_files(add_file1, add_file2, results_file):
    1004     ''' adds the points and attruibutes of 2 pts or xya files and
     779    """ adds the points and attruibutes of 2 pts or xya files and
    1005780    writes it to a pts file
    1006781   
    1007782    NOTE will add the function to check and remove points from one set
    1008783    that are shared. This will require some work and maybe __subtract__ function
    1009     '''
     784    """
    1010785   
    1011786    G1 = Geospatial_data(file_name = add_file1)
  • inundation/geospatial_data/test_geospatial_data.py

    r2890 r2928  
    3653651.0, 0.0, 10.4, 40.0\n")
    366366        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])
    374373
    375374    def test_loadxya2(self):
     
    3863851.0 0.0 10.4 40.0\n")
    387386        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])
    395395     
    396396    def test_loadxya3(self):
     
    4114113\n")
    412412        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
    433421    def test_read_write_points_file_bad2(self):
    434 #        dict = {}
    435422        att_dict = {}
    436423        pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    437424        att_dict['elevation'] = array([10.0, 0.0, 10.4])
    438425        att_dict['brightness'] = array([10.0, 0.0, 10.4])
    439 #        dict['attributelist'] = att_dict
    440426        geo_reference = Geo_reference(56,1.9,1.9)
    441427       
     
    448434            pass
    449435        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!')
    452440                   
    453441    def test_loadxy_bad(self):
     
    463451        #print fileName
    464452        try:
    465             dict = import_points_file(fileName,delimiter = ' ')
     453            results = Geospatial_data(fileName, delimiter = ' ')
    466454        except IOError:
    467455            pass
    468456        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!')
    471461        os.remove(fileName)
    472462       
     
    483473        #print fileName
    484474        try:
    485             dict = import_points_file(fileName,delimiter = ' ')
     475            results = Geospatial_data(fileName, delimiter = ' ')
    486476        except IOError:
    487477            pass
    488478        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
    491481        os.remove(fileName)
    492482   
     
    505495        file.close()
    506496        try:
    507             dict = import_points_file(fileName,delimiter = ' ')
     497            results = Geospatial_data(fileName, delimiter = ' ')
    508498        except IOError:
    509499            pass
    510500        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
    513503        os.remove(fileName)
    514504     
    515     def test_loadxy_bad4(self):
    516         """
    517         specifying wrong delimiter
    518         """
    519         import os
    520        
    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             pass
    533         else:
    534             self.failUnless(0 ==1,
    535                         'bad xya file did not raise error!')   
    536         os.remove(fileName)
    537  
    538505    def test_loadxy_bad4(self):
    539506        """
     
    551518        file.close()
    552519        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 = ' ')
    554524        except IOError:
    555525            pass
    556526        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!')   
    559532        os.remove(fileName)
    560533
     
    575548        file.close()
    576549        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 = ' ')
    578554        except IOError:
    579555            pass
    580556        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!')   
    583562        os.remove(fileName)             
    584563
     
    587566       
    588567        fileName = tempfile.mktemp(".xya")
    589         #print fileName
    590568        try:
    591             dict = import_points_file(fileName,delimiter = ' ')
     569            results = Geospatial_data(fileName, delimiter = ' ')
    592570        except IOError:
    593571            pass
    594572        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
    597582                       
    598583  ###################### .XYA ##############################
     
    611596        G = Geospatial_data(pointlist, att_dict, geo_reference)
    612597        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)
    614601        #print "fileName",fileName
    615602        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])
    620606        answer = [10.0, 0.0, 10.4]
    621         assert allclose(dict2['attributelist']['brightness'], answer)
     607        assert allclose(results.get_attributes(attribute_name = 'brightness'), answer)
    622608        #print "dict2['geo_reference']",dict2['geo_reference']
    623         self.failUnless(geo_reference == dict2['geo_reference'],
     609        self.failUnless(results.get_geo_reference() == geo_reference,
    624610                         'test_writepts failed. Test geo_reference')
    625611
    626612    def test_export_xya_file2(self):
     613        """test absolute xya file
     614        """
    627615        att_dict = {}
    628616        pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     
    633621        G = Geospatial_data(pointlist, att_dict)
    634622        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])
    640629        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        """
    644635        att_dict = {}
    645636        pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     
    651642        fileName = tempfile.mktemp(".xya")
    652643        G = Geospatial_data(pointlist, att_dict, geo_reference)
     644       
    653645        G.export_points_file(fileName, absolute=True)
    654         dict2 = import_points_file(fileName)
    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])
     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])
    662654        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                       
    668661    def test_new_export_pts_file(self):
    669662        att_dict = {}
     
    678671        G.export_points_file(fileName)
    679672
    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])
    686679        answer = [10.0, 1.0, 10.4]
    687         assert allclose(dict2['attributelist']['brightness'], answer)
     680        assert allclose(results.get_attributes(attribute_name = 'brightness'), answer)
    688681
    689682    def test_new_export_absolute_pts_file(self):
     
    700693        G.export_points_file(fileName, absolute=True)
    701694
    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])
    709701        answer = [10.0, 1.0, 10.4]
    710         assert allclose(dict2['attributelist']['brightness'], answer)
     702        assert allclose(results.get_attributes(attribute_name = 'brightness'), answer)
    711703
    712704    def test_loadpts(self):
     
    740732        outfile.close()
    741733       
    742         dict = import_points_file(fileName)
     734        results = Geospatial_data(file_name = fileName)
    743735        os.remove(fileName)
    744736        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])
    749739       
    750740    def test_writepts(self):
    751 #        dict = {}
    752741        att_dict = {}
    753742        pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    754743        att_dict['elevation'] = array([10.0, 0.0, 10.4])
    755744        att_dict['brightness'] = array([10.0, 0.0, 10.4])
    756 #        dict['attributelist'] = att_dict
    757745        geo_reference = Geo_reference(56,1.9,1.9)
    758746       
     
    763751        G.export_points_file(fileName)
    764752       
    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])
    771759        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,
    778764                         'test_writepts failed. Test geo_reference')
    779765       
     
    787773        #print fileName
    788774        try:
    789             dict = import_points_file(fileName)
     775            results = Geospatial_data(file_name = fileName)
     776#            dict = import_points_file(fileName)
    790777        except IOError:
    791778            pass
    792779        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!')
    795784
    796785
     
    916905        fileName2 = tempfile.mktemp(".pts")
    917906
    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)
    922910       
    923911        # add files
    924912       
    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
    929918       
    930919        #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),
    936921                        [[2.0, 0.0],[1.0, 1.0],
    937922                         [2.0, 0.0],[2.0, 2.0],
    938923                         [1.0, 3.0],[2.0, 2.0]])
    939         assert allclose(results_dict['attributelist']['elevation'],
     924        assert allclose(G.get_attributes(attribute_name = 'elevation'),
    940925                        [-10.0, 0.0, 10.4, 1.0, 15.0, 1.4])
    941926       
    942927        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,
    946931                         'test_writepts failed. Test geo_reference')
    947932                         
    948 #        os.remove(fileName1)
    949 #        os.remove(fileName2)
    950         os.remove(results_file)
    951 
    952    
     933        os.remove(fileName1)
     934        os.remove(fileName2)
     935       
    953936    def test_ensure_absolute(self):
    954937        points = [[2.0, 0.0],[1.0, 1.0],
     
    1010993        assert allclose(new_points, ab_points)
    1011994       
     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\
     10021.0 0.0 10.0 0.0\n\
     10030.0 1.0 0.0 10.0\n\
     10041.0 0.0 10.4 40.0\n\
     1005#geocrap\n\
     100656\n\
     100756.6\n\
     10083\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
    10121046if __name__ == "__main__":
    10131047    #suite = unittest.makeSuite(Test_Geospatial_data, 'test_ensure_absolute')
Note: See TracChangeset for help on using the changeset viewer.