Changeset 3738
- Timestamp:
- Oct 11, 2006, 10:22:45 AM (18 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
r3735 r3738 5 5 6 6 from os import access, F_OK, R_OK 7 from types import DictType 8 7 9 from Numeric import concatenate, array, Float, shape, reshape, ravel, take 8 10 … … 124 126 125 127 else: 126 ### data = {} 127 # watch for case where file name and points, attributes etc are provided!! 128 # if file name then all provided info will be removed! 129 self.import_points_file(file_name, delimiter, verbose) 128 # watch for case where file name and points, attributes etc are provided!! 129 # if file name then all provided info will be removed! 130 self.import_points_file(file_name, delimiter, verbose) 130 131 131 self.check_data_points(self.data_points) 132 self.set_attributes(self.attributes) 133 self.set_geo_reference(self.geo_reference) 134 self.set_default_attribute_name(default_attribute_name) 132 self.check_data_points(self.data_points) 133 self.set_attributes(self.attributes) 134 self.set_geo_reference(self.geo_reference) 135 self.set_default_attribute_name(default_attribute_name) 136 137 138 assert self.attributes is None or isinstance(self.attributes, DictType) 139 135 140 136 141 def __len__(self): … … 159 164 """ 160 165 161 from types import DictType162 163 166 if attributes is None: 164 167 self.attributes = None … … 235 238 points = self.get_data_points() 236 239 inside_indices = inside_polygon(points, polygon, closed) 237 return Geospatial_data(take(points, inside_indices)) 240 241 clipped_points = take(points, inside_indices) 242 243 # Clip all attributes 244 attributes = self.get_all_attributes() 245 246 clipped_attributes = {} 247 if attributes is not None: 248 for key, att in attributes.items(): 249 clipped_attributes[key] = take(att, inside_indices) 250 251 return Geospatial_data(clipped_points, clipped_attributes) 238 252 239 253 … … 261 275 points = self.get_data_points() 262 276 outside_indices = outside_polygon(points, polygon, closed) 263 return Geospatial_data(take(points, outside_indices)) 277 278 clipped_points = take(points, outside_indices) 279 280 # Clip all attributes 281 attributes = self.get_all_attributes() 282 283 clipped_attributes = {} 284 if attributes is not None: 285 for key, att in attributes.items(): 286 clipped_attributes[key] = take(att, outside_indices) 287 288 return Geospatial_data(clipped_points, clipped_attributes) 289 264 290 265 291 def _set_using_lat_long(self, … … 350 376 """ 351 377 Return values for all attributes. 378 The return value is either None or a dictionary (possibly empty). 352 379 """ 353 380 … … 445 472 Note: will throw an IOError if it can't load the file. 446 473 Catch these! 474 475 Post condition: self.attributes dictionary has been set 447 476 """ 448 477 -
anuga_core/source/anuga/geospatial_data/test_geospatial_data.py
r3736 r3738 395 395 [[0.5, 0.5], [1, -0.5], [1.5, 0]]) 396 396 397 def no_test_clip1(self): 397 def test_clip0_with_attributes(self): 398 """test_clip0_with_attributes(self): 399 400 Test that point sets with attributes can be clipped by a polygon 401 """ 402 403 from anuga.coordinate_transforms.geo_reference import Geo_reference 404 405 points = [[-1, 4], [0.2, 0.5], [1.0, 2.1], [0.4, 0.3], [3.0, 5.3], 406 [0, 0], [2.4, 3.3]] 407 408 attributes = [2, -4, 5, 76, -2, 0.1, 3] 409 att_dict = {'att1': attributes, 410 'att2': array(attributes)+1} 411 412 G = Geospatial_data(points, att_dict) 413 414 # First try the unit square 415 U = [[0,0], [1,0], [1,1], [0,1]] 416 assert allclose(G.clip(U).get_data_points(), [[0.2, 0.5], [0.4, 0.3], [0, 0]]) 417 assert allclose(G.clip(U).get_attributes('att1'), [-4, 76, 0.1]) 418 assert allclose(G.clip(U).get_attributes('att2'), [-3, 77, 1.1]) 419 420 # Then a more complex polygon 421 polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] 422 points = [ [0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]] 423 424 # This time just one attribute 425 attributes = [2, -4, 5, 76, -2, 0.1] 426 G = Geospatial_data(points, attributes) 427 428 assert allclose(G.clip(polygon).get_data_points(), 429 [[0.5, 0.5], [1, -0.5], [1.5, 0]]) 430 assert allclose(G.clip(polygon).get_attributes(), [-4, 5, 76]) 431 432 433 def test_clip1(self): 398 434 """test_clip1(self): 399 435 … … 408 444 attributes = [2, -4, 5, 76, -2, 0.1, 3] 409 445 att_dict = {'att1': attributes, 410 'att2': array(attributes) 446 'att2': array(attributes)+1} 411 447 G = Geospatial_data(points, att_dict) 412 448 … … 417 453 418 454 assert allclose(G.clip(U).get_attributes('att1'), [-4, 76, 0.1]) 419 #assert allclose(G.clip(U).get_attributes('att2'), ???)455 assert allclose(G.clip(U).get_attributes('att2'), [-3, 77, 1.1]) 420 456 421 457 # Then a more complex polygon 422 458 points = [ [0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]] 423 G = Geospatial_data(points) 459 attributes = [2, -4, 5, 76, -2, 0.1] 460 G = Geospatial_data(points, attributes) 424 461 polygon = Geospatial_data([[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]]) 425 462 … … 427 464 assert allclose(G.clip(polygon).get_data_points(), 428 465 [[0.5, 0.5], [1, -0.5], [1.5, 0]]) 466 assert allclose(G.clip(polygon).get_attributes(), [-4, 5, 76]) 467 429 468 430 469 def test_clip0_outside(self): … … 438 477 points = [[-1, 4], [0.2, 0.5], [1.0, 2.1], [0.4, 0.3], [3.0, 5.3], 439 478 [0, 0], [2.4, 3.3]] 440 G = Geospatial_data(points) 479 attributes = [2, -4, 5, 76, -2, 0.1, 3] 480 G = Geospatial_data(points, attributes) 441 481 442 482 # First try the unit square … … 444 484 assert allclose(G.clip_outside(U).get_data_points(), 445 485 [[-1, 4], [1.0, 2.1], [3.0, 5.3], [2.4, 3.3]]) 486 #print G.clip_outside(U).get_attributes() 487 assert allclose(G.clip_outside(U).get_attributes(), [2, 5, -2, 3]) 488 446 489 447 490 # Then a more complex polygon 448 491 polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] 449 492 points = [ [0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]] 450 G = Geospatial_data(points) 493 attributes = [2, -4, 5, 76, -2, 0.1] 494 G = Geospatial_data(points, attributes) 451 495 452 496 assert allclose(G.clip_outside(polygon).get_data_points(), 453 [[0.5, 1.4], [0.5, 1.5], [0.5, -0.5]]) 497 [[0.5, 1.4], [0.5, 1.5], [0.5, -0.5]]) 498 assert allclose(G.clip_outside(polygon).get_attributes(), [2, -2, 0.1]) 454 499 455 500 … … 465 510 points = [[-1, 4], [0.2, 0.5], [1.0, 2.1], [0.4, 0.3], [3.0, 5.3], 466 511 [0, 0], [2.4, 3.3]] 467 G = Geospatial_data(points) 512 attributes = [2, -4, 5, 76, -2, 0.1, 3] 513 G = Geospatial_data(points, attributes) 468 514 469 515 # First try the unit square … … 471 517 assert allclose(G.clip_outside(U).get_data_points(), 472 518 [[-1, 4], [1.0, 2.1], [3.0, 5.3], [2.4, 3.3]]) 519 assert allclose(G.clip(U).get_attributes(), [-4, 76, 0.1]) 473 520 474 521 # Then a more complex polygon 475 522 points = [ [0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]] 476 G = Geospatial_data(points) 523 attributes = [2, -4, 5, 76, -2, 0.1] 524 G = Geospatial_data(points, attributes) 525 477 526 polygon = Geospatial_data([[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]]) 478 527 … … 480 529 assert allclose(G.clip_outside(polygon).get_data_points(), 481 530 [[0.5, 1.4], [0.5, 1.5], [0.5, -0.5]]) 531 assert allclose(G.clip_outside(polygon).get_attributes(), [2, -2, 0.1]) 532 482 533 483 534 … … 493 544 points = [[-1, 4], [0.2, 0.5], [1.0, 2.1], [0.4, 0.3], [3.0, 5.3], 494 545 [0, 0], [2.4, 3.3]] 495 496 G = Geospatial_data(points )546 attributes = [2, -4, 5, 76, -2, 0.1, 3] 547 G = Geospatial_data(points, attributes) 497 548 498 549 # First try the unit square … … 500 551 G1 = G.clip(U) 501 552 assert allclose(G1.get_data_points(),[[0.2, 0.5], [0.4, 0.3], [0, 0]]) 553 assert allclose(G.clip(U).get_attributes(), [-4, 76, 0.1]) 502 554 503 555 G2 = G.clip_outside(U) 504 556 assert allclose(G2.get_data_points(),[[-1, 4], [1.0, 2.1], 505 557 [3.0, 5.3], [2.4, 3.3]]) 558 assert allclose(G.clip_outside(U).get_attributes(), [2, 5, -2, 3]) 506 559 507 560 … … 510 563 [[-1, 4], [1.0, 2.1], [3.0, 5.3], [2.4, 3.3]] 511 564 565 new_attributes = [-4, 76, 0.1, 2, 5, -2, 3] 566 512 567 assert allclose((G1+G2).get_data_points(), new_points) 568 assert allclose((G1+G2).get_attributes(), new_attributes) 513 569 514 570 G = G1+G2 … … 523 579 # Check result 524 580 assert allclose(G3.get_data_points(), new_points) 525 581 assert allclose(G3.get_attributes(), new_attributes) 526 582 527 583 os.remove(FN)
Note: See TracChangeset
for help on using the changeset viewer.