Changeset 2306


Ignore:
Timestamp:
Jan 31, 2006, 3:34:13 PM (18 years ago)
Author:
ole
Message:

Maps to and from geospatial_data object to points_dictionary and unit tests

Location:
inundation/geospatial_data
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/geospatial_data/geospatial_data.py

    r2303 r2306  
    109109
    110110
    111 def geospatial_data2points_dictionary2(geospatial_data):
     111def geospatial_data2points_dictionary(geospatial_data):
    112112    """Convert geospatial data to points_dictionary
    113113    """
    114     pass
     114
     115    points_dictionary = {}
     116    points_dictionary['pointlist'] = geospatial_data.data_points
     117
     118    points_dictionary['attributelist'] = {}
     119
     120    for attribute_name in geospatial_data.attributes.keys():
     121        val = geospatial_data.attributes[attribute_name]
     122        points_dictionary['attributelist'][attribute_name] = val
     123
     124    points_dictionary['geo_reference'] = geospatial_data.geo_reference
     125
     126    return points_dictionary
     127
     128   
     129   
     130
     131
     132
    115133
    116134def points_dictionary2geospatial_data(points_dictionary):
    117135    """Convert points_dictionary to geospatial data object
    118136    """
    119     pass
    120137
     138    msg = 'Points dictionary must have key pointlist'
     139    assert points_dictionary.has_key('pointlist'), msg
     140
     141    msg = 'Points dictionary must have key attributelist'     
     142    assert points_dictionary.has_key('attributelist'), msg       
     143
     144    if points_dictionary.has_key('geo_reference'):
     145        geo = points_dictionary['geo_reference']
     146    else:
     147        geo = None
     148   
     149    return Geospatial_data(points_dictionary['pointlist'],
     150                           points_dictionary['attributelist'],
     151                           geo_reference = geo)
     152
     153   
    121154
    122155           
  • inundation/geospatial_data/test_geospatial_data.py

    r2303 r2306  
    108108            raise 'Should have raised exception'
    109109
     110
     111    def test_conversions_to_points_dict(self):
     112        """test conversions to points_dict
     113        """
     114       
     115        from coordinate_transforms.geo_reference import Geo_reference
     116        points = [[1.0, 2.1], [3.0, 5.3]]
     117        attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]}
     118        G = Geospatial_data(points, attributes,
     119                            geo_reference = Geo_reference(56, 100, 200),
     120                            default_attribute_name = 'a1')
     121
     122
     123        points_dict = geospatial_data2points_dictionary(G)
     124
     125        assert points_dict.has_key('pointlist')
     126        assert points_dict.has_key('attributelist')       
     127        assert points_dict.has_key('geo_reference')
     128
     129        assert allclose( points_dict['pointlist'], points )
     130
     131        A = points_dict['attributelist']
     132        assert A.has_key('a0')
     133        assert A.has_key('a1')
     134        assert A.has_key('a2')       
     135
     136        assert allclose( A['a0'], [0, 0] )
     137        assert allclose( A['a1'], [2, 4] )       
     138        assert allclose( A['a2'], [79.4, -7] )
     139
     140
     141        geo = points_dict['geo_reference']
     142        assert geo is G.geo_reference
     143
     144
     145    def test_conversions_from_points_dict(self):
     146        """test conversions from points_dict
     147        """
     148
     149        from coordinate_transforms.geo_reference import Geo_reference
     150       
     151        points = [[1.0, 2.1], [3.0, 5.3]]
     152        attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]}
     153
     154        points_dict = {}
     155        points_dict['pointlist'] = points
     156        points_dict['attributelist'] = attributes
     157        points_dict['geo_reference'] = Geo_reference(56, 100, 200)
    110158       
    111159
     160        G = points_dictionary2geospatial_data(points_dict)
     161
     162        P = G.get_data_points()
     163        assert allclose(P, [[1.0, 2.1], [3.0, 5.3]])       
     164       
     165        #V = G.get_attribute_values() #Get default attribute
     166        #assert allclose(V, [2, 4])
     167
     168        V = G.get_attribute_values('a0') #Get by name
     169        assert allclose(V, [0, 0])
     170
     171        V = G.get_attribute_values('a1') #Get by name
     172        assert allclose(V, [2, 4])
     173
     174        V = G.get_attribute_values('a2') #Get by name
     175        assert allclose(V, [79.4, -7])
     176
     177
     178       
     179
     180
     181       
    112182
    113183if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.