Changeset 2375 for inundation/utilities


Ignore:
Timestamp:
Feb 9, 2006, 2:37:57 PM (19 years ago)
Author:
ole
Message:

Added georeferencing to polygon_function and added test

Location:
inundation/utilities
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/utilities/polygon.py

    r2351 r2375  
    333333    Note: If two polygons overlap, the one last in the list takes precedence
    334334
    335     FIXME? : Currently, coordinates specified here are assumed to be relative to the origin (georeference) used by domain. This function is more general than domain so perhaps it'd be an idea to allow the optional argument georeference???
    336 
    337     """
    338 
    339     def __init__(self, regions, default = 0.0):
     335    Coordinates specified in the call are assumed to be relative to the origin (georeference)
     336    e.g. used by domain. By specifying the optional argument georeference, all points are made relative.
     337
     338    FIXME: This should really work with geo_spatial point sets.
     339    """
     340
     341    def __init__(self, regions, default = 0.0, geo_reference = None):
    340342
    341343        try:
     
    355357        assert a == 2, 'Must have two component each: %s' %T
    356358
    357         self.regions = regions
    358         self.default = default
     359
     360        if geo_reference is None:
     361            from coordinate_transforms.geo_reference import Geo_reference
     362            geo_reference = Geo_reference()
     363
     364
     365        self.default = default
     366
     367        #Make points in polygons relative to geo_reference
     368        self.regions = []           
     369        for polygon, value in regions:
     370            P = geo_reference.change_points_geo_ref(polygon)           
     371            self.regions.append( (P, value) )
     372
     373
    359374
    360375
     
    392407
    393408def read_polygon(filename):
    394     """Read points assumed to form a polygon
    395        There must be exactly two numbers in each line
     409    """Read points assumed to form a polygon.
     410       There must be exactly two numbers in each line separated by a comma.
     411       No header.
    396412    """
    397413
  • inundation/utilities/test_polygon.py

    r2311 r2375  
    5656
    5757
     58    def test_polygon_function_georef(self):
     59        """Check that georeferencing works
     60        """
     61
     62        from coordinate_transforms.geo_reference import Geo_reference
     63
     64        geo = Geo_reference(56, 200, 1000)
     65
     66        #Make points 'absolute'
     67        p1 = [[200,1000], [210,1000], [210,1010], [200,1010]]
     68        p2 = [[200,1000], [210,1010], [215,1005], [220, 1010], [225,1000],
     69              [230,1010], [240,990]]
     70
     71        f = Polygon_function( [(p1, 1.0)], geo_reference = geo )
     72        z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1
     73
     74        assert allclose(z, [1,1,0,0])
     75
     76
     77        f = Polygon_function( [(p2, 2.0)], geo_reference = geo )
     78        z = f([5, 5, 27, 35], [5, 9, 8, -5]) #First and last inside p2
     79        assert allclose(z, [2,0,0,2])
     80
     81
     82        #Combined
     83        f = Polygon_function( [(p1, 1.0), (p2, 2.0)], geo_reference = geo )
     84        z = f([5, 5, 27, 35], [5, 9, 8, -5])
     85        assert allclose(z, [2,1,0,2])
     86
     87
     88        #Check that it would fail without geo
     89        f = Polygon_function( [(p1, 1.0), (p2, 2.0)])
     90        z = f([5, 5, 27, 35], [5, 9, 8, -5])
     91        assert not allclose(z, [2,1,0,2])       
     92
     93
     94
    5895    def test_polygon_function_callable(self):
    5996        """Check that values passed into Polygon_function can be callable
     
    84121        z = f([5, 5, 27, 35], [5, 9, 8, -5])
    85122        assert allclose(z, [2,14,35,2])
     123
    86124
    87125
Note: See TracChangeset for help on using the changeset viewer.