Changeset 2465
- Timestamp:
- Mar 2, 2006, 3:36:17 PM (19 years ago)
- 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 2 associated attributes. 3 3 4 4 """ … … 6 6 7 7 from utilities.numerical_tools import ensure_numeric 8 9 from Numeric import concatenate 8 10 9 11 class Geospatial_data: … … 83 85 self.geo_reference = geo_reference 84 86 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.' 86 89 raise msg 87 90 … … 94 97 return self.geo_reference 95 98 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 98 107 99 108 def get_attributes(self, attribute_name = None): … … 107 116 attribute_name = self.default_attribute_name 108 117 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 110 120 111 121 … … 114 124 115 125 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 116 180 117 181 … … 159 223 geo_reference = geo) 160 224 161 162 163 164 165 225 226 227 228 -
inundation/geospatial_data/test_geospatial_data.py
r2454 r2465 3 3 4 4 import unittest 5 from Numeric import zeros, array, allclose 5 from Numeric import zeros, array, allclose, concatenate 6 6 from math import sqrt, pi 7 7 8 9 8 from geospatial_data import * 9 10 from coordinate_transforms.geo_reference import Geo_reference 10 11 11 12 class Test_Geospatial_data(unittest.TestCase): … … 64 65 65 66 66 P = G.get_data_points( )67 P = G.get_data_points(absolute = False) 67 68 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 69 73 V = G.get_attributes() #Simply get them 70 74 assert allclose(V, [2, 4]) … … 72 76 V = G.get_attributes('attribute') #Get by name 73 77 assert allclose(V, [2, 4]) 74 75 78 76 79 def test_get_attributes_2(self): … … 86 89 87 90 88 P = G.get_data_points( )91 P = G.get_data_points(absolute = False) 89 92 assert allclose(P, [[1.0, 2.1], [3.0, 5.3]]) 90 93 … … 160 163 G = points_dictionary2geospatial_data(points_dict) 161 164 162 P = G.get_data_points( )165 P = G.get_data_points(absolute = False) 163 166 assert allclose(P, [[1.0, 2.1], [3.0, 5.3]]) 164 167 … … 175 178 assert allclose(V, [79.4, -7]) 176 179 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]} 180 187 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 182 195 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]) 186 206 assert allclose(G.get_data_points(), [[1.0, 2.1], [3.0, 5.3], 187 207 [1.0, 2.1], [3.0, 5.3]]) 188 208 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 191 231 192 232
Note: See TracChangeset
for help on using the changeset viewer.