Changeset 2465


Ignore:
Timestamp:
Mar 2, 2006, 3:36:17 PM (19 years ago)
Author:
nick
Message:

Continued development of "add" function for geospatial_data
still requires some tests

Location:
inundation/geospatial_data
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/geospatial_data/geospatial_data.py

    r2309 r2465  
    1 """Class Geospatial_data - Manipulation of locations on the planet and associated attributes.
    2 
     1"""Class Geospatial_data - Manipulation of locations on the planet and
     2associated attributes.
    33
    44"""
     
    66
    77from utilities.numerical_tools import ensure_numeric
     8
     9from Numeric import concatenate
    810       
    911class Geospatial_data:
     
    8385            self.geo_reference = geo_reference
    8486        else:
    85             msg = 'Argument geo_reference must be a valid Geo_reference object or None.'
     87            msg = 'Argument geo_reference must be a valid Geo_reference \
     88                    object or None.'
    8689            raise msg
    8790
     
    9497        return self.geo_reference
    9598       
    96     def get_data_points(self):
    97         return self.data_points
     99    def get_data_points(self, absolute = True):
     100        if absolute is True:
     101            xll = self.geo_reference.xllcorner
     102            yll = self.geo_reference.yllcorner
     103            return self.data_points + [xll, yll]
     104           
     105        else:
     106            return self.data_points
    98107
    99108    def get_attributes(self, attribute_name = None):
     
    107116                attribute_name = self.default_attribute_name
    108117            else:
    109                 attribute_name = self.attributes.keys()[0] #Take the first one from keys
     118                attribute_name = self.attributes.keys()[0]
     119                # above line takes the first one from keys
    110120               
    111121
     
    114124
    115125        return self.attributes[attribute_name]
     126
     127    def __add__(self,other):
     128        """
     129        returns the addition of 2 geospatical objects,
     130        objects are concatencated to the end of each other
     131           
     132        NOTE: doesn't add if objects contain different
     133        attributes test (yet) if attributes of different kinds
     134        are added to each other, this could be a problem
     135        """
     136
     137        a_points = self.get_data_points()
     138        b_points = other.get_data_points()
     139       
     140        a_attribute_keys = self.attributes.keys()
     141        b_attribute_keys = other.attributes.keys()
     142       
     143        geo_ref1 = self.get_geo_reference()
     144        zone1 = geo_ref1.get_zone()
     145       
     146        geo_ref2 = other.get_geo_reference()
     147        zone2 = geo_ref2.get_zone()
     148       
     149        if zone1 == zone2:  # and some more checks to add
     150           
     151            c_points = concatenate((a_points, b_points), axis = 0)
     152            new_dictionary = {}
     153            for x in self.attributes.keys():
     154                if other.attributes.has_key(x):
     155
     156                    attrib_a = self.attributes[x]
     157                    attrib_b = other.attributes[x]
     158                    new_dictionary[x] = concatenate((attrib_a, attrib_b))
     159
     160                else:
     161                    msg = 'Both geospatial_data objects must have the same \
     162                    attributes to allow addition.'
     163                    raise msg
     164 
     165            return Geospatial_data(c_points, new_dictionary)
     166           
     167        else:
     168            msg = 'Both geospatial_data objects must be in the same \
     169            ZONE to allow addition.'
     170            raise msg
     171
     172
     173#        else:
     174#            print 'zones are not the same'
     175           
     176       
     177
     178   
     179     
    116180       
    117181
     
    159223                           geo_reference = geo)
    160224
    161    
    162 
    163            
    164            
    165        
     225
     226           
     227           
     228       
  • inundation/geospatial_data/test_geospatial_data.py

    r2454 r2465  
    33
    44import unittest
    5 from Numeric import zeros, array, allclose
     5from Numeric import zeros, array, allclose, concatenate
    66from math import sqrt, pi
    77
    8 
    98from geospatial_data import *
     9
     10from coordinate_transforms.geo_reference import Geo_reference
    1011
    1112class Test_Geospatial_data(unittest.TestCase):
     
    6465
    6566
    66         P = G.get_data_points()
     67        P = G.get_data_points(absolute = False)
    6768        assert allclose(P, [[1.0, 2.1], [3.0, 5.3]])       
    68        
     69
     70        P = G.get_data_points(absolute = True)
     71        assert allclose(P, [[101.0, 202.1], [103.0, 205.3]])       
     72
    6973        V = G.get_attributes() #Simply get them
    7074        assert allclose(V, [2, 4])
     
    7276        V = G.get_attributes('attribute') #Get by name
    7377        assert allclose(V, [2, 4])
    74 
    7578
    7679    def test_get_attributes_2(self):
     
    8689
    8790
    88         P = G.get_data_points()
     91        P = G.get_data_points(absolute = False)
    8992        assert allclose(P, [[1.0, 2.1], [3.0, 5.3]])       
    9093       
     
    160163        G = points_dictionary2geospatial_data(points_dict)
    161164
    162         P = G.get_data_points()
     165        P = G.get_data_points(absolute = False)
    163166        assert allclose(P, [[1.0, 2.1], [3.0, 5.3]])       
    164167       
     
    175178        assert allclose(V, [79.4, -7])
    176179
    177     def xtest_add(self):
    178         points = [[1.0, 2.1], [3.0, 5.3]]
    179         attributes = [2, 4]
     180    def test_add(self):
     181        """ test the addition of two geospatical objects
     182            no geo_reference see next test
     183        """
     184        points = [[1.0, 2.1], [3.0, 5.3]]
     185        attributes = {'depth':[2, 4], 'elevation':[6.1, 5]}
     186        attributes1 = {'depth':[2, 4], 'elevation':[2.5, 1]}
    180187        G1 = Geospatial_data(points, attributes)       
    181         G2 = Geospatial_data(points, attributes)       
     188        G2 = Geospatial_data(points, attributes1)
     189       
     190#        print 'G1 depth=', G1.get_attributes('depth')
     191#        print 'G1 attrib keys=', G1.attributes.keys()
     192        g3 = geospatial_data2points_dictionary(G1)
     193#        print 'g3=', g3
     194       
    182195        G = G1 + G2
    183        
    184         assert G.attributes.keys()[0] == 'attribute'
    185         assert allclose(G.attributes.values()[0], [2, 4, 2, 4])
     196
     197#        g = geospatial_data2points_dictionary(G)
     198#        print 'G=', g
     199#        print 'G points =', G.get_data_points()
     200#        print 'G attrib keys =', G.attributes.keys()
     201#        print 'G test =', G.get_attributes('elevation')
     202        assert G.attributes.has_key('depth')
     203        assert G.attributes.has_key('elevation')
     204        assert allclose(G.attributes['depth'], [2, 4, 2, 4])
     205        assert allclose(G.attributes['elevation'], [6.1, 5, 2.5, 1])
    186206        assert allclose(G.get_data_points(), [[1.0, 2.1], [3.0, 5.3],
    187207                                              [1.0, 2.1], [3.0, 5.3]])
    188208       
    189 
    190 
     209    def xtest_add1 (self):
     210        """
     211        difference in Geo_reference resolved
     212        """
     213        points = [[1.0, 2.1], [3.0, 5.3]]
     214        points1 = [[5.0, 6.1], [6.0, 3.3]]
     215        attributes = [2, 4]
     216        attributes1 = [5, 76]
     217        geo_ref = Geo_reference(55, 1.0, 2.0)
     218        geo_ref1 = Geo_reference(zone = 55, xllcorner = 0.1,
     219                                yllcorner = 3.0, datum = 'wgs84',
     220                                projection = 'UTM', units = 'm')
     221                               
     222        G1 = Geospatial_data(points, attributes, geo_ref)
     223        G2 = Geospatial_data(points1, attributes1, geo_ref1)
     224
     225        G = G1 + G2
     226       
     227        assert allclose(G.get_geo_reference().get_xllcorner(), 0.1)
     228        assert allclose(G.get_geo_reference().get_yllcorner(), 2.0)
     229        assert allclose(G.get_data_points(), [[2.0, 4.1], [4.0, 7.3], [5.1, 9.3], [6.1, 6.3]])                             
     230       
    191231       
    192232
Note: See TracChangeset for help on using the changeset viewer.