Changeset 4549
- Timestamp:
- Jun 20, 2007, 4:33:33 PM (17 years ago)
- Location:
- anuga_core/source/anuga/geospatial_data
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/geospatial_data/geospatial_data.py
r4535 r4549 416 416 417 417 Always return absolute points! 418 """ 418 This alse means, that if you add None to the object, 419 it will be turned into absolute coordinates 420 421 other can be None in which case nothing is added to self. 422 """ 423 419 424 420 425 # find objects zone and checks if the same 421 426 geo_ref1 = self.get_geo_reference() 422 427 zone1 = geo_ref1.get_zone() 423 424 geo_ref2 = other.get_geo_reference() 425 zone2 = geo_ref2.get_zone() 426 427 geo_ref1.reconcile_zones(geo_ref2) 428 429 430 # sets xll and yll as the smallest from self and other 431 # FIXME (Duncan and Ole): use lower left corner derived from 432 # absolute coordinates 433 #if self.geo_reference.xllcorner <= other.geo_reference.xllcorner: 434 # xll = self.geo_reference.xllcorner 435 #else: 436 # xll = other.geo_reference.xllcorner 437 438 #if self.geo_reference.yllcorner <= other.geo_reference.yllcorner: 439 # yll = self.geo_reference.yllcorner 440 #else: 441 # yll = other.geo_reference.yllcorner 442 443 #new_geo_ref = Geo_reference(geo_ref1.get_zone(), xll, yll) 444 445 #xll = yll = 0. 446 447 # relative_points1 = self.get_data_points(absolute = False) 448 # relative_points2 = other.get_data_points(absolute = False) 449 450 451 # relative_points1 = new_geo_ref.\ 452 # change_points_geo_ref(relative_points1, 453 # geo_ref1) 454 # relative_points2 = new_geo_ref.\ 455 # change_points_geo_ref(relative_points2, 456 # geo_ref2) 457 458 # # Now both point sets are relative to new_geo_ref and 459 # # zones have been reconciled 460 461 # # Concatenate points 462 # new_points = concatenate((relative_points1, 463 # relative_points2), 464 # axis = 0) 465 466 new_points = concatenate((self.get_data_points(absolute = True), 467 other.get_data_points(absolute = True)), 468 axis = 0) 428 429 430 if other is not None: 431 432 geo_ref2 = other.get_geo_reference() 433 zone2 = geo_ref2.get_zone() 434 435 geo_ref1.reconcile_zones(geo_ref2) 436 437 438 new_points = concatenate((self.get_data_points(absolute=True), 439 other.get_data_points(absolute=True)), 440 axis = 0) 469 441 470 442 471 443 472 # Concatenate attributes if any 473 if self.attributes is None: 474 if other.attributes is not None: 475 msg = 'Geospatial data must have the same \n' 476 msg += 'attributes to allow addition.' 477 raise Exception, msg 478 479 new_attributes = None 480 else: 481 new_attributes = {} 482 for x in self.attributes.keys(): 483 if other.attributes.has_key(x): 484 485 attrib1 = self.attributes[x] 486 attrib2 = other.attributes[x] 487 new_attributes[x] = concatenate((attrib1, attrib2)) 488 489 else: 444 # Concatenate attributes if any 445 if self.attributes is None: 446 if other.attributes is not None: 490 447 msg = 'Geospatial data must have the same \n' 491 448 msg += 'attributes to allow addition.' 492 449 raise Exception, msg 450 451 new_attributes = None 452 else: 453 new_attributes = {} 454 for x in self.attributes.keys(): 455 if other.attributes.has_key(x): 456 457 attrib1 = self.attributes[x] 458 attrib2 = other.attributes[x] 459 new_attributes[x] = concatenate((attrib1, attrib2)) 460 461 else: 462 msg = 'Geospatial data must have the same \n' 463 msg += 'attributes to allow addition.' 464 raise Exception, msg 465 466 467 else: 468 #other is None: 469 470 new_points = self.get_data_points(absolute=True) 471 new_attributes = self.attributes 472 473 493 474 494 475 # Instantiate new data object and return absolute coordinates … … 497 478 new_attributes, 498 479 new_geo_ref) 480 481 482 def __radd__(self, other): 483 """Handle cases like None + Geospatial_data(...) 484 """ 485 486 return self + other 487 488 499 489 500 490 ### -
anuga_core/source/anuga/geospatial_data/test_geospatial_data.py
r4535 r4549 433 433 434 434 assert allclose(P, concatenate( (points1,points2) )) 435 436 437 def test_add_with_None(self): 438 """ test that None can be added to a geospatical objects 439 """ 440 441 points1 = array([[2.0, 4.1], [4.0, 7.3]]) 442 points2 = array([[5.1, 9.1], [6.1, 6.3]]) 443 444 geo_ref1= Geo_reference(55, 1.0, 2.0) 445 geo_ref2 = Geo_reference(zone=55, 446 xllcorner=0.1, 447 yllcorner=3.0, 448 datum='wgs84', 449 projection='UTM', 450 units='m') 451 452 453 attributes1 = {'depth':[2, 4.7], 'elevation':[6.1, 5]} 454 attributes2 = {'depth':[-2.3, 4], 'elevation':[2.5, 1]} 455 456 457 G1 = Geospatial_data(points1, attributes1, geo_ref1) 458 assert allclose(G1.get_geo_reference().get_xllcorner(), 1.0) 459 assert allclose(G1.get_geo_reference().get_yllcorner(), 2.0) 460 assert G1.attributes.has_key('depth') 461 assert G1.attributes.has_key('elevation') 462 assert allclose(G1.attributes['depth'], [2, 4.7]) 463 assert allclose(G1.attributes['elevation'], [6.1, 5]) 464 465 G2 = Geospatial_data(points2, attributes2, geo_ref2) 466 assert allclose(G2.get_geo_reference().get_xllcorner(), 0.1) 467 assert allclose(G2.get_geo_reference().get_yllcorner(), 3.0) 468 assert G2.attributes.has_key('depth') 469 assert G2.attributes.has_key('elevation') 470 assert allclose(G2.attributes['depth'], [-2.3, 4]) 471 assert allclose(G2.attributes['elevation'], [2.5, 1]) 472 473 #Check that absolute values are as expected 474 P1 = G1.get_data_points(absolute=True) 475 assert allclose(P1, [[3.0, 6.1], [5.0, 9.3]]) 476 477 P2 = G2.get_data_points(absolute=True) 478 assert allclose(P2, [[5.2, 12.1], [6.2, 9.3]]) 479 480 # Normal add 481 G = G1 + None 482 483 assert G.attributes.has_key('depth') 484 assert G.attributes.has_key('elevation') 485 assert allclose(G.attributes['depth'], [2, 4.7]) 486 assert allclose(G.attributes['elevation'], [6.1, 5]) 487 488 # Points are now absolute. 489 assert allclose(G.get_geo_reference().get_xllcorner(), 0.0) 490 assert allclose(G.get_geo_reference().get_yllcorner(), 0.0) 491 P = G.get_data_points(absolute=True) 492 assert allclose(P, [[3.0, 6.1], [5.0, 9.3]]) 493 494 495 G = G2 + None 496 assert G.attributes.has_key('depth') 497 assert G.attributes.has_key('elevation') 498 assert allclose(G.attributes['depth'], [-2.3, 4]) 499 assert allclose(G.attributes['elevation'], [2.5, 1]) 500 501 assert allclose(G.get_geo_reference().get_xllcorner(), 0.0) 502 assert allclose(G.get_geo_reference().get_yllcorner(), 0.0) 503 P = G.get_data_points(absolute=True) 504 assert allclose(P, [[5.2, 12.1], [6.2, 9.3]]) 505 506 507 508 # Reverse add 509 G = None + G1 510 511 assert G.attributes.has_key('depth') 512 assert G.attributes.has_key('elevation') 513 assert allclose(G.attributes['depth'], [2, 4.7]) 514 assert allclose(G.attributes['elevation'], [6.1, 5]) 515 516 # Points are now absolute. 517 assert allclose(G.get_geo_reference().get_xllcorner(), 0.0) 518 assert allclose(G.get_geo_reference().get_yllcorner(), 0.0) 519 P = G.get_data_points(absolute=True) 520 assert allclose(P, [[3.0, 6.1], [5.0, 9.3]]) 521 522 523 G = None + G2 524 assert G.attributes.has_key('depth') 525 assert G.attributes.has_key('elevation') 526 assert allclose(G.attributes['depth'], [-2.3, 4]) 527 assert allclose(G.attributes['elevation'], [2.5, 1]) 528 529 assert allclose(G.get_geo_reference().get_xllcorner(), 0.0) 530 assert allclose(G.get_geo_reference().get_yllcorner(), 0.0) 531 P = G.get_data_points(absolute=True) 532 assert allclose(P, [[5.2, 12.1], [6.2, 9.3]]) 533 534 535 536 435 537 436 538
Note: See TracChangeset
for help on using the changeset viewer.