source: inundation/geospatial_data/geospatial_data.py @ 2309

Last change on this file since 2309 was 2309, checked in by ole, 18 years ago

Deployed geo_spatial stuff

File size: 5.1 KB
Line 
1"""Class Geospatial_data - Manipulation of locations on the planet and associated attributes.
2
3
4"""
5
6
7from utilities.numerical_tools import ensure_numeric
8       
9class Geospatial_data:
10
11    def __init__(self,
12                 data_points,
13                 attributes = None,
14                 geo_reference = None,
15                 default_attribute_name = None):
16
17        """Create instance from data points and associated attributes
18
19
20        data_points: x,y coordinates in meters. Type must be either a
21        sequence of 2-tuples or an Mx2 Numeric array of floats.
22
23        attributes: Associated values for each data point. The type
24        must be either a list or an array of length M or a dictionary
25        of lists (or arrays) of length M. In the latter case the keys
26        in the dictionary represent the attribute names, in the former
27        the attribute will get the default name 'attribute'.
28       
29        geo_reference: Object representing the origin of the data
30        points. It contains UTM zone, easting and northing and data
31        points are assumed to be relative to this origin.
32        If geo_reference is None, the default geo ref object is used
33
34        default_attribute_name: Name of default attribute to be used with
35        get_attribute_values. The idea is that the dataset can be
36        equipped with information about which attribute to return.
37        If None, the default is the 'first'
38       
39        """
40
41        self.data_points = ensure_numeric(data_points)
42        self.set_attributes(attributes)
43        self.set_geo_reference(geo_reference)
44        self.set_default_attribute_name(default_attribute_name)
45       
46
47
48
49    def set_attributes(self, attributes):
50        """Check and assign attributes dictionary
51        """
52       
53        from types import DictType
54       
55        if attributes is None:
56            self.attributes = None
57            return
58       
59        if not isinstance(attributes, DictType):
60            #Convert single attribute into dictionary
61            attributes = {'attribute': attributes}
62
63        #Check input attributes   
64        for key in attributes.keys():
65            try:
66                attributes[key] = ensure_numeric(attributes[key])
67            except:
68                msg = 'Attribute %s could not be converted' %key
69                msg += 'to a numeric vector'
70                raise msg
71
72        self.attributes = attributes   
73
74
75    def set_geo_reference(self, geo_reference):
76
77        from coordinate_transforms.geo_reference import Geo_reference
78
79
80        if geo_reference is None:
81            self.geo_reference = Geo_reference() # Use default
82        elif isinstance(geo_reference, Geo_reference):
83            self.geo_reference = geo_reference
84        else:
85            msg = 'Argument geo_reference must be a valid Geo_reference object or None.'
86            raise msg
87
88
89    def set_default_attribute_name(self, default_attribute_name):
90        self.default_attribute_name = default_attribute_name
91
92
93    def get_geo_reference(self):
94        return self.geo_reference
95       
96    def get_data_points(self):
97        return self.data_points
98
99    def get_attributes(self, attribute_name = None):
100        """Return values for one named attribute.
101
102        If attribute_name is None, default_attribute_name is used
103        """
104
105        if attribute_name is None:
106            if self.default_attribute_name is not None:
107                attribute_name = self.default_attribute_name
108            else:
109                attribute_name = self.attributes.keys()[0] #Take the first one from keys
110               
111
112        msg = 'Attribute name %s does not exist in data set' %attribute_name
113        assert self.attributes.has_key(attribute_name), msg
114
115        return self.attributes[attribute_name]
116       
117
118
119def geospatial_data2points_dictionary(geospatial_data):
120    """Convert geospatial data to points_dictionary
121    """
122
123    points_dictionary = {}
124    points_dictionary['pointlist'] = geospatial_data.data_points
125
126    points_dictionary['attributelist'] = {}
127
128    for attribute_name in geospatial_data.attributes.keys():
129        val = geospatial_data.attributes[attribute_name]
130        points_dictionary['attributelist'][attribute_name] = val
131
132    points_dictionary['geo_reference'] = geospatial_data.geo_reference
133
134    return points_dictionary
135
136   
137   
138
139
140
141
142def points_dictionary2geospatial_data(points_dictionary):
143    """Convert points_dictionary to geospatial data object
144    """
145
146    msg = 'Points dictionary must have key pointlist' 
147    assert points_dictionary.has_key('pointlist'), msg
148
149    msg = 'Points dictionary must have key attributelist'     
150    assert points_dictionary.has_key('attributelist'), msg       
151
152    if points_dictionary.has_key('geo_reference'):
153        geo = points_dictionary['geo_reference']
154    else:
155        geo = None
156   
157    return Geospatial_data(points_dictionary['pointlist'],
158                           points_dictionary['attributelist'],
159                           geo_reference = geo)
160
161   
162
163           
164           
165       
Note: See TracBrowser for help on using the repository browser.